Instance collision#699
Conversation
If this is creating a body with the shape for each instance, I think an optimization can be had here. If a single body is used and the same shape is added to it multiple times with transforms matching the instances, it will use significantly less memory. Through my tests, adding 250K bodies using the same shape RID results in ~400 megabytes of additional RAM usage. Using a single body, but adding 250K of the same shape to it at different transforms was only ~100 megabytes extra RAM usage. Rebuilding shapes on a single body is also extremely fast compared to creating multiple bodies. |
4d2ace6 to
b4ed412
Compare
|
Work on only updating edge cells, and spawning body per cell, shape per instance This will be helpful: Terrain3DInstancer::_get_cell(const Vector3 &p_global_position, const int p_region_size) will give the cell index for the current region. so cell_local_pos.xz = instance_pos.xz - cell * cell_size. |
92fb6c4 to
a397db9
Compare
|
@TokisanGames I think this working nicely now. Could you please review and let me have your feedback? |
|
Thanks, I will look when I can. I am a bit behind. You could add edit the collision document, and document the new functions in the XML (generate it then edit it) if you like.
Never merge. Only rebase. See the contributing doc. |
Thanks and no worries, I actually dreamed an improvement last night so I need another day or so to try it out. I'll also take a look into editing the docs and sorry for the merge. |
|
As the user traverses the instancer cells and they leave behind 10 instances of TreeA and approach 10 instances of TreeA, it would be more efficient to just move the transforms, and store any remaining in an unused cache rather than delete and recreate them every time. The latter is slower, will likely fragment memory, and probably run into any limits faster. The former is what the terrain collision does. It builds a fixed number of shapes at initialization, then never creates nor destroys any until shutdown.
This also isn't what we want. Do an interactive rebase, drop both of these commits. Rebase your PR, and force push. |
Yes I saw the fixed number of shapes in the terrain collision which is great when there is a fixed number of shapes of the same type. I can manage active and inactive shapes for each shape type, but how many to create of each? What do you think about creating shapes only when there are no unused ones? |
Yes, and in the cleanup, save 10 and delete the rest. Or, we have a fixed cell size and a calculated mesh density. It could be a number based on density, clamped between 5-20. Or, if the camera has moved to a new cell block, triggering an update, we know how many are needed in the new blocks, and how many are extra in the old blocks. You could just move all of those, create as needed, free the rest, store none. The latter is probably best. In OOTA, we have some 60+ meshes and growing. We don't need 600 unused shapes hanging around. The same types of foliage grow together in an area, so in this scenario, it doesn't store unnecessarily, and the reuse count is high, and it gradually changes retained types as the planted foliage changes. |
|
This PR still isn't rebased properly. None of these commits should be in your branch. You should never use
Watch this. |
d751a7b to
86e32b0
Compare
71ce214 to
a21a1d3
Compare
|
This PR has a build script and a godot-cpp that it should not have. That's probably why android is failling. |
6c74762 to
d19a6da
Compare
d19a6da to
800a95b
Compare
|
OK I'm finally happy with this |
f55a649 to
1cbc4cb
Compare
Squashed commit: [f8062bd] Rename visual -> debug_meshes [80d7fcb] Fix dissapearing debug_meshes [aac0b9c] Fix collision is persistant on changing scenes (+2 squashed commit) Squashed commit: [3afa186] Update logging message [620c5fa] Do not update visual instance where none exists (+3 squashed commit) Squashed commit: [bdca4d8] Rebuild collision on MMI updates (sync MMIs with instance collision) [b0a1716] Add consts, remove whitespace [5a3c11f] add more consts (+3 squashed commit) Squashed commit: [303d111] Fix instancer/collision debug shapes race [493568e] Cleanup terrain_3d_collision.cpp [1a95405] Fix distance_to always == 0.0 (+3 squashed commit) Squashed commit: [db1782a] improve instance collision [2a1af32] update terrain_3d_collision.cpp [2ae3a9e] Build from cell audit and shape recycling (cherry picked from commit 44858e3) (cherry picked from commit 23cdbb2)
- This would previously load 32x32 cells per region, and would attempt to load regions that don't exist if region size was smaller than 32x32 cells. This also loads faster now as a result.
- Track cell locations as terrain-relative cell positions, similar to how region locations are stored - Restructure instance data dictionary to reduce memory usage for transforms - Optimize some calculations related to cell positions
…(+2 squashed commit) Squashed commit: [07f467b] Instance collision set_is_dirty() default value = true
1cbc4cb to
467ffda
Compare

This PR aims to add collision for the foliage instancer, mentioned in issue #43
The headlines :
Some further details:
It will read any CollisionShape3Ds found in the Terrain3DMeshAsset scene file and store their Shape3Ds and transforms.
When terrain collision is rebuilt in Terrain3DCollision it also spawns shapes for the instances using data read from the data/region/instances dictionary and the Shape3D and transforms stored in the MA.
In Dynamic mode:
Build an array of cell positions to build, skipping cells which already exist.
Build a dictionary of instances in said cells.
Decompose cells outside of the collision radius and store the instances within for recycling.
Compare the list of recyclable instances against the list of instances to build. Decompose unneeded instances to their component shapes, store for recycling.
Generate the new instances, preferably:
A. Recycle an old instance, simply transforming the shapes. Or...
B. Reuse spare shapes, transform and update base meshes. Or...
C. Generate brand new shapes.
Destroy and remaining shapes and update an internal RID/shape_id map.
Full mode for instance collision was shelved.
For both Editor and Game modes, the PhysicsServer3D is used. The main reason for this was I kept hitting the 2^18 object limit when stress testing Full mode.
For Editor modes, visualizations are provided via the RenderingServer.
To test it, add a new Terrain3DMeshAsset and use RockA.tscn as the scene file. Paint some rocks using the foliage instancer and run the scene.
Future plans: