Exemple #1
0
        private bool CreateCable(Node node)
        {
            var nativeCable = m_tree.GetCable(node.Uuid);

            if (nativeCable == null)
            {
                Debug.LogWarning("Unable to find native instance of cable: " + node.GameObject.name +
                                 " (UUID: " + node.Uuid.str() + ")");
                return(false);
            }

            var cable = node.GameObject.GetOrCreateComponent <Cable>();
            var route = cable.Route;

            route.Clear();

            cable.RestoreLocalDataFrom(nativeCable);
            cable.RouteAlgorithm = Cable.RouteType.Identity;

            var properties = CableProperties.Create <CableProperties>().RestoreLocalDataFrom(nativeCable.getCableProperties(),
                                                                                             nativeCable.getCablePlasticity());

            properties.name  = cable.name + "_properties";
            cable.Properties = properties = AddOrReplaceAsset(properties, node, AgXUnity.IO.AssetType.CableProperties);

            for (var it = nativeCable.getSegments().begin(); !it.EqualWith(nativeCable.getSegments().end()); it.inc())
            {
                var segment = it.get();
                route.Add(segment, attachment =>
                {
                    if (attachment == null || attachment.getRigidBody() == null)
                    {
                        return(FileInfo.PrefabInstance);
                    }
                    var rbNode = m_tree.GetNode(attachment.getRigidBody().getUuid());
                    if (rbNode == null)
                    {
                        Debug.LogWarning("Unable to find rigid body in cable attachment.");
                        return(FileInfo.PrefabInstance);
                    }
                    return(rbNode.GameObject);
                });
            }

            var materials = node.GetReferences(Node.NodeType.Material);

            if (materials.Length > 0)
            {
                cable.Material = materials[0].Asset as ShapeMaterial;
            }

            cable.GetComponent <CableRenderer>().InitializeRenderer();
            cable.GetComponent <CableRenderer>().Material = null;

            // Adding collision group from restored instance since the disabled pair
            // will be read from Space (cable.setEnableCollisions( foo, false ) will
            // work out of the box).
            var collisionGroups = cable.gameObject.GetOrCreateComponent <CollisionGroups>();

            collisionGroups.AddGroup(nativeCable.getUniqueId().ToString(), false);
            var referencedGroups = node.GetReferences(Node.NodeType.GroupId);

            foreach (var group in referencedGroups)
            {
                if (group.Object is string)
                {
                    collisionGroups.AddGroup(group.Object as string, false);
                }
            }

            return(true);
        }
