| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using DCL.Components; |
| | 6 | | using DCL.Controllers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class PhysicsCast : Singleton<PhysicsCast> |
| | 12 | | { |
| | 13 | | private LayerMask layerMaskTarget; |
| | 14 | |
|
| | 15 | | private IRaycastHandler raycastHandler; |
| | 16 | |
|
| 0 | 17 | | public PhysicsCast() |
| | 18 | | { |
| 0 | 19 | | layerMaskTarget = 1 << LayerMask.NameToLayer("OnPointerEvent"); |
| 0 | 20 | | raycastHandler = new RaycastHandler(); |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | public void Query(RaycastQuery query) |
| | 24 | | { |
| 0 | 25 | | switch (query.raycastType) |
| | 26 | | { |
| | 27 | | case RaycastType.HIT_FIRST: |
| 0 | 28 | | HitFirst(query); |
| 0 | 29 | | break; |
| | 30 | | case RaycastType.HIT_ALL: |
| 0 | 31 | | HitAll(query); |
| | 32 | | break; |
| | 33 | | } |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | private void HitFirst(RaycastQuery query) |
| | 37 | | { |
| | 38 | | WebInterface.RaycastHitEntity hitEntity; |
| | 39 | |
|
| 0 | 40 | | Environment.i.world.state.TryGetScene(query.sceneId, out IParcelScene scene); |
| | 41 | |
|
| 0 | 42 | | RaycastResultInfo raycastInfo = raycastHandler.Raycast(GetUnityRayFromQuery(query), query.ray.distance, ~lay |
| 0 | 43 | | WebInterface.RayInfo rayInfo = GetRayInfoFromQuery(query); |
| | 44 | |
|
| 0 | 45 | | if (raycastInfo != null) |
| | 46 | | { |
| 0 | 47 | | CollidersManager.i.GetColliderInfo(raycastInfo.hitInfo.hit.collider, out ColliderInfo colliderInfo); |
| | 48 | |
|
| 0 | 49 | | hitEntity = new WebInterface.RaycastHitEntity() |
| | 50 | | { |
| | 51 | | didHit = raycastInfo.hitInfo.isValid, |
| | 52 | | hitNormal = raycastInfo.hitInfo.hit.normal, |
| | 53 | | hitPoint = raycastInfo.hitInfo.hit.point, |
| | 54 | | ray = rayInfo, |
| | 55 | | entity = new WebInterface.HitEntityInfo() |
| | 56 | | { |
| | 57 | | entityId = colliderInfo.entity != null ? colliderInfo.entity.entityId : null, |
| | 58 | | meshName = colliderInfo.meshName |
| | 59 | | } |
| | 60 | | }; |
| 0 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 0 | 64 | | hitEntity = new WebInterface.RaycastHitEntity() |
| | 65 | | { |
| | 66 | | didHit = false, |
| | 67 | | ray = rayInfo |
| | 68 | | }; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | WebInterface.ReportRaycastHitFirstResult(query.sceneId, query.id, query.raycastType, hitEntity); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void HitAll(RaycastQuery query) |
| | 75 | | { |
| 0 | 76 | | WebInterface.RaycastHitEntities raycastHitEntities = new WebInterface.RaycastHitEntities(); |
| | 77 | |
|
| 0 | 78 | | IParcelScene scene = null; |
| 0 | 79 | | Environment.i.world.state.TryGetScene(query.sceneId, out scene); |
| | 80 | |
|
| 0 | 81 | | RaycastResultInfoList raycastResults = raycastHandler.RaycastAll(GetUnityRayFromQuery(query), query.ray.dist |
| | 82 | |
|
| 0 | 83 | | raycastHitEntities.ray = GetRayInfoFromQuery(query); |
| | 84 | |
|
| 0 | 85 | | if (raycastResults.hitInfo != null && raycastResults.hitInfo.Length > 0) |
| | 86 | | { |
| 0 | 87 | | int count = raycastResults.hitInfo.Length; |
| 0 | 88 | | List<WebInterface.RaycastHitEntity> hitEntityInfoList = new List<WebInterface.RaycastHitEntity>(); |
| | 89 | |
|
| 0 | 90 | | for (int i = 0; i < count; i++) |
| | 91 | | { |
| 0 | 92 | | var hitInfo = raycastResults.hitInfo[i]; |
| 0 | 93 | | CollidersManager.i.GetColliderInfo(hitInfo.hit.collider, out ColliderInfo colliderInfo); |
| | 94 | |
|
| 0 | 95 | | if (hitInfo.isValid) |
| | 96 | | { |
| 0 | 97 | | WebInterface.RaycastHitEntity hitEntity = new WebInterface.RaycastHitEntity(); |
| 0 | 98 | | hitEntity.didHit = true; |
| 0 | 99 | | hitEntity.ray = raycastHitEntities.ray; |
| 0 | 100 | | hitEntity.hitPoint = hitInfo.hit.point; |
| 0 | 101 | | hitEntity.hitNormal = hitInfo.hit.normal; |
| 0 | 102 | | hitEntity.entity = new WebInterface.HitEntityInfo(); |
| 0 | 103 | | hitEntity.entity.entityId = colliderInfo.entity != null ? colliderInfo.entity.entityId : null; |
| 0 | 104 | | hitEntity.entity.meshName = colliderInfo.meshName; |
| 0 | 105 | | hitEntityInfoList.Add(hitEntity); |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | raycastHitEntities.didHit = true; |
| 0 | 110 | | raycastHitEntities.hitPoint = raycastResults.hitInfo[0].hit.point; |
| 0 | 111 | | raycastHitEntities.hitNormal = raycastResults.hitInfo[0].hit.normal; |
| 0 | 112 | | raycastHitEntities.entities = hitEntityInfoList.ToArray(); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | WebInterface.ReportRaycastHitAllResult(query.sceneId, query.id, query.raycastType, raycastHitEntities); |
| 0 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | private void HitFirstAvatar(RaycastQuery query) { } |
| | 119 | |
|
| 0 | 120 | | private void HitAllAvatars(RaycastQuery query) { } |
| | 121 | |
|
| | 122 | | private UnityEngine.Ray GetUnityRayFromQuery(RaycastQuery query) |
| | 123 | | { |
| 0 | 124 | | UnityEngine.Ray ray = new UnityEngine.Ray(); |
| | 125 | |
|
| 0 | 126 | | ray.origin = query.ray.unityOrigin; |
| 0 | 127 | | ray.direction = query.ray.direction; |
| | 128 | |
|
| 0 | 129 | | return ray; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | private WebInterface.RayInfo GetRayInfoFromQuery(RaycastQuery query) |
| | 133 | | { |
| 0 | 134 | | WebInterface.RayInfo rayInfo = new WebInterface.RayInfo(); |
| | 135 | |
|
| 0 | 136 | | rayInfo.direction = query.ray.direction; |
| 0 | 137 | | rayInfo.distance = query.ray.distance; |
| 0 | 138 | | rayInfo.origin = query.ray.origin; |
| | 139 | |
|
| 0 | 140 | | return rayInfo; |
| | 141 | | } |
| | 142 | | } |
| | 143 | | } |