private RuntimeHandleAxis Hit() { float hit1Distance; float hit2Distance; Ray ray = SceneCamera.ScreenPointToRay(InputController._MousePosition); float scale = RuntimeHandles.GetScreenScale(Target.position, SceneCamera) * RuntimeHandles.HandleScale; if (Intersect(ray, Target.position, outerRadius * scale, out hit1Distance, out hit2Distance)) { Vector3 dpHitPoint; GetPointOnDragPlane(GetDragPlane(), InputController._MousePosition, out dpHitPoint); RuntimeHandleAxis axis = HitAxis(); if (axis != RuntimeHandleAxis.None) { return(axis); } bool isInside = (dpHitPoint - Target.position).magnitude <= innerRadius * scale; if (isInside) { return(RuntimeHandleAxis.Free); } else { return(RuntimeHandleAxis.Screen); } } return(RuntimeHandleAxis.None); }
private bool GetPointOnDragPlane(Vector3 mouse, out Vector3 point) { Ray ray = SceneCamera.ScreenPointToRay(mouse); float distance; if (m_dragPlane.Raycast(ray, out distance)) { point = ray.GetPoint(distance); return(true); } point = Vector3.zero; return(false); }
protected bool GetPointOnDragPlane(Plane dragPlane, Vector3 screenPos, out Vector3 point) { Ray ray = SceneCamera.ScreenPointToRay(screenPos); float distance; if (dragPlane.Raycast(ray, out distance)) { point = ray.GetPoint(distance); return(true); } point = Vector3.zero; return(false); }
private bool HitQuad(Vector3 axis, Matrix4x4 matrix, float size) { Ray ray = SceneCamera.ScreenPointToRay(Input.mousePosition); Plane plane = new Plane(matrix.MultiplyVector(axis).normalized, matrix.MultiplyPoint(Vector3.zero)); float distance; if (!plane.Raycast(ray, out distance)) { return(false); } Vector3 point = ray.GetPoint(distance); point = matrix.inverse.MultiplyPoint(point); Vector3 toCam = matrix.inverse.MultiplyVector(SceneCamera.transform.position - HandlePosition); float fx = Mathf.Sign(Vector3.Dot(toCam, Vector3.right)); float fy = Mathf.Sign(Vector3.Dot(toCam, Vector3.up)); float fz = Mathf.Sign(Vector3.Dot(toCam, Vector3.forward)); point.x *= fx; point.y *= fy; point.z *= fz; float lowBound = -0.01f; bool result = point.x >= lowBound && point.x <= size && point.y >= lowBound && point.y <= size && point.z >= lowBound && point.z <= size; if (result) { DragPlane = GetDragPlane(matrix, axis); } return(result); }
protected override void UpdateOverride() { base.UpdateOverride(); if (RuntimeTools.IsPointerOverGameObject()) { return; } if (IsDragging) { if ((SnapToGround || InputController.GetKey(SnapToGroundKey)) && SelectedAxis != RuntimeHandleAxis.Y) { SnapActiveTargetsToGround(ActiveTargets, SceneCamera, true); transform.position = Targets[0].position; } } if (HightlightOnHover && !IsDragging) { SelectedAxis = Hit(); } if (InputController.GetKeyDown(SnappingKey)) { if (!LockObject.IsPositionLocked) { m_isInSnappingMode = true; if (InputController.GetKey(SnappingToggle)) { RuntimeTools.IsSnapping = !RuntimeTools.IsSnapping; } BeginSnap(); m_prevMousePosition = Input.mousePosition; } } else if (InputController.GetKeyUp(SnappingKey)) { SelectedAxis = RuntimeHandleAxis.None; m_isInSnappingMode = false; if (!IsInSnappingMode) { m_handleOffset = Vector3.zero; } } if (IsInSnappingMode) { Vector2 mousePosition = Input.mousePosition; if (RuntimeTools.SnappingMode == SnappingMode.BoundingBox) { if (IsDragging) { SelectedAxis = RuntimeHandleAxis.Snap; if (m_prevMousePosition != mousePosition) { m_prevMousePosition = mousePosition; float minDistance = float.MaxValue; Vector3 minPoint = Vector3.zero; bool minPointFound = false; for (int i = 0; i < m_allExposedToEditor.Length; ++i) { ExposeToEditor exposeToEditor = m_allExposedToEditor[i]; Bounds bounds = exposeToEditor.Bounds; m_boundingBoxCorners[0] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[1] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[2] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[3] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[4] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[5] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[6] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[7] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, -bounds.extents.z); GetMinPoint(ref minDistance, ref minPoint, ref minPointFound, exposeToEditor.BoundsObject.transform); } if (minPointFound) { HandlePosition = minPoint; } } } else { SelectedAxis = RuntimeHandleAxis.None; if (m_prevMousePosition != mousePosition) { m_prevMousePosition = mousePosition; float minDistance = float.MaxValue; Vector3 minPoint = Vector3.zero; bool minPointFound = false; for (int i = 0; i < m_snapTargets.Length; ++i) { Transform snapTarget = m_snapTargets[i]; Bounds bounds = m_snapTargetsBounds[i]; m_boundingBoxCorners[0] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[1] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[2] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[3] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[4] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[5] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[6] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[7] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, -bounds.extents.z); if (Targets[i] != null) { GetMinPoint(ref minDistance, ref minPoint, ref minPointFound, snapTarget); } } if (minPointFound) { m_handleOffset = minPoint - transform.position; } } } } else { if (IsDragging) { SelectedAxis = RuntimeHandleAxis.Snap; if (m_prevMousePosition != mousePosition) { m_prevMousePosition = mousePosition; Ray ray = SceneCamera.ScreenPointToRay(mousePosition); RaycastHit hitInfo; LayerMask layerMask = (1 << Physics.IgnoreRaycastLayer); layerMask = ~layerMask; for (int i = 0; i < m_snapTargets.Length; ++i) { m_targetLayers[i] = m_snapTargets[i].gameObject.layer; m_snapTargets[i].gameObject.layer = Physics.IgnoreRaycastLayer; } GameObject closestObject = null; if (Physics.Raycast(ray, out hitInfo, float.PositiveInfinity, layerMask)) { closestObject = hitInfo.collider.gameObject; } else { float minDistance = float.MaxValue; for (int i = 0; i < m_allExposedToEditor.Length; ++i) { ExposeToEditor exposedToEditor = m_allExposedToEditor[i]; Bounds bounds = exposedToEditor.Bounds; m_boundingBoxCorners[0] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[1] = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[2] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[3] = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[4] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[5] = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, -bounds.extents.z); m_boundingBoxCorners[6] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, bounds.extents.z); m_boundingBoxCorners[7] = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, -bounds.extents.z); for (int j = 0; j < m_boundingBoxCorners.Length; ++j) { Vector2 screenPoint = SceneCamera.WorldToScreenPoint(exposedToEditor.BoundsObject.transform.TransformPoint(m_boundingBoxCorners[j])); float distance = (screenPoint - mousePosition).magnitude; if (distance < minDistance) { closestObject = exposedToEditor.gameObject; minDistance = distance; } } } } if (closestObject != null) { float minDistance = float.MaxValue; Vector3 minPoint = Vector3.zero; bool minPointFound = false; Transform meshTransform; Mesh mesh = GetMesh(closestObject, out meshTransform); GetMinPoint(meshTransform, ref minDistance, ref minPoint, ref minPointFound, mesh); if (minPointFound) { HandlePosition = minPoint; } } for (int i = 0; i < m_snapTargets.Length; ++i) { m_snapTargets[i].gameObject.layer = m_targetLayers[i]; } } } else { SelectedAxis = RuntimeHandleAxis.None; if (m_prevMousePosition != mousePosition) { m_prevMousePosition = mousePosition; float minDistance = float.MaxValue; Vector3 minPoint = Vector3.zero; bool minPointFound = false; for (int i = 0; i < RealTargets.Length; ++i) { Transform snapTarget = RealTargets[i]; Transform meshTranform; Mesh mesh = GetMesh(snapTarget.gameObject, out meshTranform); GetMinPoint(meshTranform, ref minDistance, ref minPoint, ref minPointFound, mesh); } if (minPointFound) { m_handleOffset = minPoint - transform.position; } } } } } }