| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace DCL.Models |
| | 4 | | { |
| | 5 | | public static class MeshesInfoUtils |
| | 6 | | { |
| | 7 | | public static Bounds BuildMergedBounds(Renderer[] renderers) |
| | 8 | | { |
| 324 | 9 | | Bounds bounds = new Bounds(); |
| | 10 | |
|
| 1656 | 11 | | for (int i = 0; i < renderers.Length; i++) |
| | 12 | | { |
| 504 | 13 | | if (renderers[i] == null) |
| | 14 | | continue; |
| | 15 | |
|
| 504 | 16 | | if (i == 0) |
| 324 | 17 | | bounds = renderers[i].GetSafeBounds(); |
| | 18 | | else |
| 180 | 19 | | bounds.Encapsulate(renderers[i].GetSafeBounds()); |
| | 20 | | } |
| | 21 | |
|
| 324 | 22 | | return bounds; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// This get the renderer bounds with a check to ensure the renderer is at a safe position. |
| | 27 | | /// If the renderer is too far away from 0,0,0, wasm target ensures a crash. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="renderer"></param> |
| | 30 | | /// <returns>The bounds value if the value is correct, or a mocked bounds object with clamped values if its too |
| | 31 | | public static Bounds GetSafeBounds( this Renderer renderer ) |
| | 32 | | { |
| | 33 | | // World extents are of 4800 world mts, so this limit far exceeds the world size. |
| | 34 | | const float POSITION_OVERFLOW_LIMIT = 10000; |
| | 35 | | const float POSITION_OVERFLOW_LIMIT_SQR = POSITION_OVERFLOW_LIMIT * POSITION_OVERFLOW_LIMIT; |
| | 36 | |
|
| 4008 | 37 | | if ( renderer.transform.position.sqrMagnitude > POSITION_OVERFLOW_LIMIT_SQR ) |
| 13 | 38 | | return new Bounds( Vector3.one * POSITION_OVERFLOW_LIMIT, Vector3.one * 0.1f ); |
| | 39 | |
|
| 3995 | 40 | | return renderer.bounds; |
| | 41 | | } |
| | 42 | | } |
| | 43 | | } |