| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Models |
| | 6 | | { |
| | 7 | | public static class MeshesInfoUtils |
| | 8 | | { |
| | 9 | | public static Bounds BuildMergedBounds(Renderer[] renderers) |
| | 10 | | { |
| 333 | 11 | | Bounds bounds = new Bounds(); |
| | 12 | |
|
| 1660 | 13 | | for (int i = 0; i < renderers.Length; i++) |
| | 14 | | { |
| 497 | 15 | | if (renderers[i] == null) |
| | 16 | | continue; |
| | 17 | |
|
| 497 | 18 | | if (i == 0) |
| 333 | 19 | | bounds = renderers[i].GetSafeBounds(); |
| | 20 | | else |
| 164 | 21 | | bounds.Encapsulate(renderers[i].GetSafeBounds()); |
| | 22 | | } |
| | 23 | |
|
| 333 | 24 | | return bounds; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// This get the renderer bounds with a check to ensure the renderer is at a safe position. |
| | 29 | | /// If the renderer is too far away from 0,0,0, wasm target ensures a crash. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="renderer"></param> |
| | 32 | | /// <returns>The bounds value if the value is correct, or a mocked bounds object with clamped values if its too |
| | 33 | | public static Bounds GetSafeBounds( this Renderer renderer ) |
| | 34 | | { |
| | 35 | | // World extents are of 4800 world mts, so this limit far exceeds the world size. |
| | 36 | | const float POSITION_OVERFLOW_LIMIT = 10000; |
| | 37 | | const float POSITION_OVERFLOW_LIMIT_SQR = POSITION_OVERFLOW_LIMIT * POSITION_OVERFLOW_LIMIT; |
| | 38 | |
|
| 4167 | 39 | | if ( renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR ) |
| 11 | 40 | | return new Bounds( Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f ); |
| | 41 | |
|
| 4156 | 42 | | return renderer.bounds; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public static int ComputeTotalTriangles(List<Renderer> renderers, Dictionary<Mesh, int> meshToTriangleCount) |
| | 46 | | { |
| 117 | 47 | | int result = 0; |
| | 48 | |
|
| 780 | 49 | | for ( int i = 0; i < renderers.Count; i++ ) |
| | 50 | | { |
| 273 | 51 | | var r = renderers[i]; |
| | 52 | |
|
| 273 | 53 | | if ( r is MeshRenderer ) |
| | 54 | | { |
| 162 | 55 | | int triangles = meshToTriangleCount[ r.GetComponent<MeshFilter>().sharedMesh ]; |
| 162 | 56 | | result += triangles; |
| | 57 | | } |
| | 58 | |
|
| 273 | 59 | | if ( r is SkinnedMeshRenderer skinnedMeshRenderer ) |
| | 60 | | { |
| 111 | 61 | | result += meshToTriangleCount[ skinnedMeshRenderer.sharedMesh ]; |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 117 | 65 | | return result; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(List<Mesh> meshes) |
| | 69 | | { |
| 7 | 70 | | Dictionary<Mesh, int> result = new Dictionary<Mesh, int>(); |
| | 71 | |
|
| 70 | 72 | | for ( int i = 0; i < meshes.Count; i++ ) |
| | 73 | | { |
| 28 | 74 | | Mesh mesh = meshes[i]; |
| 28 | 75 | | result[mesh] = mesh.triangles.Length; |
| | 76 | | } |
| | 77 | |
|
| 7 | 78 | | return result; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public static List<Mesh> ExtractMeshes(GameObject gameObject) |
| | 82 | | { |
| 17 | 83 | | List<Mesh> result = new List<Mesh>(); |
| 17 | 84 | | List<SkinnedMeshRenderer> skrList = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>(true).ToList(); |
| 17 | 85 | | List<MeshFilter> meshFilterList = gameObject.GetComponentsInChildren<MeshFilter>(true).ToList(); |
| | 86 | |
|
| 34 | 87 | | foreach ( var skr in skrList ) |
| | 88 | | { |
| 0 | 89 | | if ( skr.sharedMesh == null ) |
| | 90 | | continue; |
| | 91 | |
|
| 0 | 92 | | result.Add(skr.sharedMesh); |
| | 93 | | } |
| | 94 | |
|
| 130 | 95 | | foreach ( var meshFilter in meshFilterList ) |
| | 96 | | { |
| 48 | 97 | | if ( meshFilter.mesh == null ) |
| | 98 | | continue; |
| | 99 | |
|
| 48 | 100 | | result.Add( meshFilter.mesh ); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // Ensure meshes are unique |
| 17 | 104 | | result = result.Distinct().ToList(); |
| | 105 | |
|
| 17 | 106 | | return result; |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |