| | 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, HashSet<Collider> colliders) |
| | 10 | | { |
| 585 | 11 | | Bounds bounds = new Bounds(); |
| 585 | 12 | | bool initializedBounds = false; |
| | 13 | |
|
| 2896 | 14 | | for (int i = 0; i < renderers.Length; i++) |
| | 15 | | { |
| 863 | 16 | | if (renderers[i] == null) continue; |
| | 17 | |
|
| 863 | 18 | | if (!initializedBounds) |
| | 19 | | { |
| 582 | 20 | | initializedBounds = true; |
| 582 | 21 | | bounds = GetSafeBounds(renderers[i].bounds, renderers[i].transform.position); |
| 582 | 22 | | } |
| | 23 | | else |
| | 24 | | { |
| 281 | 25 | | bounds.Encapsulate(GetSafeBounds(renderers[i].bounds, renderers[i].transform.position)); |
| | 26 | | } |
| | 27 | | } |
| | 28 | |
|
| 1906 | 29 | | foreach (Collider collider in colliders) |
| | 30 | | { |
| 368 | 31 | | if (collider == null) continue; |
| | 32 | |
|
| 368 | 33 | | if (!initializedBounds) |
| | 34 | | { |
| 3 | 35 | | initializedBounds = true; |
| 3 | 36 | | bounds = GetSafeBounds(collider.bounds, collider.transform.position); |
| 3 | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 365 | 40 | | bounds.Encapsulate(GetSafeBounds(collider.bounds, collider.transform.position)); |
| | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| 585 | 44 | | return bounds; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// This get the object bounds with a check to ensure the renderer is at a safe position. |
| | 49 | | /// If the object is too far away from 0,0,0, wasm target ensures a crash. |
| | 50 | | /// NOTE: If returning a mocked bounds object becomes problematic (e.g. for getting real bounds size), |
| | 51 | | /// we should find a solution using meshFilter.mesh.bounds instead as those bounds are local. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="bounds"></param> |
| | 54 | | /// <param name="objectPosition"></param> |
| | 55 | | /// <returns>The bounds value if the value is correct, or a mocked bounds object with clamped values if its too |
| | 56 | | public static Bounds GetSafeBounds(Bounds bounds, Vector3 objectPosition) |
| | 57 | | { |
| | 58 | | // World extents are of 4800 world mts, so this limit far exceeds the world size. |
| | 59 | | const float POSITION_OVERFLOW_LIMIT = 10000; |
| | 60 | | const float POSITION_OVERFLOW_LIMIT_SQR = POSITION_OVERFLOW_LIMIT * POSITION_OVERFLOW_LIMIT; |
| | 61 | |
|
| 1287 | 62 | | if (objectPosition.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR) |
| 0 | 63 | | return new Bounds(Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f); |
| | 64 | |
|
| 1287 | 65 | | return bounds; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public static int ComputeTotalTriangles(IEnumerable<Renderer> renderers, |
| | 69 | | Dictionary<Mesh, int> meshToTriangleCount) |
| | 70 | | { |
| 158 | 71 | | int result = 0; |
| | 72 | |
|
| 1122 | 73 | | foreach (var renderer in renderers) |
| | 74 | | { |
| | 75 | | switch (renderer) |
| | 76 | | { |
| | 77 | | case MeshRenderer r: |
| 208 | 78 | | MeshFilter mf = r.GetComponent<MeshFilter>(); |
| | 79 | |
|
| 208 | 80 | | if (mf == null) |
| | 81 | | continue; |
| | 82 | |
|
| 208 | 83 | | int triangles = meshToTriangleCount[mf.sharedMesh]; |
| 208 | 84 | | result += triangles; |
| 208 | 85 | | break; |
| | 86 | | case SkinnedMeshRenderer skr: |
| 195 | 87 | | result += meshToTriangleCount[skr.sharedMesh]; |
| | 88 | | break; |
| | 89 | | } |
| | 90 | | } |
| | 91 | |
|
| 158 | 92 | | return result; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(IEnumerable<Mesh> meshes) |
| | 96 | | { |
| 39 | 97 | | Dictionary<Mesh, int> result = new Dictionary<Mesh, int>(); |
| | 98 | |
|
| 198 | 99 | | foreach (var mesh in meshes) |
| | 100 | | { |
| 60 | 101 | | result[mesh] = mesh.triangles.Length; |
| | 102 | | } |
| | 103 | |
|
| 39 | 104 | | return result; |
| | 105 | | } |
| | 106 | |
|
| 1 | 107 | | private static List<int> texIdsCache = new List<int>(); |
| 1 | 108 | | private static List<string> texNameCache = new List<string>(); |
| | 109 | |
|
| | 110 | | public static HashSet<Animation> ExtractUniqueAnimations(GameObject container) |
| | 111 | | { |
| 119 | 112 | | return new HashSet<Animation>(container.GetComponentsInChildren<Animation>(true)); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public static HashSet<AnimationClip> ExtractUniqueAnimationClips(HashSet<Animation> animations) |
| | 116 | | { |
| 107 | 117 | | HashSet<AnimationClip> result = new HashSet<AnimationClip>(); |
| | 118 | |
|
| 234 | 119 | | foreach (var anim in animations) |
| | 120 | | { |
| 46 | 121 | | foreach (AnimationState state in anim) |
| | 122 | | { |
| 13 | 123 | | result.Add(state.clip); |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| 107 | 127 | | return result; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container) |
| | 131 | | { |
| 552 | 132 | | return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true)); |
| | 133 | | } |
| | 134 | |
|
| | 135 | | public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers) |
| | 136 | | { |
| 158 | 137 | | return new HashSet<Material>(renderers.SelectMany((x) => |
| 820 | 138 | | x.sharedMaterials.Where((mat) => mat != null && mat.shader.name != "DCL/FX/Hologram") |
| | 139 | | )); |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public static HashSet<Texture> ExtractUniqueTextures(IEnumerable<Material> materials) |
| | 143 | | { |
| 158 | 144 | | return new HashSet<Texture>( |
| | 145 | | materials.SelectMany( |
| | 146 | | (mat) => |
| | 147 | | { |
| 266 | 148 | | mat.GetTexturePropertyNameIDs(texIdsCache); |
| 266 | 149 | | mat.GetTexturePropertyNames(texNameCache); |
| 266 | 150 | | List<Texture> result = new List<Texture>(); |
| 9388 | 151 | | for (int i = 0; i < texIdsCache.Count; i++) |
| | 152 | | { |
| 4428 | 153 | | var tex = mat.GetTexture(texIdsCache[i]); |
| | 154 | |
|
| 4428 | 155 | | if (tex != null) |
| | 156 | | { |
| 282 | 157 | | result.Add(tex); |
| | 158 | | } |
| | 159 | | } |
| | 160 | |
|
| 266 | 161 | | return result; |
| | 162 | | })); |
| | 163 | | } |
| | 164 | |
|
| | 165 | | public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers) |
| | 166 | | { |
| 51 | 167 | | List<Mesh> result = new List<Mesh>(); |
| | 168 | |
|
| 318 | 169 | | foreach (Renderer renderer in renderers) |
| | 170 | | { |
| | 171 | | switch (renderer) |
| | 172 | | { |
| | 173 | | case SkinnedMeshRenderer skr: |
| 68 | 174 | | if (skr.sharedMesh == null) |
| | 175 | | continue; |
| | 176 | |
|
| 68 | 177 | | result.Add(skr.sharedMesh); |
| 68 | 178 | | break; |
| | 179 | | case MeshRenderer mr: |
| 40 | 180 | | MeshFilter mf = mr.GetComponent<MeshFilter>(); |
| | 181 | |
|
| 40 | 182 | | if (mf.mesh == null) |
| | 183 | | continue; |
| | 184 | |
|
| 40 | 185 | | result.Add(mf.mesh); |
| | 186 | | break; |
| | 187 | | } |
| | 188 | | } |
| | 189 | |
|
| | 190 | | // Ensure meshes are unique |
| 51 | 191 | | return new HashSet<Mesh>(result); |
| | 192 | | } |
| | 193 | | } |
| | 194 | | } |