- 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 minor 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 Mesh? (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. Might 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

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Setting audio volume with horizontal slider (to many AudioSources with different volume) 2 Answers

Changing Music Volume = Calling a function in another script in another scene 2 Answers

Separate Listeners for Sound FX and Music? 0 Answers

Change volume of objects with a tag 1 Answer

Volume Slider 0 Answers