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