| | 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 | | { |
| 356 | 11 | | Bounds bounds = new Bounds(); |
| 356 | 12 | | bool initializedBounds = false; |
| | 13 | |
|
| 356 | 14 | | if (renderers != null) |
| | 15 | | { |
| 1742 | 16 | | for (int i = 0; i < renderers.Length; i++) |
| | 17 | | { |
| 515 | 18 | | if (renderers[i] == null) continue; |
| | 19 | |
|
| 515 | 20 | | if (!initializedBounds) |
| | 21 | | { |
| 350 | 22 | | initializedBounds = true; |
| 350 | 23 | | bounds = GetSafeBounds(renderers[i].bounds, renderers[i].transform.position); |
| | 24 | | } |
| 165 | 25 | | else { bounds.Encapsulate(GetSafeBounds(renderers[i].bounds, renderers[i].transform.position)); } |
| | 26 | | } |
| | 27 | | } |
| | 28 | |
|
| 356 | 29 | | if (colliders != null) |
| | 30 | | { |
| 1464 | 31 | | foreach (Collider collider in colliders) |
| | 32 | | { |
| 376 | 33 | | if (collider == null) continue; |
| | 34 | |
|
| 376 | 35 | | if (!initializedBounds) |
| | 36 | | { |
| 3 | 37 | | initializedBounds = true; |
| 3 | 38 | | bounds = GetSafeBounds(collider.bounds, collider.transform.position); |
| | 39 | | } |
| 373 | 40 | | else { bounds.Encapsulate(GetSafeBounds(collider.bounds, collider.transform.position)); } |
| | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| 356 | 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 | |
|
| 931 | 62 | | if (objectPosition.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR) |
| 0 | 63 | | return new Bounds(Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f); |
| | 64 | |
|
| 931 | 65 | | return bounds; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public static int ComputeTotalTriangles(IEnumerable<Renderer> renderers, |
| | 69 | | Dictionary<Mesh, int> meshToTriangleCount) |
| | 70 | | { |
| 19 | 71 | | int result = 0; |
| | 72 | |
|
| 118 | 73 | | foreach (var renderer in renderers) |
| | 74 | | { |
| | 75 | | switch (renderer) |
| | 76 | | { |
| | 77 | | case MeshRenderer r: |
| 40 | 78 | | MeshFilter mf = r.GetComponent<MeshFilter>(); |
| | 79 | |
|
| 40 | 80 | | if (mf == null) |
| | 81 | | continue; |
| | 82 | |
|
| 40 | 83 | | int triangles = meshToTriangleCount[mf.sharedMesh]; |
| 40 | 84 | | result += triangles; |
| 40 | 85 | | break; |
| | 86 | | case SkinnedMeshRenderer skr: |
| 0 | 87 | | result += meshToTriangleCount[skr.sharedMesh]; |
| | 88 | | break; |
| | 89 | | } |
| | 90 | | } |
| | 91 | |
|
| 19 | 92 | | return result; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public static Dictionary<Mesh, int> ExtractMeshToTriangleMap(IEnumerable<Mesh> meshes) |
| | 96 | | { |
| 7 | 97 | | Dictionary<Mesh, int> result = new Dictionary<Mesh, int>(); |
| | 98 | |
|
| 98 | 99 | | foreach (var mesh in meshes) { result[mesh] = mesh.triangles.Length; } |
| | 100 | |
|
| 7 | 101 | | return result; |
| | 102 | | } |
| | 103 | |
|
| 1 | 104 | | private static List<int> texIdsCache = new List<int>(); |
| 1 | 105 | | private static List<string> texNameCache = new List<string>(); |
| | 106 | |
|
| | 107 | | public static HashSet<Animation> ExtractUniqueAnimations(GameObject container) |
| | 108 | | { |
| 12 | 109 | | return new HashSet<Animation>(container.GetComponentsInChildren<Animation>(true)); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public static HashSet<AnimationClip> ExtractUniqueAnimationClips(HashSet<Animation> animations) |
| | 113 | | { |
| 0 | 114 | | HashSet<AnimationClip> result = new HashSet<AnimationClip>(); |
| | 115 | |
|
| 0 | 116 | | foreach (var anim in animations) |
| | 117 | | { |
| 0 | 118 | | foreach (AnimationState state in anim) { result.Add(state.clip); } |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | return result; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | public static HashSet<Renderer> ExtractUniqueRenderers(GameObject container) |
| | 125 | | { |
| 347 | 126 | | return new HashSet<Renderer>(container.GetComponentsInChildren<Renderer>(true)); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public static HashSet<Material> ExtractUniqueMaterials(IEnumerable<Renderer> renderers) |
| | 130 | | { |
| 31 | 131 | | return new HashSet<Material>(renderers.SelectMany((x) => |
| 118 | 132 | | x.sharedMaterials.Where((mat) => mat != null && mat.shader.name != "DCL/FX/Hologram") |
| | 133 | | )); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | public static HashSet<Texture> ExtractUniqueTextures(IEnumerable<Material> materials) |
| | 137 | | { |
| 19 | 138 | | return new HashSet<Texture>( |
| | 139 | | materials.SelectMany( |
| | 140 | | (mat) => |
| | 141 | | { |
| 54 | 142 | | mat.GetTexturePropertyNameIDs(texIdsCache); |
| 54 | 143 | | mat.GetTexturePropertyNames(texNameCache); |
| 54 | 144 | | List<Texture> result = new List<Texture>(); |
| | 145 | |
|
| 1828 | 146 | | for (int i = 0; i < texIdsCache.Count; i++) |
| | 147 | | { |
| 860 | 148 | | var tex = mat.GetTexture(texIdsCache[i]); |
| | 149 | |
|
| 907 | 150 | | if (tex != null) { result.Add(tex); } |
| | 151 | | } |
| | 152 | |
|
| 54 | 153 | | return result; |
| | 154 | | })); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public static HashSet<Mesh> ExtractUniqueMeshes(IEnumerable<Renderer> renderers) |
| | 158 | | { |
| 19 | 159 | | List<Mesh> result = new List<Mesh>(); |
| | 160 | |
|
| 118 | 161 | | foreach (Renderer renderer in renderers) |
| | 162 | | { |
| | 163 | | switch (renderer) |
| | 164 | | { |
| | 165 | | case SkinnedMeshRenderer skr: |
| 0 | 166 | | if (skr.sharedMesh == null) |
| | 167 | | continue; |
| | 168 | |
|
| 0 | 169 | | result.Add(skr.sharedMesh); |
| 0 | 170 | | break; |
| | 171 | | case MeshRenderer mr: |
| 40 | 172 | | MeshFilter mf = mr.GetComponent<MeshFilter>(); |
| | 173 | |
|
| 40 | 174 | | if (mf.mesh == null) |
| | 175 | | continue; |
| | 176 | |
|
| 40 | 177 | | result.Add(mf.mesh); |
| | 178 | | break; |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| | 182 | | // Ensure meshes are unique |
| 19 | 183 | | return new HashSet<Mesh>(result); |
| | 184 | | } |
| | 185 | | } |
| | 186 | | } |