| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | #if !UNITY_DOTSRUNTIME |
| | | 6 | | |
| | | 7 | | using Unity.Mathematics; |
| | | 8 | | using UnityEngine; |
| | | 9 | | |
| | | 10 | | namespace GDX |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// <see cref="UnityEngine.Mesh" /> Based Extension Methods |
| | | 14 | | /// </summary> |
| | | 15 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | | 16 | | [VisualScriptingCompatible(2)] |
| | | 17 | | public static class MeshExtensions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Determine the volume of a given mesh. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <remarks> |
| | | 23 | | /// Based off of https://n-e-r-v-o-u-s.com/blog/?p=4415. |
| | | 24 | | /// </remarks> |
| | | 25 | | /// <param name="targetMesh">The mesh to evaluate for its volume.</param> |
| | | 26 | | /// <returns>The meshes volume.</returns> |
| | | 27 | | public static float CalculateVolume(this Mesh targetMesh) |
| | 0 | 28 | | { |
| | 0 | 29 | | float volume = 0; |
| | 0 | 30 | | Vector3[] vertices = targetMesh.vertices; |
| | 0 | 31 | | int[] triangles = targetMesh.triangles; |
| | 0 | 32 | | int triangleLength = targetMesh.triangles.Length; |
| | | 33 | | |
| | 0 | 34 | | for (int i = 0; i < triangleLength; i += 3) |
| | 0 | 35 | | { |
| | 0 | 36 | | float3 p1 = vertices[triangles[i + 0]]; |
| | 0 | 37 | | float3 p2 = vertices[triangles[i + 1]]; |
| | 0 | 38 | | float3 p3 = vertices[triangles[i + 2]]; |
| | 0 | 39 | | volume += math.dot(math.cross(p1, p2), p3); |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return math.abs(volume / 6.0f); |
| | 0 | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | #endif // !UNITY_DOTSRUNTIME |