Esempio n. 1
0
        public void Draw(GraphicsDeviceManager gcm, BasicEffect be, RasterizerState rs)
        {
            this.UpdateSkinCondition();
            this.ComputeMatricesWithChanges();


            for (int mesh = 0; mesh < this.Triangle.Count; mesh++)
            {
                int ind = this.Triangle[mesh].Count - 1;
                for (int triInd = 0; triInd < this.Triangle[mesh].Count; triInd += 3)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        VertexPositionColorTexture vpt = new VertexPositionColorTexture();
                        vpt.Position = GetTriangleVertex(mesh, triInd + k);
                        var infsV = GetInfluences(mesh, triInd + k);
                        var infs  = GetMatrices(mesh, triInd + k);

                        Vector3 final = Vector3.Zero;

                        for (int l = 0; l < infs.Count; l++)
                        {
                            Matrix  mat = this.Skeleton.BonesMatrices[infs[l]];
                            float   inf = infsV[l];
                            Vector3 v3  = Vector3.Transform(vpt.Position, Matrix.Invert(mat));

                            v3       = Vector3.Transform(v3, this.Skeleton.BonesReMatrices[infs[l]]);
                            final.X += v3.X * inf;
                            final.Y += v3.Y * inf;
                            final.Z += v3.Z * inf;
                        }
                        vpt.Position          = final;
                        vpt.TextureCoordinate = GetTriangleUv(mesh, triInd + k);
                        vpt.Color             = GetTriangleColor(mesh, triInd + k);

                        vpt.Position += this.Position;
                        this.RenderBuffer[mesh][ind] = vpt;
                        ind--;
                    }
                    //ApplyTransformations
                }


                gcm.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

                if (be != null)
                {
                    be.TextureEnabled = true;
                    be.Texture        = SrkBinary.GetT2D(this.materialFileNames[this.MaterialIndices[mesh]]);
                    be.CurrentTechnique.Passes[0].Apply();
                }
                if (rs != null)
                {
                    gcm.GraphicsDevice.RasterizerState = rs;
                }
                gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, this.RenderBuffer[mesh], 0, this.Triangle[mesh].Count / 3);
            }
            this.Skeleton.GetGraphic();
            this.Skeleton.Draw(gcm, be, rs);
        }
Esempio n. 2
0
        public Tim_t7(ref BAR b)
        {
            this.file                = b;
            this.Patches             = new List <Bitmap>(0);
            this.PatchesPositions    = new List <Point>(0);
            this.PatchesSizes        = new List <Size>(0);
            this.PatchesCounts       = new List <int>(0);
            this.PatchesDestinations = new List <int>(0);

            bool containsTexture = false;

            for (int i = 0; i < b.fileList.Count; i++)
            {
                if (b.fileList[i].type == 7)
                {
                    this.binary = new SrkBinary(ref b.fileList[i].data);

                    this.t7_index = i;
                    this.st       = new SrkTim(b.fileList[i]);
                    this.st.ParseTim();

                    this.ct = new CatzTim(b.fileList[i]);
                    this.ct.parse();

                    catzMode = this.st.Patches.Count == 0 || (ct.imageCount > st.Bitmaps.Count);

                    if (catzMode)
                    {
                        this.textures = new Texture2D[this.ct.imageCount];
                        UpdateCatzT2D();
                    }
                    else
                    {
                        this.Patches             = this.st.Patches;
                        this.PatchesPositions    = this.st.PatchLocs;
                        this.PatchesSizes        = this.st.PatchSizes;
                        this.PatchesCounts       = this.st.PatchCounts;
                        this.PatchesDestinations = this.st.PatchTextureIndexes;
                    }

                    containsTexture = true;
                    break;
                }
            }
            if (!containsTexture)
            {
                Console.WriteLine("No TIM2 found inside that file.");
                Console.WriteLine("Exiting...");
                System.Threading.Thread.Sleep(2000);
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }
Esempio n. 3
0
        public SrkTim(BAR.BARFile b)
        {
            this.barFile   = b;
            this.timBinary = new SrkBinary(ref this.barFile.data);

            this.DisplayTextures     = new Microsoft.Xna.Framework.Graphics.Texture2D[0];
            this.Palettes            = new List <System.Drawing.Color[]>(0);
            this.Bitmaps             = new List <Bitmap>(0);
            this.textureAddresses    = new List <int>(0);
            this.paletteAddresses    = new List <int>(0);
            this.Patches             = new List <Bitmap>(0);
            this.PatchIndexes        = new List <int>(0);
            this.PatchTextureIndexes = new List <int>(0);
            this.PatchCounts         = new List <int>(0);
            this.PatchSizes          = new List <Size>(0);
            this.PatchLocs           = new List <Point>(0);
        }
Esempio n. 4
0
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Object3D.emptyObject           = new Object3D();
            Object3D.emptyObject.MinCoords = new Vector3(-500, -500, -500);
            Object3D.emptyObject.MaxCoords = new Vector3(500, 500, 500);
            Object3D.emptyObject.Skeleton  = new Skeleton(Object3D.emptyObject);
            focusedObject = Object3D.emptyObject;

            SrkBinary.InitEmptyData();

            backgroundColor   = new Color(30, 30, 30);
            transformationBox = new TransformationBox();

            for (int i = 0; i < Program.args_.Length; i++)
            {
                string fname = Program.args_[i].ToLower();
                if (Path.GetExtension(fname) == ".dae")
                {
                    DAE dae = new DAE(fname);
                    dae.Parse();
                    dae.Skeleton.AllowBoneSelect = true;
                    dae.Skeleton.ShowIndices     = false;
                    focusedObject = dae;
                }
                if (Path.GetExtension(fname) == ".obj")
                {
                    OBJ obj = new OBJ(fname);
                    obj.Parse();
                    focusedObject = obj;
                }
                if (Path.GetExtension(fname) == ".mdlx")
                {
                    MDLX mdl = new MDLX(fname);
                    mdl.Parse();
                    mdl.Skeleton.AllowBoneSelect = true;
                    mdl.Skeleton.ShowIndices     = false;
                    focusedObject = mdl;
                }
                focusedObject.FileName = fname;
            }
            Controls.DefaultCamera();
            Controls.ResetCamToTarget();
        }
Esempio n. 5
0
        public void Draw(GraphicsDeviceManager gcm, BasicEffect be, RasterizerState rs)
        {
            for (int mesh = 0; mesh < this.Triangle.Count; mesh++)
            {
                int ind = this.Triangle[mesh].Count - 1;
                //Object3D.orderZindex
                for (int triInd = 0; triInd < this.Triangle[mesh].Count; triInd += 3)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        VertexPositionColorTexture vpt = new VertexPositionColorTexture();
                        vpt.Position          = GetTriangleVertex(mesh, triInd + k);
                        vpt.TextureCoordinate = GetTriangleUv(mesh, triInd + k);
                        vpt.Color             = Microsoft.Xna.Framework.Color.White;

                        vpt.Position           += this.Position;
                        RenderBuffer[mesh][ind] = vpt;
                        ind--;
                    }
                }
                if (be != null)
                {
                    be.TextureEnabled = true;
                    be.Texture        = SrkBinary.GetT2D(this.materialFileNames[this.MaterialIndices[mesh]]);
                    be.CurrentTechnique.Passes[0].Apply();
                }
                if (rs != null)
                {
                    gcm.GraphicsDevice.RasterizerState = rs;
                }
                if (RenderBuffer.Length > 0)
                {
                    gcm.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                    gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, RenderBuffer[mesh], 0, RenderBuffer[mesh].Length / 3);
                }
            }
            //this.Skeleton.GetGraphic();
            //this.Skeleton.Draw(gcm, be, rs);
        }
