private static Model.ModelInfo GetModelInfo(Imd imd) { return(new Model.ModelInfo { //BoxTest boxX = imd.Body.BoxTest.Position.X, boxY = imd.Body.BoxTest.Position.Y, boxZ = imd.Body.BoxTest.Position.Z, boxW = imd.Body.BoxTest.Size.X, boxH = imd.Body.BoxTest.Size.Y, boxD = imd.Body.BoxTest.Size.Z, boxPosScale = 1 << imd.Body.BoxTest.PosScale, boxInvPosScale = 1f / (1 << imd.Body.BoxTest.PosScale), //ModelInfo firstUnusedMtxStackID = 1, //todo: Use real value posScale = 1 << imd.Body.ModelInfo.PosScale, invPosScale = 1f / (1 << imd.Body.ModelInfo.PosScale), numMat = imd.Body.ModelInfo.MaterialCount, numNode = imd.Body.ModelInfo.NodeCount, numShp = (byte)imd.Body.PolygonArray.Size, numPolygon = (ushort)imd.Body.OutputInfo.PolygonSize, numQuad = (ushort)imd.Body.OutputInfo.QuadSize, numTriangle = (ushort)imd.Body.OutputInfo.TriangleSize, numVertex = (ushort)imd.Body.OutputInfo.VertexSize, sbcType = 0, //todo: Use real value scalingRule = 0, //todo: Use real value texMtxMode = 0 //todo: Use real value }); }
private static Dictionary <TEX0.DictTexData> GetDictTexData(Imd imd) { var dict = new Dictionary <TEX0.DictTexData>(); foreach (var tex in imd.Body.TexImageArray.TexImages) { var imgFormat = Textures.ImageFormat.NONE; switch (tex.Format) { case "tex4x4": imgFormat = Textures.ImageFormat.COMP4x4; break; case "palette4": imgFormat = Textures.ImageFormat.PLTT4; break; case "palette16": imgFormat = Textures.ImageFormat.PLTT16; break; case "palette256": imgFormat = Textures.ImageFormat.PLTT256; break; case "direct": imgFormat = Textures.ImageFormat.DIRECT; break; case "a3i5": imgFormat = Textures.ImageFormat.A3I5; break; case "a5i3": imgFormat = Textures.ImageFormat.A5I3; break; } var data = new TEX0.DictTexData { Data = tex.Bitmap.Bytes, Data4x4 = tex.Tex4x4PaletteIndex?.Bytes, Fmt = imgFormat, S = (ushort)tex.Width, T = (ushort)tex.Height, TransparentColor = tex.Color0Mode != null && tex.Color0Mode == "transparency" }; dict.Add(tex.Name.Length > 16 ? tex.Name.Substring(0, 16) : tex.Name, data); } return(dict); }
private static NodeData[] GetNodeData(Imd imd) { return(new[] { new NodeData { //TODO: Matrix flag = 0xf807 //? } }); }
public static TEX0 GetTextures(Imd imd) { var tex = new TEX0 { TexInfo = new TEX0.texInfo(), Tex4x4Info = new TEX0.tex4x4Info(), PlttInfo = new TEX0.plttInfo(), dictTex = GetDictTexData(imd), dictPltt = GetDictPlttData(imd) }; return(tex); }
public static Model GetModel(Imd imd) { var model = new Model { evpMatrices = null, //todo: Use real value info = GetModelInfo(imd), nodes = GetNodeSet(imd), materials = GetMaterialSet(imd), shapes = GetShapeSet(imd), sbc = GetSbc(imd) }; return(model); }
private static Dictionary <TEX0.DictPlttData> GetDictPlttData(Imd imd) { var dict = new Dictionary <TEX0.DictPlttData>(); foreach (var plt in imd.Body.TexPaletteArray.TexPalettes) { var data = new TEX0.DictPlttData { Data = plt.Bytes }; dict.Add(plt.Name.Length > 16 ? plt.Name.Substring(0, 16) : plt.Name, data); } return(dict); }
private static NodeSet GetNodeSet(Imd imd) { var nodes = new NodeSet { dict = new Dictionary <NodeSetData>() }; foreach (var node in imd.Body.NodeArray.Nodes) { nodes.dict.Add(node.Name, GetNodeSetData(node)); } nodes.data = GetNodeData(imd); return(nodes); }
public NsbmdConverter(string path) { _imd = Imd.Read(path); if (!ValidateImd(_imd)) { throw new InvalidDataException("IMD file has errors inside."); } _imdName = Path.GetFileNameWithoutExtension(path); _nsbmd = new NSBMD(false); GetModelSet(); GetTextureSet(); //File.WriteAllBytes("testfiles/test.nsbmd",_nsbmd.Write()); }
private static bool ValidateImd(Imd imd) { var valid = true; //Textures //Check texture names clashes var texes = new List <string>(); foreach (var tex in imd.Body.TexImageArray.TexImages) { var name = tex.Name.Length > 16 ? tex.Name.Substring(0, 16) : tex.Name; if (!texes.Contains(name)) { texes.Add(name); } else { Console.Write($"Error: There are multiple textures with the same name '{name}'"); valid = false; } } //Palettes //Check palette names clashes var pals = new List <string>(); foreach (var pal in imd.Body.TexPaletteArray.TexPalettes) { var name = pal.Name.Length > 16 ? pal.Name.Substring(0, 16) : pal.Name; if (!pals.Contains(name)) { pals.Add(name); } else { Console.Write($"Error: There are multiple palettes with the same name '{name}'"); valid = false; } } return(valid); }
private static byte[] GetSbc(Imd imd) { var w = new Sbc.Sbc(); w.CommandList.Add(new SbcNodeDesc(1, 0, 0, 0)); w.CommandList.Add(new SbcNop()); w.CommandList.Add(new SbcNode(0, true)); w.CommandList.Add(new SbcPosScale(0)); foreach (var display in imd.Body.NodeArray.Nodes[0].Displays) { w.CommandList.Add(new SbcMat(0, (byte)display.Material)); w.CommandList.Add(new SbcShape((byte)display.Polygon)); } w.CommandList.Add(new SbcPosScale(1)); w.CommandList.Add(new SbcRet()); w.CommandList.Add(new SbcNop()); return(w.Write()); }
private static ShapeSet GetShapeSet(Imd imd) { var shapes = new ShapeSet { dict = new Dictionary <ShapeSetData>(), shape = new Shape[imd.Body.PolygonArray.Polygons.Count] }; int idx = 0; foreach (var polygon in imd.Body.PolygonArray.Polygons) { shapes.dict.Add(polygon.Name, new ShapeSetData()); Shape.NNS_G3D_SHPFLAG flag = 0; if (polygon.TexFlag == "on") { flag = Shape.NNS_G3D_SHPFLAG.NNS_G3D_SHPFLAG_USE_TEXCOORD; } if (polygon.ClrFlag == "on") { flag |= Shape.NNS_G3D_SHPFLAG.NNS_G3D_SHPFLAG_USE_COLOR; } if (polygon.NrmFlag == "on") { flag |= Shape.NNS_G3D_SHPFLAG.NNS_G3D_SHPFLAG_USE_NORMAL; } //TODO shapes.shape[idx++] = new Shape { flag = flag, DL = G3dDisplayList.Encode(polygon.MatrixPrimitives[0].PrimitiveArray.GetDisplayList()) }; } return(shapes); }
public static MaterialSet GetMaterialSet(Imd imd) { var matSet = new MaterialSet { dict = new Dictionary <MaterialSetData>(), dictPlttToMatList = new Dictionary <PlttToMatData>(), dictTexToMatList = new Dictionary <TexToMatData>(), materials = new MaterialSet.Material[imd.Body.MaterialArray.Materials.Count] }; byte matIdx = 0; foreach (var mat in imd.Body.MaterialArray.Materials) { ushort texH = 0, texW = 0; if (mat.TexImageIdx != -1) { var texName = imd.Body.TexImageArray.TexImages[mat.TexImageIdx].Name; var palName = imd.Body.TexImageArray.TexImages[mat.TexImageIdx].PaletteName; texH = (ushort)imd.Body.TexImageArray.TexImages[mat.TexImageIdx].OriginalHeight; texW = (ushort)imd.Body.TexImageArray.TexImages[mat.TexImageIdx].OriginalWidth; if (!matSet.dictTexToMatList.Contains(texName)) { matSet.dictTexToMatList.Add(texName, new TexToMatData { NrMat = 1, Materials = new [] { matIdx } }); } else { var entry = matSet.dictTexToMatList[texName]; entry.NrMat++; var newMats = new byte[entry.NrMat]; entry.Materials.CopyTo(newMats, 0); entry.Materials = newMats; } if (!matSet.dictPlttToMatList.Contains(palName)) { matSet.dictPlttToMatList.Add(palName, new PlttToMatData { NrMat = 1, Materials = new[] { matIdx } }); } else { var entry = matSet.dictPlttToMatList[palName]; entry.NrMat++; var newMats = new byte[entry.NrMat]; entry.Materials.CopyTo(newMats, 0); entry.Materials = newMats; } } matSet.dict.Add(mat.Name, new MaterialSetData()); matSet.materials[matIdx] = new MaterialSet.Material(); var curMat = matSet.materials[matIdx]; curMat.SetAmbient(mat.GetAmbient()); curMat.SetDiffuse(mat.GetDiffuse()); curMat.SetEmission(mat.GetEmission()); curMat.SetSpecular(mat.GetSpecular()); curMat.SetAlpha(mat.Alpha); curMat.flag = TEXMTX_TRANSZERO | TEXMTX_ROTZERO | TEXMTX_SCALEONE | DIFFUSE | VTXCOLOR | SHININESS | AMBIENT | EMISSION | SPECULAR; if (mat.TexImageIdx != -1) { curMat.origHeight = texH; curMat.origWidth = texW; } //Todo: use real value curMat.magH = 1; curMat.magW = 1; matIdx++; } return(matSet); }