| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using UnityEditor; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| |
|
| | |
| | |
| | |
| |
|
| | [CustomEditor(typeof(MjComponent), true)] |
| | public class MjMouseSpring : Editor { |
| | private bool _lastShiftKeyState = false; |
| |
|
| | private Plane _mouseDragPlane; |
| | private Vector3 _mouseDragCurrentPoint = Vector3.negativeInfinity; |
| |
|
| | private Color _translucentRed = new Color(1, 0, 0, 0.1f); |
| |
|
| | public void OnDisable() { |
| | |
| | int uniqueID = GUIUtility.GetControlID(FocusType.Passive); |
| | if (GUIUtility.hotControl == uniqueID) { |
| | GUIUtility.hotControl = 0; |
| | } |
| | } |
| |
|
| | private void SetDragOriginAndDragPlane(Vector3 planeOrigin, Vector3 normal) { |
| | normal[1] = 0; |
| | normal = _lastShiftKeyState ? Vector3.up : normal; |
| | _mouseDragPlane.SetNormalAndPosition(normal, planeOrigin); |
| | } |
| |
|
| | private void UpdatePositionOnDragPlane(Vector3 mousePosition) { |
| | float rayDist; |
| | Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition); |
| |
|
| | if (_mouseDragPlane.Raycast(ray, out rayDist)) { |
| | _mouseDragCurrentPoint = ray.GetPoint(rayDist); |
| | } |
| | } |
| |
|
| | private void DrawMouseSpringGui(Vector3 bodyPosition, Vector3 direction, Camera renderCamera) { |
| | |
| | var rot = Quaternion.FromToRotation(Vector3.up, direction); |
| |
|
| | |
| | var backupColour = Handles.color; |
| |
|
| | |
| | var discRadius = (_mouseDragCurrentPoint - bodyPosition).magnitude; |
| |
|
| | Handles.color = Color.red; |
| | Handles.DrawWireDisc(bodyPosition, _mouseDragPlane.normal, discRadius); |
| | Handles.color = _translucentRed; |
| | Handles.DrawSolidDisc(bodyPosition, _mouseDragPlane.normal, discRadius); |
| |
|
| | |
| | Handles.color = Color.white; |
| | Handles.DrawAAPolyLine(6, new Vector3[] { bodyPosition, _mouseDragCurrentPoint }); |
| |
|
| | |
| | Handles.color = backupColour; |
| | } |
| |
|
| | public unsafe void OnSceneGUI() { |
| | if (!Application.isPlaying) { |
| | return; |
| | } |
| |
|
| | var currentEvent = UnityEngine.Event.current; |
| |
|
| | |
| | int uniqueID = GUIUtility.GetControlID(FocusType.Passive); |
| |
|
| | |
| | |
| | bool mouseSpringActive = GUIUtility.hotControl == uniqueID; |
| | bool mouseSpringStarting = currentEvent.control && currentEvent.button == 0; |
| | if (!mouseSpringStarting && !mouseSpringActive) { |
| | return; |
| | } |
| |
|
| | var sceneCamera = SceneView.currentDrawingSceneView?.camera; |
| | if (sceneCamera == null) { |
| | Debug.LogError("SceneView.currentDrawingSceneView is null"); |
| | return; |
| | } |
| |
|
| | var targetObject = target as MjComponent; |
| | MjBody body = targetObject.GetComponentInParent<MjBody>(); |
| | if(!body) |
| | return; |
| |
|
| | Vector3 bodyPosition = |
| | body != null ? body.transform.position : Vector3.zero; |
| | var scene = MjScene.Instance; |
| |
|
| | switch (currentEvent.type) { |
| | case EventType.MouseDown: |
| | if (EditorWindow.mouseOverWindow == UnityEditor.EditorWindow.GetWindow<SceneView>()) { |
| | |
| | |
| | GUIUtility.hotControl = uniqueID; |
| |
|
| | _lastShiftKeyState = false; |
| | SetDragOriginAndDragPlane(bodyPosition, -sceneCamera.transform.forward); |
| |
|
| | currentEvent.Use(); |
| | } |
| | return; |
| |
|
| | case EventType.MouseDrag: |
| | if (mouseSpringActive) { |
| | |
| | GUIUtility.hotControl = uniqueID; |
| | currentEvent.Use(); |
| | } |
| | return; |
| |
|
| | case EventType.MouseUp: |
| | if (mouseSpringActive) { |
| | |
| | |
| | GUIUtility.hotControl = 0; |
| | currentEvent.Use(); |
| | |
| | scene.Data->xfrc_applied[6*body.MujocoId + 0] = 0; |
| | scene.Data->xfrc_applied[6*body.MujocoId + 1] = 0; |
| | scene.Data->xfrc_applied[6*body.MujocoId + 2] = 0; |
| | } |
| | return; |
| |
|
| | case EventType.Repaint: { |
| | if (mouseSpringActive) { |
| |
|
| | if (currentEvent.shift != _lastShiftKeyState) { |
| | _lastShiftKeyState = currentEvent.shift; |
| | SetDragOriginAndDragPlane(bodyPosition, -sceneCamera.transform.forward); |
| | } |
| |
|
| | |
| | UpdatePositionOnDragPlane(currentEvent.mousePosition); |
| |
|
| | Vector3 bodyVel = Vector3.one; |
| | double[] mjBodyVel = new double[6]; |
| | fixed (double* res = mjBodyVel) { |
| | MujocoLib.mj_objectVelocity( |
| | scene.Model, scene.Data, (int)MujocoLib.mjtObj.mjOBJ_BODY, body.MujocoId, res, 0); |
| | |
| | bodyVel = MjEngineTool.UnityVector3( |
| | MjEngineTool.MjVector3AtEntry(res, 1)); |
| | } |
| |
|
| | float springStiffness = 100; |
| | var settings = MjGlobalSettings.Instance; |
| | if (settings) { |
| | springStiffness = settings.MouseSpringStiffness; |
| | } |
| |
|
| | Vector3 delta = _mouseDragCurrentPoint - bodyPosition; |
| | float mass = 1.0f / (float)scene.Model->body_invweight0[2*body.MujocoId]; |
| | Vector3 unityForce = delta * springStiffness * mass; |
| | unityForce -= bodyVel * Mathf.Sqrt(springStiffness) * mass; |
| | Vector3 mjForce = MjEngineTool.MjVector3(unityForce); |
| | scene.Data->xfrc_applied[6*body.MujocoId + 0] = mjForce.x; |
| | scene.Data->xfrc_applied[6*body.MujocoId + 1] = mjForce.y; |
| | scene.Data->xfrc_applied[6*body.MujocoId + 2] = mjForce.z; |
| |
|
| | |
| | DrawMouseSpringGui(body.transform.position, delta, sceneCamera); |
| | SceneView.RepaintAll(); |
| | } |
| | return; |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|