-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose an API for spatial queries (such as BVH) #7811
Comments
I wrote all this below, but do suspect in your case, for the scatter addon you may be able to just use the existing SPSI'll just call these structures "spatial partitioning structures" (SPS for short) just because we have several of these available (3.x: my BVH in physics & rendering, 4.x: my BVH physics, Juan's rendering), and they could in theory change in future. There's a massive load of considerations here and I can't pretend I've thought of them all, this is just a stream of some initial thought on the subject: Physics & RenderingThe physics and rendering already use SPS, but there are two potential snags with using these for a general purpose:
This is the reason these functions in 3.x in VisualServer (and RenderingServer in 4.x) are marked not to be used, because they cause a stall:
Other ConsiderationsHowever there are some other considerations:
There are probably existing workarounds to get this info with physics (Areas etc) but they may require working in a different mindset because of the delay. Client side SPSThere are 2 obvious potential ways of exposing another SPS (if we decided to do it):
(1) would hide most of the complexity, but be an added cost, and would have to be carefully thought out to cover all use cases. Just to mention also that you can reduce the maintenance cost of an SPS by just making it a flat list of objects, and using raw CPU to test the objects. The physics and rendering use tree structures (which are more expensive to update) as a trade off, so they will in turn be faster for the types of tests required. Testing in some cases does not require a tree structure, just that the tests are done efficiently (e.g. using SIMD etc). Relationship with ray castingWorth mentioning as well is this ties in with the whole discussion going on about ray casting. The needs / requirements of a ray casting API are similar to those in this proposal. It would ideally be client side, have an SPS, but be able to additionally optionally go down to the level of ray tracing rather than bounding boxes. Funnily enough I wrote a ray tracing library 3 years ago for my lightmapping module, and exposed it to gdscript for a test: I never went anywhere with this, but it shows that efficient ray tracing is possible from user code (i.e. millions of ray tests) as opposed to using the physics API which is really only suitable for small numbers of tests. This is just a suggestion that the two could be combined. For ray tracing functionality, you'd probably need to maintain a separate copy of the geometry data in accelerated form, so there would be some RAM costs. |
The SPS need not be BVH. Even an oct/quad tree exposed by API would be enough for a whole lot of useful things. Maybe even a spatial hash/dictionary thing but done by boffins in optimized C++. I'm not on your brain-level with this stuff, so I'm not sure how to communicate that I (a simple dev) need a simple spatial structure thing to use and be quick. Like FastNoiseLite, perhaps a Resource that provides an SPS could be invented? |
I concur, just a quad/octree would be enough for most end users. |
I disagree that a Quadtree is enough. In fact, it rarely is unless for the most boring culling tasks. Generally, a kd-Tree may be better than just a Quadtree here; but I believe it should just be a part of Space(2D/3D) However, I would not want this data structure to be exposed (unless to GDExtension), because I believe most users would actually want a spatial query. So query functions in the same spirit as Sorely missing Spatial Queries:2D and 3D spatial queries, with overloads for: Physics Layers and Masks, as well as Rendering Layers:
Already provided by
|
As long as the queries can also take Transforms and run them against a custom array of other Transforms or points. What I require, for example, is to know which Transforms in a multimesh are nearest to my player. It makes sense to have some way to tell this hypothetical query to:
|
Since this thread and others I have started using the cull methods as described here: class-renderingserver-method-instances-cull-convex The problem is much clearer to me nowIn order to test an AABB against a MultiMeshInstance3D, I must:
This seems wasteful and slow. Would not a method something like this be better?
var me = $player
var player_aabb = me.mesh.get_aabb()
var mmesh = $field.multimesh
var mmesh_aabb = mmesh.get_aabb()
var sc = self.get_world3d().scenarrio
var hits = RenderingServer.instances_cull_aabb_array(player_aabb, mmesh_aabb, mmesh.transform_array, sc)
if hits:
explodyboom() Naturally for rays and convex too, as well as the 3D equivalents. |
Godot would absolutely benefit from a tree search that can return x nearest points or all points within y range. Blender's kdtree is exactly what I'm used to and looking for. My personal use cases are mostly for points, so AABB and mesh stuff should be separate, imo, and needs there are beyond my understanding. The astar algorithm can already return closest point, this just needs to be expanded and exposed to include searches for x nearest or within y distance, I assume. It feels like 95% of it is there already. I have not been successful in my attempts at doing this in gdscript for personal utilities. Seeing this implemented as a native search would be a huge relief. |
I am also running into issues with this because GDScript is just too slow for a quadtree or spatial hash grid with lots of elements. |
Describe the project you are working on
Describe the problem or limitation you are having in your project
Right now, I have a few choices:
There should be an API to some kind of spatial partitioning/querying algorithm for scripts to use.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
(All in global space)
First case
I have a player and their bbox.
I have a multimesh instance 3d node with a thousand meshes (transform instances) scattered.
As the player moves through the multimesh I want to know which meshes are near (some sphere around the player) to them, so they can 'react'.
A spatial API would let me query player's transform vs multimesh instance transform.
Another case
I am writing a scatter addon. I want to scatter a cylinder and a sphere. I do not want them to overlap.
A spatial API would let me query the candidate with what is already (maybe) in the same space.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Here's a toy API:
The objects should take up space (like a bbox size) and position in the spatialMagic structure.
Think a spatial hash using a dict, but done in c++ with voodo. If this BVH thing can do that, then please expose it to gdscript!
If this enhancement will not be used often, can it be worked around with a few lines of script?
No idea.
Is there a reason why this should be core and not an add-on in the asset library?
My line of reasoning goes that if core needs BVH (and needed Octtrees in the past) then why would users of the engine not have similar needs?
If it becomes a plugin/addon that would be fine, but make it an 'official' one perhaps. The fear that plugins give is that they are frozen in time and may not work in 2 versions hence.
The text was updated successfully, but these errors were encountered: