Exemplo n.º 1
0
        public Mesh[] LoadMesh()
        {
            Mesh[] mesh = new Mesh[DAEGeometry.Count];


            for (int i = 0; i < DAEGeometry.Count; i++)
            {
                var verts = new VertexRigged3D[DAEGeometry[i].Indeces.Length];

                int offset = 0;
                foreach (var mat in DAEGeometry[i].MaterialIndexTable)
                {
                    mat.Offset = offset;
                    for (int n = 0; n < mat.Count; n++)
                    {
                        var indx = mat.Indexes;
                        var mul  = indx.Length / mat.Count;
                        verts[offset] = new VertexRigged3D(DAEGeometry[i].Positions[indx[n * mul + mat.OffsetPosition]],
                                                           DAEGeometry[i].Normals[indx[n * mul + mat.OffsetNormal]], DAEGeometry[i].UVs[indx[n * mul + mat.OffsetUV]],
                                                           new IVector4(new int[4] {
                            0, 0, 0, 0
                        }), Vector4.Zero);
                        DAEGeometry[i].Indeces[offset] = offset++;
                    }
                }
                mesh[i] = new Mesh(verts, DAEGeometry[i].Indeces);
            }
            return(mesh);
        }
Exemplo n.º 2
0
        void ReadMesh(Reader reader)
        {
            int vertexCount = reader.ReadInt32();
            var vertices    = new VertexRigged3D[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                var vertex = new VertexRigged3D();
                vertex.Position          = reader.readVector3() * multipler;
                vertex.Position.Z        = -vertex.Position.Z;
                vertex.Normal            = reader.readVector3();
                vertex.UV                = reader.readVector2();
                vertex.BoneIndices.bone1 = reader.ReadInt16();
                vertex.BoneIndices.bone2 = reader.ReadInt16();

                var weight = reader.ReadByte();
                if (weight > 100)
                {
                    weight = 100;
                }
                vertex.BoneWeigths[0] = (float)weight / 100;
                vertex.BoneWeigths[1] = 1 - vertex.BoneWeigths[0];
                //non edge flag
                reader.ReadByte();
                vertices[i] = vertex;
            }

            int faceCount = reader.ReadInt32();
            var indexes   = new int[faceCount];

            for (int i = 0; i < faceCount; i++)
            {
                indexes[i] = reader.ReadUInt16();
            }

            meshRigged = new Mesh(vertices, indexes);
        }
Exemplo n.º 3
0
        void ReadMesh(Reader reader)
        {
            int meshSize = reader.ReadInt32();

            VertexRigged3D [] verticesR = new VertexRigged3D[meshSize];
            //Vertex3D[] vertices = new Vertex3D[meshSize];
            for (int i = 0; i < meshSize; i++)
            {
                Vector3 pos          = reader.readVector3() * multipler;
                Vector3 normal       = reader.readVector3().Normalized();
                Vector2 uv           = reader.readVector2();
                int[]   bonesIndexes = { 0, 0, 0, 0 };
                Vector4 bonesWeigth  = new Vector4(0f);

                //skipping appendix
                if (header.GetAppendixUV != 0)
                {
                    for (int n = 0; n < header.GetAppendixUV; n++)
                    {
                        reader.readVector4();
                    }
                }

                //bones
                byte Weigth = reader.ReadByte();
                switch (Weigth)
                {
                case 0:                         //BDEF
                    bonesIndexes[0] = reader.readVal(header.GetBoneIndexSize);
                    bonesWeigth[0]  = 1f;
                    break;

                case 1:                         //BDEF2

                    bonesIndexes[0] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[1] = reader.readVal(header.GetBoneIndexSize);
                    bonesWeigth[0]  = reader.ReadSingle();
                    bonesWeigth[1]  = 1f - bonesWeigth[0];
                    break;

                case 2:                         //BDEF4
                    bonesIndexes[0] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[1] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[2] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[3] = reader.readVal(header.GetBoneIndexSize);
                    bonesWeigth[0]  = reader.ReadSingle();
                    bonesWeigth[1]  = reader.ReadSingle();
                    bonesWeigth[2]  = reader.ReadSingle();
                    bonesWeigth[3]  = reader.ReadSingle();
                    break;

                case 3:                         //SDEF
                    bonesIndexes[0] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[1] = reader.readVal(header.GetBoneIndexSize);
                    bonesWeigth[0]  = reader.ReadSingle();
                    bonesWeigth[1]  = 1f - bonesWeigth[0];
                    reader.readVector3();
                    reader.readVector3();
                    reader.readVector3();
                    break;

                case 4:                         //QDEF
                    bonesIndexes[0] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[1] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[2] = reader.readVal(header.GetBoneIndexSize);
                    bonesIndexes[3] = reader.readVal(header.GetBoneIndexSize);
                    bonesWeigth[0]  = reader.ReadSingle();
                    bonesWeigth[1]  = reader.ReadSingle();
                    bonesWeigth[2]  = reader.ReadSingle();
                    bonesWeigth[3]  = reader.ReadSingle();
                    break;

                default:
                    throw new Exception("Not suppornet weigth code " + Weigth);
                }
                //Convert left to right coordinates
                pos.Z    = -pos.Z;
                normal.Z = -normal.Z;

                verticesR[i] = new VertexRigged3D(pos, normal, uv, new IVector4(bonesIndexes), bonesWeigth);
                //vertices[i] = new Vertex3D(pos, normal, uv);
                float outline = reader.ReadSingle();
            }
            int indexSize = reader.ReadInt32();

            int[] indexes = new int[indexSize];
            for (int i = 0; i < indexSize; i++)
            {
                //indexes[i] = reader.readVal(header.GetVertexIndexSize);
                //invert triangles to convert left to right coordinates
                int res = i % 3;
                if (res == 0)
                {
                    indexes[i + 1] = reader.readVal(header.GetVertexIndexSize);
                }
                else if (res == 1)
                {
                    indexes[i - 1] = reader.readVal(header.GetVertexIndexSize);
                }
                else
                {
                    indexes[i] = reader.readVal(header.GetVertexIndexSize);
                }
            }
            //mesh = new Mesh(vertices, indexes);

            /*
             * var ofst =  3 * 51424;
             * Console.WriteLine("{0} {1} {2}", indexes[ofst], indexes[ofst + 1], indexes[ofst+ 2]);
             * Console.WriteLine(verticesR[indexes[ofst]].Position);
             * Console.WriteLine(verticesR[indexes[ofst+1]].Position);
             * Console.WriteLine(verticesR[indexes[ofst+2]].Position);
             */
            meshRigged = new Mesh(verticesR, indexes);
        }
Exemplo n.º 4
0
        MeshDrawer ReadMeshChunk(int streamOffset)
        {
            //mesh name
            reader.BaseStream.Position = streamOffset + 0x7;
            int chunkSize   = reader.ReadByte();
            int meshNamePos = (int)reader.BaseStream.Position + reader.ReadInt32();

            reader.BaseStream.Position = meshNamePos;
            string meshName = reader.readString();

            //material reference
            reader.BaseStream.Position = streamOffset + 0x14;
            int matNamePos = (int)reader.BaseStream.Position + reader.ReadInt32();

            reader.BaseStream.Position = matNamePos + 0x8;
            string matName = reader.readString();

            //bones reference
            reader.BaseStream.Position = streamOffset + 0x58;
            int weightBoneNameTableStart = (int)reader.BaseStream.Position + reader.ReadInt32();

            reader.BaseStream.Position = streamOffset + 0x5c;
            int WeightBoneTableStart = (int)reader.BaseStream.Position + reader.ReadInt32();

            reader.BaseStream.Position = streamOffset + 0x78;
            int facesCount = reader.ReadInt32();

            reader.BaseStream.Position = streamOffset + 0x84;
            int  verticesCount = reader.ReadInt32();
            byte size          = 4;
            int  SizeTest      = verticesCount * chunkSize;

            if (SizeTest < 0x100)
            {
                size = 1;
            }
            else if (SizeTest < 0x10000)
            {
                size = 2;
            }

            reader.BaseStream.Position += 8;
            int vertSize   = reader.readVal(size);
            int VertOffset = (int)reader.BaseStream.Position;

            List <VertexRigged3D> vertices = new List <VertexRigged3D>();

            for (int i = 0; i < verticesCount; i++)
            {
                VertexRigged3D vertice = new VertexRigged3D();
                vertice.Position = reader.readVector3();
                //vertex color unsupported
                reader.BaseStream.Position += 4;
                //if (chunkSize == 0x24 || chunkSize == 0x28)
                if (chunkSize == 0x24)
                {
                    reader.BaseStream.Position += 4;
                }
                else if (chunkSize == 0x30)
                {
                    reader.BaseStream.Position += 4;
                    reader.BaseStream.Position += 0xc;
                }
                vertice.UV   = new Vector2();
                vertice.UV.X = Half.FromBytes(reader.ReadBytes(2), 0);
                vertice.UV.Y = Half.FromBytes(reader.ReadBytes(2), 0);

                /*
                 * if (chunkSize == 0x28)
                 *  file.BaseStream.Position += 4;
                 */
                vertice.BoneIndices = new IVector4(new int[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte() });
                if (chunkSize == 0x28)
                {
                    vertice.BoneWeigths = reader.readVector4();
                }
                else
                {
                    vertice.BoneWeigths  = new Vector4(reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16());
                    vertice.BoneWeigths /= 65535f;
                }
                vertices.Add(vertice);
            }

            int unknownSize = 4;

            if (size == 1)
            {
                unknownSize = 2;
            }

            reader.BaseStream.Position = VertOffset + vertSize + size + unknownSize;
            int unknownCount = reader.ReadInt32();

            reader.BaseStream.Position += 0x10 * unknownCount;
            reader.ReadInt32();
            //Read Faces
            size = 4;
            if (facesCount < 0x100)
            {
                size = 1;
            }
            else if (facesCount < 0x10000)
            {
                size = 2;
            }

            int  faceSize       = reader.readVal(size);
            byte indexValueSize = 4;

            if (verticesCount < 0x100)
            {
                indexValueSize = 1;
            }
            else if (verticesCount < 0x10000)
            {
                indexValueSize = 2;
            }

            int faceOffset = (int)reader.BaseStream.Position;

            int[] indexes = new int[facesCount];
            for (int i = 0; i < facesCount; i++)
            {
                indexes[i] = reader.readVal(indexValueSize);
            }

            //Read Weigth Bones dictionary
            reader.BaseStream.Position = weightBoneNameTableStart;
            int weightBoneCount = reader.ReadInt32();

            int[] boneIdDict = new int[weightBoneCount];
            for (int i = 0; i < weightBoneCount; i++)
            {
                reader.BaseStream.Position  = weightBoneNameTableStart + i * 4 + 4;
                reader.BaseStream.Position += reader.ReadInt32();
                string weightBoneName = reader.readString();
                boneIdDict[i] = Array.FindIndex(bones, (b) => b.Name == weightBoneName);
            }

            var vv = vertices.ToArray();

            //set bone id to global
            for (int i = 0; i < vv.Length; i++)
            {
                IVector4 boneIndexes = vv[i].BoneIndices;
                boneIndexes.bone1 = boneIdDict[boneIndexes.bone1];
                boneIndexes.bone2 = boneIdDict[boneIndexes.bone2];
                boneIndexes.bone3 = boneIdDict[boneIndexes.bone3];
                boneIndexes.bone4 = boneIdDict[boneIndexes.bone4];
                vv[i].BoneIndices = boneIndexes;
            }

            CalculateVertNormals.CalculateNormals(vv, indexes);
            var      mesh = new Mesh(vv, indexes);
            Material mat;

            if (materialTable.TryGetValue(matName, out mat))
            {
                return(new MeshDrawerRigged(mesh, new Material[] { mat }, boneControll));
            }
            else
            {
                return(new MeshDrawerRigged(mesh, boneControll));
            }
        }