//*****// private void DoScaleControls() { AddControl(ControlType.ScaleCenter, MyHandleUtility.DistanceToCircle(transform.position, _handleSize * 0.15f)); AddControl(ControlType.ScaleX, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.right * _handleSize)); AddControl(ControlType.ScaleX, MyHandleUtility.DistanceToCircle(transform.position + transform.right * _handleSize, _handleSize * 0.2f)); AddControl(ControlType.ScaleY, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.up * _handleSize)); AddControl(ControlType.ScaleY, MyHandleUtility.DistanceToCircle(transform.position + transform.up * _handleSize, _handleSize * 0.2f)); AddControl(ControlType.ScaleZ, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.forward * _handleSize)); AddControl(ControlType.ScaleZ, MyHandleUtility.DistanceToCircle(transform.position + transform.forward * _handleSize, _handleSize * 0.2f)); _currentControl = NearestControl; if (_currentControl != ControlType.None) { _freezeObjectPicking = true; _currentMousePosition = _startMousePosition = Event.current.mousePosition; if (CurrentControl == ControlType.ScaleCenter) { _scaleValueDrag = 0.0f; _startScaleValue = transform.localScale.x; } else if (CurrentControl == ControlType.ScaleX) { _startScaleValue = transform.localScale.x; } else if (CurrentControl == ControlType.ScaleY) { _startScaleValue = transform.localScale.y; } else if (CurrentControl == ControlType.ScaleZ) { _startScaleValue = transform.localScale.z; } } }
public static Vector3 ClosestPointToPolyLine(params Vector3[] vertices) { float num1 = DistanceToLine(vertices[0], vertices[1]); int index1 = 0; for (int index2 = 2; index2 < vertices.Length; ++index2) { float num2 = DistanceToLine(vertices[index2 - 1], vertices[index2]); if ((double)num2 < (double)num1) { num1 = num2; index1 = index2 - 1; } } Vector3 vector3_1 = vertices[index1]; Vector3 vector3_2 = vertices[index1 + 1]; Vector2 vector2_1 = Event.current.mousePosition - MyHandleUtility.WorldToGUIPoint(vector3_1); Vector2 vector2_2 = MyHandleUtility.WorldToGUIPoint(vector3_2) - MyHandleUtility.WorldToGUIPoint(vector3_1); float magnitude = vector2_2.magnitude; float num3 = Vector3.Dot((Vector3)vector2_2, (Vector3)vector2_1); if ((double)magnitude > 9.99999997475243E-07) { num3 /= magnitude * magnitude; } float t = Mathf.Clamp01(num3); return(Vector3.Lerp(vector3_1, vector3_2, t)); }
private void DoTranslateHandle() { _currentMousePosition += Event.current.delta; var translateAxis = GetAxis(_currentControl); var translationDist = MyHandleUtility.CalcLineTranslation(_startMousePosition, _currentMousePosition, _translateStartPosition, translateAxis); transform.position = _translateStartPosition + translateAxis * translationDist; }
private void DoRotateHandle() { _currentMousePosition += Event.current.delta; var normalized = Vector3.Cross(_rotateStartAxis, transform.position - _rotateStartPosition).normalized; var rotationDist = (float)((double)MyHandleUtility.CalcLineTranslation(_startMousePosition, _currentMousePosition, _rotateStartPosition, normalized) / (double)_handleSize * 30.0); transform.rotation = Quaternion.AngleAxis(rotationDist * -1f, _rotateStartAxis) * _rotateStartRotation; }
private void DrawRotateHandle() { MyHandleUtility.DrawWireDisc(transform.position, Camera.current.transform.forward, _handleSize, GetColor(ControlType.RotateInner)); MyHandleUtility.DrawWireDisc(transform.position, Camera.current.transform.forward, _handleSize * 1.1f, GetColor(ControlType.RotateOuter)); MyHandleUtility.DrawWireArc(transform.position, transform.right, Vector3.Cross(transform.right, Camera.current.transform.forward).normalized, 180f, _handleSize, GetColor(ControlType.RotateX)); MyHandleUtility.DrawWireArc(transform.position, transform.up, Vector3.Cross(transform.up, Camera.current.transform.forward).normalized, 180f, _handleSize, GetColor(ControlType.RotateY)); MyHandleUtility.DrawWireArc(transform.position, transform.forward, Vector3.Cross(transform.forward, Camera.current.transform.forward).normalized, 180f, _handleSize, GetColor(ControlType.RotateZ)); }
private void DrawTranslateHandle() { MyHandleUtility.DrawLine(transform.position, transform.position + transform.right * _handleSize * 0.9f, GetColor(ControlType.TranslateX)); MyHandleUtility.DrawLine(transform.position, transform.position + transform.up * _handleSize * 0.9f, GetColor(ControlType.TranslateY)); MyHandleUtility.DrawLine(transform.position, transform.position + transform.forward * _handleSize * 0.9f, GetColor(ControlType.TranslateZ)); MyHandleUtility.DrawConeCap(transform.position + transform.right * _handleSize, Quaternion.LookRotation(transform.right), _handleSize * 0.2f, GetColor(ControlType.TranslateX)); MyHandleUtility.DrawConeCap(transform.position + transform.up * _handleSize, Quaternion.LookRotation(transform.up), _handleSize * 0.2f, GetColor(ControlType.TranslateY)); MyHandleUtility.DrawConeCap(transform.position + transform.forward * _handleSize, Quaternion.LookRotation(transform.forward), _handleSize * 0.2f, GetColor(ControlType.TranslateZ)); }
//*****// public void BeginHandle() { _handleSize = MyHandleUtility.GetHandleSize(transform.position); if (Event.current.type == EventType.Layout) { _nearestDistance = 5f; _nearestControl = ControlType.None; } }
private void DrawScaleHandle() { MyHandleUtility.DrawLine(transform.position, transform.position + transform.right * (_handleSize * _drawScaleValue.x - _handleSize * 0.0500000007450581f), GetColor(ControlType.ScaleX)); MyHandleUtility.DrawLine(transform.position, transform.position + transform.up * (_handleSize * _drawScaleValue.y - _handleSize * 0.0500000007450581f), GetColor(ControlType.ScaleY)); MyHandleUtility.DrawLine(transform.position, transform.position + transform.forward * (_handleSize * _drawScaleValue.z - _handleSize * 0.0500000007450581f), GetColor(ControlType.ScaleZ)); MyHandleUtility.DrawCubeCap(transform.position, transform.rotation, _handleSize * 0.15f, GetColor(ControlType.ScaleCenter)); MyHandleUtility.DrawCubeCap(transform.position + transform.right * _handleSize * _drawScaleValue.x, transform.rotation, _handleSize * 0.1f, GetColor(ControlType.ScaleX)); MyHandleUtility.DrawCubeCap(transform.position + transform.up * _handleSize * _drawScaleValue.y, transform.rotation, _handleSize * 0.1f, GetColor(ControlType.ScaleY)); MyHandleUtility.DrawCubeCap(transform.position + transform.forward * _handleSize * _drawScaleValue.z, transform.rotation, _handleSize * 0.1f, GetColor(ControlType.ScaleZ)); }
public static float DistanceToLine(Vector3 p1, Vector3 p2) { p1 = (Vector3)MyHandleUtility.WorldToGUIPoint(p1); p2 = (Vector3)MyHandleUtility.WorldToGUIPoint(p2); float num = MyHandleUtility.DistancePointLine((Vector3)Event.current.mousePosition, p1, p2); if ((double)num < 0.0) { num = 0.0f; } return(num); }
private void DoScaleHandle() { _currentMousePosition += Event.current.delta; var scaleAxis = GetAxis(_currentControl); if (CurrentControl == ControlType.ScaleCenter) { _scaleValueDrag += MyHandleUtility.niceMouseDelta * 0.01f; var scaleFactor = (_scaleValueDrag + 1f) * _startScaleValue; var num2 = scaleFactor / transform.localScale.x; var newScale = transform.localScale; newScale.x = scaleFactor; newScale.y *= num2; newScale.z *= num2; transform.localScale = newScale; _drawScaleValue.x = newScale.x / _startScaleValue; _drawScaleValue.y = newScale.x / _startScaleValue; _drawScaleValue.z = newScale.x / _startScaleValue; } else { float num = 1.0f + MyHandleUtility.CalcLineTranslation(_startMousePosition, _currentMousePosition, transform.position, -GetAxis(_currentControl)) / _handleSize; var newScale = transform.localScale; if (CurrentControl == ControlType.ScaleX) { newScale.x = _startScaleValue * num; _drawScaleValue.x = newScale.x / _startScaleValue; } else if (CurrentControl == ControlType.ScaleY) { newScale.y = _startScaleValue * num; _drawScaleValue.y = newScale.y / _startScaleValue; } else if (CurrentControl == ControlType.ScaleZ) { newScale.z = _startScaleValue * num; _drawScaleValue.z = newScale.z / _startScaleValue; } transform.localScale = newScale; } }
//*****// private void DoTranslateControls() { AddControl(ControlType.TranslateX, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.right * _handleSize)); AddControl(ControlType.TranslateX, MyHandleUtility.DistanceToCircle(transform.position + transform.right * _handleSize, _handleSize * 0.2f)); AddControl(ControlType.TranslateY, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.up * _handleSize)); AddControl(ControlType.TranslateY, MyHandleUtility.DistanceToCircle(transform.position + transform.up * _handleSize, _handleSize * 0.2f)); AddControl(ControlType.TranslateZ, MyHandleUtility.DistanceToLine(transform.position, transform.position + transform.forward * _handleSize)); AddControl(ControlType.TranslateZ, MyHandleUtility.DistanceToCircle(transform.position + transform.forward * _handleSize, _handleSize * 0.2f)); _currentControl = NearestControl; if (_currentControl != ControlType.None) { _freezeObjectPicking = true; _currentControl = NearestControl; _translateStartPosition = transform.position; _currentMousePosition = _startMousePosition = Event.current.mousePosition; } }
//*****// private void OnRenderObject() { if (!_enabled || Camera.current != Camera.main) { return; } _handleSize = MyHandleUtility.GetHandleSize(transform.position); if (GetComponent <MeshRenderer>() && GetComponent <MeshRenderer>().enabled) { MyHandleUtility.DrawWireMesh(GetComponent <MeshFilter>().sharedMesh, transform, new Color(0.5f, 0.8f, 0.5f)); } else if (GetComponent <SphereCollider>() && GetComponent <SphereCollider>().enabled) { Color color = new Color(0.5f, 0.8f, 0.5f); var sphereCollider = GetComponent <SphereCollider>(); Vector3 lossyScale = sphereCollider.transform.lossyScale; float num1 = Mathf.Max(Mathf.Max(Mathf.Abs(lossyScale.x), Mathf.Abs(lossyScale.y)), Mathf.Abs(lossyScale.z)); float radius = Mathf.Max(Mathf.Abs(num1 * sphereCollider.radius), 1E-05f); Vector3 position = sphereCollider.transform.TransformPoint(sphereCollider.center); Quaternion rotation = sphereCollider.transform.rotation; MyHandleUtility.DoRadiusHandle(rotation, position, radius, color); } switch (_state) { case HandleSelectionState.Scale: DrawScaleHandle(); break; case HandleSelectionState.Rotate: DrawRotateHandle(); break; case HandleSelectionState.Translate: DrawTranslateHandle(); break; default: throw new ArgumentOutOfRangeException(); } }
public static float DistanceToCircle(Vector3 position, float radius) { Vector2 vector2_1 = MyHandleUtility.WorldToGUIPoint(position); Camera current = Camera.current; Vector2 zero = Vector2.zero; if ((bool)((Object)current)) { Vector2 vector2_2 = MyHandleUtility.WorldToGUIPoint(position + current.transform.right * radius); radius = (vector2_1 - vector2_2).magnitude; } float magnitude = (vector2_1 - Event.current.mousePosition).magnitude; if ((double)magnitude < (double)radius) { return(0.0f); } return(magnitude - radius); }
//*****// private void DoRotateControls() { AddControl(ControlType.RotateInner, MyHandleUtility.DistanceToDisc(transform.position, Camera.main.transform.forward, _handleSize) / 2f); AddControl(ControlType.RotateOuter, MyHandleUtility.DistanceToDisc(transform.position, Camera.main.transform.forward, _handleSize * 1.1f) / 2f); AddControl(ControlType.RotateX, MyHandleUtility.DistanceToArc(transform.position, transform.right, Vector3.Cross(transform.right, Camera.main.transform.forward).normalized, 180f, _handleSize) / 2f); AddControl(ControlType.RotateY, MyHandleUtility.DistanceToArc(transform.position, transform.up, Vector3.Cross(transform.up, Camera.main.transform.forward).normalized, 180f, _handleSize) / 2f); AddControl(ControlType.RotateZ, MyHandleUtility.DistanceToArc(transform.position, transform.forward, Vector3.Cross(transform.forward, Camera.main.transform.forward).normalized, 180f, _handleSize) / 2f); _currentControl = NearestControl; if (_currentControl != ControlType.None) { _freezeObjectPicking = true; _currentControl = NearestControl; _rotateStartAxis = GetAxis(_currentControl); _rotateStartRotation = transform.rotation; _currentMousePosition = _startMousePosition = Event.current.mousePosition; _rotateStartPosition = MyHandleUtility.ClosestPointToDisc(transform.position, _rotateStartAxis, _handleSize); //var normalized = Vector3.Cross(_rotateStartAxis, Camera.main.transform.forward).normalized; //_rotateStartPosition = MyHandleUtility.ClosestPointToArc(transform.position, _rotateStartAxis, normalized, 180f, _handleSize); } }
public static void DebugD(Vector3 p1) { Debug.Log(MyHandleUtility.WorldToGUIPoint(p1)); Debug.Log((Vector3)Event.current.mousePosition); Debug.Log("***"); }
public static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd) { return(Vector3.Magnitude(MyHandleUtility.ProjectPointLine(point, lineStart, lineEnd) - point)); }
public static float DistanceToArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius) { Vector3[] dest = new Vector3[60]; MyHandleUtility.SetDiscSectionPoints(dest, 60, center, normal, from, angle, radius); return(DistanceToPolyLine(dest)); }