Initial release of the Unity plugin.
PiperOrigin-RevId: 425947505
This commit is contained in:
committed by
Saran Tunyasuvunakool
parent
8ec3dd97a4
commit
6de9eafa13
@@ -0,0 +1,26 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomPropertyDrawer(typeof(AbsoluteValueAttribute))]
|
||||
public class AbsoluteValuePropertyDrawer : PropertyDrawer {
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
|
||||
property.floatValue = Mathf.Abs(EditorGUI.FloatField(position, label, property.floatValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac61b66fbbd264beebb1df6cd423d122
|
||||
timeCreated: 1538126279
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,199 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomEditor(typeof(MjActuator))]
|
||||
[CanEditMultipleObjects]
|
||||
public class MjActuatorEditor : Editor {
|
||||
private bool _showCustomParams = false;
|
||||
private bool _showDebugValues = false;
|
||||
private bool _showDynPrm = false;
|
||||
private bool _showGainPrm = false;
|
||||
private bool _showBiasValues = false;
|
||||
|
||||
// General actuator properties.
|
||||
private SerializedProperty _dynType;
|
||||
private SerializedProperty _gainType;
|
||||
private SerializedProperty _biasType;
|
||||
private SerializedProperty _dynPrm;
|
||||
private SerializedProperty _gainPrm;
|
||||
private SerializedProperty _biasPrm;
|
||||
// Position actuator properties.
|
||||
private SerializedProperty _kp;
|
||||
// Velocity actuator properties.
|
||||
private SerializedProperty _kv;
|
||||
// Cylinder actuator properties.
|
||||
private SerializedProperty _cylinderTimeConst;
|
||||
private SerializedProperty _area;
|
||||
private SerializedProperty _diameter;
|
||||
private SerializedProperty _bias;
|
||||
// Cylinder actuator properties.
|
||||
private SerializedProperty _muscleTimeConst;
|
||||
private SerializedProperty _range;
|
||||
private SerializedProperty _force;
|
||||
private SerializedProperty _scale;
|
||||
private SerializedProperty _lmin;
|
||||
private SerializedProperty _lmax;
|
||||
private SerializedProperty _vmax;
|
||||
private SerializedProperty _fpmax;
|
||||
private SerializedProperty _fvmax;
|
||||
|
||||
public void OnEnable() {
|
||||
_showCustomParams = false;
|
||||
_showDebugValues = false;
|
||||
_showDynPrm = false;
|
||||
_showGainPrm = false;
|
||||
_showBiasValues = false;
|
||||
|
||||
var customParams = serializedObject.FindProperty("CustomParams");
|
||||
// General actuator properties.
|
||||
_dynType = customParams.FindPropertyRelative("DynType");
|
||||
_gainType = customParams.FindPropertyRelative("GainType");
|
||||
_biasType = customParams.FindPropertyRelative("BiasType");
|
||||
_dynPrm = customParams.FindPropertyRelative("DynPrm");
|
||||
_gainPrm = customParams.FindPropertyRelative("GainPrm");
|
||||
_biasPrm = customParams.FindPropertyRelative("BiasPrm");
|
||||
// Position actuator properties.
|
||||
_kp = customParams.FindPropertyRelative("Kp");
|
||||
// Velocity actuator properties.
|
||||
_kv = customParams.FindPropertyRelative("Kv");
|
||||
// Cylinder actuator properties.
|
||||
_cylinderTimeConst = customParams.FindPropertyRelative("CylinderTimeConst");
|
||||
_area = customParams.FindPropertyRelative("Area");
|
||||
_diameter = customParams.FindPropertyRelative("Diameter");
|
||||
_bias = customParams.FindPropertyRelative("Bias");
|
||||
// Cylinder actuator properties.
|
||||
_muscleTimeConst = customParams.FindPropertyRelative("MuscleTimeConst");
|
||||
_range = customParams.FindPropertyRelative("Range");
|
||||
_force = customParams.FindPropertyRelative("Force");
|
||||
_scale = customParams.FindPropertyRelative("Scale");
|
||||
_lmin = customParams.FindPropertyRelative("LMin");
|
||||
_lmax = customParams.FindPropertyRelative("LMax");
|
||||
_vmax = customParams.FindPropertyRelative("VMax");
|
||||
_fpmax = customParams.FindPropertyRelative("FpMax");
|
||||
_fvmax = customParams.FindPropertyRelative("FvMax");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
serializedObject.Update();
|
||||
foreach (var target in serializedObject.targetObjects) {
|
||||
EditActuator(target as MjActuator);
|
||||
}
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
private void EditActuator(MjActuator actuator) {
|
||||
DrawDefaultInspector();
|
||||
_showCustomParams = EditorGUILayout.Foldout(_showCustomParams, "Custom Params");
|
||||
if (_showCustomParams) {
|
||||
EditCustomParams(actuator.Type, actuator.CustomParams);
|
||||
}
|
||||
_showDebugValues = EditorGUILayout.Foldout(_showDebugValues, "Debug");
|
||||
if (_showDebugValues) {
|
||||
using (new EditorGUI.DisabledScope(true)) {
|
||||
EditorGUILayout.FloatField("Length", actuator.Length);
|
||||
EditorGUILayout.FloatField("Velocity", actuator.Velocity);
|
||||
EditorGUILayout.FloatField("Force", actuator.Force);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EditCustomParams(MjActuator.ActuatorType type,
|
||||
MjActuator.CustomParameters parameters) {
|
||||
switch (type) {
|
||||
case MjActuator.ActuatorType.General: {
|
||||
EditGeneralParams(parameters);
|
||||
break;
|
||||
}
|
||||
case MjActuator.ActuatorType.Position: {
|
||||
EditPositionParams(parameters);
|
||||
break;
|
||||
}
|
||||
case MjActuator.ActuatorType.Velocity: {
|
||||
EditVelocityParams(parameters);
|
||||
break;
|
||||
}
|
||||
case MjActuator.ActuatorType.Cylinder: {
|
||||
EditCylinderParams(parameters);
|
||||
break;
|
||||
}
|
||||
case MjActuator.ActuatorType.Muscle: {
|
||||
EditMuscleParams(parameters);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EditGeneralParams(MjActuator.CustomParameters parameters) {
|
||||
EditorGUILayout.PropertyField(_dynType);
|
||||
EditorGUILayout.PropertyField(_gainType);
|
||||
EditorGUILayout.PropertyField(_biasType);
|
||||
|
||||
_showDynPrm = EditorGUILayout.Foldout(_showDynPrm, "DynPrm");
|
||||
if (_showDynPrm) {
|
||||
for (var i = 0; i < parameters.DynPrm.Count; ++i) {
|
||||
EditorGUILayout.PropertyField(_dynPrm.GetArrayElementAtIndex(i));
|
||||
}
|
||||
}
|
||||
_showGainPrm = EditorGUILayout.Foldout(_showGainPrm, "GainPrm");
|
||||
if (_showGainPrm) {
|
||||
for (var i = 0; i < parameters.GainPrm.Count; ++i) {
|
||||
EditorGUILayout.PropertyField(_gainPrm.GetArrayElementAtIndex(i));
|
||||
}
|
||||
}
|
||||
_showBiasValues = EditorGUILayout.Foldout(_showBiasValues, "BiasPrm");
|
||||
if (_showBiasValues) {
|
||||
for (var i = 0; i < parameters.BiasPrm.Count; ++i) {
|
||||
EditorGUILayout.PropertyField(_biasPrm.GetArrayElementAtIndex(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EditPositionParams(MjActuator.CustomParameters parameters) {
|
||||
EditorGUILayout.PropertyField(_kp);
|
||||
}
|
||||
|
||||
private void EditVelocityParams(MjActuator.CustomParameters parameters) {
|
||||
EditorGUILayout.PropertyField(_kv);
|
||||
}
|
||||
|
||||
private void EditCylinderParams(MjActuator.CustomParameters parameters) {
|
||||
EditorGUILayout.PropertyField(_cylinderTimeConst);
|
||||
EditorGUILayout.PropertyField(_area);
|
||||
EditorGUILayout.PropertyField(_diameter);
|
||||
_showBiasValues = EditorGUILayout.Foldout(_showBiasValues, "Bias");
|
||||
if (_showBiasValues) {
|
||||
for (var i = 0; i < parameters.Bias.Length; ++i) {
|
||||
EditorGUILayout.PropertyField(_bias.GetArrayElementAtIndex(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EditMuscleParams(MjActuator.CustomParameters parameters) {
|
||||
EditorGUILayout.PropertyField(_muscleTimeConst, new GUIContent("TimeConst"));
|
||||
EditorGUILayout.PropertyField(_range);
|
||||
EditorGUILayout.PropertyField(_force);
|
||||
EditorGUILayout.PropertyField(_scale);
|
||||
EditorGUILayout.PropertyField(_lmin);
|
||||
EditorGUILayout.PropertyField(_lmax);
|
||||
EditorGUILayout.PropertyField(_vmax);
|
||||
EditorGUILayout.PropertyField(_fpmax);
|
||||
EditorGUILayout.PropertyField(_fvmax);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76f90e10df489475490b387a53dd88da
|
||||
timeCreated: 1547050687
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomEditor(typeof(MjGeom), true)]
|
||||
[CanEditMultipleObjects]
|
||||
public class MjGeomEditor : MjShapeComponentEditor {
|
||||
public override void OnInspectorGUI() {
|
||||
serializedObject.Update();
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("Mass"));
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("Density"));
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
base.OnInspectorGUI();
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/MjGeom/Add mesh renderer")]
|
||||
private static void AddMeshComponents(MenuCommand menuCommand) {
|
||||
var geom = menuCommand.context as MjGeom;
|
||||
geom.gameObject.AddComponent<MjMeshFilter>();
|
||||
geom.gameObject.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Diffuse"));
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/MjGeom/Convert to free object")]
|
||||
private static void ConvertToFreeObject(MenuCommand menuCommand) {
|
||||
var geom = menuCommand.context as MjGeom;
|
||||
if (geom.GetComponentInParent<MjBody>()) {
|
||||
Debug.LogError("This geom already has a Body parent.", geom.GetComponentInParent<MjBody>());
|
||||
return;
|
||||
}
|
||||
var parent = new GameObject(geom.gameObject.name + " Body").transform;
|
||||
var root = geom.transform.root;
|
||||
if (root != geom.transform) {
|
||||
parent.parent = geom.transform.parent;
|
||||
}
|
||||
parent.gameObject.AddComponent<MjBody>();
|
||||
var joint = new GameObject("Free Joint").AddComponent<MjFreeJoint>();
|
||||
joint.transform.parent = parent;
|
||||
geom.transform.parent = parent;
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/Collider/Add a matching MuJoCo geom")]
|
||||
private static void AddMatchingGeom(MenuCommand menuCommand) {
|
||||
var collider = menuCommand.context as Collider;
|
||||
collider.enabled = false;
|
||||
var geom = collider.gameObject.AddComponent<MjGeom>();
|
||||
if (collider as BoxCollider) {
|
||||
geom.ShapeType = MjShapeComponent.ShapeTypes.Box;
|
||||
} else if (collider as SphereCollider) {
|
||||
geom.ShapeType = MjShapeComponent.ShapeTypes.Sphere;
|
||||
} else if (collider as CapsuleCollider) {
|
||||
geom.ShapeType = MjShapeComponent.ShapeTypes.Capsule;
|
||||
} else if (collider as MeshCollider) {
|
||||
geom.ShapeType = MjShapeComponent.ShapeTypes.Mesh;
|
||||
((MjMeshShape)geom.Shape).Mesh = ((MeshCollider)collider).sharedMesh;
|
||||
} else {
|
||||
Debug.LogError("Collider type not supported yet.", collider);
|
||||
collider.enabled = true;
|
||||
GameObject.DestroyImmediate(geom);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5409b149ad694715a57f4dc8bb7a857
|
||||
timeCreated: 1552041114
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.AnimatedValues;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
public static class MjHandles {
|
||||
|
||||
// Renders a visualization of an axis, anchored at the specified world space origin.
|
||||
//
|
||||
// Args:
|
||||
// origin: World space coordinates of the anchoring point for the visualization.
|
||||
// direction: Axis direction.
|
||||
public static void Axis(Vector3 origin, Vector3 direction) {
|
||||
var size = HandleUtility.GetHandleSize(origin) * 0.3f;
|
||||
var axisEnd = origin + direction * size;
|
||||
var capRotation = Quaternion.LookRotation(direction);
|
||||
Handles.DrawLine(origin, axisEnd);
|
||||
Handles.ConeHandleCap(0, axisEnd, capRotation, size, EventType.Repaint);
|
||||
}
|
||||
|
||||
// Renders a visualization of linear (translational) limits, anchored at the specified world
|
||||
// space origin.
|
||||
//
|
||||
// Args:
|
||||
// origin: World space coordinates of the anchoring point for the visualization.
|
||||
// lower: Distance from the origin below, where the limit expires.
|
||||
// upper: Distance from the origin above, where the limit expires.
|
||||
// axis: Axis along which the limit works.
|
||||
public static void LinearLimits(Vector3 origin, float lower, float upper, Vector3 axis) {
|
||||
if (upper > lower) {
|
||||
var discRadius = HandleUtility.GetHandleSize(origin) * 0.7f;
|
||||
var startPosition = origin + axis * lower;
|
||||
var endPosition = origin + axis * upper;
|
||||
Handles.DrawSolidDisc(startPosition, axis, discRadius);
|
||||
Handles.DrawSolidDisc(endPosition, axis, discRadius);
|
||||
Handles.DrawLine(origin, endPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a810c29762134ebb9bbee3903172764
|
||||
timeCreated: 1537794456
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.AnimatedValues;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomEditor(typeof(MjHingeJoint))]
|
||||
public class MjHingeJointEditor : Editor {
|
||||
private MjHingeJoint _joint;
|
||||
|
||||
protected virtual void OnEnable() {
|
||||
_joint = (MjHingeJoint)target;
|
||||
}
|
||||
|
||||
protected virtual void OnSceneGUI() {
|
||||
DrawHandles(_joint);
|
||||
}
|
||||
|
||||
public static void DrawHandles(MjHingeJoint joint) {
|
||||
Handles.color = Color.yellow;
|
||||
MjHandles.Axis(joint.transform.position, joint.RotationAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdaa159fcf7064108aed2229bc39ac0d
|
||||
timeCreated: 1537794793
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,176 @@
|
||||
#if UNITY_EDITOR
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
// During play mode, in the scene view, with a MjBody component selected, holding down control
|
||||
// and left mouse drag will apply a force on the body. Holding shift down will change the applied
|
||||
// force direction between World XZ plane and Y[camera-up].
|
||||
|
||||
[CustomEditor(typeof(MjBody))]
|
||||
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() {
|
||||
// If we're still the hot control at this stage, we need to release.
|
||||
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) {
|
||||
// Update and draw the drag indicator.
|
||||
var rot = Quaternion.FromToRotation(Vector3.up, direction);
|
||||
|
||||
// Backup defaults.
|
||||
var backupColour = Handles.color;
|
||||
|
||||
// Update the drag plane indicator disc and outline...
|
||||
var discRadius = (_mouseDragCurrentPoint - bodyPosition).magnitude;
|
||||
|
||||
Handles.color = Color.red;
|
||||
Handles.DrawWireDisc(bodyPosition, _mouseDragPlane.normal, discRadius);
|
||||
Handles.color = _translucentRed;
|
||||
Handles.DrawSolidDisc(bodyPosition, _mouseDragPlane.normal, discRadius);
|
||||
|
||||
// ...and give it a phat poly line to show the drag anchor origin.
|
||||
Handles.color = Color.white;
|
||||
Handles.DrawAAPolyLine(6, new Vector3[] { bodyPosition, _mouseDragCurrentPoint });
|
||||
|
||||
// Restore defaults.
|
||||
Handles.color = backupColour;
|
||||
}
|
||||
|
||||
public unsafe void OnSceneGUI() {
|
||||
if (!Application.isPlaying) {
|
||||
return;
|
||||
}
|
||||
|
||||
var currentEvent = UnityEngine.Event.current;
|
||||
|
||||
// Cache the hot control to determine whether we're currently capturing mouse input.
|
||||
int uniqueID = GUIUtility.GetControlID(FocusType.Passive);
|
||||
|
||||
// Mouse spring is active if the control key is held down and the user is dragging the
|
||||
// left mouse button, or if we're already in the process of capturing mouse input.
|
||||
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;
|
||||
}
|
||||
|
||||
MjBody body = target as MjBody;
|
||||
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>()) {
|
||||
// The mouse was pressed in the scene view, so start capturing all mouse input until
|
||||
// the mouse button's released.
|
||||
GUIUtility.hotControl = uniqueID;
|
||||
|
||||
_lastShiftKeyState = false;
|
||||
SetDragOriginAndDragPlane(bodyPosition, -sceneCamera.transform.forward);
|
||||
|
||||
currentEvent.Use();
|
||||
}
|
||||
return;
|
||||
|
||||
case EventType.MouseDrag:
|
||||
if (mouseSpringActive) {
|
||||
// We're currently capturing all mouse input, so consume the event.
|
||||
GUIUtility.hotControl = uniqueID;
|
||||
currentEvent.Use();
|
||||
}
|
||||
return;
|
||||
|
||||
case EventType.MouseUp:
|
||||
if (mouseSpringActive) {
|
||||
// We're still capturing all mouse input so consume the event, but we can release our
|
||||
// control over capturing input now as the mouse button's been released.
|
||||
GUIUtility.hotControl = 0;
|
||||
currentEvent.Use();
|
||||
// as opposed to unity's addforce, xfrc_applied is persistent
|
||||
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);
|
||||
}
|
||||
|
||||
// Raycast towards the drag plane to update _mouseDragCurrentPoint.
|
||||
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);
|
||||
// linear velocity is in the last 3 entries
|
||||
bodyVel = MjEngineTool.UnityVector3(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;
|
||||
|
||||
// Draw and immediately force a repaint.
|
||||
DrawMouseSpringGui(body.transform.position, delta, sceneCamera);
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c4ae8cbdd0cac0a387364d88408d932
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomEditor(typeof(MjShapeComponent), true)]
|
||||
[CanEditMultipleObjects]
|
||||
public class MjShapeComponentEditor : Editor {
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
|
||||
serializedObject.Update();
|
||||
var shapeType = serializedObject.FindProperty("ShapeType");
|
||||
EditorGUILayout.PropertyField(shapeType);
|
||||
EditorGUILayout.PropertyField(
|
||||
serializedObject.FindProperty($"{shapeType.enumDisplayNames[shapeType.enumValueIndex]}"),
|
||||
includeChildren: true);
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21af9fef28ac0899e982e791916b5fc5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright 2019 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.AnimatedValues;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mujoco {
|
||||
|
||||
[CustomEditor(typeof(MjSlideJoint))]
|
||||
public class MjSlideJointEditor : Editor {
|
||||
private MjSlideJoint _joint;
|
||||
|
||||
protected virtual void OnEnable() {
|
||||
_joint = (MjSlideJoint)target;
|
||||
}
|
||||
|
||||
protected virtual void OnSceneGUI() {
|
||||
DrawHandles(_joint);
|
||||
}
|
||||
|
||||
// Draw the handles for the slide joint.
|
||||
public static void DrawHandles(MjSlideJoint joint) {
|
||||
// Draw the handles.
|
||||
if (joint.Settings.Solver.Limited) {
|
||||
Handles.color = Color.blue;
|
||||
MjHandles.LinearLimits(joint.transform.position, joint.RangeLower, joint.RangeUpper,
|
||||
joint.SlideAxis);
|
||||
}
|
||||
Handles.color = Color.yellow;
|
||||
MjHandles.Axis(joint.transform.position, joint.SlideAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf7c14a263938456e9393a8d0fce7b9f
|
||||
timeCreated: 1538066332
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user