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