Exemplo n.º 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);
        }
Exemplo n.º 2
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.WireNode.Type.CONNECTING && nativeWire.getFirstNode().getNodeType() == agxWire.WireNode.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.WireNode.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.WireNode.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.WireNode.Type.BODY_FIXED)
            {
                route.Remove(route.Last());
            }

            if (nativeEndWinch != null)
            {
                route.Add(nativeEndWinch,
                          findRigidBody(nativeEndWinch.getRigidBody()));
            }
            else if (nativeIt.prev().get().getNodeType() != agxWire.WireNode.Type.CONNECTING && nativeWire.getLastNode().getNodeType() == agxWire.WireNode.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);
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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>();

            // Is the constraint enabled/active?
            // Somewhat strange though: Why is not only the Constraint component disabled?
            // For RigidBody component, only that component is disabled, but the below code disables the whole constraint GameObject.
            // Works for now
            constraint.gameObject.SetActive(nativeConstraint.isEnabled());

            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);
        }
Exemplo n.º 5
0
        private void Generate(Node node)
        {
            if (node == null)
            {
                return;
            }

            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:
                // Ignoring materials - the referenced materials should have been restored
                // by now using RestoreShapeMaterial.
                break;

            case Node.NodeType.ContactMaterial:
                var           materialNodes = node.GetReferences(Node.NodeType.Material);
                Func <string> contactMaterialMaterialNames = () =>
                {
                    return(m_tree.GetMaterial(materialNodes[0].Uuid).getName() + " <-> " +
                           m_tree.GetMaterial(materialNodes[1].Uuid).getName());
                };
                if (materialNodes.Length == 0)
                {
                    Debug.LogWarning("No materials referenced to ContactMaterial node - ignoring contact material.");
                    break;
                }
                else if (materialNodes.Length > 2)
                {
                    Debug.LogWarning("More than two materials referenced to contact material - first two will be used: " +
                                     contactMaterialMaterialNames());
                }

                var materials = new ShapeMaterial[2]
                {
                    materialNodes[0].Asset as ShapeMaterial,
                    materialNodes.Length > 1 ?
                    materialNodes[1].Asset as ShapeMaterial :
                    materialNodes[0].Asset as ShapeMaterial
                };

                var nativeContactMaterial = m_tree.GetContactMaterial(node.Uuid);
                var nativeFrictionModel   = nativeContactMaterial.getFrictionModel();
                if (materials[0] == null || materials[1] == null)
                {
                    Debug.LogWarning($"Unreferenced ShapeMaterial(s) in ContactMaterial: {contactMaterialMaterialNames()} - ignoring contact material.");
                    break;
                }
                Predicate <ContactMaterial> materialsMatcher = cm => (cm.Material1 == materials[0] && cm.Material2 == materials[1]) ||
                                                               (cm.Material2 == materials[0] && cm.Material1 == materials[1]);
                var contactMaterial = FileInfo.ObjectDb.GetOrCreateAsset(materialsMatcher,
                                                                         FindName($"ContactMaterial_{materials[ 0 ].name}_{materials[ 1 ].name}",
                                                                                  node.Type.ToString()),
                                                                         cm =>
                {
                    cm.Material1 = materials[0];
                    cm.Material2 = materials[1];
                    cm.RestoreLocalDataFrom(nativeContactMaterial);
                });
                if (nativeFrictionModel != null)
                {
                    contactMaterial.FrictionModel = FileInfo.ObjectDb.GetOrCreateAsset(contactMaterial.FrictionModel,
                                                                                       $"FrictionModel_{contactMaterial.name}",
                                                                                       fm => fm.RestoreLocalDataFrom(nativeFrictionModel));
                }

                node.Asset = 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;

            case Node.NodeType.ObserverFrame:
                var nativeObserverFrame = m_tree.GetObserverFrame(node.Uuid);

                node.GameObject      = GetOrCreateGameObject(node);
                node.GameObject.name = FindName(nativeObserverFrame.getName(), "AGXUnity.ObserverFrame");

                var rbNode = node.GetReferences(Node.NodeType.RigidBody).FirstOrDefault();
                node.GameObject.GetOrCreateComponent <ObserverFrame>().RestoreLocalDataFrom(nativeObserverFrame,
                                                                                            rbNode != null ? rbNode.GameObject : null);

                break;
            }

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