| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.XR; |
| | 8 | |
|
| | 9 | | public class SceneBoundsFeedbackStyle_BIW : ISceneBoundsFeedbackStyle |
| | 10 | | { |
| | 11 | | class InvalidMeshInfo |
| | 12 | | { |
| 0 | 13 | | public List<GameObject> wireframeObjects = new List<GameObject>(); |
| | 14 | | public MeshesInfo meshesInfo; |
| | 15 | | public System.Action OnResetMaterials; |
| | 16 | |
|
| 0 | 17 | | public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; } |
| | 18 | |
|
| | 19 | | public void ResetMaterials() |
| | 20 | | { |
| 0 | 21 | | if (meshesInfo.meshRootGameObject == null) |
| 0 | 22 | | return; |
| | 23 | |
|
| 0 | 24 | | int wireframeObjectscount = wireframeObjects.Count; |
| 0 | 25 | | for (int i = 0; i < wireframeObjectscount; i++) |
| | 26 | | { |
| 0 | 27 | | Utils.SafeDestroy(wireframeObjects[i]); |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | OnResetMaterials?.Invoke(); |
| 0 | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| | 34 | | const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh"; |
| | 35 | |
|
| 7 | 36 | | Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>(); |
| 7 | 37 | | HashSet<Renderer> invalidSubmeshes = new HashSet<Renderer>(); |
| 7 | 38 | | private readonly List<MeshesInfo> currentMeshesInvalidated = new List<MeshesInfo>(); |
| | 39 | |
|
| 21 | 40 | | public SceneBoundsFeedbackStyle_BIW() { invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>(); } |
| | 41 | |
|
| | 42 | | public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 43 | | { |
| 0 | 44 | | if (isInsideBoundaries) |
| | 45 | | { |
| 0 | 46 | | RemoveInvalidMeshEffect(meshesInfo); |
| 0 | 47 | | return; |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | AddInvalidMeshEffect(meshesInfo); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void CleanFeedback() |
| | 54 | | { |
| 2 | 55 | | for (int i = 0; i < currentMeshesInvalidated.Count; i++) |
| | 56 | | { |
| 0 | 57 | | RemoveInvalidMeshEffect(currentMeshesInvalidated[i]); |
| | 58 | | } |
| | 59 | |
|
| 1 | 60 | | currentMeshesInvalidated.Clear(); |
| 1 | 61 | | } |
| | 62 | |
|
| | 63 | | public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo) |
| | 64 | | { |
| 0 | 65 | | List<Material> result = new List<Material>(); |
| | 66 | |
|
| 0 | 67 | | for (int i = 0; i < meshesInfo.renderers.Length; i++) |
| | 68 | | { |
| 0 | 69 | | result.AddRange(meshesInfo.renderers[i].sharedMaterials); |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | return result; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | void RemoveInvalidMeshEffect(MeshesInfo meshesInfo) |
| | 76 | | { |
| 0 | 77 | | if (meshesInfo == null || WasGameObjectInAValidPosition(meshesInfo.innerGameObject)) |
| 0 | 78 | | return; |
| | 79 | |
|
| 0 | 80 | | PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject); |
| | 81 | |
|
| 0 | 82 | | if (shapePoolableObjectBehaviour != null) |
| 0 | 83 | | shapePoolableObjectBehaviour.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials; |
| | 84 | |
|
| 0 | 85 | | var renderers = meshesInfo.renderers; |
| | 86 | |
|
| 0 | 87 | | if (renderers != null) |
| | 88 | | { |
| 0 | 89 | | for (int i = 0; i < renderers.Length; i++) |
| | 90 | | { |
| 0 | 91 | | if (invalidSubmeshes.Contains(renderers[i])) |
| 0 | 92 | | invalidSubmeshes.Remove(renderers[i]); |
| | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials(); |
| 0 | 97 | | currentMeshesInvalidated.Remove(meshesInfo); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | void AddInvalidMeshEffect(MeshesInfo meshesInfo) |
| | 101 | | { |
| 0 | 102 | | if (currentMeshesInvalidated.Contains(meshesInfo)) |
| 0 | 103 | | return; |
| 0 | 104 | | if (!WasGameObjectInAValidPosition(meshesInfo.innerGameObject)) |
| 0 | 105 | | return; |
| | 106 | |
|
| 0 | 107 | | InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo); |
| | 108 | |
|
| 0 | 109 | | invalidMeshInfo.OnResetMaterials = () => { invalidMeshesInfo.Remove(meshesInfo.innerGameObject); }; |
| | 110 | |
|
| 0 | 111 | | PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject); |
| 0 | 112 | | if (shapePoolableObjectBehaviour != null) |
| | 113 | | { |
| 0 | 114 | | shapePoolableObjectBehaviour.OnRelease -= invalidMeshInfo.ResetMaterials; |
| 0 | 115 | | shapePoolableObjectBehaviour.OnRelease += invalidMeshInfo.ResetMaterials; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | // Apply invalid mesh |
| 0 | 119 | | Renderer[] entityRenderers = meshesInfo.renderers; |
| 0 | 120 | | for (int i = 0; i < entityRenderers.Length; i++) |
| | 121 | | { |
| | 122 | | // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, parent) |
| | 123 | | // overload because we need to set the position and scale before parenting, to deal with scaled objects) |
| 0 | 124 | | GameObject wireframeObject = GameObject.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME)); |
| 0 | 125 | | wireframeObject.transform.position = entityRenderers[i].bounds.center; |
| 0 | 126 | | wireframeObject.transform.localScale = entityRenderers[i].bounds.size * 1.01f; |
| 0 | 127 | | wireframeObject.transform.SetParent(meshesInfo.innerGameObject.transform); |
| | 128 | |
|
| 0 | 129 | | invalidMeshInfo.wireframeObjects.Add(wireframeObject); |
| 0 | 130 | | entityRenderers[i].enabled = true; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | currentMeshesInvalidated.Add(meshesInfo); |
| 0 | 134 | | invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo); |
| 0 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | public bool WasGameObjectInAValidPosition(GameObject gameObject) { return !invalidMeshesInfo.ContainsKey(gameObject) |
| | 138 | | } |