| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.ECS7; |
| | 5 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 6 | | using DCL.ECS7.InternalComponents; |
| | 7 | | using DCL.ECSComponents; |
| | 8 | | using DCL.ECSComponents.Utils; |
| | 9 | | using DCL.ECSRuntime; |
| | 10 | | using DCL.Helpers; |
| | 11 | | using DCL.Models; |
| | 12 | | using System.Collections.Generic; |
| | 13 | | using UnityEngine; |
| | 14 | | using Ray = UnityEngine.Ray; |
| | 15 | | using RaycastHit = DCL.ECSComponents.RaycastHit; |
| | 16 | | using Vector3 = Decentraland.Common.Vector3; |
| | 17 | |
|
| | 18 | | namespace ECSSystems.ECSRaycastSystem |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// This system executes the needed raycasts on every entity that has a Raycast component and |
| | 22 | | /// adds a RaycastResult component to the corresponding raycasting entities, complying with |
| | 23 | | /// ADR-200: https://adr.decentraland.org/adr/ADR-200 |
| | 24 | | /// </summary> |
| | 25 | | public class ECSRaycastSystem |
| | 26 | | { |
| | 27 | | private readonly IInternalECSComponent<InternalRaycast> internalRaycastComponent; |
| | 28 | | private readonly IInternalECSComponent<InternalColliders> physicsColliderComponent; |
| | 29 | | private readonly IInternalECSComponent<InternalColliders> onPointerColliderComponent; |
| | 30 | | private readonly IInternalECSComponent<InternalColliders> customLayerColliderComponent; |
| | 31 | | private readonly IInternalECSComponent<InternalEngineInfo> engineInfoComponent; |
| | 32 | | private readonly IReadOnlyDictionary<int, ComponentWriter> componentsWriter; |
| | 33 | | private readonly WrappedComponentPool<IWrappedComponent<PBRaycastResult>> raycastResultPool; |
| | 34 | |
|
| 29 | 35 | | public ECSRaycastSystem( |
| | 36 | | IInternalECSComponent<InternalRaycast> internalRaycastComponent, |
| | 37 | | IInternalECSComponent<InternalColliders> physicsColliderComponent, |
| | 38 | | IInternalECSComponent<InternalColliders> onPointerColliderComponent, |
| | 39 | | IInternalECSComponent<InternalColliders> customLayerColliderComponent, |
| | 40 | | IInternalECSComponent<InternalEngineInfo> engineInfoComponent, |
| | 41 | | IReadOnlyDictionary<int, ComponentWriter> componentsWriter, |
| | 42 | | WrappedComponentPool<IWrappedComponent<PBRaycastResult>> raycastResultPool) |
| | 43 | | { |
| 29 | 44 | | this.internalRaycastComponent = internalRaycastComponent; |
| 29 | 45 | | this.physicsColliderComponent = physicsColliderComponent; |
| 29 | 46 | | this.onPointerColliderComponent = onPointerColliderComponent; |
| 29 | 47 | | this.customLayerColliderComponent = customLayerColliderComponent; |
| 29 | 48 | | this.engineInfoComponent = engineInfoComponent; |
| 29 | 49 | | this.componentsWriter = componentsWriter; |
| 29 | 50 | | this.raycastResultPool = raycastResultPool; |
| 29 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Update() |
| | 54 | | { |
| 31 | 55 | | var raycastComponentGroup = internalRaycastComponent.GetForAll(); |
| 31 | 56 | | int entitiesCount = raycastComponentGroup.Count; |
| | 57 | |
|
| | 58 | | // Note: the components are traversed backwards as some internal raycast components may be removed during it |
| 122 | 59 | | for (int i = entitiesCount - 1; i >= 0 ; i--) |
| | 60 | | { |
| 30 | 61 | | PBRaycast model = raycastComponentGroup[i].value.model.raycastModel; |
| 30 | 62 | | IDCLEntity entity = raycastComponentGroup[i].value.entity; |
| 30 | 63 | | IParcelScene scene = raycastComponentGroup[i].value.scene; |
| | 64 | |
|
| | 65 | | // Wait until scene initial GLTFs are finished loading (handled at SceneStateHandler) |
| 30 | 66 | | if (!scene.IsInitMessageDone()) continue; |
| | 67 | |
|
| 30 | 68 | | if (model.QueryType == RaycastQueryType.RqtNone) |
| | 69 | | { |
| 0 | 70 | | internalRaycastComponent.RemoveFor(scene, entity); |
| 0 | 71 | | continue; |
| | 72 | | } |
| | 73 | |
|
| 30 | 74 | | Ray ray = CreateRay(scene, entity, model); |
| 30 | 75 | | if (ray.direction == UnityEngine.Vector3.zero) |
| | 76 | | { |
| 0 | 77 | | Debug.LogError("Raycast error: direction cannot be Vector3.Zero(); Raycast aborted."); |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 30 | 81 | | var pooledRaycastResult = raycastResultPool.Get(); |
| 30 | 82 | | PBRaycastResult result = pooledRaycastResult.WrappedComponent.Model; |
| 30 | 83 | | result.Direction = ProtoConvertUtils.UnityVectorToPBVector(ray.direction); |
| 30 | 84 | | result.GlobalOrigin = ProtoConvertUtils.UnityVectorToPBVector(ray.origin); |
| 30 | 85 | | result.Timestamp = model.Timestamp; |
| 30 | 86 | | result.TickNumber = engineInfoComponent.GetFor(scene, SpecialEntityId.SCENE_ROOT_ENTITY).Value.model.Sce |
| | 87 | |
|
| | 88 | | // Hit everything by default except 'OnPointer' layer |
| 30 | 89 | | int raycastUnityLayerMask = CreateRaycastLayerMask(model); |
| | 90 | |
|
| 30 | 91 | | UnityEngine.RaycastHit[] hits = null; |
| | 92 | |
|
| | 93 | | // RaycastAll is used because 'Default' layer represents a combination of ClPointer and ClPhysics |
| | 94 | | // and 'SDKCustomLayer' layer represents 8 different SDK layers: ClCustom1~8 |
| 30 | 95 | | hits = Physics.RaycastAll(ray, model.MaxDistance, raycastUnityLayerMask); |
| 30 | 96 | | if (hits != null) |
| | 97 | | { |
| 30 | 98 | | RaycastHit closestHit = null; |
| 132 | 99 | | for (int j = 0; j < hits.Length; j++) |
| | 100 | | { |
| 36 | 101 | | UnityEngine.RaycastHit currentHit = hits[j]; |
| 36 | 102 | | uint raycastSDKCollisionMask = model.GetCollisionMask(); |
| 36 | 103 | | KeyValuePair<IDCLEntity, uint>? hitEntity = null; |
| | 104 | |
|
| 36 | 105 | | if (LayerMaskUtils.IsInLayerMask(raycastSDKCollisionMask, (int)ColliderLayer.ClPhysics)) |
| 19 | 106 | | hitEntity = FindMatchingColliderEntity(physicsColliderComponent.GetForAll(), currentHit.coll |
| 36 | 107 | | if(hitEntity == null && LayerMaskUtils.IsInLayerMask(raycastSDKCollisionMask, (int)ColliderLayer |
| 5 | 108 | | hitEntity = FindMatchingColliderEntity(onPointerColliderComponent.GetForAll(), currentHit.co |
| 36 | 109 | | if(hitEntity == null && LayerMaskUtils.LayerMaskHasAnySDKCustomLayer(raycastSDKCollisionMask)) |
| 17 | 110 | | hitEntity = FindMatchingColliderEntity(customLayerColliderComponent.GetForAll(), currentHit. |
| | 111 | |
|
| 36 | 112 | | var hit = CreateSDKRaycastHit(scene, model, currentHit, hitEntity, result.GlobalOrigin); |
| 36 | 113 | | if (hit == null) continue; |
| | 114 | |
|
| 23 | 115 | | if (model.QueryType == RaycastQueryType.RqtHitFirst) |
| | 116 | | { |
| | 117 | | // Since Unity's RaycastAll() resulting collection order is random (not based on hit distanc |
| | 118 | | // the closest hit has to be identified and populate the final collection with only that one |
| 14 | 119 | | if (closestHit == null || currentHit.distance < closestHit.Length) |
| 7 | 120 | | closestHit = hit; |
| | 121 | | } |
| | 122 | | else |
| | 123 | | { |
| 9 | 124 | | result.Hits.Add(hit); |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| 30 | 128 | | if (model.QueryType == RaycastQueryType.RqtHitFirst && closestHit != null) |
| 6 | 129 | | result.Hits.Add(closestHit); |
| | 130 | | } |
| | 131 | |
|
| 30 | 132 | | if (componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer)) |
| | 133 | | { |
| 27 | 134 | | writer.Put(entity.entityId, ComponentID.RAYCAST_RESULT, pooledRaycastResult); |
| | 135 | | } |
| | 136 | |
|
| | 137 | | // If the raycast on that entity isn't 'continuous' the internal component is removed and won't |
| | 138 | | // be used for raycasting on the next system iteration. If its timestamp changes, the handler |
| | 139 | | // adds the internal raycast component again. |
| 30 | 140 | | if(!model.HasContinuous || !model.Continuous) |
| 27 | 141 | | internalRaycastComponent.RemoveFor(scene, entity); |
| | 142 | | } |
| 31 | 143 | | } |
| | 144 | |
|
| | 145 | | private KeyValuePair<IDCLEntity, uint>? FindMatchingColliderEntity(IReadOnlyList<KeyValueSetTriplet<IParcelScene |
| | 146 | | { |
| 41 | 147 | | int componentsCount = componentGroup.Count; |
| 174 | 148 | | for (int i = 0; i < componentsCount; i++) |
| | 149 | | { |
| 76 | 150 | | var colliders = componentGroup[i].value.model.colliders; |
| 76 | 151 | | if (colliders.ContainsKey(targetCollider)) |
| | 152 | | { |
| 30 | 153 | | return new KeyValuePair<IDCLEntity, uint>(componentGroup[i].value.entity, colliders[targetCollider]) |
| | 154 | | } |
| | 155 | | } |
| | 156 | |
|
| 11 | 157 | | return null; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | private Ray CreateRay(IParcelScene scene, IDCLEntity entity, PBRaycast model) |
| | 161 | | { |
| 30 | 162 | | Transform entityTransform = entity.gameObject.transform; |
| 30 | 163 | | UnityEngine.Vector3 sceneWorldPosition = Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sce |
| 30 | 164 | | UnityEngine.Vector3 sceneUnityPosition = PositionUtils.WorldToUnityPosition(sceneWorldPosition); |
| 30 | 165 | | UnityEngine.Vector3 rayOrigin = entityTransform.position + (model.OriginOffset != null ? ProtoConvertUtils.P |
| | 166 | |
|
| 30 | 167 | | UnityEngine.Vector3 rayDirection = UnityEngine.Vector3.forward; |
| 30 | 168 | | switch (model.DirectionCase) |
| | 169 | | { |
| | 170 | | case PBRaycast.DirectionOneofCase.LocalDirection: |
| | 171 | | // The direction of the ray in local coordinates (relative to the origin point) |
| 5 | 172 | | UnityEngine.Vector3 localDirectionVector = entityTransform.rotation * ProtoConvertUtils.PBVectorToUn |
| 5 | 173 | | if(localDirectionVector != UnityEngine.Vector3.zero) |
| 4 | 174 | | rayDirection = localDirectionVector; |
| 4 | 175 | | break; |
| | 176 | | case PBRaycast.DirectionOneofCase.GlobalTarget: |
| | 177 | | // Target position to cast the ray towards, in global coordinates |
| 3 | 178 | | UnityEngine.Vector3 directionTowardsGlobalTarget = sceneUnityPosition + ProtoConvertUtils.PBVectorTo |
| 3 | 179 | | if(directionTowardsGlobalTarget != UnityEngine.Vector3.zero) |
| 2 | 180 | | rayDirection = directionTowardsGlobalTarget; |
| 2 | 181 | | break; |
| | 182 | | case PBRaycast.DirectionOneofCase.TargetEntity: |
| | 183 | | // Target entity to cast the ray towards |
| 2 | 184 | | if (scene.entities.TryGetValue(model.TargetEntity, out IDCLEntity targetEntity)) |
| | 185 | | { |
| 1 | 186 | | rayDirection = targetEntity.gameObject.transform.position - entityTransform.position; |
| | 187 | | } |
| | 188 | | else |
| | 189 | | { |
| 1 | 190 | | UnityEngine.Vector3 directionTowardsSceneRootEntity = scene.GetSceneTransform().position - entit |
| 1 | 191 | | if (directionTowardsSceneRootEntity != UnityEngine.Vector3.zero) |
| 1 | 192 | | rayDirection = directionTowardsSceneRootEntity; |
| | 193 | | } |
| 1 | 194 | | break; |
| | 195 | | case PBRaycast.DirectionOneofCase.GlobalDirection: |
| | 196 | | // The direction of the ray in global coordinates |
| 20 | 197 | | UnityEngine.Vector3 globalDirectionVector = ProtoConvertUtils.PBVectorToUnityVector(model.GlobalDire |
| 20 | 198 | | if(globalDirectionVector != UnityEngine.Vector3.zero) |
| 19 | 199 | | rayDirection = globalDirectionVector; |
| | 200 | | break; |
| | 201 | | } |
| | 202 | |
|
| 30 | 203 | | Ray ray = new Ray |
| | 204 | | { |
| | 205 | | origin = rayOrigin, |
| | 206 | | direction = rayDirection.normalized |
| | 207 | | }; |
| 30 | 208 | | return ray; |
| | 209 | | } |
| | 210 | |
|
| | 211 | | private RaycastHit CreateSDKRaycastHit(IParcelScene scene, PBRaycast model, UnityEngine.RaycastHit unityRaycastH |
| | 212 | | { |
| 36 | 213 | | RaycastHit hit = null; |
| 36 | 214 | | uint modelCollisionLayerMask = model.GetCollisionMask(); |
| | 215 | |
|
| 36 | 216 | | if (hitEntity != null) // SDK7 entity, otherwise the ray hit an SDK6 entity |
| | 217 | | { |
| | 218 | | // TODO: figure out how we can cache or pool this hit instance to reduce allocations. |
| | 219 | | // since is part of a protobuf message it life span is uncertain, it could be disposed |
| | 220 | | // after message is sent to the scene or dropped for a new message |
| 30 | 221 | | IDCLEntity entity = hitEntity.Value.Key; |
| 30 | 222 | | uint collisionMask = hitEntity.Value.Value; |
| | 223 | |
|
| | 224 | | // hitEntity has to be evaluated since 'Default' layer represents a combination of ClPointer |
| | 225 | | // and ClPhysics, and 'SDKCustomLayer' layer represents 8 different SDK layers: ClCustom1~8 |
| 30 | 226 | | if ((modelCollisionLayerMask & collisionMask) == 0) |
| 7 | 227 | | return null; |
| | 228 | |
|
| 23 | 229 | | hit = new RaycastHit(); |
| 23 | 230 | | hit.EntityId = (uint)entity.entityId; |
| | 231 | | } |
| 6 | 232 | | else if ((!scene.isPersistent && !scene.isPortableExperience) |
| | 233 | | || (modelCollisionLayerMask & (int)ColliderLayer.ClPhysics) == 0) // 'Physics' layer for non-sdk7 c |
| | 234 | | { |
| 6 | 235 | | return null; |
| | 236 | | } |
| | 237 | |
|
| 23 | 238 | | hit ??= new RaycastHit(); |
| 23 | 239 | | hit.MeshName = unityRaycastHit.collider.name; |
| 23 | 240 | | hit.Length = unityRaycastHit.distance; |
| 23 | 241 | | hit.GlobalOrigin = globalOrigin; |
| | 242 | |
|
| 23 | 243 | | var worldPosition = WorldStateUtils.ConvertUnityToScenePosition(unityRaycastHit.point, scene); |
| 23 | 244 | | hit.Position = new Vector3(); |
| 23 | 245 | | hit.Position.X = worldPosition.x; |
| 23 | 246 | | hit.Position.Y = worldPosition.y; |
| 23 | 247 | | hit.Position.Z = worldPosition.z; |
| | 248 | |
|
| 23 | 249 | | hit.NormalHit = new Vector3(); |
| 23 | 250 | | hit.NormalHit.X = unityRaycastHit.normal.x; |
| 23 | 251 | | hit.NormalHit.Y = unityRaycastHit.normal.y; |
| 23 | 252 | | hit.NormalHit.Z = unityRaycastHit.normal.z; |
| | 253 | |
|
| 23 | 254 | | return hit; |
| | 255 | | } |
| | 256 | |
|
| | 257 | | private int CreateRaycastLayerMask(PBRaycast model) |
| | 258 | | { |
| 30 | 259 | | uint sdkLayerMask = 0; |
| 30 | 260 | | int unityLayerMask = (1 << PhysicsLayers.characterLayer); |
| 30 | 261 | | sdkLayerMask = model.GetCollisionMask(); |
| | 262 | |
|
| 30 | 263 | | if (sdkLayerMask == (int)ColliderLayer.ClPointer) |
| | 264 | | { |
| 2 | 265 | | unityLayerMask |= (1 << PhysicsLayers.onPointerEventLayer); |
| 2 | 266 | | unityLayerMask |= (1 << PhysicsLayers.defaultLayer); // Pointer + Physics ends up in 'Default' GO Layer |
| 2 | 267 | | return unityLayerMask; |
| | 268 | | } |
| | 269 | |
|
| 28 | 270 | | if (sdkLayerMask == (int)ColliderLayer.ClPhysics) |
| | 271 | | { |
| 24 | 272 | | unityLayerMask |= (1 << PhysicsLayers.characterOnlyLayer); |
| 24 | 273 | | unityLayerMask |= (1 << PhysicsLayers.defaultLayer); // Pointer + Physics ends up in 'Default' GO Layer |
| 24 | 274 | | return unityLayerMask; |
| | 275 | | } |
| | 276 | |
|
| 4 | 277 | | unityLayerMask |= (1 << PhysicsLayers.defaultLayer) |
| | 278 | | | (1 << PhysicsLayers.characterOnlyLayer) |
| | 279 | | | (1 << PhysicsLayers.onPointerEventLayer); |
| | 280 | |
|
| 4 | 281 | | if (LayerMaskUtils.LayerMaskHasAnySDKCustomLayer(sdkLayerMask)) |
| | 282 | | { |
| 4 | 283 | | unityLayerMask |= (1 << PhysicsLayers.sdkCustomLayer); |
| | 284 | | } |
| | 285 | |
|
| 4 | 286 | | return unityLayerMask; |
| | 287 | | } |
| | 288 | | } |
| | 289 | | } |