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