Exemple #1
0
        public static BasicModel LoadSdkMesh(Device device, TextureManager11 texMgr, string filename, string texturePath)
        {
            // NOTE: this assumes that the model file only contains a single mesh
            var sdkMesh = new SdkMesh(filename);
            var ret     = new BasicModel();

            var faceStart   = 0;
            var vertexStart = 0;

            foreach (var sdkMeshSubset in sdkMesh.Subsets)
            {
                var subset = new MeshSubset()
                {
                    FaceCount   = (int)(sdkMeshSubset.IndexCount / 3),
                    FaceStart   = faceStart,
                    VertexCount = (int)sdkMeshSubset.VertexCount,
                    VertexStart = vertexStart
                };
                // fixup any subset indices that assume that all vertices and indices are not in the same buffers
                faceStart   = subset.FaceStart + subset.FaceCount;
                vertexStart = subset.VertexStart + subset.VertexCount;
                ret.Subsets.Add(subset);
            }
            ret._meshCount = ret.SubsetCount;

            var max = new Vector3(float.MinValue);
            var min = new Vector3(float.MaxValue);

            foreach (var vb in sdkMesh.VertexBuffers)
            {
                foreach (var vertex in vb.Vertices)
                {
                    max = MathF.Maximize(max, vertex.Pos);
                    min = MathF.Minimize(min, vertex.Pos);
                    ret.Vertices.Add(vertex);
                }
            }
            ret.BoundingBox = new BoundingBox(min, max);

            foreach (var ib in sdkMesh.IndexBuffers)
            {
                ret.Indices.AddRange(ib.Indices.Select(i => i));
            }
            foreach (var sdkMeshMaterial in sdkMesh.Materials)
            {
                var material = new Noire.Common.Material {
                    Ambient  = sdkMeshMaterial.Ambient,
                    Diffuse  = sdkMeshMaterial.Diffuse,
                    Reflect  = Color.Black,
                    Specular = sdkMeshMaterial.Specular
                };
                material.Specular.Alpha = sdkMeshMaterial.Power;
                ret.Materials.Add(material);
                if (!string.IsNullOrEmpty(sdkMeshMaterial.DiffuseTexture))
                {
                    ret.DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, sdkMeshMaterial.DiffuseTexture)));
                }
                else
                {
                    ret.DiffuseMapSRV.Add(texMgr[TextureManager11.TexDefault]);
                }
                if (!string.IsNullOrEmpty(sdkMeshMaterial.NormalTexture))
                {
                    ret.NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, sdkMeshMaterial.NormalTexture)));
                }
                else
                {
                    ret.NormalMapSRV.Add(texMgr[TextureManager11.TexDefaultNorm]);
                }
            }
            ret.ModelMesh.SetSubsetTable(ret.Subsets);
            ret.ModelMesh.SetVertices(device, ret.Vertices);
            ret.ModelMesh.SetIndices(device, ret.Indices);

            return(ret);
        }
Exemple #2
0
 public void SetMaterial(Material m) {
     // 这里如果内存释放不当非常容易引发内存泄漏!
     using (var dataStream = DataStream.Create(NoireUtilities.StructureToBytes(m), false, false)) {
         _mat.SetRawValue(dataStream, Material.Stride);
     }
 }