Exemple #2
0
        private bool CreateConstraint(Node node)
        {
            var nativeConstraint = m_tree.GetConstraint(node.Uuid);

            if (nativeConstraint == null)
            {
                Debug.LogWarning("Unable to find native constraint instance with name: " +
                                 node.GameObject.name +
                                 " (UUID: " + node.Uuid.str() + ")");
                return(false);
            }

            var bodyNodes = node.GetReferences(Node.NodeType.RigidBody);

            if (bodyNodes.Length < 1 || bodyNodes.Length > 2)
            {
                Debug.LogWarning("Unsupported number of body references to constraint with name: " +
                                 node.GameObject.name +
                                 " (#bodies: " + bodyNodes.Length + ")");
                return(false);
            }

            var constraintType = Constraint.FindType(nativeConstraint);

            if (constraintType == ConstraintType.Unknown)
            {
                Debug.LogWarning("Unknown/unsupported constraint type of constraint with name: " +
                                 node.GameObject.name +
                                 " (UUID: " + node.Uuid.str() + ")");
                return(false);
            }

            Constraint constraint = node.GameObject.GetOrCreateComponent <Constraint>();

            constraint.SetType(constraintType, true);

            try {
                constraint.TryAddElementaryConstraints(nativeConstraint);
                constraint.VerifyImplementation();
            }
            catch (System.Exception e) {
                Debug.LogException(e);
                return(false);
            }

            // Scaling damping to our (sigh) hard coded time step.
            float fixedStepTime = AgXUnity.Simulation.DefaultTimeStep;
            float readTimeStep  = Convert.ToSingle(Simulation.getTimeStep());
            float timeStepRatio = fixedStepTime / readTimeStep;

            if (!Mathf.Approximately(timeStepRatio, 1.0f))
            {
                foreach (var ec in constraint.ElementaryConstraints)
                {
                    foreach (var rowData in ec.RowData)
                    {
                        if (rowData.Compliance < -float.Epsilon)
                        {
                            Debug.LogWarning("Constraint: " + constraint.name +
                                             " (ec name: " + rowData.ElementaryConstraint.NativeName + ")," +
                                             " has too low compliance: " + rowData.Compliance + ". Setting to zero.");
                            rowData.Compliance = 0.0f;
                        }
                        else if (rowData.Compliance > float.MaxValue)
                        {
                            Debug.LogWarning("Constraint: " + constraint.name +
                                             " (ec name: " + rowData.ElementaryConstraint.NativeName + ")," +
                                             " has too high compliance: " + rowData.Compliance + ". Setting to a large value.");
                            rowData.Compliance = 0.5f * float.MaxValue;
                        }
                        rowData.Damping *= timeStepRatio;
                    }
                }
            }

            constraint.AttachmentPair.ReferenceFrame.SetParent(bodyNodes[0].GameObject);
            constraint.AttachmentPair.ReferenceFrame.LocalPosition = nativeConstraint.getAttachment(0ul).getFrame().getLocalTranslate().ToHandedVector3();
            constraint.AttachmentPair.ReferenceFrame.LocalRotation = nativeConstraint.getAttachment(0ul).getFrame().getLocalRotate().ToHandedQuaternion();

            if (bodyNodes.Length > 1)
            {
                constraint.AttachmentPair.ConnectedFrame.SetParent(bodyNodes[1].GameObject);
            }
            else
            {
                constraint.AttachmentPair.ConnectedFrame.SetParent(FileInfo.PrefabInstance);
            }

            constraint.AttachmentPair.ConnectedFrame.LocalPosition = nativeConstraint.getAttachment(1ul).getFrame().getLocalTranslate().ToHandedVector3();
            constraint.AttachmentPair.ConnectedFrame.LocalRotation = nativeConstraint.getAttachment(1ul).getFrame().getLocalRotate().ToHandedQuaternion();

            constraint.AttachmentPair.Synchronized = constraintType != ConstraintType.DistanceJoint;

            return(true);
        }
