| | 1 | | using DCL.Builder; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class BIWRaycastController : BIWController, IBIWRaycastController |
| | 6 | | { |
| | 7 | | public event System.Action<IBIWGizmosAxis> OnGizmosAxisPressed; |
| | 8 | |
|
| | 9 | | private Camera builderCamera; |
| | 10 | | private IBIWEntityHandler entityHandler; |
| | 11 | | private IBIWModeController modeController; |
| | 12 | |
|
| | 13 | | private const float RAYCAST_MAX_DISTANCE = 10000f; |
| | 14 | |
|
| 0 | 15 | | public LayerMask gizmoMask { get; private set; } |
| | 16 | |
|
| | 17 | | public override void Initialize(IContext context) |
| | 18 | | { |
| 31 | 19 | | base.Initialize(context); |
| | 20 | |
|
| 31 | 21 | | entityHandler = context.editorContext.entityHandler; |
| 31 | 22 | | modeController = context.editorContext.modeController; |
| 31 | 23 | | gizmoMask = BIWSettings.GIZMOS_LAYER; |
| 31 | 24 | | BIWInputWrapper.OnMouseDown += OnMouseDown; |
| | 25 | |
|
| 31 | 26 | | builderCamera = context.sceneReferences.mainCamera; |
| 31 | 27 | | } |
| | 28 | |
|
| | 29 | | public override void Dispose() |
| | 30 | | { |
| 31 | 31 | | base.Dispose(); |
| 31 | 32 | | BIWInputWrapper.OnMouseDown -= OnMouseDown; |
| 31 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnMouseDown(int buttonId, Vector3 mousePosition) |
| | 36 | | { |
| 0 | 37 | | if (buttonId != 0) |
| 0 | 38 | | return; |
| | 39 | |
|
| 0 | 40 | | CheckGizmosRaycast(mousePosition); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public BIWEntity GetEntityOnPointer() |
| | 44 | | { |
| 1 | 45 | | Camera camera = Camera.main; |
| | 46 | |
|
| 1 | 47 | | if (camera == null) |
| 0 | 48 | | return null; |
| | 49 | |
|
| | 50 | | RaycastHit hit; |
| 1 | 51 | | UnityEngine.Ray ray = camera.ScreenPointToRay(modeController.GetMousePosition()); |
| 1 | 52 | | float distanceToSelect = modeController.GetMaxDistanceToSelectEntities(); |
| | 53 | |
|
| 1 | 54 | | if (Physics.Raycast(ray, out hit, distanceToSelect, BIWSettings.COLLIDER_SELECTION_LAYER)) |
| | 55 | | { |
| 0 | 56 | | string entityID = hit.collider.gameObject.name; |
| | 57 | |
|
| 0 | 58 | | if (sceneToEdit.entities.ContainsKey(entityID)) |
| | 59 | | { |
| 0 | 60 | | return entityHandler.GetConvertedEntity(sceneToEdit.entities[entityID]); |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 1 | 64 | | return null; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public bool RayCastFloor(out Vector3 position) |
| | 68 | | { |
| | 69 | | RaycastHit hit; |
| | 70 | |
|
| 2 | 71 | | UnityEngine.Ray ray = GetMouseRay(Input.mousePosition); |
| | 72 | |
|
| 2 | 73 | | if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, BIWSettings.GROUND_LAYER)) |
| | 74 | | { |
| 2 | 75 | | position = hit.point; |
| 2 | 76 | | return true; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | position = Vector3.zero; |
| 0 | 80 | | return false; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public bool Raycast(Vector3 mousePosition, LayerMask mask, out RaycastHit hitInfo, System.Func<RaycastHit[], Raycast |
| | 84 | | { |
| | 85 | | RaycastHit[] hits; |
| 0 | 86 | | hits = Physics.RaycastAll(GetMouseRay(mousePosition), RAYCAST_MAX_DISTANCE, mask); |
| 0 | 87 | | if (hits.Length > 0) |
| | 88 | | { |
| 0 | 89 | | hitInfo = hitComparer(hits); |
| 0 | 90 | | return true; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | hitInfo = new RaycastHit(); |
| 0 | 94 | | return false; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public VoxelEntityHit GetCloserUnselectedVoxelEntityOnPointer() |
| | 98 | | { |
| | 99 | | RaycastHit[] hits; |
| 0 | 100 | | UnityEngine.Ray ray = GetMouseRay(Input.mousePosition); |
| | 101 | |
|
| 0 | 102 | | float currentDistance = 9999; |
| 0 | 103 | | VoxelEntityHit voxelEntityHit = null; |
| | 104 | |
|
| 0 | 105 | | hits = Physics.RaycastAll(ray, BIWSettings.RAYCAST_MAX_DISTANCE, BIWSettings.COLLIDER_SELECTION_LAYER); |
| | 106 | |
|
| 0 | 107 | | foreach (RaycastHit hit in hits) |
| | 108 | | { |
| 0 | 109 | | string entityID = hit.collider.gameObject.name; |
| | 110 | |
|
| 0 | 111 | | if (sceneToEdit.entities.ContainsKey(entityID)) |
| | 112 | | { |
| 0 | 113 | | BIWEntity entityToCheck = entityHandler.GetConvertedEntity(sceneToEdit.entities[entityID]); |
| | 114 | |
|
| 0 | 115 | | if (entityToCheck == null) |
| | 116 | | continue; |
| | 117 | |
|
| 0 | 118 | | if (entityToCheck.isSelected || !entityToCheck.gameObject.CompareTag(BIWSettings.VOXEL_TAG)) |
| | 119 | | continue; |
| | 120 | |
|
| 0 | 121 | | Camera camera = Camera.main; |
| 0 | 122 | | if (Vector3.Distance(camera.transform.position, entityToCheck.rootEntity.gameObject.transform.position) |
| | 123 | | continue; |
| | 124 | |
|
| 0 | 125 | | voxelEntityHit = new VoxelEntityHit(entityToCheck, hit); |
| 0 | 126 | | currentDistance = Vector3.Distance(camera.transform.position, entityToCheck.rootEntity.gameObject.transf |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | return voxelEntityHit; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | public Vector3 GetFloorPointAtMouse(Vector3 mousePosition) |
| | 134 | | { |
| 1 | 135 | | Camera camera = context.sceneReferences.mainCamera; |
| | 136 | |
|
| 1 | 137 | | if ( camera == null ) |
| 0 | 138 | | return Vector3.zero; |
| | 139 | |
|
| | 140 | | RaycastHit hit; |
| 1 | 141 | | UnityEngine.Ray ray = camera.ScreenPointToRay(mousePosition); |
| | 142 | |
|
| 1 | 143 | | if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, BIWSettings.GROUND_LAYER)) |
| 1 | 144 | | return hit.point; |
| | 145 | |
|
| 0 | 146 | | return Vector3.zero; |
| | 147 | | } |
| | 148 | |
|
| 2 | 149 | | public Ray GetMouseRay(Vector3 mousePosition) { return builderCamera.ScreenPointToRay(mousePosition); } |
| | 150 | |
|
| | 151 | | #region Gizmos |
| | 152 | |
|
| | 153 | | private void CheckGizmosRaycast(Vector3 mousePosition) |
| | 154 | | { |
| | 155 | | RaycastHit hit; |
| 0 | 156 | | if (Raycast(mousePosition, gizmoMask, out hit, CompareSelectionHit)) |
| | 157 | | { |
| 0 | 158 | | BIWGizmosAxis gizmosAxis = hit.collider.gameObject.GetComponent<BIWGizmosAxis>(); |
| 0 | 159 | | if (gizmosAxis != null) |
| 0 | 160 | | OnGizmosAxisPressed?.Invoke(gizmosAxis); |
| | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| 0 | 164 | | public bool RaycastToGizmos(Ray ray, out RaycastHit hitInfo) { return Physics.Raycast(ray, out hitInfo, RAYCAST_MAX_ |
| | 165 | |
|
| 0 | 166 | | public bool RaycastToGizmos(Vector3 mousePosition, out RaycastHit hitInfo) { return RaycastToGizmos(GetMouseRay(mous |
| | 167 | |
|
| | 168 | | private RaycastHit CompareSelectionHit(RaycastHit[] hits) |
| | 169 | | { |
| 0 | 170 | | RaycastHit closestHit = hits[0]; |
| | 171 | |
|
| 0 | 172 | | if (IsGizmoHit(closestHit)) // Gizmos has always priority |
| 0 | 173 | | return closestHit; |
| | 174 | |
|
| | 175 | | return closestHit; |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | private bool IsGizmoHit(RaycastHit hit) { return hit.collider.gameObject.GetComponent<BIWGizmosAxis>() != null; } |
| | 179 | |
|
| | 180 | | #endregion |
| | 181 | | } |