public void AddSubset(Subset subset) { if (subset.TotalArea > Area) { return; } var duplicate = Subsets.FirstOrDefault(s => s.Items.SequenceEqual(subset.Items, new ItemDimensionalComparer())); if (duplicate != null) { if (duplicate.TotalValue >= subset.TotalValue) { return; } Subsets.Remove(duplicate); Subsets.Add(subset); } else { Subsets.Add(subset); } }
protected override void InitFromMeshData(Device device, GeometryGenerator.MeshData mesh) { var subset = new MeshGeometry.Subset { FaceCount = mesh.Indices.Count / 3, FaceStart = 0, VertexCount = mesh.Vertices.Count, VertexStart = 0 }; Subsets.Add(subset); var max = new Vector3(float.MinValue); var min = new Vector3(float.MaxValue); foreach (var vertex in mesh.Vertices) { max = Vector3.Maximize(max, vertex.Position); min = Vector3.Minimize(min, vertex.Position); } BoundingBox = new BoundingBox(min, max); Vertices.AddRange(mesh.Vertices.Select(v => new PosNormalTexTan(v.Position, v.Normal, v.TexC, v.TangentU)).ToList()); Indices.AddRange(mesh.Indices.Select(i => (short)i)); Materials.Add(new Material { Ambient = Color.Gray, Diffuse = Color.White, Specular = new Color4(16, 1, 1, 1) }); DiffuseMapSRV.Add(null); NormalMapSRV.Add(null); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
/*----------------------------------------------------------------------------------------- * * Computational and optimization stuff : * * -----------------------------------------------------------------------------------------*/ /// <summary> /// Defragmentates mesh subsets with same materials /// </summary> public void DefragmentSubsets(Scene scene, bool takeFromTriangleMtrlIndices) { // if there are not shading groups, // take them from per triangle material indices if (!Subsets.Any() || takeFromTriangleMtrlIndices) { for (int i = 0; i < Triangles.Count; i++) { MeshSubset sg = new MeshSubset(); sg.MaterialIndex = Triangles[i].MaterialIndex; sg.StartPrimitive = i; sg.PrimitiveCount = 1; Subsets.Add(sg); //Console.Write( "*{0}", Triangles[i].MaterialIndex ); } } if (Subsets.Count == 1) { return; } List <List <MeshTriangle> > perMtrlTris = new List <List <MeshTriangle> >(); foreach (var mtrl in scene.Materials) { perMtrlTris.Add(new List <MeshTriangle>()); } foreach (var sg in Subsets) { for (int i = sg.StartPrimitive; i < sg.StartPrimitive + sg.PrimitiveCount; i++) { perMtrlTris[sg.MaterialIndex].Add(Triangles[i]); } } Subsets.Clear(); Triangles.Clear(); for (int i = 0; i < perMtrlTris.Count; i++) { var sg = new MeshSubset(); sg.MaterialIndex = i; sg.StartPrimitive = Triangles.Count; sg.PrimitiveCount = perMtrlTris[i].Count; if (sg.PrimitiveCount == 0) { continue; } Triangles.AddRange(perMtrlTris[i]); Subsets.Add(sg); } }
private Subset CreateSubset(List <State> statesList) { foreach (Subset item in Subsets) { if (!item.States.Except(statesList).Any()) { findState = true; return(item); } } Subset subset = new Subset("S" + indexState, statesList.OrderBy(or => or.StateName).ToList()); Subsets.Add(subset); indexState++; findState = false; return(subset); }
public ModelSection(CacheBase Cache, int Address) { EndianReader Reader = Cache.Reader; Reader.SeekTo(Address); #region Submesh Block int iCount = Reader.ReadInt32(); int iOffset = Reader.ReadInt32() - Cache.Magic; for (int i = 0; i < iCount; i++) { Submeshes.Add(new Submesh(Cache, iOffset + 24 * i)); } #endregion #region Subset Block Reader.SeekTo(Address + 12); iCount = Reader.ReadInt32(); iOffset = Reader.ReadInt32() - Cache.Magic; for (int i = 0; i < iCount; i++) { Subsets.Add(new Subset(Cache, iOffset + 16 * i)); } #endregion #region Other Reader.SeekTo(Address + 24); VertsIndex = Reader.ReadInt16(); Reader.ReadInt32(); UnknownIndex = Reader.ReadInt16(); Reader.SeekTo(Address + 40); FacesIndex = Reader.ReadInt16(); Reader.SeekTo(Address + 45); TransparentNodesPerVertex = Reader.ReadByte(); NodeIndex = Reader.ReadByte(); VertexFormat = Reader.ReadByte(); OpaqueNodesPerVertex = Reader.ReadByte(); #endregion }
public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipUV = false) { var importer = new AssimpImporter(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported! Cannot load {1}", "filename"); } #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUV) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { var verts = new List <PosNormalTexTan>(); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.GetTextureCoords(0)[i] : new Vector3D(); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public override bool Deserialize(object data) { if (data is BinaryReader) { BinaryReader br = data as BinaryReader; int vc = br.ReadInt32(); for (int i = 0; i < vc; ++i) { CVertex v = new CVertex(); if (v.Deserialize(br) == true) { Vertices.Add(v); } } int ic = br.ReadInt32(); for (int i = 0; i < ic; ++i) { int index = br.ReadInt32(); Indices.Add(index); } int sc = br.ReadInt32(); for (int i = 0; i < sc; ++i) { CSubset subset = new CSubset(); if (subset.Deserialize(br) == true) { Subsets.Add(subset); } } } else if (data is JObject) { JObject jmesh = data as JObject; foreach (JObject jv in jmesh["Vertices"]) { CVertex v = new CVertex(); v.Deserialize(jv); Vertices.Add(v); } foreach (int index in jmesh["Indices"]) { Indices.Add(index); } foreach (JObject js in jmesh["Subsets"]) { CSubset subset = new CSubset(); subset.Deserialize(js); Subsets.Add(subset); } } else { return(false); } return(true); }
private BasicModel(Device device, TextureManager11 textureManager, string filename, string texturePath, bool autoLoadTextures, bool flipUv, bool tex1By1) { var importer = new AssimpContext(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException($"Model format {Path.GetExtension(filename)} is not supported. Cannot load {filename}.", nameof(filename)); } #if DEBUG var logStream = new ConsoleLogStream(); logStream.Attach(); #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUv) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); _meshCount = model.Meshes.Count; foreach (var mesh in model.Meshes) { var verts = new List <VertPosNormTexTan>(); var subset = new MeshSubset() { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = MathF.Minimize(min, pos); max = MathF.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : (tex1By1 ? new Vector3D(1, 1, 0) : new Vector3D()); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new VertPosNormTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => i + subset.VertexStart).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); if (autoLoadTextures) { TextureSlot diffuseSlot; mat.GetMaterialTexture(TextureType.Diffuse, 0, out diffuseSlot); var diffusePath = diffuseSlot.FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } var fullDiffusePath = diffusePath == null ? null : Path.Combine(texturePath, diffusePath); if (File.Exists(fullDiffusePath)) { DiffuseMapSRV.Add(textureManager.CreateTexture(fullDiffusePath)); } else { DiffuseMapSRV.Add(textureManager.CreateColor1By1(material.Diffuse.ToColor())); } TextureSlot normalSlot; mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot); var normalPath = normalSlot.FilePath; var fullNormalPath = normalPath == null ? null : Path.Combine(texturePath, normalPath); string textureName; if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { if (File.Exists(fullDiffusePath)) { var normalExt = Path.GetExtension(fullDiffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; fullNormalPath = Path.Combine(texturePath, normalPath); if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { textureName = TextureManager11.TexDefaultNorm; } } else { textureName = TextureManager11.TexDefaultNorm; } } NormalMapSRV.Add(textureManager.CreateTexture(textureName)); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false) { var importer = new AssimpImporter(); #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace); // Load animation data Animator = new SceneAnimator(); Animator.Init(model); // create our vertex-to-boneweights lookup var vertToBoneWeight = new Dictionary <uint, List <VertexWeight> >(); // create bounding box extents _min = new Vector3(float.MaxValue); _max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { ExtractBoneWeightsFromMesh(mesh, vertToBoneWeight); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); var verts = ExtractVertices(mesh, vertToBoneWeight, flipTexY); Vertices.AddRange(verts); // extract indices and shift them to the proper offset into the combined vertex buffer var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); // extract materials var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); // extract material textures var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { // for models created without a normal map baked, we'll check for a texture with the same // filename as the diffure texture, and _nmap suffixed // this lets us add our own normal maps easily var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; if (File.Exists(Path.Combine(texturePath, normalPath))) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } } BoundingBox = new BoundingBox(_min, _max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }