예제 #1
0
        /// <summary>
        ///     Loads this class with data from a stream.
        /// </summary>
        /// <param name="graphicsDevice">The graphicsDevice device.</param>
        /// <param name="stream">The stream that contains the model data.</param>
        public void Load(GraphicsDevice graphicsDevice, Stream stream)
        {
            this.graphics = graphicsDevice;

            using (var reader = new BinaryReader(stream))
            {
                this.BoundingBox.Min = reader.ReadVector3();
                this.BoundingBox.Max = reader.ReadVector3();

                int numBones = reader.ReadInt32();
                for (int i = 0; i < numBones; i++)
                {
                    byte[] dataBones   = reader.ReadBytes(8);
                    int    boneIndex   = BitConverter.ToInt32(dataBones, 0);
                    int    parentIndex = BitConverter.ToInt32(dataBones, 4);

                    string boneName = reader.ReadString();

                    Matrix matrix;
                    reader.ReadMatrix(out matrix);

                    this.Bones.Add(new Bone(boneIndex, parentIndex, boneName, matrix));
                }

                int numMeshes = reader.ReadInt32();

                for (int i = 0; i < numMeshes; i++)
                {
                    string meshName = reader.ReadString();

                    byte[] dataHeader = reader.ReadBytes(8);

                    int parentBone = BitConverter.ToInt32(dataHeader, 0);
                    int meshParts  = BitConverter.ToInt32(dataHeader, 4);

                    for (int j = 0; j < meshParts; j++)
                    {
                        byte[] dataPart = reader.ReadBytes(24);

                        int vertexOffset      = BitConverter.ToInt32(dataPart, 0);
                        int numVertices       = BitConverter.ToInt32(dataPart, 4);
                        int startIndex        = BitConverter.ToInt32(dataPart, 8);
                        int primitiveCount    = BitConverter.ToInt32(dataPart, 12);
                        int numVertexElements = BitConverter.ToInt32(dataPart, 20);

                        var properties = new VertexElementProperties[numVertexElements];

                        byte[] data = reader.ReadBytes(numVertexElements * 16);
                        for (int k = 0; k < numVertexElements; k++)
                        {
                            VertexElementProperties item = properties[k];

                            item.Offset     = BitConverter.ToInt32(data, 0 + (k * 16));
                            item.Format     = (VertexElementFormat)BitConverter.ToInt32(data, 4 + (k * 16));
                            item.Usage      = (VertexElementUsage)BitConverter.ToInt32(data, 8 + (k * 16));
                            item.UsageIndex = BitConverter.ToInt32(data, 12 + (k * 16));

                            properties[k] = item;
                        }

                        bool      hasCollision      = reader.ReadBoolean();
                        Vector3[] collisionVertices = null;
                        ushort[]  collisionIndices  = null;

                        if (hasCollision)
                        {
                            int collisionVerticesCount = reader.ReadInt32();
                            collisionVertices = new Vector3[collisionVerticesCount];
                            for (int k = 0; k < collisionVerticesCount; k++)
                            {
                                collisionVertices[k] = reader.ReadVector3();
                            }

                            int collisionIndicesCount = reader.ReadInt32();
                            collisionIndices = new ushort[collisionIndicesCount];
                            for (int k = 0; k < collisionIndicesCount; k++)
                            {
                                collisionIndices[k] = reader.ReadUInt16();
                            }
                        }

                        int    bufferSize = reader.ReadInt32();
                        byte[] bufferData = reader.ReadBytes(bufferSize);

                        int indexSize = reader.ReadInt32();
                        var indices   = new ushort[indexSize];

                        byte[] dataIndices = reader.ReadBytes(indexSize * 2);

                        for (int k = 0; k < indexSize; k++)
                        {
                            indices[k] = BitConverter.ToUInt16(dataIndices, k * 2);
                        }

                        var vertexBuffer = new VertexBuffer(new VertexBufferFormat(properties));
                        vertexBuffer.SetData(bufferData, numVertices);
                        var indexBuffer = new IndexBuffer(indices);
                        var mesh        = new StaticMesh(vertexOffset, numVertices, startIndex, primitiveCount, vertexBuffer, indexBuffer, hasCollision, collisionVertices, collisionIndices, PrimitiveType.TriangleList);
                        mesh.Name = meshName;

                        this.Meshes.Add(mesh);
                        this.MeshBonePairs.Add(this.Meshes.Count - 1, parentBone);
                    }
                }
            }

            for (int i = 0; i < this.Bones.Count; i++)
            {
                Bone bone = this.Bones[i];
                if (bone.ParentIndex == -1)
                {
                    this.Bones[i].AbsoluteTransform = bone.LocalTransform;
                }
                else
                {
                    this.Bones[i].AbsoluteTransform = bone.LocalTransform
                                                      * this.Bones[bone.ParentIndex].AbsoluteTransform;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Loads this class with data from a stream.
        /// </summary>
        /// <param name="graphicsDevice">The graphicsDevice device.</param>
        /// <param name="stream">The stream that contains the model data.</param>
        public void Load(GraphicsDevice graphicsDevice, Stream stream)
        {
            this.graphics = graphicsDevice;

            using (var reader = new BinaryReader(stream))
            {
                this.internalindices      = new List <ushort[]>();
                this.BoundingBox.Min      = reader.ReadVector3();
                this.BoundingBox.Max      = reader.ReadVector3();
                this.BoundingBoxBoneIndex = reader.ReadInt32();

                int numMeshes = reader.ReadInt32();
                for (int i = 0; i < numMeshes; i++)
                {
                    string meshName = reader.ReadString();

                    reader.ReadInt32(); // mesh count

                    for (int j = 0; j < 1; j++)
                    {
                        ////bool usesTangent = false;

                        int vertexOffset   = reader.ReadInt32();
                        int numVertices    = reader.ReadInt32();
                        int startIndex     = reader.ReadInt32();
                        int primitiveCount = reader.ReadInt32();

                        reader.ReadInt32(); // vertex stride
                        int numVertexElements = reader.ReadInt32();

                        var properties = new VertexElementProperties[numVertexElements];

                        for (int k = 0; k < numVertexElements; k++)
                        {
                            properties[k].Offset     = reader.ReadInt32();
                            properties[k].Format     = (VertexElementFormat)reader.ReadInt32();
                            properties[k].Usage      = (VertexElementUsage)reader.ReadInt32();
                            properties[k].UsageIndex = reader.ReadInt32();
                        }

                        reader.ReadInt32(); // bufferSize

                        var bufferData = new SkinnedVertex[numVertices];

                        for (int k = 0; k < numVertices; k++)
                        {
                            foreach (VertexElementProperties property in properties)
                            {
                                switch (property.Usage)
                                {
                                case VertexElementUsage.Position:
                                    bufferData[k].Position = reader.ReadVector3();
                                    break;

                                case VertexElementUsage.TextureCoordinate:
                                    bufferData[k].TexCoord = reader.ReadVector2();
                                    break;

                                case VertexElementUsage.Normal:
                                    bufferData[k].Normal = reader.ReadVector3();
                                    break;

                                case VertexElementUsage.BlendIndices:
                                    bufferData[k].BlendIndices = reader.ReadByte4();
                                    break;

                                case VertexElementUsage.BlendWeight:
                                    bufferData[k].BlendWeights = reader.ReadVector2();
                                    break;

                                case VertexElementUsage.Tangent:
                                    bufferData[k].Tangent = reader.ReadVector3();
                                    break;

                                case VertexElementUsage.Binormal:
                                    bufferData[k].Binormal = reader.ReadVector3();
                                    break;
                                }
                            }
                        }

                        int indexSize = reader.ReadInt32();
                        var indices   = new ushort[indexSize];
                        for (int k = 0; k < indexSize; k++)
                        {
                            indices[k] = reader.ReadUInt16();
                        }

                        this.internalindices.Add(indices);

                        var newSkinnedBuffer = new SkinnedVertexBuffer(SkinnedVertex.VertexFormat);
                        newSkinnedBuffer.SetData(bufferData, numVertices);
                        var mesh = new SkinnedMesh(
                            vertexOffset,
                            numVertices,
                            startIndex,
                            primitiveCount,
                            newSkinnedBuffer,
                            new IndexBuffer(indices),
                            PrimitiveType.TriangleList);
                        mesh.Name = meshName;

                        this.Meshes.Add(mesh);
                    }
                }
            }
        }