| | 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 | | { |
| 374 | 11 | | Bounds bounds = new Bounds(); |
| | 12 | |
|
| 1730 | 13 | | for (int i = 0; i < renderers.Length; i++) |
| | 14 | | { |
| 491 | 15 | | if (renderers[i] == null) |
| | 16 | | continue; |
| | 17 | |
|
| 491 | 18 | | if (i == 0) |
| 374 | 19 | | bounds = renderers[i].GetSafeBounds(); |
| | 20 | | else |
| 117 | 21 | | bounds.Encapsulate(renderers[i].GetSafeBounds()); |
| | 22 | | } |
| | 23 | |
|
| 374 | 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 | |
|
| 579 | 39 | | if ( renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR ) |
| 0 | 40 | | return new Bounds( Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f ); |
| | 41 | |
|
| 579 | 42 | | return renderer.bounds; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public static int ComputeTotalTriangles(IEnumerable<Renderer> renderers, |
| | 46 | | Dictionary<Mesh, int> meshToTriangleCount) |
| | 47 | | { |
| 127 | 48 | | int result = 0; |
| | 49 | |
|
| 902 | 50 | | foreach ( var renderer in renderers ) |
| | 51 | | { |
| | 52 | | switch (renderer) |
| | 53 | | { |
| | 54 | | case MeshRenderer r: |
| 176 | 55 | | MeshFilter mf = r.GetComponent<MeshFilter>(); |
| | 56 | |
|
| 176 | 57 | | if ( mf == null ) |
| | 58 | | continue; |
| | 59 | |
|
| 176 | 60 | | int triangles = meshToTriangleCount[ mf.sharedMesh ]; |
| 176 | 61 | | result += triangles; |
| 176 | 62 | | break; |
| | 63 | | case SkinnedMeshRenderer skr: |
| 148 | 64 | | result += meshToTriangleCount[ skr.sharedMesh ]; |
| | 65 | | break; |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| 127 | 69 | | return result; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(IEnumerable<Mesh> meshes) |
| | 73 | | { |
| 19 | 74 | | Dictionary<Mesh, int> result = new Dictionary<Mesh, int>(); |
| | 75 | |
|
| 76 | 76 | | foreach (var mesh in meshes) |
| | 77 | | { |
| 19 | 78 | | result[mesh] = mesh.triangles.Length; |
| | 79 | | } |
| | 80 | |
|
| 19 | 81 | | return result; |
| | 82 | | } |
| | 83 | |
|
| 1 | 84 | | private static List<int> texIdsCache = new List<int>(); |
| 1 | 85 | | private static List<string> texNameCache = new List<string>(); |
| | 86 | |
|
| | 87 | | public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container) |
| | 88 | | { |
| 500 | 89 | | return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true)); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers) |
| | 93 | | { |
| 127 | 94 | | return new HashSet<Material>( renderers.SelectMany( (x) => |
| 648 | 95 | | x.sharedMaterials.Where((mat) => mat != null && mat.shader.name != "DCL/FX/Hologram") |
| | 96 | | ) ); |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public static HashSet<Texture> ExtractUniqueTextures(IEnumerable<Material> materials) |
| | 100 | | { |
| 127 | 101 | | return new HashSet<Texture>( |
| | 102 | | materials.SelectMany( |
| | 103 | | (mat) => |
| | 104 | | { |
| 191 | 105 | | mat.GetTexturePropertyNameIDs(texIdsCache); |
| 191 | 106 | | mat.GetTexturePropertyNames(texNameCache); |
| 191 | 107 | | List<Texture> result = new List<Texture>(); |
| 6972 | 108 | | for ( int i = 0; i < texIdsCache.Count; i++ ) |
| | 109 | | { |
| 3295 | 110 | | var tex = mat.GetTexture(texIdsCache[i]); |
| | 111 | |
|
| 3295 | 112 | | if ( tex != null ) |
| | 113 | | { |
| 234 | 114 | | result.Add(tex); |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 191 | 118 | | return result; |
| | 119 | | } ) ); |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers) |
| | 123 | | { |
| 30 | 124 | | List<Mesh> result = new List<Mesh>(); |
| | 125 | |
|
| 168 | 126 | | foreach ( Renderer renderer in renderers ) |
| | 127 | | { |
| | 128 | | switch ( renderer ) |
| | 129 | | { |
| | 130 | | case SkinnedMeshRenderer skr: |
| 43 | 131 | | if ( skr.sharedMesh == null ) |
| | 132 | | continue; |
| | 133 | |
|
| 43 | 134 | | result.Add(skr.sharedMesh); |
| 43 | 135 | | break; |
| | 136 | | case MeshRenderer mr: |
| 11 | 137 | | MeshFilter mf = mr.GetComponent<MeshFilter>(); |
| | 138 | |
|
| 11 | 139 | | if ( mf.mesh == null ) |
| | 140 | | continue; |
| | 141 | |
|
| 11 | 142 | | result.Add( mf.mesh ); |
| | 143 | | break; |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| | 147 | | // Ensure meshes are unique |
| 30 | 148 | | return new HashSet<Mesh>(result); |
| | 149 | | } |
| | 150 | | } |
| | 151 | | } |