Exemple #3
0
        private bool CreateWire(Node node)
        {
            var nativeWire = m_tree.GetWire(node.Uuid);

            if (nativeWire == null)
            {
                Debug.LogWarning("Unable to find native instance of wire: " + node.GameObject.name +
                                 " (UUID: " + node.Uuid.str() + ")");
                return(false);
            }

            Func <agx.RigidBody, GameObject> findRigidBody = (nativeRb) =>
            {
                if (nativeRb == null)
                {
                    return(FileInfo.PrefabInstance);
                }

                // Do not reference lumped nodes!
                if (agxWire.Wire.isLumpedNode(nativeRb))
                {
                    return(FileInfo.PrefabInstance);
                }

                Node rbNode = m_tree.GetNode(nativeRb.getUuid());
                if (rbNode == null)
                {
                    Debug.LogWarning("Unable to find reference rigid body: " + nativeRb.getName() + " (UUID: " + nativeRb.getUuid().str() + ")");
                    return(FileInfo.PrefabInstance);
                }
                if (rbNode.GameObject == null)
                {
                    Debug.LogWarning("Referenced native rigid body hasn't a game object: " + nativeRb.getName() + " (UUID: " + rbNode.Uuid.str() + ")");
                    return(FileInfo.PrefabInstance);
                }

                return(rbNode.GameObject);
            };

            var wire  = node.GameObject.GetOrCreateComponent <Wire>();
            var route = wire.Route;

            route.Clear();

            wire.RestoreLocalDataFrom(nativeWire);

            var nativeIt         = nativeWire.getRenderBeginIterator();
            var nativeEndIt      = nativeWire.getRenderEndIterator();
            var nativeBeginWinch = nativeWire.getWinchController(0u);
            var nativeEndWinch   = nativeWire.getWinchController(1u);

            if (nativeBeginWinch != null)
            {
                route.Add(nativeBeginWinch,
                          findRigidBody(nativeBeginWinch.getRigidBody()));
            }
            // Connecting nodes will show up in render iterators.
            else if (nativeIt.get().getNodeType() != agxWire.Node.Type.CONNECTING && nativeWire.getFirstNode().getNodeType() == agxWire.Node.Type.BODY_FIXED)
            {
                route.Add(nativeWire.getFirstNode(), findRigidBody(nativeWire.getFirstNode().getRigidBody()));
            }

            while (!nativeIt.EqualWith(nativeEndIt))
            {
                var nativeNode = nativeIt.get();

                // Handing ContactNode and ShapeContactNode parenting.
                GameObject nodeParent = null;
                if (nativeNode.getType() == agxWire.Node.Type.CONTACT)
                {
                    var nativeGeometry = nativeNode.getAsContact().getGeometry();
                    var geometryNode   = m_tree.GetNode(nativeGeometry.getUuid());
                    if (geometryNode != null && geometryNode.GetChildren(Node.NodeType.Shape).Length > 0)
                    {
                        nodeParent = geometryNode.GetChildren(Node.NodeType.Shape)[0].GameObject;
                    }
                }
                else if (nativeNode.getType() == agxWire.Node.Type.SHAPE_CONTACT)
                {
                    var nativeShape = nativeNode.getAsShapeContact().getShape();
                    var shapeNode   = m_tree.GetNode(nativeShape.getUuid());
                    if (shapeNode != null)
                    {
                        nodeParent = shapeNode.GameObject;
                    }
                }

                if (nodeParent == null)
                {
                    nodeParent = findRigidBody(nativeNode.getRigidBody());
                }

                route.Add(nativeNode, nodeParent);
                nativeIt.inc();
            }

            // Remove last node if we should have a winch or a body fixed node there.
            if (route.Last().Type == Wire.NodeType.FreeNode && nativeWire.getLastNode().getNodeType() == agxWire.Node.Type.BODY_FIXED)
            {
                route.Remove(route.Last());
            }

            if (nativeEndWinch != null)
            {
                route.Add(nativeEndWinch,
                          findRigidBody(nativeEndWinch.getRigidBody()));
            }
            else if (nativeIt.prev().get().getNodeType() != agxWire.Node.Type.CONNECTING && nativeWire.getLastNode().getNodeType() == agxWire.Node.Type.BODY_FIXED)
            {
                route.Add(nativeWire.getLastNode(), findRigidBody(nativeWire.getLastNode().getRigidBody()));
            }

            var materials = node.GetReferences(Node.NodeType.Material);

            if (materials.Length > 0)
            {
                wire.Material = materials[0].Asset as ShapeMaterial;
            }

            wire.GetComponent <WireRenderer>().InitializeRenderer();
            // Reset to assign default material.
            wire.GetComponent <WireRenderer>().Material = null;

            // Adding collision group from restored instance since the disabled pair
            // will be read from Space (wire.setEnableCollisions( foo, false ) will
            // work out of the box).
            var collisionGroups = wire.gameObject.GetOrCreateComponent <CollisionGroups>();

            collisionGroups.AddGroup(nativeWire.getGeometryController().getDisabledCollisionsGroupId().ToString(), false);
            foreach (var id in nativeWire.getGeometryController().getGroupIds())
            {
                collisionGroups.AddGroup(id.ToString(), false);
            }

            return(true);
        }
