3

In a Unity code base, I saw this:

// the game object currently has no mesh attached
MeshFilter mFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
// no problem so far
mFilter.mesh = MakeASmallQuadMesh(0.1f);
// great stuff
mFilter.mesh.bounds = SomeSpecificBounds();
// what ?

The function "MakeASmallQuadMesh" has the usual completely normal code for making a mesh, so

Mesh mesh = new Mesh();
mesh.SetVertices(verts);
mesh.SetIndices(indices);
mesh.SetUVs(0, uvs);
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;

No worries there. It makes a quad 10cm across.

But what about the line of code

mFilter.mesh.bounds = SomeSpecificBounds();
  1. I was amazed to learn you can set mesh.bounds, I assumed it would be read-only.

  2. What possible "meaning" is there to "setting" the bounds? It would be like: there's a written measurement in a doctor's office stating that Jane is 6'. You change the record to 5'10". Of course, Jane's height does not change at all. You've just, bizarrely made the record wrong.

  3. Could it be that: so, the bounds of a mesh are used by various, indeed many, Unity systems. (Culling, etc etc.) Could the pattern be that by "forcing" the bounds like this (the bounds are now "totally wrong" for the object - they're just some value you forced in there) the programmer wanted (for some reason) Unity's system (say, culling) to use those forced, nonsensical (to the actual object) values?

  4. Wild guess, maybe there's a pattern (I have never heard of) where you "force" the bounds of object A to be the same as object B - for some reason I cannot guess at?

What could possibly be the pattern / reason here?

I would just assume it's a basic mistake, but assumptions kill.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    Wild guess here, too, so I'm not sure, but maybe setting the bounds will scale the object to fit those bounds. For instance, I want a gameObject Car and I want it to fit perfectly inside a parking spot, so I set as bounds the length of the parking... – Jonas Grumann Mar 29 '19 at 14:01
  • hi @JonasGrumann! Indeed, NO, that is absolutely not the case. (Indeed it's a typical mistake when experienced programmers first do something in unity!) The bounds are very much only "based on" the mesh. Indeed, when you dynamically make or modify a mesh in code, you finish with a Unity call "RecalculateBounds" which correctly figures out and sets the bounds; similarly when Unity is setting up a mesh from a file (so, your elephant or whatever from Maya), Unity calls "RecalculateBounds". "Force" setting the bounds does not at all "change the underlying mesh or object". It remains a mystery! – Fattie Mar 29 '19 at 14:57
  • @JonasGrumann , perhaps you've given the answer to the question. **The programmer incorrectly thought that would actually change the mesh or perhaps scale the game object** .......... maybe that's all it is. Seems suspicious though. – Fattie Mar 29 '19 at 14:59
  • Indeed... suspicious – Jonas Grumann Mar 29 '19 at 15:03
  • Also a guess but I just realized your code example is about a quad. So if you would use `RecalculateBounds();` I'ld expect to get a bounding box with a height of `0` since the mesh contains only one single flad plane. So maybe in the example they simply wanted (for some reason) to actually have MinMax bounds other than `0`. Also there are `Expand` and `SetMinMax` which speak in favor for theory 3 I guess. – derHugo Mar 29 '19 at 15:50

1 Answers1

4

I was actually curious about this myself, and I happened to have a program that explicitly generated large numbers of custom meshes, so I decided to test a few things.

The first thing I wanted to confirm was that the bounds were in fact set automatically. A rudimentary inspection revealed that this was indeed the case: Specifically, a mesh's bounds are automatically recalculated any time you set the mesh.vertices property. This probably explains why the property is a fixed length array rather than a list. (fun side note: If you try to assign secondary properties like uv coords or normals to the mesh before assigning vertex positions, unity gently nags you about mismatched array lengths before promptly exploding. So Don't Do That.)

As for what this actually impacts, I had a hunch: I set the extents of my mesh bounds to be 0. Immediately, meshes at the corner of my viewport stated getting visually culled. This tells us a few things:

  1. Setting bounds explicitly does have an effect.
  2. Unity does actually make use of custom bounds data.
  3. Unity uses mesh bounds to perform frustum culling.

According to Unity's manual, there are three cases where the Bounds class is used: Mesh.bounds, Renderer.bounds, and Collider.bounds. Of those three, Mesh.bounds is the only property that isn't read only.

As for the question of why anybody would want to set mesh bounds explicitly, it's not impossible that you could perform some clever culling optimization like looking at a complex mesh through a window or some such, but if I had to guess, whoever wrote that code didn't trust Unity to set mesh bounds accurately or explicitly.

VPellen
  • 1,071
  • 5
  • 10
  • new user VPellen, I am SO GLAD you joined and made the post! Thanks! Investigating all that you said now ........ – Fattie Mar 30 '19 at 12:50
  • 1
    It's very important to note that the new-ish Advanced Mesh API (for use with the DOTS/jobs system) does **not** automatically set the bounds of a mesh and must be defined manually. – arkon May 28 '22 at 05:59