public void BuildModel(int modelIndex) { _srModel = _srFile.Models[modelIndex]; String modelName = _objectName + "-" + modelIndex.ToString(); #region Materials ProgressStage = "Model " + modelIndex.ToString() + " - Creating Materials"; Thread.Sleep(500); for (int materialIndex = 0; materialIndex < _srModel.MaterialCount; materialIndex++) { Material material = new Material(); Color colorDiffuse = Color.FromArgb((int)unchecked (_srModel.Materials[materialIndex].colour)); material.Diffuse = colorDiffuse; material.TextureFileName = GetTextureName(_srModel, materialIndex); Materials.Add(material); progressLevel += _srModel.IndexCount / _srModel.Groups.Length; } #endregion #region Groups for (int groupIndex = 0; groupIndex < _srModel.Groups.Length; groupIndex++) { ProgressStage = "Model " + modelIndex.ToString() + " - Creating Group " + groupIndex.ToString(); Thread.Sleep(100); Tree srGroup = _srModel.Groups[groupIndex]; String groupName = String.Format("{0}-{1}-group-{2}", _objectName, modelIndex, groupIndex); if (srGroup != null && srGroup.mesh != null && srGroup.mesh.indexCount > 0 && srGroup.mesh.polygonCount > 0) { Node group = new Node(); SRMeshParser meshParser = new SRMeshParser(_objectName, _srFile); meshParser.BuildMesh(modelIndex, groupIndex, 0); foreach (SubMesh subMesh in meshParser.SubMeshes) { // If the mesh parser knew the total submeshes for the model, // then this could be done inside BuildMesh. subMesh.MeshIndex = Meshes.Count; group.SubMeshIndices.Add(SubMeshes.Count); SubMeshes.Add(subMesh); } Meshes.Add(meshParser.Mesh); group.Name = groupName; Groups.Add(group); } } #endregion ModelName = modelName; if (_srFile.Asset == CDC.Asset.Unit) { Model = new Unit(this); } else { Model = new Physical(this); } }
public void BuildMesh(RenderResource resource, int modelIndex, int groupIndex, int meshIndex) { _srModel = _srFile.Models[modelIndex]; _srGroup = _srModel.Groups[groupIndex]; String modelName = String.Format("{0}-{1}", _objectName, modelIndex); String groupName = String.Format("{0}-{1}-group-{2}", _objectName, modelIndex, groupIndex); String meshName = String.Format("{0}-{1}-group-{2}-mesh-{3}", _objectName, modelIndex, groupIndex, meshIndex); int startIndexLocation = 0; for (int materialIndex = 0; materialIndex < _srModel.MaterialCount; materialIndex++) { int indexCount = 0; int totalIndexCount = (int)_srGroup.mesh.indexCount; for (int v = 0; v < totalIndexCount; v++) { if (_srGroup.mesh.polygons[v / 3].material.ID == materialIndex) { _vertexList.Add(v); _indexList.Add(_indexList.Count - startIndexLocation); indexCount++; } } if (indexCount > 0) { String subMeshName = String.Format("{0}-{1}-group-{2}-submesh-{3}", _objectName, modelIndex, groupIndex, materialIndex); SubMesh subMesh = new SubMesh { Name = subMeshName, MaterialIndex = materialIndex, indexCount = indexCount, startIndexLocation = startIndexLocation, baseVertexLocation = startIndexLocation }; SubMeshes.Add(subMesh); startIndexLocation += indexCount; } } if (SubMeshes.Count > 0) { MeshName = meshName; Technique = "DefaultRender"; if (_srFile.Asset == CDC.Asset.Unit) { //Mesh = new MeshPCT(this); Mesh = new MeshMorphingUnit(resource, this); } else { Mesh = new MeshPNT(resource, this); } } }
public void BuildModel(RenderResource resource, int modelIndex, CDC.Objects.ExportOptions options) { _srModel = _srFile.Models[modelIndex]; String modelName = _objectName + "-" + modelIndex.ToString(); #region Materials for (int materialIndex = 0; materialIndex < _srModel.MaterialCount; materialIndex++) { Material material = new Material(); material.Visible = _srModel.Materials[materialIndex].visible; // Breaks early SR1 builds. //material.BlendMode = _srModel.Materials[materialIndex].blendMode; //int sortPush = unchecked((sbyte)_srModel.Materials[materialIndex].sortPush); //sortPush = 128 - sortPush; //material.DepthBias = (1.0f / 100000.0f) * sortPush; // Maybe use a hack for warpgates WARPGATE_DrawWarpGateRim indicates tree 3 should have lower priority. Color colorDiffuse = Color.FromArgb((int)unchecked (_srModel.Materials[materialIndex].colour)); material.Diffuse = colorDiffuse; material.TextureFileName = CDC.Objects.Models.SRModel.GetTextureName(_srModel, materialIndex, options); Materials.Add(material); } #endregion #region Groups for (int groupIndex = 0; groupIndex < _srModel.Groups.Length; groupIndex++) { Tree srGroup = _srModel.Groups[groupIndex]; String groupName = String.Format("{0}-{1}-group-{2}", _objectName, modelIndex, groupIndex); if (srGroup != null && srGroup.mesh != null && srGroup.mesh.indexCount > 0 && srGroup.mesh.polygonCount > 0) { ModelNode group = new ModelNode(); SRMeshParser meshParser = new SRMeshParser(_objectName, _srFile); meshParser.BuildMesh(resource, modelIndex, groupIndex, 0); foreach (SubMesh subMesh in meshParser.SubMeshes) { // If the mesh parser knew the total submeshes for the model, // then this could be done inside BuildMesh. subMesh.MeshIndex = Meshes.Count; group.SubMeshIndices.Add(SubMeshes.Count); SubMeshes.Add(subMesh); } Meshes.Add(meshParser.Mesh); group.Name = groupName; Groups.Add(group); } } #endregion ModelName = modelName; Model = new Model(this); }
public void LoadTextures(string fileName, CDC.Objects.ExportOptions options) { FileShaderResourceViewDictionary.Clear(); Type currentFileType = File.GetType(); if (currentFileType == typeof(TRLFile)) { try { TRLPCTextureFile textureFile = new TRLPCTextureFile(fileName); SceneCDC.progressLevel = 0; SceneCDC.progressLevels = textureFile.TextureCount; SceneCDC.ProgressStage = "Loading Textures"; for (int t = 0; t < textureFile.TextureCount; t++) { String textureName = SRModel.GetPS2TextureName(File.Name, (int)textureFile.TextureDefinitions[t].ID) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetDataAsStream(t); //textureFile.ExportFile(t, "C:\\Users\\A\\Desktop\\Lara\\" + textureName); if (stream != null) { if (textureFile.TextureDefinitions[t].Format == BenLincoln.TheLostWorlds.CDTextures.DRMFormat.Uncompressed) { MemoryStream stream2 = textureFile.GetUncompressedDataAsStream2(t); AddTexture(stream2, textureName); } else { AddTexture(stream, textureName); } _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(t)); } SceneCDC.progressLevel++; } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } else if (currentFileType == typeof(SR2File) || currentFileType == typeof(DefianceFile)) { try { SR2PCTextureFile textureFile = new SR2PCTextureFile(fileName); SceneCDC.progressLevel = 0; SceneCDC.progressLevels = textureFile.TextureCount; SceneCDC.ProgressStage = "Loading Textures"; for (int t = 0; t < textureFile.TextureCount; t++) { String textureName = SRModel.GetPS2TextureName(File.Name, textureFile.TextureDefinitions[t].Flags1) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetDataAsStream(t); //textureFile.ExportFile(t, "C:\\Users\\A\\Desktop\\" + textureName); if (stream != null) { AddTexture(stream, textureName); _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(t)); } SceneCDC.progressLevel++; } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } else if (currentFileType == typeof(SR1File)) { if (File.Platform == CDC.Platform.PC) { try { SR1PCTextureFile textureFile = new SR1PCTextureFile(fileName); SceneCDC.progressLevel = 0; SceneCDC.progressLevels = File.GetNumMaterials(); SceneCDC.ProgressStage = "Loading Textures"; foreach (SRModel srModel in File.Models) { foreach (CDC.Material material in srModel.Materials) { if (material.textureUsed) { System.IO.MemoryStream stream = textureFile.GetDataAsStream(material.textureID); if (stream != null) { String textureName = SRModel.GetSoulReaverPCOrDreamcastTextureName(srModel.Name, material.textureID) + TextureExtension; AddTexture(stream, textureName); if (!_TexturesAsPNGs.ContainsKey(textureName)) { _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(material.textureID)); } } } SceneCDC.progressLevel++; } } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } else if (File.Platform == CDC.Platform.Dreamcast) { try { SR1DCTextureFile textureFile = new SR1DCTextureFile(fileName); SceneCDC.progressLevel = 0; SceneCDC.progressLevels = File.GetNumMaterials(); SceneCDC.ProgressStage = "Loading Textures"; foreach (SRModel srModel in File.Models) { foreach (CDC.Material material in srModel.Materials) { if (material.textureUsed) { int textureID = material.textureID; System.IO.MemoryStream stream = textureFile.GetDataAsStream(textureID); if (stream != null) { String textureName = SRModel.GetSoulReaverPCOrDreamcastTextureName(srModel.Name, material.textureID) + TextureExtension; AddTexture(stream, textureName); if (!_TexturesAsPNGs.ContainsKey(textureName)) { _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(material.textureID)); } } } SceneCDC.progressLevel++; } } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } else { try { SR1PSTextureFile textureFile = new SR1PSTextureFile(fileName); SceneCDC.progressLevel = 0; SceneCDC.progressLevels = 100; // Arbitrarily large number. SceneCDC.ProgressStage = "Loading Textures"; // Trick it into showing a progress bar. System.Threading.Thread.Sleep(100); SceneCDC.progressLevel = 100; UInt32 polygonCountAllModels = 0; foreach (SRModel srModel in File.Models) { polygonCountAllModels += srModel.PolygonCount; } SR1PSTextureFile.SoulReaverPlaystationPolygonTextureData[] polygons = new SR1PSTextureFile.SoulReaverPlaystationPolygonTextureData[polygonCountAllModels]; int polygonNum = 0; foreach (SRModel srModel in File.Models) { foreach (CDC.Polygon polygon in srModel.Polygons) { polygons[polygonNum].paletteColumn = polygon.paletteColumn; polygons[polygonNum].paletteRow = polygon.paletteRow; polygons[polygonNum].u = new int[3]; polygons[polygonNum].v = new int[3]; //polygons[polygonNum].materialColour = polygon.material.colour; polygons[polygonNum].materialColour = polygon.colour; polygons[polygonNum].u[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].u * 255); polygons[polygonNum].v[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].v * 255); polygons[polygonNum].u[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].u * 255); polygons[polygonNum].v[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].v * 255); polygons[polygonNum].u[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].u * 255); polygons[polygonNum].v[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].v * 255); polygons[polygonNum].textureID = polygon.material.textureID; polygons[polygonNum].CLUT = polygon.material.clutValue; polygons[polygonNum].textureUsed = polygon.material.textureUsed; polygons[polygonNum].visible = polygon.material.visible; //polygons[polygonNum].materialColour = polygon.material.colour; polygonNum++; } } bool drawGreyscaleFirst = false; bool quantizeBounds = true; textureFile.BuildTexturesFromPolygonData(polygons, drawGreyscaleFirst, quantizeBounds, options); // For all models for (int t = 0; t < textureFile.TextureCount; t++) { String textureName = SRModel.GetPlayStationTextureNameDefault(File.Name, t) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetDataAsStream(t); if (stream != null) { AddTexture(stream, textureName); } //string exportedTextureFileName = Path.ChangeExtension(textureName, "png"); //_TexturesAsPNGs.Add(exportedTextureFileName, textureFile.GetTextureAsBitmap(t)); Bitmap b = textureFile.GetTextureAsBitmap(t); // this is a hack that's being done here for now because I don't know for sure which of the flags/attributes controls // textures that should be alpha-masked. Alpha-masking EVERY texture is expensive. if (options.AlsoInferAlphaMaskingFromTexturePixels) { if (BitmapHasTransparentPixels(b)) { for (int modelNum = 0; modelNum < File.Models.Length; modelNum++) { if (t < File.Models[modelNum].Materials.Length) { File.Models[modelNum].Materials[t].UseAlphaMask = true; } } } } _TexturesAsPNGs.Add(textureName, b); // dump all textures as PNGs for debugging //Bitmap exportedTexture = textureFile.GetTextureAsBitmap(t); //string exportedTextureFileName = Path.ChangeExtension(textureName, "png"); //exportedTexture.Save(exportedTextureFileName, ImageFormat.Png); //texNum = 0; //foreach (Bitmap tex in _Textures) //{ // tex.Save(@"C:\Debug\Tex-" + texNum + ".png", ImageFormat.Png); // texNum++; //} SceneCDC.progressLevel++; } // for models that use index/CLUT textures, if the user has enabled this option if (options.UseEachUniqueTextureCLUTVariation) { foreach (int textureID in textureFile.TexturesByCLUT.Keys) { Dictionary <ushort, Bitmap> textureCLUTCollection = textureFile.TexturesByCLUT[textureID]; foreach (ushort clut in textureCLUTCollection.Keys) { String textureName = SRModel.GetPlayStationTextureNameWithCLUT(File.Name, textureID, clut) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetTextureWithCLUTAsStream(textureID, clut); if (stream != null) { AddTexture(stream, textureName); } Bitmap b = textureFile.TexturesByCLUT[textureID][clut]; _TexturesAsPNGs.Add(textureName, b); SceneCDC.progressLevel++; } } } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } } else { try { Gex3PSTextureFile textureFile = new Gex3PSTextureFile(fileName); UInt32 polygonCountAllModels = 0; foreach (SRModel srModel in File.Models) { polygonCountAllModels += srModel.PolygonCount; } Gex3PSTextureFile.Gex3PlaystationPolygonTextureData[] polygons = new Gex3PSTextureFile.Gex3PlaystationPolygonTextureData[polygonCountAllModels]; int polygonNum = 0; foreach (SRModel srModel in File.Models) { foreach (CDC.Polygon polygon in srModel.Polygons) { polygons[polygonNum].paletteColumn = polygon.paletteColumn; polygons[polygonNum].paletteRow = polygon.paletteRow; polygons[polygonNum].u = new int[3]; polygons[polygonNum].v = new int[3]; //polygons[polygonNum].materialColour = polygon.material.colour; polygons[polygonNum].materialColour = polygon.colour; polygons[polygonNum].u[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].u * 255); polygons[polygonNum].v[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].v * 255); polygons[polygonNum].u[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].u * 255); polygons[polygonNum].v[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].v * 255); polygons[polygonNum].u[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].u * 255); polygons[polygonNum].v[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].v * 255); polygons[polygonNum].textureID = polygon.material.textureID; polygons[polygonNum].CLUT = polygon.material.clutValue; polygons[polygonNum].textureUsed = polygon.material.textureUsed; polygons[polygonNum].visible = polygon.material.visible; //polygons[polygonNum].materialColour = polygon.material.colour; polygonNum++; } } bool drawGreyscaleFirst = false; bool quantizeBounds = true; textureFile.BuildTexturesFromPolygonData(polygons, ((GexFile)File).TPages, drawGreyscaleFirst, quantizeBounds, options); // For all models for (int t = 0; t < textureFile.TextureCount; t++) { String textureName = SRModel.GetPlayStationTextureNameDefault(File.Name, t) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetDataAsStream(t); if (stream != null) { AddTexture(stream, textureName); } //string exportedTextureFileName = Path.ChangeExtension(textureName, "png"); //_TexturesAsPNGs.Add(exportedTextureFileName, textureFile.GetTextureAsBitmap(t)); Bitmap b = textureFile.GetTextureAsBitmap(t); // this is a hack that's being done here for now because I don't know for sure which of the flags/attributes controls // textures that should be alpha-masked. Alpha-masking EVERY texture is expensive. if (options.AlsoInferAlphaMaskingFromTexturePixels) { if (BitmapHasTransparentPixels(b)) { for (int modelNum = 0; modelNum < File.Models.Length; modelNum++) { if (t < File.Models[modelNum].Materials.Length) { File.Models[modelNum].Materials[t].UseAlphaMask = true; } } } } _TexturesAsPNGs.Add(textureName, b); // dump all textures as PNGs for debugging //Bitmap exportedTexture = textureFile.GetTextureAsBitmap(t); //string exportedTextureFileName = Path.ChangeExtension(textureName, "png"); //exportedTexture.Save(exportedTextureFileName, ImageFormat.Png); //texNum = 0; //foreach (Bitmap tex in _Textures) //{ // tex.Save(@"C:\Debug\Tex-" + texNum + ".png", ImageFormat.Png); // texNum++; //} } // for models that use index/CLUT textures, if the user has enabled this option if (options.UseEachUniqueTextureCLUTVariation) { foreach (int textureID in textureFile.TexturesByCLUT.Keys) { Dictionary <ushort, Bitmap> textureCLUTCollection = textureFile.TexturesByCLUT[textureID]; foreach (ushort clut in textureCLUTCollection.Keys) { String textureName = SRModel.GetPlayStationTextureNameWithCLUT(File.Name, textureID, clut) + TextureExtension; System.IO.MemoryStream stream = textureFile.GetTextureWithCLUTAsStream(textureID, clut); if (stream != null) { AddTexture(stream, textureName); } Bitmap b = textureFile.TexturesByCLUT[textureID][clut]; _TexturesAsPNGs.Add(textureName, b); } } } } catch (FileNotFoundException) { Console.WriteLine("Error: couldn't find a texture file"); } catch (Exception ex) { Console.Write(ex.ToString()); } } }