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