Exemple #4
0
        private void Generate(Node node)
        {
            if (node == null)
            {
                return;
            }

            // TODO: Skip if node.GameObject != null means "use Unity configuration".

            switch (node.Type)
            {
            case Node.NodeType.Assembly:
                agx.Frame frame = m_tree.GetAssembly(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName("", node.Type.ToString());

                node.GameObject.transform.position = frame.getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = frame.getRotate().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <Assembly>();

                break;

            case Node.NodeType.RigidBody:
                agx.RigidBody nativeRb = m_tree.GetRigidBody(node.Uuid);
                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeRb.getName(), node.Type.ToString());

                node.GameObject.transform.position = nativeRb.getPosition().ToHandedVector3();
                node.GameObject.transform.rotation = nativeRb.getRotation().ToHandedQuaternion();

                node.GameObject.GetOrCreateComponent <RigidBody>().RestoreLocalDataFrom(nativeRb);

                break;

            case Node.NodeType.Geometry:
                // Ignoring geometries - handling Shape == Geometry.
                // The shapes are children to this node.
                break;

            case Node.NodeType.Shape:
                var nativeGeometry  = m_tree.GetGeometry(node.Parent.Uuid);
                var nativeShape     = m_tree.GetShape(node.Uuid);
                var nativeShapeType = (agxCollide.Shape.Type)nativeShape.getType();

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeGeometry.getName() +
                                                "_" +
                                                nativeShapeType.ToString().ToLower().FirstCharToUpperCase(),
                                                node.Type.ToString());

                node.GameObject.transform.position = nativeShape.getTransform().getTranslate().ToHandedVector3();
                node.GameObject.transform.rotation = nativeShape.getTransform().getRotate().ToHandedQuaternion();

                if (!CreateShape(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Material:
                var nativeMaterial = m_tree.GetMaterial(node.Uuid);
                node.Asset      = ScriptAsset.Create <ShapeMaterial>().RestoreLocalDataFrom(nativeMaterial);
                node.Asset.name = FindName(nativeMaterial.getName(), node.Type.ToString());

                node.Asset = AddOrReplaceAsset(node.Asset as ShapeMaterial, node, AgXUnity.IO.AssetType.ShapeMaterial);

                break;

            case Node.NodeType.ContactMaterial:
                var nativeContactMaterial = m_tree.GetContactMaterial(node.Uuid);
                var nativeFrictionModel   = nativeContactMaterial.getFrictionModel();

                var contactMaterial = ScriptAsset.Create <ContactMaterial>().RestoreLocalDataFrom(nativeContactMaterial);
                contactMaterial.name = FindName("ContactMaterial_" +
                                                nativeContactMaterial.getMaterial1().getName() +
                                                "_" +
                                                nativeContactMaterial.getMaterial2().getName(),
                                                node.Type.ToString());

                var materials = node.GetReferences(Node.NodeType.Material);
                if (materials.Length == 0)
                {
                    Debug.LogWarning("No materials referenced to ContactMaterial node.");
                }
                else if (materials.Length == 1)
                {
                    contactMaterial.Material1 = contactMaterial.Material2 = materials[0].Asset as ShapeMaterial;
                }
                else if (materials.Length > 1)
                {
                    contactMaterial.Material1 = materials[0].Asset as ShapeMaterial;
                    contactMaterial.Material2 = materials[1].Asset as ShapeMaterial;
                    if (materials.Length > 2)
                    {
                        Debug.LogWarning("More than two materials referenced to ContactMaterial (" + node.Asset.name + "). First two are used.");
                    }
                }

                if (nativeFrictionModel != null)
                {
                    var frictionModelAsset = ScriptAsset.Create <FrictionModel>().RestoreLocalDataFrom(nativeFrictionModel);
                    frictionModelAsset.name       = "FrictionModel_" + contactMaterial.name;
                    contactMaterial.FrictionModel = AddOrReplaceAsset(frictionModelAsset, node, AgXUnity.IO.AssetType.FrictionModel);
                }

                node.Asset = contactMaterial = AddOrReplaceAsset(contactMaterial, node, AgXUnity.IO.AssetType.ContactMaterial);

                break;

            case Node.NodeType.Constraint:
                var nativeConstraint = m_tree.GetConstraint(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeConstraint.getName(),
                                                "AgXUnity." + Constraint.FindType(nativeConstraint));

                if (!CreateConstraint(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Wire:
                var nativeWire = m_tree.GetWire(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeWire.getName(), "AgXUnity.Wire");

                if (!CreateWire(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;

            case Node.NodeType.Cable:
                var nativeCable = m_tree.GetCable(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeCable.getName(), "AgXUnity.Cable");

                if (!CreateCable(node))
                {
                    GameObject.DestroyImmediate(node.GameObject);
                }

                break;
            }

            foreach (var child in node.Children)
            {
                Generate(child);
            }
        }