Esempio n. 6
0
        public Mdl_t4(ref BAR b)
        {
            bool containsModel = false;

            for (int i = 0; i < b.fileList.Count; i++)
            {
                if (b.fileList[i].type == 4)
                {
                    this.binary = new SrkBinary(ref b.fileList[i].data);

                    t4_index      = i;
                    containsModel = true;
                    break;
                }
            }
            if (!containsModel)
            {
                Console.WriteLine("No model found inside that file.");
                Console.WriteLine("Exiting...");
                System.Threading.Thread.Sleep(2000);
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }
Esempio n. 7
0
        public void ParseMeshes()
        {
            int modelType = this.t4.binary.ReadInt(0, true); /* Normal = 3  || Shadow = 4 */

            int bonesCount               = this.t4.binary.ReadShort(0x10, true);
            int skeletonOffset           = this.t4.binary.ReadShort(0x14, true);
            int skeletonDefinitonsOffset = this.t4.binary.ReadShort(0x18, true);
            int meshCount = this.t4.binary.ReadInt(0x1C, true);

            if (modelType == 3)
            {
                byte[] skeletonBytes = new byte[bonesCount * 0x40];
                Array.Copy(this.t4.binary.Buffer, this.t4.binary.Pointer + skeletonOffset, skeletonBytes, 0, skeletonBytes.Length);
                this.binarySkeleton           = new SrkBinary(ref skeletonBytes);
                this.Skeleton                 = new Skeleton(this);
                this.Skeleton.Bones           = new Bone[bonesCount];
                this.Skeleton.BonesMatrices   = new Matrix[bonesCount];
                this.Skeleton.BonesReMatrices = new Matrix[bonesCount];
                for (int i = 0; i < bonesCount; i++)
                {
                    short id         = this.binarySkeleton.ReadShort(i * 0x40, false);
                    short parentId   = this.binarySkeleton.ReadShort(i * 0x40 + 4, false);
                    float scaleX     = this.binarySkeleton.ReadFloat(i * 0x40 + 0x10, false);
                    float scaleY     = this.binarySkeleton.ReadFloat(i * 0x40 + 0x14, false);
                    float scaleZ     = this.binarySkeleton.ReadFloat(i * 0x40 + 0x18, false);
                    float rotateX    = this.binarySkeleton.ReadFloat(i * 0x40 + 0x20, false);
                    float rotateY    = this.binarySkeleton.ReadFloat(i * 0x40 + 0x24, false);
                    float rotateZ    = this.binarySkeleton.ReadFloat(i * 0x40 + 0x28, false);
                    float translateX = this.binarySkeleton.ReadFloat(i * 0x40 + 0x30, false);
                    float translateY = this.binarySkeleton.ReadFloat(i * 0x40 + 0x34, false);
                    float translateZ = this.binarySkeleton.ReadFloat(i * 0x40 + 0x38, false);

                    this.Skeleton.Bones[i]          = new Bone(id);
                    this.Skeleton.Bones[i].ParentID = parentId;
                    this.Skeleton.Bones[i].ScaleX   = scaleX;
                    this.Skeleton.Bones[i].ScaleY   = scaleY;
                    this.Skeleton.Bones[i].ScaleZ   = scaleZ;

                    this.Skeleton.Bones[i].RotateX = rotateX;
                    this.Skeleton.Bones[i].RotateY = rotateY;
                    this.Skeleton.Bones[i].RotateZ = rotateZ;

                    this.Skeleton.Bones[i].TranslateX = translateX;
                    this.Skeleton.Bones[i].TranslateY = translateY;
                    this.Skeleton.Bones[i].TranslateZ = translateZ;
                }
                //Bone.outpt = "";
                GetMatrices();
                ComputeMatrices();
                //File.WriteAllText("matsXNA.txt", Bone.outpt);


                byte[] skeletonDefinitionsBytes = new byte[0x110];
                Array.Copy(this.t4.binary.Buffer, this.t4.binary.Pointer + skeletonDefinitonsOffset, skeletonDefinitionsBytes, 0, skeletonDefinitionsBytes.Length);
                this.binarySkeletonDefinitons = new SrkBinary(ref skeletonBytes);
            }

            for (int currMeshIndex = 0; currMeshIndex < meshCount; currMeshIndex++)
            {
                int dmaOffset       = this.t4.binary.ReadInt(0x20 + currMeshIndex * 0x20 + 0x10, true);
                int meshStartOffset = this.t4.binary.ReadInt(dmaOffset + 4, true);

                int matiOffset = this.t4.binary.ReadInt(0x20 + currMeshIndex * 0x20 + 0x14, true);
                int matiCount  = this.t4.binary.ReadInt(matiOffset, true);

                int meshEndOffset = matiOffset + matiCount * 4;
                SrkBinary.Align16(ref meshEndOffset);

                int meshSize = meshEndOffset - meshStartOffset;

                byte[] meshBytes = new byte[0x20 + meshSize];
                Array.Copy(this.t4.binary.Buffer, this.t4.binary.Pointer + 0x20 + currMeshIndex * 0x20, meshBytes, 0, 0x20);
                Array.Copy(this.t4.binary.Buffer, this.t4.binary.Pointer + meshStartOffset, meshBytes, 0x20, meshSize);
                Mesh currMesh = new Mesh(meshBytes);
                currMesh.Objet  = this;
                currMesh.Binary = new SrkBinary(ref meshBytes);

                int dmaOffsetLocal  = dmaOffset - meshStartOffset;
                int matiOffsetLocal = matiOffset - meshStartOffset;

                currMesh.Binary.WriteInt(0x10, dmaOffsetLocal, false);
                currMesh.Binary.WriteInt(0x14, matiOffsetLocal, false);
                currMesh.Binary.Pointer = 0x20;

                int dmaPosition = dmaOffsetLocal;

                bool readDMAs = true;
                while (readDMAs)
                {
                    int currVifOffset = currMesh.Binary.ReadInt(dmaPosition + 4, true) - meshStartOffset;
                    currMesh.Binary.WriteInt(dmaPosition + 4, currVifOffset, true); /* local dans fichier de sortie */
                    currVifOffset += 8;                                             /* skip 01 01 00 01     00 80 04 6C */

                    int type = currMesh.Binary.ReadInt(currVifOffset, true);
                    int matCountPerVert_count = currMesh.Binary.ReadInt(currVifOffset + ((type == 2) ? 0x2C : 0x3C), true);

                    currMesh.DMA_Pointers.Add(dmaPosition);
                    dmaPosition += matCountPerVert_count * 0x10 + 0x20;

                    if (currMesh.Binary.ReadUInt(dmaPosition + 0x18, true) != 0x01000101)
                    {
                        readDMAs = false;
                    }
                }
                //File.WriteAllBytes("mesh" + currMeshIndex.ToString("d3") + ".bin", meshBytes);
                currMesh.GetData();
                if (modelType == 3)
                {
                    this.meshes.Add(currMesh);
                }
                else
                {
                    this.shadowMeshes.Add(currMesh);
                }
            }
        }
Esempio n. 8
0
        public void Parse()
        {
            byte[] data = SrkBinary.GetBytesArray(this.FileName);

            string fileData = Encoding.ASCII.GetString(data);

            fileData = fileData.Replace("xmlns", "whocares");
            this.Document.LoadXml(fileData);


            XmlNodeList materials = this.Document.SelectNodes("//library_materials/material");

            if (materials != null)
            {
                for (int i = 0; i < materials.Count; i++)
                {
                    string materialID = materials[i].SelectNodes("@id")[0].InnerText;
                    string effectID   = materials[i].SelectNodes("instance_effect/@url")[0].InnerText.Remove(0, 1);

                    XmlNode     effectNode   = this.Document.SelectNodes("//library_effects/effect[@id='" + effectID + "']")[0];
                    string      imageID      = "";
                    XmlNodeList surfInitNode = effectNode.SelectNodes("profile_COMMON//surface/init_from");
                    if (surfInitNode != null && surfInitNode.Count > 0)
                    {
                        imageID = surfInitNode[0].InnerText;
                    }

                    XmlNodeList textureNode = effectNode.SelectNodes("profile_COMMON//texture/@texture");
                    if (imageID.Length == 0 && textureNode != null && textureNode.Count > 0)
                    {
                        imageID = textureNode[0].InnerText;
                    }
                    string fileName = this.Document.SelectNodes("//library_images/image[@id='" + imageID + "']/init_from")[0].InnerText;
                    fileName = fileName.Replace("file://", "");
                    fileName = fileName.Replace("../", "");
                    fileName = fileName.Replace("./", "");
                    fileName = fileName.Replace("/", "\\");


                    if (Path.GetExtension(fileName).Length == 0)
                    {
                        string dir         = Path.GetDirectoryName(fileName);
                        bool   notFoundExt = true;
                        if (Directory.Exists(dir))
                        {
                            string[] dirFiles = Directory.GetFiles(dir);
                            for (int d = 0; d < dirFiles.Length; d++)
                            {
                                if (Path.GetFileNameWithoutExtension(dirFiles[d]) == Path.GetFileNameWithoutExtension(fileName))
                                {
                                    fileName   += Path.GetExtension(dirFiles[d]);
                                    notFoundExt = false;
                                    break;
                                }
                            }
                        }
                        if (notFoundExt)
                        {
                            fileName += ".png";
                        }
                    }

                    string[] spli   = fileName.Split('\\');
                    bool     exists = false;

                    string fname = "";
                    if (spli.Length > 0)
                    {
                        fileName = spli[spli.Length - 1];
                        if (File.Exists(this.DirectoryName + @"\" + fileName))
                        {
                            fname  = this.DirectoryName + @"\" + fileName;
                            exists = true;
                        }
                    }

                    if (!exists)
                    {
                        fname  = fileName;
                        exists = true;
                    }

                    this.materialNames.Add(materialID);
                    this.materialFileNames.Add(fname);
                }
            }

            var         wrapnode   = this.Document.SelectNodes("//technique[@profile='MAYA']");
            bool        wrapUV     = wrapnode != null && wrapnode.Count > 0;
            XmlNodeList geometries = this.Document.SelectNodes("//library_geometries/geometry");

            for (int i = 0; i < geometries.Count; i++)
            {
                XmlNode mesh = geometries[i].SelectNodes("mesh")[0];
                this.GeometryIDs.Add(geometries[i].SelectNodes("@id")[0].InnerText);

                XmlNodeList inputs = mesh.SelectNodes("*/input[@semantic]");


                string vertexID   = "";
                string normalID   = "";
                string texcoordID = "";
                string colorID    = "";

                vertexTriInd.Add(-1);
                normalTriInd.Add(-1);
                uvTriInd.Add(-1);
                colorTriInd.Add(-1);
                int stride = 0;
                for (int j = 0; j < inputs.Count; j++)
                {
                    string      semanticAtt = inputs[j].SelectNodes("@semantic")[0].InnerText.ToUpper();
                    XmlNodeList offsetAtt   = inputs[j].SelectNodes("@offset");

                    switch (semanticAtt)
                    {
                    case "VERTEX":
                        vertexTriInd[vertexTriInd.Count - 1] = 0;
                        if (offsetAtt != null && offsetAtt.Count > 0)
                        {
                            vertexTriInd[vertexTriInd.Count - 1] = int.Parse(offsetAtt[0].InnerText);
                            stride++;
                        }
                        break;

                    case "POSITION":
                        vertexID = inputs[j].SelectNodes("@source")[0].InnerText.Remove(0, 1);
                        break;

                    case "NORMAL":
                        normalTriInd[normalTriInd.Count - 1] = 0;
                        if (offsetAtt != null && offsetAtt.Count > 0)
                        {
                            normalTriInd[normalTriInd.Count - 1] = int.Parse(offsetAtt[0].InnerText);
                            stride++;
                        }
                        normalID = inputs[j].SelectNodes("@source")[0].InnerText.Remove(0, 1);
                        break;

                    case "TEXCOORD":
                        uvTriInd[uvTriInd.Count - 1] = 0;
                        if (offsetAtt != null && offsetAtt.Count > 0)
                        {
                            uvTriInd[uvTriInd.Count - 1] = int.Parse(offsetAtt[0].InnerText);
                            stride++;
                        }
                        texcoordID = inputs[j].SelectNodes("@source")[0].InnerText.Remove(0, 1);
                        break;

                    case "COLOR":
                        colorTriInd[colorTriInd.Count - 1] = 0;
                        if (offsetAtt != null && offsetAtt.Count > 0)
                        {
                            colorTriInd[colorTriInd.Count - 1] = int.Parse(offsetAtt[0].InnerText);
                            stride++;
                        }
                        colorID = inputs[j].SelectNodes("@source")[0].InnerText.Remove(0, 1);
                        break;
                    }
                }

                if (vertexID.Length == 0)
                {
                    throw new Exception("Error: Mesh " + i + " does not have vertices. Remove this mesh before to use this tool.");
                }

                string[] vertexArray   = new string[0];
                string[] normalArray   = new string[0];
                string[] texcoordArray = new string[0];
                string[] colorArray    = new string[0];

                if (vertexID.Length > 0)
                {
                    vertexArray = Format(mesh.SelectNodes(@"source[@id='" + vertexID + "']/float_array")[0].InnerText).Split(' ');
                }
                if (normalID.Length > 0)
                {
                    normalArray = Format(mesh.SelectNodes(@"source[@id='" + normalID + "']/float_array")[0].InnerText).Split(' ');
                }
                if (texcoordID.Length > 0)
                {
                    texcoordArray = Format(mesh.SelectNodes(@"source[@id='" + texcoordID + "']/float_array")[0].InnerText).Split(' ');
                }
                if (colorID.Length > 0)
                {
                    colorArray = Format(mesh.SelectNodes(@"source[@id='" + colorID + "']/float_array")[0].InnerText).Split(' ');
                }

                this.Vertices.Add(new List <Vector3>(0));
                this.Normal.Add(new List <Vector3>(0));
                this.TextureCoordinates.Add(new List <Vector2>(0));
                this.VertexColor.Add(new List <byte[]>(0));

                this.Triangle.Add(new List <int[]>(0));

                this.Influences.Add(new List <List <float> >(0));
                this.InfluencesIndices.Add(new List <List <int> >(0));

                for (int j = 0; j < vertexArray.Length; j += 3)
                {
                    float x = Single.Parse(vertexArray[j]);
                    float y = Single.Parse(vertexArray[j + 1]);
                    float z = Single.Parse(vertexArray[j + 2]);

                    if (x > this.MaxCoords.X)
                    {
                        this.MaxCoords.X = x;
                    }
                    if (x < this.MinCoords.X)
                    {
                        this.MinCoords.X = x;
                    }

                    if (y > this.MaxCoords.Y)
                    {
                        this.MaxCoords.Y = y;
                    }
                    if (y < this.MinCoords.Y)
                    {
                        this.MinCoords.Y = y;
                    }

                    if (z > this.MaxCoords.Z)
                    {
                        this.MaxCoords.Z = z;
                    }
                    if (z < this.MinCoords.Z)
                    {
                        this.MinCoords.Z = z;
                    }

                    this.Vertices.Last().Add(new Vector3(x, y, z));
                }

                for (int j = 0; j < normalArray.Length; j += 3)
                {
                    this.Normal.Last().Add(new Vector3(Single.Parse(normalArray[j]), Single.Parse(normalArray[j + 1]), Single.Parse(normalArray[j + 2])));
                }

                for (int j = 0; j < texcoordArray.Length; j += 2)
                {
                    Vector2 uv = new Vector2(Single.Parse(texcoordArray[j]), 1 - Single.Parse(texcoordArray[j + 1]));
                    this.TextureCoordinates.Last().Add(uv);
                }

                for (int j = 0; j < colorArray.Length; j += 4)
                {
                    this.VertexColor.Last().Add(new byte[] {
                        (byte)(Single.Parse(colorArray[j]) * 128),
                        (byte)(Single.Parse(colorArray[j + 1]) * 128),
                        (byte)(Single.Parse(colorArray[j + 2]) * 128),
                        (byte)(Single.Parse(colorArray[j + 3]) * 128)
                    });
                }

                string triangleString = mesh.SelectNodes(@"triangles/p")[0].InnerText;

                string[] triangleArray = Format(triangleString).Split(' ');
                for (int j = 0; j < triangleArray.Length; j += stride)
                {
                    int[] indices = new int[stride];
                    for (int k = 0; k < stride; k++)
                    {
                        indices[k] = int.Parse(triangleArray[j + k]);
                    }

                    this.Triangle.Last().Add(indices);
                }
            }

            var bonesNode = this.Document.SelectNodes(@"//library_visual_scenes/visual_scene//node[@type='JOINT']"); // and not(contains(@name,'mesh'))

            if (bonesNode != null && bonesNode.Count > 0)
            {
                this.Skeleton.Bones           = new Bone[bonesNode.Count];
                this.Skeleton.BonesMatrices   = new Matrix[bonesNode.Count];
                this.Skeleton.BonesReMatrices = new Matrix[bonesNode.Count];

                bonesNode = this.Document.SelectNodes(@"//library_visual_scenes/visual_scene/node[@type='JOINT']");
                for (int i = 0; i < bonesNode.Count; i++)
                {
                    XmlNode bone000 = bonesNode[i];
                    GetBoneNames(bone000);
                    GetBones(bone000);
                    UnwrapSkeleton(bone000, -1);
                }
                ComputeMatrices();
            }

            XmlNodeList skinnings = this.Document.SelectNodes(@"//library_controllers/controller/skin");

            for (int i = 0; i < skinnings.Count; i++)
            {
                var sourceNodes = skinnings[i].SelectNodes("@source");
                if (sourceNodes == null || sourceNodes.Count == 0)
                {
                    continue;
                }
                int geometryIndex = this.GeometryIDs.IndexOf(sourceNodes[0].InnerText.Remove(0, 1));
                if (geometryIndex < 0)
                {
                    continue;
                }

                string   jointID      = skinnings[i].SelectNodes(@"*/input[@semantic='JOINT']/@source")[0].InnerText.Remove(0, 1);
                string[] jointsArray  = Format(skinnings[i].SelectNodes(@"source[@id='" + jointID + "']/Name_array")[0].InnerText).Split(' ');
                string[] skinCounts   = Format(skinnings[i].SelectNodes(@"vertex_weights/vcount")[0].InnerText).Split(' ');
                string[] skins        = Format(skinnings[i].SelectNodes(@"vertex_weights/v")[0].InnerText).Split(' ');
                string[] skinsWeights = Format(skinnings[i].SelectNodes(@"source[contains(@id, 'eights')]/float_array")[0].InnerText).Split(' ');

                int tabIndex = 0;

                for (int j = 0; j < skinCounts.Length; j++)
                {
                    int influenceCount = int.Parse(skinCounts[j]);

                    this.Influences[geometryIndex].Add(new List <float>(0));
                    this.InfluencesIndices[geometryIndex].Add(new List <int>(0));

                    for (int k = 0; k < influenceCount; k++)
                    {
                        int   jointIndex  = int.Parse(skins[tabIndex]);
                        int   weightIndex = int.Parse(skins[tabIndex + 1]);
                        float weight      = Single.Parse(skinsWeights[weightIndex]);

                        string jointName = jointsArray[jointIndex];

                        this.Influences[geometryIndex].Last().Add(weight);
                        this.InfluencesIndices[geometryIndex].Last().Add(BoneNames.IndexOf(jointName));

                        tabIndex += 2;
                    }
                }
            }

            List <string> materialsFnames = new List <string>(0);

            for (int i = 0; i < this.Triangle.Count; i++)
            {
                string currController = "";
                for (int l = 0; l < skinnings.Count; l++)
                {
                    var node = skinnings[l].SelectNodes("@source");
                    if (node != null && node.Count > 0 && node[0].InnerText == "#" + this.GeometryIDs[i])
                    {
                        node = skinnings[l].ParentNode.SelectNodes("@id");
                        if (node != null && node.Count > 0)
                        {
                            currController = node[0].InnerText;
                            break;
                        }
                    }
                }
                string matID = "";
                if (matID.Length == 0)
                {
                    XmlNodeList instanceGeometry = this.Document.SelectNodes("//library_visual_scenes/visual_scene/node/instance_geometry[@url='#" + GeometryIDs[i] + "']//instance_material/@target");
                    if (instanceGeometry != null && instanceGeometry.Count > 0)
                    {
                        matID = instanceGeometry[0].InnerText.Remove(0, 1);
                        if (!materialNames.Contains(matID))
                        {
                            matID = "";
                        }
                    }
                }
                if (matID.Length == 0)
                {
                    XmlNodeList instanceController = this.Document.SelectNodes("//library_visual_scenes/visual_scene/node/instance_controller[@url='#" + currController + "']//instance_material/@target");
                    if (instanceController != null && instanceController.Count > 0)
                    {
                        matID = instanceController[0].InnerText.Remove(0, 1);
                        if (!materialNames.Contains(matID))
                        {
                            matID = "";
                        }
                    }
                }

                if (matID.Length > 0)
                {
                    string fname          = materialFileNames[materialNames.IndexOf(matID)];
                    int    mmaterialIndex = materialsFnames.Count;

                    if (!materialsFnames.Contains(fname))
                    {
                        materialsFnames.Add(fname);
                    }
                    else
                    {
                        mmaterialIndex = materialsFnames.IndexOf(fname);
                    }
                    MaterialIndices.Add(mmaterialIndex);
                }
            }
            this.RenderBuffer = new VertexPositionColorTexture[this.Triangle.Count][];
            for (int i = 0; i < this.RenderBuffer.Length; i++)
            {
                this.RenderBuffer[i] = new VertexPositionColorTexture[this.Triangle[i].Count];
                for (int j = 0; j < this.RenderBuffer[i].Length; j++)
                {
                    this.RenderBuffer[i][j] = new VertexPositionColorTexture();
                }
            }
        }
Esempio n. 9
0
        public void Draw(GraphicsDeviceManager gcm, BasicEffect be, RasterizerState rs)
        {
            //this.SkeletonCache.ValidateSelectionCache(gcm);
            UpdateControls();
            Vector3 position = View.mainCamera.GlobalPosition();
            double  hypo     = 1;
            float   diffX    = 0;
            float   diffY    = 0;
            float   diffZ    = 0;

            VertexPositionColorTexture[] toDraw;
            gcm.GraphicsDevice.DepthStencilState = DepthStencilState.None;

            float diffWithZoom = Vector3.Distance(position, View.focusedObject.Position + View.focusedObject.MiddleCoord) / (View.focusedObject.Zoom * 1.5f);

            if (this.ShowIndices && diffWithZoom < 1)
            {
                if (this.bonesTexture.Length > 0 /*&&MainViewer.form.skeletonIndCheckBox.Enabled && MainViewer.form.skeletonIndCheckBox.Checked &&
                                                  * (!MainViewer.form.swapCheckBox.Checked||objet.MeshCount==0)*/)
                {
                    toDraw = new VertexPositionColorTexture[this.bonesTexture.Length];

                    Vector3 bonePos = Vector3.Zero;
                    for (int j = 0; j < toDraw.Length; j++)
                    {
                        if (j % 24 == 0)
                        {
                            bonePos = Vector3.Transform(Vector3.Zero, this.BonesReMatrices[j / 24]);

                            diffX = position.X - (bonePos.X + objet.Position.X);
                            diffY = position.Y - (bonePos.Y + objet.Position.Y);
                            diffZ = position.Z - (bonePos.Z + objet.Position.Z);
                            hypo  = Math.Pow(diffX * diffX + diffY * diffY + diffZ * diffZ, 0.5f);
                        }

                        toDraw[j] = this.bonesTexture[j];
                        //if (MainViewer.form.rotateIndCheckBox.Enabled && MainViewer.form.rotateIndCheckBox.Checked)
                        toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateFromYawPitchRoll(View.mainCamera.Yaw, View.mainCamera.Pitch, 0));
                        //if (!MainViewer.form.scaleIndCheckBox.Enabled || MainViewer.form.scaleIndCheckBox.Checked)
                        toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateScale((float)hypo / 80f));
                        //else
                        //toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateScale(objet.Zoom / 1000f));

                        toDraw[j].Position += bonePos;
                        toDraw[j].Position += objet.Position;

                        if (j % 24 == 23 && this.selectedBone == j / 24)
                        {
                            for (int k = 6; k < 24; k++)
                            {
                                toDraw[j - 23 + k].Color.R = 255;
                                toDraw[j - 23 + k].Color.G = 0;
                                toDraw[j - 23 + k].Color.B = 0;
                            }
                        }

                        toDraw[j].Color.A = (byte)(255 - diffWithZoom * 255);
                    }

                    if (be != null)
                    {
                        be.TextureEnabled = true;
                        be.Texture        = SrkBinary.GetT2D(View.GetCurrentDirectory() + @"\Content\boneTexture.png");
                        be.CurrentTechnique.Passes[0].Apply();
                    }
                    if (rs != null)
                    {
                        gcm.GraphicsDevice.RasterizerState = rs;
                    }

                    gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, toDraw, 0, toDraw.Length / 3);
                }
            }

            if (this.bonesTextureCircles.Length > 0 /*&&MainViewer.form.skeletonCheckBox.Checked*/)
            {
                toDraw = new VertexPositionColorTexture[this.bonesTextureCircles.Length];
                Vector3 bonePos = Vector3.Zero;
                for (int j = 0; j < toDraw.Length; j++)
                {
                    if (j % 6 == 0)
                    {
                        bonePos = Vector3.Transform(Vector3.Zero, this.BonesReMatrices[j / 6]);

                        diffX = position.X - (bonePos.X + objet.Position.X);
                        diffY = position.Y - (bonePos.Y + objet.Position.Y);
                        diffZ = position.Z - (bonePos.Z + objet.Position.Z);
                        hypo  = Math.Pow(diffX * diffX + diffY * diffY + diffZ * diffZ, 0.5f);
                    }
                    toDraw[j]          = this.bonesTextureCircles[j];
                    toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateFromYawPitchRoll(View.mainCamera.Yaw, View.mainCamera.Pitch, 0));

                    //if (!MainViewer.form.scaleIndCheckBox.Enabled||MainViewer.form.scaleIndCheckBox.Checked)
                    toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateScale(((float)hypo / 60f)));
                    //else
                    //toDraw[j].Position = Vector3.Transform(toDraw[j].Position, Matrix.CreateScale((objet.Zoom / 1000f) / 1));

                    toDraw[j].Position += bonePos;
                    if (this.HighlightedBone && j / 6 == this.HighlightedBoneParentID)
                    {
                        toDraw[j].TextureCoordinate.X += (192f / 382f);
                        toDraw[j].Color = HighligthedColor;
                    }
                    else
                    if (this.Bones[j / 6].Selected)
                    {
                        if (this.Bones[j / 6].ParentID < 0 || !this.Bones[this.Bones[j / 6].ParentID].Selected)
                        {
                            toDraw[j].TextureCoordinate.X += (192f / 382f);
                        }
                        toDraw[j].Color = SelectedColor;
                    }
                    else
                    {
                        toDraw[j].Color = UnselectedColor;
                    }

                    toDraw[j].Position += objet.Position;
                }

                if (be != null)
                {
                    be.TextureEnabled = true;
                    be.Texture        = SrkBinary.GetT2D(View.GetCurrentDirectory() + @"\Content\boneTexture.png");
                    be.CurrentTechnique.Passes[0].Apply();
                }
                if (rs != null)
                {
                    gcm.GraphicsDevice.RasterizerState = rs;
                }

                gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, toDraw, 0, toDraw.Length / 3);

                VertexPositionColorTexture[] toDrawLines = new VertexPositionColorTexture[this.bonesTextureLinesLength];
                if (this.bonesTextureLinesLength > 0)
                {
                    for (int i = 0; i < toDrawLines.Length; i++)
                    {
                        toDrawLines[i]           = this.bonesTextureLines[i];
                        toDrawLines[i].Position += objet.Position;
                    }
                    gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, toDrawLines, 0, toDrawLines.Length / 2);
                }
            }
        }