internal static SdkMeshRawMaterial Read(BinaryReader reader)
        {
            var material = new SdkMeshRawMaterial
            {
                Name = Encoding.UTF8.GetString(reader.ReadBytes(MaxMaterialName).TakeWhile(t => t != 0).ToArray()),
                MaterialInstancePath = Encoding.UTF8.GetString(reader.ReadBytes(MaxMaterialPath).TakeWhile(t => t != 0).ToArray()),
                DiffuseTexture       = Encoding.UTF8.GetString(reader.ReadBytes(MaxTextureName).TakeWhile(t => t != 0).ToArray()),
                NormalTexture        = Encoding.UTF8.GetString(reader.ReadBytes(MaxTextureName).TakeWhile(t => t != 0).ToArray()),
                SpecularTexture      = Encoding.UTF8.GetString(reader.ReadBytes(MaxTextureName).TakeWhile(t => t != 0).ToArray()),
                Diffuse                   = new XMFloat4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                Ambient                   = new XMFloat4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                Specular                  = new XMFloat4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                Emissive                  = new XMFloat4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()),
                Power                     = reader.ReadSingle(),
                DiffuseTextureOffset      = reader.ReadInt64(),
                NormalTextureOffset       = reader.ReadInt64(),
                SpecularTextureOffset     = reader.ReadInt64(),
                DiffuseTextureViewOffset  = reader.ReadInt64(),
                NormalTextureViewOffset   = reader.ReadInt64(),
                SpecularTextureViewOffset = reader.ReadInt64()
            };

            return(material);
        }
        public static SdkMeshRawFile FromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var file = new SdkMeshRawFile();

            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                file.Header = SdkMeshRawHeader.Read(reader);

                if (file.Header.Version != FileVersion)
                {
                    throw new InvalidDataException();
                }

                if (stream.Position != file.Header.VertexStreamHeadersOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.VertexStreamHeadersOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumVertexBuffers; i++)
                {
                    SdkMeshRawVertexBufferHeader header = SdkMeshRawVertexBufferHeader.Read(reader);
                    file.VertexBufferHeaders.Add(header);
                }

                if (stream.Position != file.Header.IndexStreamHeadersOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.IndexStreamHeadersOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumIndexBuffers; i++)
                {
                    SdkMeshRawIndexBufferHeader header = SdkMeshRawIndexBufferHeader.Read(reader);
                    file.IndexBufferHeaders.Add(header);
                }

                if (stream.Position != file.Header.MeshDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.MeshDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumMeshes; i++)
                {
                    SdkMeshRawMesh mesh = SdkMeshRawMesh.Read(reader);
                    file.Meshes.Add(mesh);
                }

                if (stream.Position != file.Header.SubsetDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.SubsetDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumTotalSubsets; i++)
                {
                    SdkMeshRawSubset subset = SdkMeshRawSubset.Read(reader);
                    file.Subsets.Add(subset);
                }

                if (stream.Position != file.Header.FrameDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.FrameDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumFrames; i++)
                {
                    SdkMeshRawFrame frame = SdkMeshRawFrame.Read(reader);
                    file.Frames.Add(frame);
                }

                if (stream.Position != file.Header.MaterialDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.MaterialDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumMaterials; i++)
                {
                    SdkMeshRawMaterial material = SdkMeshRawMaterial.Read(reader);
                    file.Materials.Add(material);
                }

                foreach (SdkMeshRawMesh mesh in file.Meshes)
                {
                    if (stream.Position != mesh.SubsetOffset)
                    {
                        throw new InvalidDataException();
                    }

                    stream.Seek(mesh.SubsetOffset, SeekOrigin.Begin);
                    for (int i = 0; i < mesh.NumSubsets; i++)
                    {
                        int index = reader.ReadInt32();
                        mesh.SubsetsIndices.Add(index);
                    }

                    if (stream.Position != mesh.FrameInfluenceOffset)
                    {
                        throw new InvalidDataException();
                    }

                    stream.Seek(mesh.FrameInfluenceOffset, SeekOrigin.Begin);
                    for (int i = 0; i < mesh.NumFrameInfluences; i++)
                    {
                        int index = reader.ReadInt32();
                        mesh.FrameInfluencesIndices.Add(index);
                    }
                }

                if (stream.Position != file.Header.HeaderSize + file.Header.NonBufferDataSize)
                {
                    throw new InvalidDataException();
                }

                foreach (SdkMeshRawVertexBufferHeader buffer in file.VertexBufferHeaders)
                {
                    stream.Seek(buffer.DataOffset, SeekOrigin.Begin);

                    byte[] bytes = reader.ReadBytes((int)buffer.SizeBytes);
                    file.VertexBufferBytes.Add(bytes);
                }

                foreach (SdkMeshRawIndexBufferHeader buffer in file.IndexBufferHeaders)
                {
                    stream.Seek(buffer.DataOffset, SeekOrigin.Begin);

                    byte[] bytes = reader.ReadBytes((int)buffer.SizeBytes);
                    file.IndexBufferBytes.Add(bytes);
                }
            }

            return(file);
        }
Esempio n. 3
0
        internal SdkMeshMaterial(D3D11Device device, D3D11DeviceContext deviceContext, string directory, SdkMeshRawMaterial rawMaterial)
        {
            this.Name = rawMaterial.Name;

            if (!string.IsNullOrEmpty(rawMaterial.DiffuseTexture))
            {
                this.DiffuseTextureName = Path.GetFileName(rawMaterial.DiffuseTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.DiffuseTexture)), out D3D11ShaderResourceView textureView);
                this.DiffuseTextureView = textureView;
            }

            if (!string.IsNullOrEmpty(rawMaterial.NormalTexture))
            {
                this.NormalTextureName = Path.GetFileName(rawMaterial.NormalTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.NormalTexture)), out D3D11ShaderResourceView textureView);
                this.NormalTextureView = textureView;
            }

            if (!string.IsNullOrEmpty(rawMaterial.SpecularTexture))
            {
                this.SpecularTextureName = Path.GetFileName(rawMaterial.SpecularTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.SpecularTexture)), out D3D11ShaderResourceView textureView);
                this.SpecularTextureView = textureView;
            }
        }