| | 1 | | using UnityEngine; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Components; |
| | 4 | |
|
| | 5 | | namespace Builder |
| | 6 | | { |
| | 7 | | public class DCLBuilderRaycast : MonoBehaviour |
| | 8 | | { |
| | 9 | | public const string LAYER_GIZMOS = "Gizmo"; |
| | 10 | | public const string LAYER_SELECTION = "Selection"; |
| | 11 | |
|
| | 12 | | public Camera builderCamera; |
| | 13 | |
|
| | 14 | | private const float RAYCAST_MAX_DISTANCE = 10000f; |
| | 15 | |
|
| 0 | 16 | | public LayerMask defaultMask { get; private set; } |
| 0 | 17 | | public LayerMask gizmoMask { get; private set; } |
| | 18 | |
|
| | 19 | | private Plane groundPlane; |
| | 20 | | private Plane entityHitPlane; |
| | 21 | |
|
| | 22 | | private void Awake() |
| | 23 | | { |
| 1 | 24 | | defaultMask = LayerMask.GetMask(DCLBuilderSelectionCollider.LAYER_BUILDER_POINTER_CLICK); |
| 1 | 25 | | gizmoMask = LayerMask.GetMask(LAYER_GIZMOS); |
| | 26 | |
|
| 1 | 27 | | groundPlane = new Plane(Vector3.up, Vector3.zero); |
| 1 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | public void SetEntityHitPlane(float height) { entityHitPlane = new Plane(Vector3.up, new Vector3(0, height, 0)); |
| | 31 | |
|
| | 32 | | public bool Raycast(Vector3 mousePosition, LayerMask mask, out RaycastHit hitInfo, bool checkGizmo = false) |
| | 33 | | { |
| 0 | 34 | | if (checkGizmo) |
| | 35 | | { |
| 0 | 36 | | if (Raycast(mousePosition, gizmoMask, out hitInfo)) |
| | 37 | | { |
| 0 | 38 | | return true; |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | return Physics.Raycast(GetMouseRay(mousePosition), out hitInfo, RAYCAST_MAX_DISTANCE, mask); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public bool Raycast(Vector3 mousePosition, LayerMask mask, out RaycastHit hitInfo, System.Func<RaycastHit[], Ray |
| | 46 | | { |
| | 47 | | RaycastHit[] hits; |
| 0 | 48 | | hits = Physics.RaycastAll(GetMouseRay(mousePosition), RAYCAST_MAX_DISTANCE, mask); |
| 0 | 49 | | if (hits.Length > 0) |
| | 50 | | { |
| 0 | 51 | | hitInfo = hitComparer(hits); |
| 0 | 52 | | return true; |
| | 53 | | } |
| 0 | 54 | | hitInfo = new RaycastHit(); |
| 0 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| 11 | 58 | | public Ray GetMouseRay(Vector3 mousePosition) { return builderCamera.ScreenPointToRay(mousePosition); } |
| | 59 | |
|
| 11 | 60 | | public bool RaycastToGizmos(Ray ray, out RaycastHit hitInfo) { return Physics.Raycast(ray, out hitInfo, RAYCAST_ |
| | 61 | |
|
| 11 | 62 | | public bool RaycastToGizmos(Vector3 mousePosition, out RaycastHit hitInfo) { return RaycastToGizmos(GetMouseRay( |
| | 63 | |
|
| | 64 | | public bool RaycastToGround(Vector3 mousePosition, out Vector3 hitPosition) |
| | 65 | | { |
| 0 | 66 | | Ray ray = GetMouseRay(mousePosition); |
| 0 | 67 | | return RaycastToGround(ray, out hitPosition); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public bool RaycastToGround(Ray ray, out Vector3 hitPosition) |
| | 71 | | { |
| 0 | 72 | | float raycastDistance = 0.0f; |
| | 73 | |
|
| 0 | 74 | | if (groundPlane.Raycast(ray, out raycastDistance)) |
| | 75 | | { |
| 0 | 76 | | hitPosition = ray.GetPoint(raycastDistance); |
| 0 | 77 | | return true; |
| | 78 | | } |
| 0 | 79 | | hitPosition = Vector3.zero; |
| 0 | 80 | | return false; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public Vector3 RaycastToEntityHitPlane(Vector3 mousePosition) |
| | 84 | | { |
| 0 | 85 | | Ray ray = GetMouseRay(mousePosition); |
| 0 | 86 | | float raycastDistance = 0.0f; |
| | 87 | |
|
| 0 | 88 | | if (entityHitPlane.Raycast(ray, out raycastDistance)) |
| | 89 | | { |
| 0 | 90 | | return ray.GetPoint(raycastDistance); |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | return Vector3.zero; |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |