Ejemplo n.º 1
0
            // template MeshNormals
            // {
            //     DWORD nNormals;
            //     array Vector normals[nNormals];
            //     DWORD nFaceNormals;
            //     array MeshFace faceNormals[nFaceNormals];
            // }
            /// <summary>
            /// Imports the normals associated with the current mesh.
            /// </summary>
            private void ImportNormals()
            {
                tokens.ReadName();
                hasNormals = true;

                int numNormals = tokens.NextInt();

                if (numNormals == mesh.Positions.Count)
                {
                    normals = new Vector3[numNormals];
                }
                else
                {
                    hasNormals = false;
                }

                for (int i = 0; i < numNormals; i++)
                {
                    Vector3 norm = tokens.NextVector3();
                    if (numNormals == mesh.Positions.Count)
                    {
                        normals[i]    = norm;
                        normals[i].Z *= -1;
                    }
                }

                int numFaces = tokens.NextInt();

                for (int i = 0; i < numFaces; i++)
                {
                    int numNormalsPerFace = tokens.NextInt();
                    tokens.SkipTokens(2 * numNormalsPerFace + 1);
                }
                // end of mesh normals
                tokens.SkipToken();
            }
Ejemplo n.º 2
0
        public override NodeContent Import(string filename, ContentImporterContext context)
        {
            System.Threading.Thread currentThread = System.Threading.Thread.CurrentThread;
            CultureInfo             culture       = new CultureInfo("en-US");

            currentThread.CurrentCulture   = culture;
            currentThread.CurrentUICulture = culture;
            this.fileName = filename;
            this.context  = context;
            // Create an instance of a class that splits a .X file into tokens and provides
            // functionality for iterating and parsing the tokens
            tokens = new XFileTokenizer(filename);

            // skip header
            tokens.SkipTokens(3);
            root          = new NodeContent();
            root.Identity = new ContentIdentity(fileName, "Animation Library XImporter");
            // fill in the tree
            ImportRoot();

            Matrix absoluteMeshTransform = Matrix.Identity;
            List <SkinTransformContent[]> skinTransforms = new List <SkinTransformContent[]>();
            List <Matrix> absMeshTransforms = new List <Matrix>();

            // Now that we have mapped bone names to their indices, we can create the vertices
            // in each mesh so that they contain indices and weights
            foreach (XMeshImporter mesh in meshes)
            {
                mesh.CreateGeometry();
                SkinTransformContent[] meshSkinTransforms = mesh.SkinTransforms;
                skinTransforms.Add(mesh.SkinTransforms);
                if (mesh.SkinTransforms != null && mesh.SkinTransforms.Length > 0)
                {
                    absoluteMeshTransform = mesh.Mesh.AbsoluteTransform;
                }
                absMeshTransforms.Add(mesh.Mesh.AbsoluteTransform);
            }

            FindBones(root);
            root.OpaqueData.Add("AbsoluteMeshTransforms", absMeshTransforms);
            // Calculates bind pose as required for compatibility with Xna Content DOM
            Dictionary <string, Matrix> absTransformsDict = new Dictionary <string, Matrix>(skinTransforms.Count);
            int k = 0;

            foreach (SkinTransformContent[] sst in skinTransforms)
            {
                if (sst != null)
                {
                    absoluteMeshTransform = meshes[k].Mesh.AbsoluteTransform;
                    foreach (SkinTransformContent st in sst)
                    {
                        Matrix abs = Matrix.Invert(Matrix.Invert(absoluteMeshTransform) * st.Transform);
                        absTransformsDict.Add(st.BoneName, abs);
                    }
                }
                k++;
            }



            FixBindPose(root, absTransformsDict);

            return(root);
        }