| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECS7; |
| | 4 | | using DCL.ECS7.InternalComponents; |
| | 5 | | using DCL.ECSComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using DCL.Models; |
| | 9 | | using UnityEngine; |
| | 10 | | using Ray = UnityEngine.Ray; |
| | 11 | | using RaycastHit = UnityEngine.RaycastHit; |
| | 12 | | using Vector3 = Decentraland.Common.Vector3; |
| | 13 | |
|
| | 14 | | namespace DCLPlugins.ECSComponents.Raycast |
| | 15 | | { |
| | 16 | | public class RaycastComponentHandler : IECSComponentHandler<PBRaycast> |
| | 17 | | { |
| | 18 | | // Lamport timestamp for raycast responses, we may want to move it to scene-level in the future |
| | 19 | | private static int responseTimestamp = 0; |
| | 20 | |
|
| | 21 | | private IECSComponentWriter componentWriter; |
| | 22 | | private LayerMask raycastLayerMaskTarget; |
| | 23 | | private IInternalECSComponent<InternalColliders> physicsColliderComponent; |
| | 24 | |
|
| 4 | 25 | | public RaycastComponentHandler(IECSComponentWriter componentWriter, IInternalECSComponent<InternalColliders> phy |
| | 26 | | { |
| 4 | 27 | | this.componentWriter = componentWriter; |
| 4 | 28 | | this.physicsColliderComponent = physicsColliderComponent; |
| | 29 | |
|
| | 30 | | // Cast all layers except the OnPointerEvent one |
| 4 | 31 | | raycastLayerMaskTarget = ~(1 << PhysicsLayers.onPointerEventLayer); |
| 4 | 32 | | } |
| | 33 | |
|
| | 34 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 35 | | { |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 39 | | { |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBRaycast model) |
| | 43 | | { |
| 6 | 44 | | var worldGridPosition = Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositi |
| 6 | 45 | | UnityEngine.Vector3 origin = new UnityEngine.Vector3(model.Origin.X, model.Origin.Y, model.Origin.Z); |
| 6 | 46 | | Ray ray = new Ray(); |
| 6 | 47 | | ray.origin = PositionUtils.WorldToUnityPosition(origin + worldGridPosition); |
| 6 | 48 | | ray.direction = new UnityEngine.Vector3(model.Direction.X, model.Direction.Y, model.Direction.Z); |
| | 49 | |
|
| 6 | 50 | | PBRaycastResult result = new PBRaycastResult(); |
| 6 | 51 | | result.Direction = model.Direction.Clone(); |
| 6 | 52 | | result.Origin = model.Origin.Clone(); |
| 6 | 53 | | result.Timestamp = responseTimestamp; |
| 6 | 54 | | responseTimestamp++; |
| | 55 | |
|
| 6 | 56 | | RaycastHit[] hits = null; |
| 6 | 57 | | if (model.QueryType == RaycastQueryType.RqtHitFirst) |
| | 58 | | { |
| 5 | 59 | | bool hasHit = Physics.Raycast(ray, out RaycastHit hit, model.MaxDistance, raycastLayerMaskTarget); |
| 5 | 60 | | if (hasHit) |
| | 61 | | { |
| 4 | 62 | | hits = new RaycastHit[1]; |
| 4 | 63 | | hits[0] = hit; |
| | 64 | | } |
| | 65 | | } |
| 1 | 66 | | else if (model.QueryType == RaycastQueryType.RqtQueryAll) |
| | 67 | | { |
| 1 | 68 | | hits = Physics.RaycastAll(ray, model.MaxDistance, raycastLayerMaskTarget); |
| | 69 | | } |
| | 70 | |
|
| 6 | 71 | | if (hits != null) |
| | 72 | | { |
| 22 | 73 | | for (int i = 0; i < hits.Length; i++) |
| | 74 | | { |
| 6 | 75 | | IDCLEntity collisionEntity = null; |
| | 76 | |
|
| 32 | 77 | | foreach (var currentEntity in scene.entities.Values) |
| | 78 | | { |
| 13 | 79 | | var collider = physicsColliderComponent.GetFor(scene, currentEntity); |
| 13 | 80 | | if (collider == null) |
| | 81 | | continue; |
| | 82 | |
|
| 7 | 83 | | if (collider.model.colliders.Contains(hits[i].collider)) |
| | 84 | | { |
| 6 | 85 | | collisionEntity = currentEntity; |
| 6 | 86 | | break; |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| 6 | 90 | | DCL.ECSComponents.RaycastHit hit = new DCL.ECSComponents.RaycastHit(); |
| 6 | 91 | | hit.MeshName = hits[i].collider.name; |
| 6 | 92 | | hit.Length = hits[i].distance; |
| 6 | 93 | | hit.Origin = model.Origin.Clone(); |
| | 94 | |
|
| 6 | 95 | | var worldPosition = DCL.WorldStateUtils.ConvertUnityToScenePosition(hits[i].point, scene); |
| 6 | 96 | | hit.Position = new Vector3(); |
| 6 | 97 | | hit.Position.X = worldPosition.x; |
| 6 | 98 | | hit.Position.Y = worldPosition.y; |
| 6 | 99 | | hit.Position.Z = worldPosition.z; |
| | 100 | |
|
| 6 | 101 | | hit.NormalHit = new Vector3(); |
| 6 | 102 | | hit.NormalHit.X = hits[i].normal.x; |
| 6 | 103 | | hit.NormalHit.Y = hits[i].normal.y; |
| 6 | 104 | | hit.NormalHit.Z = hits[i].normal.z; |
| | 105 | |
|
| 6 | 106 | | if (collisionEntity != null) |
| | 107 | | { |
| 6 | 108 | | hit.EntityId = (int)collisionEntity.entityId; |
| | 109 | | } |
| | 110 | |
|
| 6 | 111 | | result.Hits.Add(hit); |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 6 | 115 | | componentWriter.PutComponent( |
| | 116 | | scene.sceneData.sceneNumber, entity.entityId, |
| | 117 | | ComponentID.RAYCAST_RESULT, |
| | 118 | | result, |
| | 119 | | ECSComponentWriteType.WRITE_STATE_LOCALLY | ECSComponentWriteType.SEND_TO_SCENE |
| | 120 | | ); |
| 6 | 121 | | } |
| | 122 | |
|
| | 123 | | public static void ResetRaycastResponseTimestamp() |
| | 124 | | { |
| 1 | 125 | | responseTimestamp = 0; |
| 1 | 126 | | } |
| | 127 | | } |
| | 128 | | } |