Hi all,
I’m working on a project where I have monoliths , each with its own mini-world of vegetation rendered via DrawMeshInstancedIndirect. I also wrote a custom tree-painting tool (an Editor script) that lets me place trees on a mesh surface. However, I’ve encountered a strange issue where updating the bounding box (in Graphics.DrawMeshInstancedIndirect
) sometimes misplaces the trees in my scene.
What I’m Seeing
- When I hard-code the bounds to something like:
_bounds =new Bounds(Vector3.zero, new Vector3(20,15,30));
everything is drawn in the correct spot, and painting additional trees also works fine.
- But as soon as I try to compute the bounds more accurately, like:
var center = (_min + _max) * 0.5f;
var size = _max - _min;
_bounds = new Bounds(center, size * 1.2f);
the trees appear to move out of place.
- I understand from the documentation and from other devs that the bounds in
DrawMeshInstancedIndirect
is only for culling (frustum checks) and doesn’t actually move the objects. But from my perspective, it really looks like the bounding box is moving the trees—since if the box is small or centered incorrectly, they vanish or shift.
My Setup
- Tree Data: I store
Tree
objects, each with aPosition
,Scale
, andRotation
. - Renderer Code: I have a script derived from the typical
DrawMeshInstancedIndirect
examples. I gather all trees, compute each instance’s worldMatrix4x4
withMatrix4x4.TRS(...)
, and store them in aComputeBuffer
. - Custom Editor Tool: The tool uses raycasts against the mesh to find positions, then saves those positions in the
TreeData
asset. I call aTriggerRepaint()
method on theTreeRenderer
script so it rebuilds the buffers.
Here’s a simplified snippet of my Renderer
private void RecomputeBounds()
{
_min = Vector3.positiveInfinity;
_max = Vector3.negativeInfinity;
foreach (var pair in TreeDataList)
{
if (pair.TreeData == null) continue;
// pick transform t
foreach (var tree in pair.TreeData.Trees)
{
// convert local to world
Vector3 worldPos = t.TransformPoint(tree.Position);
_min = Vector3.Min(_min, worldPos);
_max = Vector3.Max(_max, worldPos);
}
}
_center = (_min + _max) * 0.5f;
Vector3 size = (_max - _min) * 1.2f;
_bounds = new Bounds(_center, size);
}
private void InitializeBuffers()
{
// ... gather instance data ...
// final transform:
Matrix4x4 mtx = Matrix4x4.TRS(
t.TransformPoint(tree.Position),
t.rotation * tree.Rotation,
Vector3.Scale(t.lossyScale, tree.Scale)
);
// ...
Graphics.DrawMeshInstancedIndirect(Mesh, 0, Material, _bounds, ...);
}
And in the Editor painting tool, I store tree.Position
in local coordinates, then rely on t.TransformPoint(tree.Position)
for the final world position at render time.
My Question
How can I ensure that my bounding box is computed properly for culling, without shifting or re-centering my instanced trees
Thank you!