| | 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, EntityIdHelper helper) |
| | 24 | | { |
| 0 | 25 | | switch (query.raycastType) |
| | 26 | | { |
| | 27 | | case RaycastType.HIT_FIRST: |
| 0 | 28 | | HitFirst(query, helper); |
| 0 | 29 | | break; |
| | 30 | | case RaycastType.HIT_ALL: |
| 0 | 31 | | HitAll(query, helper); |
| | 32 | | break; |
| | 33 | | } |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | private void HitFirst(RaycastQuery query,EntityIdHelper helper) |
| | 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, |
| | 43 | | ~layerMaskTarget, scene); |
| 0 | 44 | | WebInterface.RayInfo rayInfo = GetRayInfoFromQuery(query); |
| | 45 | |
|
| 0 | 46 | | if (raycastInfo != null) |
| | 47 | | { |
| 0 | 48 | | CollidersManager.i.GetColliderInfo(raycastInfo.hitInfo.hit.collider, out ColliderInfo colliderInfo); |
| | 49 | |
|
| 0 | 50 | | hitEntity = new WebInterface.RaycastHitEntity() |
| | 51 | | { |
| | 52 | | didHit = raycastInfo.hitInfo.isValid, |
| | 53 | | hitNormal = raycastInfo.hitInfo.hit.normal, |
| | 54 | | hitPoint = raycastInfo.hitInfo.hit.point, |
| | 55 | | ray = rayInfo, |
| | 56 | | entity = new WebInterface.HitEntityInfo() |
| | 57 | | { |
| | 58 | | entityId = colliderInfo.entity != null ? helper.GetOriginalId(colliderInfo.entity.entityId) : " |
| | 59 | | meshName = colliderInfo.meshName |
| | 60 | | } |
| | 61 | | }; |
| 0 | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| 0 | 65 | | hitEntity = new WebInterface.RaycastHitEntity() |
| | 66 | | { |
| | 67 | | didHit = false, |
| | 68 | | ray = rayInfo |
| | 69 | | }; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | WebInterface.ReportRaycastHitFirstResult(query.sceneId, query.id, query.raycastType, hitEntity); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | private void HitAll(RaycastQuery query, EntityIdHelper helper) |
| | 76 | | { |
| 0 | 77 | | WebInterface.RaycastHitEntities raycastHitEntities = new WebInterface.RaycastHitEntities(); |
| | 78 | |
|
| 0 | 79 | | IParcelScene scene = null; |
| 0 | 80 | | Environment.i.world.state.TryGetScene(query.sceneId, out scene); |
| | 81 | |
|
| 0 | 82 | | RaycastResultInfoList raycastResults = raycastHandler.RaycastAll(GetUnityRayFromQuery(query), |
| | 83 | | query.ray.distance, ~layerMaskTarget, scene); |
| | 84 | |
|
| 0 | 85 | | raycastHitEntities.ray = GetRayInfoFromQuery(query); |
| | 86 | |
|
| 0 | 87 | | if (raycastResults.hitInfo != null && raycastResults.hitInfo.Length > 0) |
| | 88 | | { |
| 0 | 89 | | int count = raycastResults.hitInfo.Length; |
| 0 | 90 | | List<WebInterface.RaycastHitEntity> hitEntityInfoList = new List<WebInterface.RaycastHitEntity>(); |
| | 91 | |
|
| 0 | 92 | | for (int i = 0; i < count; i++) |
| | 93 | | { |
| 0 | 94 | | var hitInfo = raycastResults.hitInfo[i]; |
| 0 | 95 | | CollidersManager.i.GetColliderInfo(hitInfo.hit.collider, out ColliderInfo colliderInfo); |
| | 96 | |
|
| 0 | 97 | | if (hitInfo.isValid) |
| | 98 | | { |
| 0 | 99 | | WebInterface.RaycastHitEntity hitEntity = new WebInterface.RaycastHitEntity(); |
| 0 | 100 | | hitEntity.didHit = true; |
| 0 | 101 | | hitEntity.ray = raycastHitEntities.ray; |
| 0 | 102 | | hitEntity.hitPoint = hitInfo.hit.point; |
| 0 | 103 | | hitEntity.hitNormal = hitInfo.hit.normal; |
| 0 | 104 | | hitEntity.entity = new WebInterface.HitEntityInfo(); |
| 0 | 105 | | hitEntity.entity.entityId = colliderInfo.entity != null ? helper.GetOriginalId(colliderInfo.enti |
| 0 | 106 | | hitEntity.entity.meshName = colliderInfo.meshName; |
| 0 | 107 | | hitEntityInfoList.Add(hitEntity); |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | raycastHitEntities.didHit = true; |
| 0 | 112 | | raycastHitEntities.hitPoint = raycastResults.hitInfo[0].hit.point; |
| 0 | 113 | | raycastHitEntities.hitNormal = raycastResults.hitInfo[0].hit.normal; |
| 0 | 114 | | raycastHitEntities.entities = hitEntityInfoList.ToArray(); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | WebInterface.ReportRaycastHitAllResult(query.sceneId, query.id, query.raycastType, raycastHitEntities); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void HitFirstAvatar(RaycastQuery query) |
| | 121 | | { |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | private void HitAllAvatars(RaycastQuery query) |
| | 125 | | { |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private UnityEngine.Ray GetUnityRayFromQuery(RaycastQuery query) |
| | 129 | | { |
| 0 | 130 | | UnityEngine.Ray ray = new UnityEngine.Ray(); |
| | 131 | |
|
| 0 | 132 | | ray.origin = query.ray.unityOrigin; |
| 0 | 133 | | ray.direction = query.ray.direction; |
| | 134 | |
|
| 0 | 135 | | return ray; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | private WebInterface.RayInfo GetRayInfoFromQuery(RaycastQuery query) |
| | 139 | | { |
| 0 | 140 | | WebInterface.RayInfo rayInfo = new WebInterface.RayInfo(); |
| | 141 | |
|
| 0 | 142 | | rayInfo.direction = query.ray.direction; |
| 0 | 143 | | rayInfo.distance = query.ray.distance; |
| 0 | 144 | | rayInfo.origin = query.ray.origin; |
| | 145 | |
|
| 0 | 146 | | return rayInfo; |
| | 147 | | } |
| | 148 | | } |
| | 149 | | } |