| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Configuration; |
| | 5 | | using DCL.ECSComponents; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public static class ECSComponentsUtils |
| | 11 | | { |
| | 12 | | public static void RemoveMaterialTransition(GameObject go) |
| | 13 | | { |
| 0 | 14 | | MaterialTransitionController[] materialTransitionControllers = go.GetComponentsInChildren<MaterialTransitionCont |
| | 15 | |
|
| 0 | 16 | | for (var i = 0; i < materialTransitionControllers.Length; i++) |
| | 17 | | { |
| 0 | 18 | | GameObject.Destroy(materialTransitionControllers[i]); |
| | 19 | | } |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | public static void UpdateMeshInfo(bool isVisible, bool withCollisions, bool isPointerBlocker, MeshesInfo meshesInfo) |
| | 23 | | { |
| 4 | 24 | | foreach (Renderer renderer in meshesInfo.renderers) |
| | 25 | | { |
| 1 | 26 | | renderer.enabled = isVisible; |
| | 27 | | } |
| | 28 | |
|
| 1 | 29 | | UpdateMeshInfoColliders(withCollisions, isPointerBlocker, meshesInfo); |
| 1 | 30 | | } |
| | 31 | |
|
| | 32 | | public static void UpdateMeshInfoColliders(bool withCollisions, bool isPointerBlocker, MeshesInfo meshesInfo) |
| | 33 | | { |
| 2 | 34 | | int colliderLayer = isPointerBlocker ? PhysicsLayers.onPointerEventLayer : PhysicsLayers.defaultLayer; |
| | 35 | |
|
| 4 | 36 | | foreach (Collider collider in meshesInfo.colliders) |
| | 37 | | { |
| 0 | 38 | | if (collider == null) continue; |
| | 39 | |
|
| 0 | 40 | | collider.enabled = withCollisions; |
| 0 | 41 | | collider.gameObject.layer = colliderLayer; |
| | 42 | | } |
| 2 | 43 | | } |
| | 44 | |
|
| | 45 | | public static MeshesInfo GeneratePrimitive(IDCLEntity entity, Mesh mesh, GameObject gameObject,bool visible, bool wi |
| | 46 | | { |
| | 47 | | // We create the unity components needed to generate the primitive |
| 26 | 48 | | MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>(); |
| 26 | 49 | | meshFilter.sharedMesh = mesh; |
| 26 | 50 | | MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>(); |
| 26 | 51 | | meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default"); |
| 26 | 52 | | Renderer[] renderers = new Renderer[] { meshRenderer }; |
| 26 | 53 | | MeshFilter[] meshFilters = new MeshFilter[] { meshFilter }; |
| | 54 | |
|
| | 55 | | // We generate the mesh info on the entity, so we can use on other systems |
| 26 | 56 | | MeshesInfo meshesInfo = new MeshesInfo(); |
| 26 | 57 | | meshesInfo.innerGameObject = gameObject; |
| 26 | 58 | | meshesInfo.meshRootGameObject = gameObject; |
| 26 | 59 | | meshesInfo.UpdateRenderersCollection(renderers,meshFilters); |
| | 60 | |
|
| | 61 | | // We generate the representation of the primitive and assign it to the meshInfo |
| 26 | 62 | | ShapeRepresentation shape = new ShapeRepresentation(); |
| 26 | 63 | | shape.UpdateModel(visible, withCollisions); |
| 26 | 64 | | meshesInfo.currentShape = shape; |
| | 65 | |
|
| | 66 | | // We should remove this relation in the future, the entity shouldn't know about the mesh |
| 26 | 67 | | entity.meshesInfo = meshesInfo; |
| | 68 | |
|
| | 69 | | // We update the rendering |
| 26 | 70 | | UpdateRenderer(entity, gameObject, renderers, visible, withCollisions, isPointerBlocker); |
| | 71 | |
|
| 26 | 72 | | return meshesInfo; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public static void UpdateRenderer(IDCLEntity entity, GameObject meshGameObject, Renderer[] renderers,bool visible, b |
| | 76 | | { |
| 27 | 77 | | ConfigurePrimitiveShapeVisibility(meshGameObject, visible,renderers); |
| | 78 | |
|
| | 79 | | // TODO: For better perfomance we should create the correct collider to each component shape instead of creating |
| 27 | 80 | | CollidersManager.i.ConfigureColliders(entity.meshRootGameObject,withCollisions, false, entity,CalculateColliders |
| 27 | 81 | | } |
| | 82 | |
|
| | 83 | | public static void ConfigurePrimitiveShapeVisibility(GameObject meshGameObject, bool shouldBeVisible, Renderer[] mes |
| | 84 | | { |
| 27 | 85 | | if (meshGameObject == null) |
| 0 | 86 | | return; |
| | 87 | |
|
| | 88 | | Collider onPointerEventCollider; |
| | 89 | |
|
| 108 | 90 | | for (var i = 0; i < meshRenderers.Length; i++) |
| | 91 | | { |
| 27 | 92 | | meshRenderers[i].enabled = shouldBeVisible; |
| | 93 | |
|
| 27 | 94 | | if (meshRenderers[i].transform.childCount > 0) |
| | 95 | | { |
| 0 | 96 | | onPointerEventCollider = meshRenderers[i].transform.GetChild(0).GetComponent<Collider>(); |
| | 97 | |
|
| 0 | 98 | | if (onPointerEventCollider != null && onPointerEventCollider.gameObject.layer == PhysicsLayers.onPointer |
| 0 | 99 | | onPointerEventCollider.enabled = shouldBeVisible; |
| | 100 | | } |
| | 101 | | } |
| 27 | 102 | | } |
| | 103 | |
|
| | 104 | | public static void RemoveRendereableFromDataStore(string sceneId, Rendereable rendereable) |
| | 105 | | { |
| 23 | 106 | | DataStore.i.sceneWorldObjects.RemoveRendereable(sceneId, rendereable); |
| 23 | 107 | | } |
| | 108 | |
|
| | 109 | | public static Rendereable AddRendereableToDataStore(string sceneId, long entityId, Mesh mesh, GameObject gameObject, |
| | 110 | | { |
| 28 | 111 | | int triangleCount = mesh.triangles.Length; |
| | 112 | |
|
| 28 | 113 | | var newRendereable = |
| | 114 | | new Rendereable() |
| | 115 | | { |
| | 116 | | container = gameObject, |
| | 117 | | totalTriangleCount = triangleCount, |
| | 118 | | meshes = new HashSet<Mesh>() { mesh }, |
| | 119 | | meshToTriangleCount = new Dictionary<Mesh, int>() { { mesh, triangleCount } } |
| | 120 | | }; |
| | 121 | |
|
| 28 | 122 | | newRendereable.renderers = new HashSet<Renderer>(renderers); |
| 28 | 123 | | newRendereable.ownerId = entityId; |
| | 124 | |
|
| 28 | 125 | | DataStore.i.sceneWorldObjects.AddRendereable(sceneId, newRendereable); |
| 28 | 126 | | return newRendereable; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public static void DisposeMeshInfo(MeshesInfo meshesInfo) |
| | 130 | | { |
| | 131 | | // Dispose renderer |
| 100 | 132 | | foreach (Renderer renderer in meshesInfo.renderers) |
| | 133 | | { |
| 24 | 134 | | Utils.CleanMaterials(renderer); |
| 24 | 135 | | GameObject.Destroy(renderer); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | // Dispose Mesh filter |
| 100 | 139 | | foreach (MeshFilter meshFilter in meshesInfo.meshFilters) |
| | 140 | | { |
| 24 | 141 | | GameObject.Destroy(meshFilter); |
| | 142 | | } |
| | 143 | |
|
| | 144 | | // Dispose collider |
| 52 | 145 | | foreach (Collider collider in meshesInfo.colliders) |
| | 146 | | { |
| 0 | 147 | | if (collider == null) continue; |
| | 148 | |
|
| 0 | 149 | | GameObject.Destroy(collider); |
| | 150 | | } |
| | 151 | |
|
| 26 | 152 | | meshesInfo.CleanReferences(); |
| 26 | 153 | | } |
| | 154 | |
|
| | 155 | | public static int CalculateNFTCollidersLayer(bool withCollisions, bool isPointerBlocker) |
| | 156 | | { |
| | 157 | | // We can't enable this layer changer logic until we redeploy all the builder and street scenes with the correct |
| | 158 | | /* if (!model.withCollisions && model.isPointerBlocker) |
| | 159 | | return PhysicsLayers.onPointerEventLayer; |
| | 160 | | else */ |
| 0 | 161 | | if (withCollisions && !isPointerBlocker) |
| 0 | 162 | | return PhysicsLayers.characterOnlyLayer; |
| | 163 | |
|
| 0 | 164 | | return PhysicsLayers.defaultLayer; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | private static int CalculateCollidersLayer(bool withCollisions, bool isPointerBlocker) |
| | 168 | | { |
| 27 | 169 | | if (isPointerBlocker) |
| 26 | 170 | | return PhysicsLayers.onPointerEventLayer; |
| 1 | 171 | | else if (withCollisions) |
| 0 | 172 | | return PhysicsLayers.characterOnlyLayer; |
| | 173 | |
|
| 1 | 174 | | return PhysicsLayers.defaultLayer; |
| | 175 | | } |
| | 176 | | } |