- Home /
mesh volume static batching
I use a script to calculate the volume of a mesh renderer which works fine but i when set static batching on in the editor the volume returns zero.
I already tried "isstatic" in the start function and then the volume is calculated correctly but that won't enable the actual static batching, it doesn't batch anything that way.
this is the code i use to calculate the volume:
public float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
{
float v321 = p3.x * p2.y * p1.z;
float v231 = p2.x * p3.y * p1.z;
float v312 = p3.x * p1.y * p2.z;
float v132 = p1.x * p3.y * p2.z;
float v213 = p2.x * p1.y * p3.z;
float v123 = p1.x * p2.y * p3.z;
return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
}
public float VolumeOfMesh(Mesh mesh)
{
float volume = 0;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
for (int i = 0; i < mesh.triangles.Length; i += 3)
{
Vector3 p1 = vertices[triangles[i + 0]];
Vector3 p2 = vertices[triangles[i + 1]];
Vector3 p3 = vertices[triangles[i + 2]];
volume += SignedVolumeOfTriangle(p1, p2, p3);
}
volume *= this.gameObject.transform.localScale.x * this.gameObject.transform.localScale.y * this.gameObject.transform.localScale.z;
gameObject.isStatic = true;
return Mathf.Abs(volume);
Is there a way i can calculate the volume of the mesh AND use static batching this way? And why does it return zero if i set the object to static?
i don't know the answer to your questions, but one $$anonymous$$or comment - i think you're calculating the surface area of the mesh/triangles here, not the volume.
It's definitely the volume because when i place a cube and calculate the x*y*z i get the exact same outcome.
i think at least lol.
hm. i'd be surprised. the volume of a triangle is zero. perhaps this is the volume of the tetrahedron joining the origin (0, 0, 0) to the triangle ?
Not familiar with batching, but this seems odd: How are you getting the $$anonymous$$esh? (Thinking maybe the batching stuff sets it to an empty mesh, so it wont be drawn by the renderer, just by the batch. If so, perhaps caching it during Start(), before the batching stuff get's its mitts on it, will help?)
Not sure how often you call this, but worth noting: If the mesh verticies are not changing, just the transform, you can compute a scaleOne, or "Unit" volume, one time, during startup, and only multiply by the transform's scale each cycle. $$anonymous$$ight also help work around the batching issue.
(by the way, how does a triangle have volume with no thickness? I just can't wrap my head around what that volume of triangle function is doing!)
i call this function quite alot, most of the times on the start function to calculate literally every mesh volume in the scene.
the verticies are not changing and the transform is also not changing during the calculation(start function).
How can i compute a scaleOne or Unit volume then? i am not familiar with it.
i don't know how the formula actually works, i just know that it works because i calculated a square x*y*z and that return the exact same volume of the other script
You DO compute the Unit volume. Then you multiply it by the scale. It's just the volume before you scale it. But if this is being done in start only, I think you can safely ignore my comments.
Still don't know where you get the mesh passed into this function. I'd bet that's where the issue lies.
Your answer

Follow this Question
Related Questions
Mask a MeshRenderer 0 Answers
Why are static objects that are batched simply not showing up on some Android devices? 1 Answer
Baking Static Batches at Editor Time 0 Answers
Static batching and windzones - does wind count as moving the object? 0 Answers
Sound volume based on rigidbody velocity before collision. 3 Answers