| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Controllers |
| | 8 | | { |
| | 9 | | public class SceneBoundsFeedbackStyle_RedFlicker : ISceneBoundsFeedbackStyle |
| | 10 | | { |
| | 11 | | class InvalidMeshInfo |
| | 12 | | { |
| 14 | 13 | | public Dictionary<Renderer, Material> originalMaterials = new Dictionary<Renderer, Material>(); |
| 14 | 14 | | public List<GameObject> wireframeObjects = new List<GameObject>(); |
| | 15 | | public MeshesInfo meshesInfo; |
| | 16 | | public System.Action OnResetMaterials; |
| | 17 | |
|
| 42 | 18 | | public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; } |
| | 19 | |
|
| | 20 | | public void ResetMaterials(MeshesInfo meshesInfo) |
| | 21 | | { |
| 0 | 22 | | this.meshesInfo = meshesInfo; |
| 0 | 23 | | ResetMaterials(); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public void ResetMaterials() |
| | 27 | | { |
| 5 | 28 | | if (meshesInfo.meshRootGameObject == null) |
| 0 | 29 | | return; |
| | 30 | |
|
| 26 | 31 | | for (int i = 0; i < meshesInfo.renderers.Length; i++) |
| | 32 | | { |
| 8 | 33 | | meshesInfo.renderers[i].sharedMaterial = originalMaterials[meshesInfo.renderers[i]]; |
| | 34 | | } |
| | 35 | |
|
| 5 | 36 | | int wireframeObjectscount = wireframeObjects.Count; |
| 26 | 37 | | for (int i = 0; i < wireframeObjectscount; i++) |
| | 38 | | { |
| 8 | 39 | | Utils.SafeDestroy(wireframeObjects[i]); |
| | 40 | | } |
| | 41 | |
|
| 5 | 42 | | OnResetMaterials?.Invoke(); |
| 5 | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| | 46 | | const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh"; |
| | 47 | | const string INVALID_MESH_MATERIAL_NAME = "Materials/InvalidMesh"; |
| | 48 | | const string INVALID_SUBMESH_MATERIAL_NAME = "Materials/InvalidSubMesh"; |
| | 49 | |
|
| | 50 | | Material invalidMeshMaterial; |
| | 51 | | Material invalidSubMeshMaterial; |
| 23 | 52 | | Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>(); |
| 23 | 53 | | HashSet<Renderer> invalidSubmeshes = new HashSet<Renderer>(); |
| 23 | 54 | | private readonly List<MeshesInfo> currentMeshesInvalidated = new List<MeshesInfo>(); |
| | 55 | |
|
| 23 | 56 | | public SceneBoundsFeedbackStyle_RedFlicker() |
| | 57 | | { |
| 23 | 58 | | invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>(); |
| 23 | 59 | | invalidMeshMaterial = Resources.Load(INVALID_MESH_MATERIAL_NAME) as Material; |
| 23 | 60 | | invalidSubMeshMaterial = Resources.Load(INVALID_SUBMESH_MATERIAL_NAME) as Material; |
| 23 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public int GetInvalidMeshesCount() { return invalidMeshesInfo.Count; } |
| | 64 | |
|
| | 65 | | public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 66 | | { |
| 28 | 67 | | if (isInsideBoundaries) |
| | 68 | | { |
| 14 | 69 | | RemoveInvalidMeshEffect(meshesInfo); |
| 14 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 14 | 73 | | AddInvalidMeshEffect(meshesInfo); |
| 14 | 74 | | } |
| | 75 | |
|
| | 76 | | public void CleanFeedback() |
| | 77 | | { |
| 0 | 78 | | for ( int x = 0; x < currentMeshesInvalidated.Count; x++) |
| | 79 | | { |
| 0 | 80 | | RemoveInvalidMeshEffect(currentMeshesInvalidated[x]); |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | currentMeshesInvalidated.Clear(); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | void RemoveInvalidMeshEffect(MeshesInfo meshesInfo) |
| | 87 | | { |
| 14 | 88 | | if (meshesInfo == null || WasGameObjectInAValidPosition(meshesInfo.innerGameObject)) |
| 9 | 89 | | return; |
| | 90 | |
|
| 5 | 91 | | PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject); |
| | 92 | |
|
| 5 | 93 | | if (shapePoolableObjectBehaviour != null) |
| 0 | 94 | | shapePoolableObjectBehaviour.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials; |
| | 95 | |
|
| 5 | 96 | | var renderers = meshesInfo.renderers; |
| | 97 | |
|
| 5 | 98 | | if (renderers != null) |
| | 99 | | { |
| 26 | 100 | | for (int i = 0; i < renderers.Length; i++) |
| | 101 | | { |
| 8 | 102 | | if (invalidSubmeshes.Contains(renderers[i])) |
| 8 | 103 | | invalidSubmeshes.Remove(renderers[i]); |
| | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| 5 | 107 | | invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials(); |
| 5 | 108 | | currentMeshesInvalidated.Remove(meshesInfo); |
| 5 | 109 | | } |
| | 110 | |
|
| | 111 | | void AddInvalidMeshEffect(MeshesInfo meshesInfo) |
| | 112 | | { |
| 14 | 113 | | if (!WasGameObjectInAValidPosition(meshesInfo.innerGameObject)) |
| 0 | 114 | | return; |
| | 115 | |
|
| 14 | 116 | | if (currentMeshesInvalidated.Contains(meshesInfo)) |
| 0 | 117 | | return; |
| | 118 | |
|
| 14 | 119 | | InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo); |
| | 120 | |
|
| 24 | 121 | | invalidMeshInfo.OnResetMaterials = () => { invalidMeshesInfo.Remove(meshesInfo.innerGameObject); }; |
| | 122 | |
|
| 14 | 123 | | PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject); |
| 14 | 124 | | if (shapePoolableObjectBehaviour != null) |
| | 125 | | { |
| 0 | 126 | | shapePoolableObjectBehaviour.OnRelease -= invalidMeshInfo.ResetMaterials; |
| 0 | 127 | | shapePoolableObjectBehaviour.OnRelease += invalidMeshInfo.ResetMaterials; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | // Apply invalid material |
| 14 | 131 | | Renderer[] entityRenderers = meshesInfo.renderers; |
| 74 | 132 | | for (int i = 0; i < entityRenderers.Length; i++) |
| | 133 | | { |
| | 134 | | // Save original materials |
| 23 | 135 | | invalidMeshInfo.originalMaterials.Add(entityRenderers[i], entityRenderers[i].sharedMaterial); |
| | 136 | |
|
| 23 | 137 | | if (!invalidSubmeshes.Contains(entityRenderers[i])) |
| | 138 | | { |
| | 139 | | // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, p |
| | 140 | | // overload because we need to set the position and scale before parenting, to deal with scaled obje |
| 23 | 141 | | GameObject wireframeObject = GameObject.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME |
| 23 | 142 | | wireframeObject.transform.position = entityRenderers[i].bounds.center; |
| 23 | 143 | | wireframeObject.transform.localScale = entityRenderers[i].bounds.size * 1.01f; |
| 23 | 144 | | wireframeObject.transform.SetParent(meshesInfo.innerGameObject.transform); |
| | 145 | |
|
| 23 | 146 | | entityRenderers[i].sharedMaterial = invalidSubMeshMaterial; |
| | 147 | |
|
| 23 | 148 | | invalidMeshInfo.wireframeObjects.Add(wireframeObject); |
| 23 | 149 | | invalidSubmeshes.Add(entityRenderers[i]); |
| 23 | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 0 | 153 | | entityRenderers[i].sharedMaterial = invalidMeshMaterial; |
| | 154 | | } |
| | 155 | | } |
| | 156 | |
|
| 14 | 157 | | currentMeshesInvalidated.Add(meshesInfo); |
| 14 | 158 | | invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo); |
| 14 | 159 | | } |
| | 160 | |
|
| 28 | 161 | | public bool WasGameObjectInAValidPosition(GameObject gameObject) { return !invalidMeshesInfo.ContainsKey(gameObj |
| | 162 | |
|
| | 163 | | public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo) |
| | 164 | | { |
| 39 | 165 | | if (invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject)) |
| | 166 | | { |
| 0 | 167 | | return invalidMeshesInfo[meshesInfo.innerGameObject].originalMaterials.Values.ToList(); |
| | 168 | | } |
| | 169 | |
|
| 39 | 170 | | List<Material> result = new List<Material>(); |
| | 171 | |
|
| 180 | 172 | | for (int i = 0; i < meshesInfo.renderers.Length; i++) |
| | 173 | | { |
| 51 | 174 | | result.AddRange(meshesInfo.renderers[i].sharedMaterials); |
| | 175 | | } |
| | 176 | |
|
| 39 | 177 | | return result; |
| | 178 | | } |
| | 179 | | } |
| | 180 | | } |