| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.Collections.Generic; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| |
|
| | public static class ConvertMj2Rca { |
| | private static MjActuator[] _actuators; |
| | private static MjExclude[] _excludes; |
| | public static GameObject ConvertTree(MjBody mjRoot) { |
| | _excludes = GameObject.FindObjectsOfType<MjExclude>(); |
| | _actuators = GameObject.FindObjectsOfType<MjActuator>(); |
| | |
| | bool jointFound = false; |
| | foreach (Transform mjChild in mjRoot.transform) { |
| | var joint = mjChild.gameObject.GetComponent<MjHingeJoint>(); |
| | if (joint) { |
| | jointFound = true; |
| | break; |
| | } |
| | } |
| |
|
| | var rcaRoot = new GameObject("rcaRoot"); |
| | rcaRoot.transform.position = mjRoot.transform.position + Vector3.right; |
| | rcaRoot.transform.rotation = mjRoot.transform.rotation; |
| | var rca = rcaRoot.AddComponent<ArticulationBody>(); |
| | rca.linearDamping = 0f; |
| | rca.angularDamping = 0f; |
| | rca.jointFriction = 0f; |
| | rca.maxLinearVelocity = 1.8446743E+19f; |
| | rca.maxAngularVelocity = 1.8446743E+19f; |
| |
|
| | var inertial = mjRoot.GetComponentInChildren<MjInertial>(); |
| | if (inertial != null) { |
| | rca.mass = inertial.Mass; |
| | } |
| | if (jointFound) { |
| | rca.immovable = true; |
| | |
| | var newRcaRoot = new GameObject(mjRoot.gameObject.name); |
| | newRcaRoot.transform.parent = rcaRoot.transform; |
| | newRcaRoot.transform.position = rcaRoot.transform.position; |
| | newRcaRoot.transform.rotation = rcaRoot.transform.rotation; |
| | ConvertSubtree(mjRoot, newRcaRoot); |
| | } else { |
| | ConvertSubtree(mjRoot, rcaRoot); |
| | } |
| | return rcaRoot; |
| | } |
| |
|
| | private static void ConvertSubtree(MjBody mjParent, GameObject originalRcParent) { |
| | Quaternion reverseXZ = Quaternion.Euler(0, 180, 0); |
| | |
| | GameObject rcParent = originalRcParent; |
| | ArticulationBody rcb = null; |
| |
|
| | |
| | foreach (Transform mjChild in mjParent.transform) { |
| | |
| | var mjComponent = mjChild.gameObject.GetComponent<MjComponent>(); |
| | if (mjComponent) { |
| | if (!(mjComponent is MjBody)) { |
| | var newGO = new GameObject(mjComponent.gameObject.name); |
| | newGO.transform.parent = rcParent.transform; |
| | |
| | newGO.transform.position = mjComponent.transform.position + Vector3.right; |
| | newGO.transform.rotation = mjComponent.transform.rotation; |
| | if (mjComponent is MjGeom) { |
| | MjGeom geom = mjComponent as MjGeom; |
| | GameObject primitive = null; |
| | if (geom.ShapeType == MjShapeComponent.ShapeTypes.Capsule) { |
| | primitive = GameObject.CreatePrimitive(PrimitiveType.Capsule); |
| | primitive.name = newGO.name; |
| | primitive.transform.parent = newGO.transform.parent; |
| | primitive.transform.position = newGO.transform.position; |
| | primitive.transform.rotation = newGO.transform.rotation; |
| | |
| | |
| | primitive.transform.localScale = new Vector3( |
| | geom.Capsule.Radius * 2, |
| | geom.Capsule.HalfHeight + geom.Capsule.Radius, |
| | geom.Capsule.Radius * 2); |
| | GameObject.DestroyImmediate(newGO); |
| | } else if (geom.ShapeType == MjShapeComponent.ShapeTypes.Sphere) { |
| | primitive = GameObject.CreatePrimitive(PrimitiveType.Sphere); |
| | primitive.name = newGO.name; |
| | primitive.transform.parent = newGO.transform.parent; |
| | primitive.transform.position = newGO.transform.position; |
| | primitive.transform.rotation = newGO.transform.rotation; |
| | var r = 2 * geom.Sphere.Radius; |
| | primitive.transform.localScale = new Vector3(r, r, r); |
| | GameObject.DestroyImmediate(newGO); |
| | } else if (geom.ShapeType == MjShapeComponent.ShapeTypes.Cylinder) { |
| | primitive = GameObject.CreatePrimitive(PrimitiveType.Cylinder); |
| | primitive.name = newGO.name; |
| | primitive.transform.parent = newGO.transform.parent; |
| | primitive.transform.position = newGO.transform.position; |
| | primitive.transform.rotation = newGO.transform.rotation; |
| | primitive.transform.localScale = new Vector3(geom.Cylinder.Radius, |
| | geom.Cylinder.HalfHeight * 2, |
| | geom.Cylinder.Radius); |
| | GameObject.DestroyImmediate(newGO); |
| | } else if (geom.ShapeType == MjShapeComponent.ShapeTypes.Mesh) { |
| | if (geom.Settings.Filtering.Contype != 0 || |
| | geom.Settings.Filtering.Conaffinity != 0) { |
| | var collider = newGO.AddComponent<MeshCollider>(); |
| | collider.sharedMesh = geom.Mesh.Mesh; |
| | } |
| | var mf = newGO.AddComponent<MeshFilter>(); |
| | mf.sharedMesh = geom.Mesh.Mesh; |
| | var mr = newGO.AddComponent<MeshRenderer>(); |
| | primitive = newGO; |
| | } else { |
| | primitive = GameObject.CreatePrimitive(PrimitiveType.Cube); |
| | primitive.name = newGO.name; |
| | primitive.transform.parent = newGO.transform.parent; |
| | primitive.transform.position = newGO.transform.position; |
| | primitive.transform.rotation = newGO.transform.rotation; |
| | Vector3 scale = new Vector3(1, 1, 1); |
| | if (geom.ShapeType == MjShapeComponent.ShapeTypes.Ellipsoid) { |
| | scale = 2 * geom.Ellipsoid.Radiuses; |
| | } else if (geom.ShapeType == MjShapeComponent.ShapeTypes.Box) { |
| | scale = 2 * geom.Box.Extents; |
| | } |
| | primitive.transform.localScale = scale; |
| | GameObject.DestroyImmediate(newGO); |
| | } |
| | if (geom.Settings.Filtering.Contype == 0 && |
| | geom.Settings.Filtering.Conaffinity == 0) { |
| | var collider = primitive.GetComponent<Collider>(); |
| | if (collider) { |
| | GameObject.DestroyImmediate(collider); |
| | } |
| | } |
| |
|
| | primitive.GetComponent<MeshRenderer>().sharedMaterial = |
| | geom.gameObject.GetComponent<MeshRenderer>().sharedMaterial; |
| | } else { |
| | var nameParts = mjComponent.GetType().ToString().Split('.'); |
| | var elementType = nameParts[nameParts.Length - 1].Substring(2); |
| | newGO.name = mjComponent.gameObject.name + "_" + elementType; |
| | } |
| | } |
| | } else { |
| | var mjCamera = mjChild.GetComponent<Camera>(); |
| | if (mjCamera != null) { |
| | var newGO = new GameObject(mjChild.name); |
| | newGO.transform.parent = rcParent.transform; |
| | |
| | newGO.transform.position = mjChild.transform.position + Vector3.right; |
| | newGO.transform.rotation = mjChild.transform.rotation; |
| | var pxCamera = newGO.AddComponent<Camera>(); |
| | pxCamera.fieldOfView = mjCamera.fieldOfView; |
| | pxCamera.nearClipPlane = mjCamera.nearClipPlane; |
| | } |
| | } |
| | } |
| |
|
| | |
| | int jointsFound = 0; |
| | foreach (Transform mjChild in mjParent.transform) { |
| | var joint = mjChild.GetComponent<MjHingeJoint>(); |
| | if (joint) { |
| | if (jointsFound == 0) { |
| | rcb = rcParent.AddComponent<ArticulationBody>(); |
| | var inertial = mjParent.GetComponentInChildren<MjInertial>(); |
| | if (inertial != null) { |
| | rcb.mass = inertial.Mass; |
| | } |
| | rcb.anchorPosition = joint.transform.localPosition; |
| | |
| | rcb.anchorRotation = joint.transform.localRotation * reverseXZ; |
| | rcParent.name += "_" + joint.gameObject.name; |
| | } else { |
| | |
| | List<Transform> childrenToMove = new List<Transform>(); |
| | foreach (Transform child in rcParent.transform) { |
| | childrenToMove.Add(child); |
| | } |
| | var newrRcParent = new GameObject(mjParent.gameObject.name + "_" + joint.gameObject.name); |
| | |
| | newrRcParent.transform.parent = rcParent.transform; |
| | newrRcParent.transform.position = joint.transform.position + Vector3.right; |
| | newrRcParent.transform.rotation = joint.transform.rotation * reverseXZ; |
| | var mass = rcb.mass; |
| | rcb.mass = 0.001f; |
| | rcParent = newrRcParent; |
| | |
| | foreach (Transform childToMove in childrenToMove) { |
| | childToMove.SetParent(rcParent.transform); |
| | } |
| | rcb = rcParent.AddComponent<ArticulationBody>(); |
| | |
| | rcb.anchorRotation = Quaternion.identity; |
| | rcb.mass = mass; |
| | } |
| | rcb.jointType = ArticulationJointType.RevoluteJoint; |
| | if (joint.RangeLower == 0 && joint.RangeUpper == 0) { |
| | rcb.twistLock = ArticulationDofLock.FreeMotion; |
| | } else { |
| | rcb.twistLock = ArticulationDofLock.LimitedMotion; |
| | } |
| | rcb.jointFriction = joint.Settings.Solver.FrictionLoss; |
| | var drive = new ArticulationDrive(); |
| | drive.lowerLimit = joint.RangeLower; |
| | drive.upperLimit = joint.RangeUpper; |
| | drive.damping = joint.Settings.Spring.Damping; |
| | var actuatorFound = false; |
| | foreach (var actuator in _actuators) { |
| | if (actuator.Joint == joint) { |
| | |
| | drive.forceLimit = actuator.CommonParams.ForceRange[1]; |
| | drive.stiffness = actuator.CommonParams.Gear[0] * actuator.CustomParams.GainPrm[0]; |
| | if (drive.stiffness == 0f) { |
| | if (drive.forceLimit == 0f) { |
| | throw new Exception( |
| | "How do I parse an actuator without force limit nor stiffness?"); |
| | } else { |
| | drive.stiffness = 1000f; |
| | } |
| | } else { |
| | if (drive.forceLimit == 0f) { |
| | drive.forceLimit = 1e10f; |
| | } |
| | } |
| | actuatorFound = true; |
| | break; |
| | } |
| | if (!actuatorFound) { |
| | |
| | drive.stiffness = 0f; |
| | drive.forceLimit = 0f; |
| | } |
| | } |
| | |
| | rcb.xDrive = drive; |
| | |
| | rcb.maxLinearVelocity = 1.8446743E+19f; |
| | rcb.maxAngularVelocity = 1.8446743E+19f; |
| | rcb.linearDamping = 0.0f; |
| | rcb.angularDamping = 0.0f; |
| |
|
| | jointsFound += 1; |
| | } |
| | } |
| |
|
| | if (jointsFound > 1) { |
| | var grandparent = originalRcParent.transform.parent?.gameObject; |
| | if (grandparent) { |
| | |
| | |
| | |
| | |
| | |
| | } |
| | } |
| | |
| | rcParent.name = mjParent.gameObject.name; |
| |
|
| | |
| | var rcParentParent = rcParent.transform.parent?.GetComponentInParent<ArticulationBody>(); |
| | if (rcParentParent != null && rcParent.GetComponent<ArticulationBody>() != null) { |
| | foreach (var exclude in _excludes) { |
| | string other = null; |
| | if (exclude.Body1 == mjParent) { |
| | other = exclude.Body2.gameObject.name; |
| | } else if (exclude.Body2 == mjParent) { |
| | other = exclude.Body1.gameObject.name; |
| | } |
| |
|
| | |
| | foreach (var rcOther in GameObject.FindObjectsOfType<ArticulationBody>()) { |
| | |
| | if (rcParentParent != rcOther && rcOther.gameObject.name == other) { |
| | |
| | |
| | |
| | |
| | |
| | break; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | |
| | foreach (Transform mjChild in mjParent.transform) { |
| | |
| | var mjComponent = mjChild.gameObject.GetComponent<MjComponent>(); |
| | if (mjComponent & mjComponent is MjBody) { |
| | var rcChild = new GameObject(mjChild.name); |
| | rcChild.transform.parent = rcParent.transform; |
| | |
| | rcChild.transform.position = mjComponent.transform.position + Vector3.right; |
| | rcChild.transform.rotation = mjComponent.transform.rotation; |
| | |
| | ConvertSubtree(mjComponent as MjBody, rcChild); |
| | } |
| | } |
| | } |
| | } |
| | } |