Exemplo n.º 1
0
 public Bone(string name, TRSTransform bindState)
 {
     Name       = name;
     _bindState = bindState.HardCopy();
 }
Exemplo n.º 2
0
        private static Bone EnumNode(
            Bone parent,
            Node node,
            Node[] nodes,
            List <ObjectInfo> objects,
            Matrix4 bindMatrix,
            Matrix4 invParent,
            List <Light> lights,
            List <Camera> cameras,
            EIgnoreFlags ignore)
        {
            Bone rootBone = null;

            Matrix4 nodeMatrix = node.GetTransformMatrix();

            bindMatrix = bindMatrix * nodeMatrix;

            Matrix4 inv = invParent;

            if (node.Type == Node.EType.JOINT)
            {
                TRSTransform transform = new TRSTransform
                {
                    Matrix = invParent * bindMatrix
                };
                Bone bone = new Bone(node.Name ?? node.ID, transform);
                node.UserData = bone;
                if (parent == null)
                {
                    rootBone = bone;
                }
                else
                {
                    bone.Parent = parent;
                }
                parent = bone;
                inv    = bindMatrix.Inverted();
            }

            foreach (Node e in node.NodeElements)
            {
                Bone b = EnumNode(parent, e, nodes, objects, bindMatrix, inv, lights, cameras, ignore);
                if (rootBone == null && b != null)
                {
                    rootBone = b;
                }
            }

            foreach (IInstanceElement inst in node.InstanceElements)
            {
                //Rigged/morphed mesh?
                if (inst is InstanceController controllerRef)
                {
                    if (ignore.HasFlag(EIgnoreFlags.Controllers))
                    {
                        continue;
                    }
                    var controller = controllerRef.GetUrlInstance();
                    var child      = controller?.SkinOrMorphElement;
                    if (child != null)
                    {
                        //Rigged mesh
                        if (child is LibraryControllers.Controller.Skin skin)
                        {
                            IID element = skin.Source.GetElement(skin.Root);
                            if (element is LibraryGeometries.Geometry geometry)
                            {
                                objects.Add(new ObjectInfo(geometry, skin, bindMatrix, controllerRef, parent, node));
                            }
                            else if (element is LibraryControllers.Controller.Morph morph)
                            {
                                objects.Add(new ObjectInfo(morph, skin, bindMatrix, controllerRef, parent, node));
                            }
                            else
                            {
                                WriteLine(skin.Source.URI + " does not point to a valid geometry or morph controller entry.");
                            }
                        }
                        //Static morphed mesh
                        else if (child is LibraryControllers.Controller.Morph morph)
                        {
                            objects.Add(new ObjectInfo(morph, null, bindMatrix, controllerRef, parent, node));
                        }
                        else
                        {
                            WriteLine("Instanced controller does not have a valid child entry.");
                        }
                    }
                    else
                    {
                        WriteLine("Instanced controller does not have a valid child entry.");
                    }
                }
                //Static mesh?
                else if (inst is InstanceGeometry geomRef)
                {
                    if (ignore.HasFlag(EIgnoreFlags.Geometry))
                    {
                        continue;
                    }
                    var geometry = geomRef.GetUrlInstance();
                    if (geometry != null)
                    {
                        objects.Add(new ObjectInfo(geometry, null, bindMatrix, geomRef, parent, node));
                    }
                    else
                    {
                        WriteLine(geomRef.Url.URI + " does not point to a valid geometry entry.");
                    }
                }
                //Camera?
                else if (inst is InstanceCamera camRef)
                {
                    if (ignore.HasFlag(EIgnoreFlags.Cameras))
                    {
                        continue;
                    }
                    var camera = camRef.GetUrlInstance();
                    //TODO: read camera information
                }
                //Light?
                else if (inst is InstanceLight lightRef)
                {
                    if (ignore.HasFlag(EIgnoreFlags.Lights))
                    {
                        continue;
                    }
                    var light = lightRef.GetUrlInstance();
                    //TODO: read light information
                }
                //Another node tree?
                else if (inst is InstanceNode nodeRef)
                {
                    var actualNode = nodeRef.GetUrlInstance();
                    var proxyNode  = nodeRef.GetProxyInstance();
                    //TODO: read actual node, proxy node is just a placeholder
                }
            }
            return(rootBone);
        }