Raycast 3D optimization [Need help and advice]

Godot Version 4.4

Optimizing Raycast 3D?

Code: GDScript
For a project, I want to do a minimum of 360*360 spherical raycasts in a VR environment. How can I optimize it? Now what’s happening is it’s making the program very slow, laggy, and even crashing. I am collecting almost all sorts of data possible to get from Raycast 3D (collider, collision point, etc.).

How can I make it optimized or lighter?

In simple terms, what I want to do is, from a point, do a spherical 3D ray cast in all directions and find all the collision points of the virtual environment. and then generate a lidar point cloud (example link: https://global.discourse-cdn.com/nvidia/original/3X/6/6/66ff0270cbb7415dbed06f33aba1bf355ae84145.jpeg)

There are a couple of ideas you can try:

  1. Use multiple threads Using multiple threads — Godot Engine (stable) documentation in English
  2. Limit how many raycasts are being queried at the same time, e.g. query only 100 raycasts in one frame, then next 100 in the next frame, and so on, until you query all of them - then repeat the process.
  3. If it’s crashing, then you might have memory issues too - consider storing your data more compactly in the PackedArrays instead of regular arrays, wherever it is possible with the data you have. And limit the data to only what you actually need, don’t store more “just in case”, unless you really need it. Consider also saving data to file and unload it from memory, instead of keep it all in the memory, if you need to just query and store it without using it.
1 Like