/// <summary> /// /// </summary> /// <param name="tobj"></param> /// <param name="bmp"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(Bitmap bmp, HSD_TOBJ tobj, GXTexFmt imgFormat, GXTlutFmt palFormat) { if (imgFormat == GXTexFmt.CI8) // doesn't work well with alpha { bmp = BitmapTools.ReduceColors(bmp, 256); } if (imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) { bmp = BitmapTools.ReduceColors(bmp, 16); } var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var length = bitmapData.Stride * bitmapData.Height; byte[] bytes = new byte[length]; Marshal.Copy(bitmapData.Scan0, bytes, 0, length); bmp.UnlockBits(bitmapData); tobj.EncodeImageData(bytes, bmp.Width, bmp.Height, imgFormat, palFormat); // dispose if we use our color reduced bitmap //if (imgFormat == GXTexFmt.CI8 || imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) // bmp.Dispose(); }
/// <summary> /// /// </summary> public void PreLoadTexture(HSD_TOBJ tobj) { if (!imageBufferTextureIndex.ContainsKey(tobj.ImageData.ImageData)) { var rawImageData = tobj.ImageData.ImageData; var width = tobj.ImageData.Width; var height = tobj.ImageData.Height; List <byte[]> mips = new List <byte[]>(); if (tobj.LOD != null && tobj.ImageData.MaxLOD != 0) { for (int i = 0; i < tobj.ImageData.MaxLOD - 1; i++) { mips.Add(tobj.GetDecodedImageData(i)); } } else { mips.Add(tobj.GetDecodedImageData()); } var index = TextureManager.Add(mips, width, height); imageBufferTextureIndex.Add(rawImageData, index); } }
private void exportFramesToolStripMenuItem_Click(object sender, EventArgs e) { if (TexAnim == null) { return; } var f = Tools.FileIO.SaveFile(ApplicationSettings.ImageFileFilter); if (f != null) { for (int i = 0; i < TexAnim.ImageCount; i++) { var v = TexAnim.ImageBuffers.Array[i]; HSD_TlutBuffer pal = null; if (TexAnim.TlutBuffers != null) { pal = TexAnim.TlutBuffers.Array[i]; } HSD_TOBJ tobj = new HSD_TOBJ(); tobj.ImageData = v.Data; if (pal != null) { tobj.TlutData = pal.Data; } var frame = Tools.BitmapTools.BGRAToBitmap(tobj.GetDecodedImageData(), v.Data.Width, v.Data.Height); frame.Save(Path.GetDirectoryName(f) + "\\" + Path.GetFileNameWithoutExtension(f) + "_" + i.ToString() + Path.GetExtension(f)); frame.Dispose(); } } }
/// <summary> /// Import TOBJ from PNG file /// </summary> /// <returns></returns> public static HSD_TOBJ ImportTOBJFromFile() { var TOBJ = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; var f = Tools.FileIO.OpenFile("PNG (.png)|*.png"); if (f != null) { using (TextureImportDialog settings = new TextureImportDialog()) { if (settings.ShowDialog() == DialogResult.OK) { InjectBitmap(TOBJ, f, settings.TextureFormat, settings.PaletteFormat); return(TOBJ); } } } return(null); }
private void importStripToolStripMenuItem1_Click(object sender, EventArgs e) { if (TEXG == null) { return; } var f = Tools.FileIO.OpenFile(ApplicationSettings.ImageFileFilter); if (f != null) { using (TextureImportDialog td = new TextureImportDialog()) { td.ShowCount = true; if (td.ShowDialog() == DialogResult.OK) { var bmp = new Bitmap(f); HSD_TOBJ[] images = new HSD_TOBJ[td.ImageCount]; for (int i = 0; i < td.ImageCount; i++) { images[i] = new HSD_TOBJ(); var image = bmp.Clone(new Rectangle(bmp.Width / td.ImageCount * i, 0, bmp.Width / td.ImageCount, bmp.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb); TOBJConverter.InjectBitmap(image, images[i], td.TextureFormat, td.PaletteFormat); image.Dispose(); } bmp.Dispose(); TEXG.SetFromTOBJs(images); Node = Node; } } } }
/// <summary> /// Injects <see cref="Bitmap"/> into <see cref="HSD_TOBJ"/> /// </summary> /// <param name="tobj"></param> /// <param name="img"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(HSD_TOBJ tobj, string filepath, GXTexFmt imgFormat, GXTlutFmt palFormat) { Bitmap bmp = new Bitmap(filepath); InjectBitmap(tobj, bmp, imgFormat, palFormat); bmp.Dispose(); }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Bitmap TOBJToBitmap(HSD_TOBJ tobj) { if (tobj == null || tobj.ImageData == null) { return(null); } var data = tobj.GetDecodedImageData(); var width = tobj.ImageData.Width; var height = tobj.ImageData.Height; if (width == 0) { width = 1; } if (height == 0) { height = 1; } Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); try { BitmapData bmpData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); Marshal.Copy(data, 0, bmpData.Scan0, data.Length); bmp.UnlockBits(bmpData); } catch { bmp.Dispose(); throw; } return(bmp); }
/// <summary> /// /// </summary> /// <param name="tobj"></param> /// <returns></returns> public static bool IsTransparent(HSD_TOBJ tobj) { if (tobj.ImageData.Format == GXTexFmt.RGB565) { return(false); } if ((tobj.ImageData.Format == GXTexFmt.CI8 && tobj.TlutData.Format == GXTlutFmt.RGB565) || (tobj.ImageData.Format == GXTexFmt.CI4 && tobj.TlutData.Format == GXTlutFmt.RGB565)) { return(false); } var d = tobj.GetDecodedImageData(); for (int i = 0; i < d.Length; i += 4) { if (d[i + 3] != 255) { return(true); } } return(false); }
public override void ExportSceneToFile(string FileName) { // update new single binds // update textures // Optimize Texture Sharing Dictionary <byte[], HSD_Image> imageDataToBuffer = new Dictionary <byte[], HSD_Image>(new ByteArrayComparer()); foreach (var tobj in tobjToSurface) { var t = new HSD_TOBJ(); t._s = tobj.Key; if (!imageDataToBuffer.ContainsKey(t.ImageData.ImageData)) { imageDataToBuffer.Add(t.ImageData.ImageData, t.ImageData); } t.ImageData = imageDataToBuffer[t.ImageData.ImageData]; } // Save to file HSDFile.Save(FileName); }
/// <summary> /// /// </summary> /// <param name="tobj"></param> /// <param name="bmp"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(Bitmap bmp, HSD_TOBJ tobj, GXTexFmt imgFormat, GXTlutFmt palFormat) { if (imgFormat != GXTexFmt.CMP) { // if (imgFormat == GXTexFmt.CI8) // doesn't work well with alpha // bmp = ReduceColors(bmp, 256); //if (imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) // bmp = BitmapTools.ReduceColors(bmp, 16); var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var length = bitmapData.Stride * bitmapData.Height; byte[] bytes = new byte[length]; Marshal.Copy(bitmapData.Scan0, bytes, 0, length); bmp.UnlockBits(bitmapData); tobj.EncodeImageData(bytes, bmp.Width, bmp.Height, imgFormat, palFormat); // dispose if we use our color reduced bitmap if (imgFormat == GXTexFmt.CI8 || imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) { bmp.Dispose(); } } else { MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png); byte[] bytes = stream.ToArray(); stream.Close(); stream.Dispose(); IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length); using (var origImage = TexHelper.Instance.LoadFromWICMemory(unmanagedPointer, bytes.Length, WIC_FLAGS.NONE)) { var scratch = origImage.Compress(0, DXGI_FORMAT.BC1_UNORM, TEX_COMPRESS_FLAGS.DEFAULT, 1); var ptr = scratch.GetPixels(); var length = scratch.GetPixelsSize(); byte[] data = new byte[length]; Marshal.Copy(ptr, data, 0, (int)length); scratch.Dispose(); tobj.EncodeImageData(data, bmp.Width, bmp.Height, GXTexFmt.CMP, GXTlutFmt.IA8); } // Call unmanaged code Marshal.FreeHGlobal(unmanagedPointer); } }
/// <summary> /// /// </summary> public static void ExportTOBJToFile(this HSD_TOBJ tobj) { var f = FileIO.SaveFile(ApplicationSettings.ImageFileFilter); if (f != null) { using (var bmp = ToBitmap(tobj)) bmp.Save(f); } }
/*private void RemakeVertexData() * { * var dobjs = GetDOBJS(); * GX_VertexCompressor c = new GX_VertexCompressor(); * foreach(var dobj in dobjs) * { * Console.WriteLine(dobj.Mobj.RenderFlags.ToString()); * dobj.Mobj.RenderFlags = RENDER_MODE.ALPHA_COMPAT | RENDER_MODE.DIFFSE_MAT; * dobj.Mobj.Textures = null; * if (dobj.Mobj.Textures != null) * foreach (var tobj in dobj.Mobj.Textures.List) * { * tobj.Flags = 0; * tobj.ImageData = null; * tobj.TlutData = null; * } * foreach (var pobj in dobj.Pobj.List) * { * int off = 0; * var displayList = pobj.ToDisplayList(); * var vertices = GX_VertexAttributeAccessor.GetDecodedVertices(displayList, pobj); * GX_DisplayList newdl = new GX_DisplayList(); * foreach (var dl in displayList.Primitives) * { * var vs = new List<GX_Vertex>(); * for(int i = 0; i < dl.Count; i++) * { * vs.Add(vertices[off+i]); * } * off += dl.Count; * //newdl.Primitives.Add(c.Compress(dl.PrimitiveType, vs.ToArray(), pobj.Attributes)); * } * pobj.FromDisplayList(newdl); * } * } * c.SaveChanges(); * }*/ #region Properties public Texture TOBJtoRenderTexture(HSD_TOBJ tobj) { if (tobjToSurface.ContainsKey(tobj._s)) { return(tobjToSurface[tobj._s].GetRenderTexture()); } else { return(DefaultTextures.Instance.defaultBlack); } }
/// <summary> /// Converts <see cref="HSD_TOBJ"/> into <see cref="Bitmap"/> /// </summary> /// <param name="tobj"></param> /// <returns></returns> public static Bitmap ToBitmap(HSD_TOBJ tobj) { if (tobj.ImageData == null) { return(null); } var rgba = tobj.GetDecodedImageData(); return(BitmapTools.BGRAToBitmap(rgba, tobj.ImageData.Width, tobj.ImageData.Height)); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="tobj"></param> /// <returns></returns> public static string FormatName(string name, HSD_TOBJ tobj) { if (tobj.ImageData != null) { name += "_" + tobj.ImageData.Format.ToString(); } if (tobj.TlutData != null) { name += "_" + tobj.TlutData.Format.ToString(); } return(name); }
/// <summary> /// Injects <see cref="Bitmap"/> into <see cref="HSD_TOBJ"/> /// </summary> /// <param name="tobj"></param> /// <param name="img"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(string filepath, HSD_TOBJ tobj, GXTexFmt imgFormat, GXTlutFmt palFormat) { // format override if (FormatFromString(filepath, out GXTexFmt fmt, out GXTlutFmt pal)) { palFormat = pal; imgFormat = fmt; } using (Bitmap bmp = LoadBitmapFromFile(filepath)) { InjectBitmap(bmp, tobj, imgFormat, palFormat); } }
/// <summary> /// /// </summary> /// <param name="tobj"></param> /// <param name="bmp"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(Bitmap bmp, HSD_TOBJ tobj, GXTexFmt imgFormat, GXTlutFmt palFormat) { // todo: this only works without alpha :/ /*if (imgFormat == GXTexFmt.CI8) // doesn't work well with alpha * bmp = BitmapTools.ReduceColors(bmp, 256); * if (imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) * bmp = BitmapTools.ReduceColors(bmp, 16);*/ tobj.EncodeImageData(bmp.GetBGRAData(), bmp.Width, bmp.Height, imgFormat, palFormat); // dispose if we use our color reduced bitmap //if (imgFormat == GXTexFmt.CI8 || imgFormat == GXTexFmt.CI4 || imgFormat == GXTexFmt.CI14X2) // bmp.Dispose(); }
/// <summary> /// Import TOBJ from PNG file /// </summary> /// <returns></returns> public static HSD_TOBJ ImportTOBJFromFile() { try { var TOBJ = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, Flags = TOBJ_FLAGS.COORD_UV | TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COLORMAP_MODULATE | TOBJ_FLAGS.ALPHAMAP_MODULATE, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; var f = Tools.FileIO.OpenFile(ApplicationSettings.ImageFileFilter); if (f != null) { using (TextureImportDialog settings = new TextureImportDialog()) { if (FormatFromString(f, out GXTexFmt fmt, out GXTlutFmt pal)) { settings.PaletteFormat = pal; settings.TextureFormat = fmt; } if (settings.ShowDialog() == DialogResult.OK) { using (Bitmap bmp = new Bitmap(f)) { settings.ApplySettings(bmp); InjectBitmap(bmp, TOBJ, settings.TextureFormat, settings.PaletteFormat); return(TOBJ); } } } } return(null); } catch (Exception e) { return(null); } }
/// <summary> /// Import TOBJ from PNG file /// </summary> /// <returns></returns> public static HSD_TOBJ ImportTOBJFromFile(string filePath, GXTexFmt imgFmt, GXTlutFmt tlutFmt) { var TOBJ = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; InjectBitmap(TOBJ, filePath, imgFmt, tlutFmt); return(TOBJ); }
/// <summary> /// /// </summary> /// <returns></returns> public static HSD_TOBJ ToTOBJ(this Bitmap bmp, GXTexFmt texFmt, GXTlutFmt palFmt) { var tobj = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, Flags = TOBJ_FLAGS.COORD_UV | TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COLORMAP_MODULATE | TOBJ_FLAGS.ALPHAMAP_MODULATE, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; tobj.EncodeImageData(bmp.GetBGRAData(), bmp.Width, bmp.Height, texFmt, palFmt); return(tobj); }
/// <summary> /// /// </summary> /// <param name="b"></param> public static HSD_TOBJ BitmapToTOBJ(Bitmap bmp, GXTexFmt imgFormat, GXTlutFmt palFormat) { var TOBJ = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, Flags = TOBJ_FLAGS.COORD_UV | TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COLORMAP_MODULATE | TOBJ_FLAGS.ALPHAMAP_MODULATE, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; InjectBitmap(bmp, TOBJ, imgFormat, palFormat); return(TOBJ); }
/// <summary> /// Injects <see cref="Bitmap"/> into <see cref="HSD_TOBJ"/> /// </summary> /// <param name="tobj"></param> /// <param name="img"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(string filepath, HSD_TOBJ tobj) { using (Bitmap bmp = LoadBitmapFromFile(filepath)) { using (TextureImportDialog settings = new TextureImportDialog()) { if (FormatFromString(filepath, out GXTexFmt fmt, out GXTlutFmt pal)) { settings.PaletteFormat = pal; settings.TextureFormat = fmt; } if (settings.ShowDialog() == DialogResult.OK) { settings.ApplySettings(bmp); InjectBitmap(bmp, tobj, settings.TextureFormat, settings.PaletteFormat); } } } }
/// <summary> /// Import TOBJ from PNG file /// </summary> /// <returns></returns> public static HSD_TOBJ ImportTOBJFromFile(string filePath, GXTexFmt imgFmt, GXTlutFmt tlutFmt) { var TOBJ = new HSD_TOBJ() { MagFilter = GXTexFilter.GX_LINEAR, Flags = TOBJ_FLAGS.COORD_UV | TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COLORMAP_MODULATE | TOBJ_FLAGS.ALPHAMAP_MODULATE, HScale = 1, WScale = 1, WrapS = GXWrapMode.CLAMP, WrapT = GXWrapMode.CLAMP, SX = 1, SY = 1, SZ = 1, GXTexGenSrc = 4, Blending = 1 }; InjectBitmap(filePath, TOBJ, imgFmt, tlutFmt); return(TOBJ); }
/// <summary> /// /// </summary> /// <returns></returns> private HSD_TOBJ[] GetTOBJs() { HSD_TOBJ[] tobjs = new HSD_TOBJ[TextureCount]; if (TOBJ != null) { tobjs[0] = TOBJ; } if (TexAnim != null) { for (int i = 0; i < TexAnim.ImageCount; i++) { var v = TexAnim.ImageBuffers.Array[i]; HSD_TlutBuffer pal = null; if (TexAnim.TlutBuffers != null) { pal = TexAnim.TlutBuffers.Array[i]; } HSD_TOBJ tobj = new HSD_TOBJ(); tobj.ImageData = v.Data; if (pal != null) { tobj.TlutData = pal.Data; } tobjs[i] = tobj; } } if (TEXG != null) { tobjs = TEXG.ConvertToTOBJs(); } return(tobjs); }
/// <summary> /// /// </summary> /// <param name="datFile"></param> /// <param name="stc"></param> /// <param name="csp"></param> /// <returns></returns> private MEXCostume ToCostume(string fileName, HSDRawFile file, HSD_TOBJ stc, HSD_TOBJ csp) { // create blank stock icon if one isn't set if (stc == null) { using (var bmp = new Bitmap(24, 24)) stc = bmp.ToTOBJ(HSDRaw.GX.GXTexFmt.CI4, HSDRaw.GX.GXTlutFmt.IA8); } // var costume = new MEXCostume() { Costume = new HSDRaw.MEX.MEX_CostumeFileSymbol() { FileName = Path.GetFileName(fileName) }, Icon = stc, CSP = csp }; // detect roots foreach (var r in file.Roots) { if (r.Name.EndsWith("_matanim_joint")) { costume.MaterialSymbol = r.Name; } else if (r.Name.EndsWith("_joint")) { costume.ModelSymbol = r.Name; } } return(costume); }
/// <summary> /// /// </summary> /// <param name="stage"></param> /// <returns></returns> public static void LoadIconDataFromVanilla(SBM_MnSelectStageDataTable stage, MEXStageIconEntry[] icons) { var tex0 = stage.IconDoubleMatAnimJoint.Child.Next.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex0_extra = stage.IconDoubleMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex1 = stage.IconLargeMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex2 = stage.IconSpecialMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var nameTOBJs = stage.StageNameMatAnimJoint.Child.Child.MaterialAnimation.TextureAnimation.ToTOBJs(); var nameTOBJsAnim = stage.StageNameMatAnimJoint.Child.Child.MaterialAnimation.TextureAnimation.AnimationObject.FObjDesc.GetDecodedKeys(); var g1 = tex0.Length - 2; var g2 = tex0.Length - 2 + tex1.Length - 2; var g3 = tex0.Length - 2 + tex1.Length - 2 + tex2.Length - 2; int icoIndex = 0; for (int i = 0; i < stage.PositionModel.Children.Length; i++) { var childIndex = i; if (unswizzle.ContainsKey(i)) { childIndex = unswizzle[i]; } HSD_TOBJ icon = null; HSD_TOBJ name = null; var anim = stage.PositionAnimation.Children[childIndex].AOBJ.FObjDesc; var keys = anim.GetDecodedKeys(); var Y = stage.PositionModel.Children[childIndex].TY; var Z = stage.PositionModel.Children[childIndex].TZ; var SX = 1f; var SY = 1f; int nameTex = i; if (i >= g3) { //RandomIcon name = nameTOBJs[(int)nameTOBJsAnim[nameTOBJsAnim.Count - 1].Value]; } else if (i >= g2) { name = nameTOBJs[(int)nameTOBJsAnim[24 + (i - g2)].Value]; icon = tex2[i - g2 + 2]; SX = 0.8f; SY = 0.8f; } else if (i >= g1) { name = nameTOBJs[(int)nameTOBJsAnim[22 + texunswizzle[i - g1]].Value]; icon = tex1[i - g1 + 2]; SY = 1.1f; } else { icon = tex0[texunswizzle[i] + 2]; name = nameTOBJs[(int)nameTOBJsAnim[texunswizzle[i]].Value * 2]; icons[icoIndex].X = keys[keys.Count - 1].Value; icons[icoIndex].Y = Y; icons[icoIndex].Z = Z; icons[icoIndex].Joint.SX = SX; icons[icoIndex].Joint.SY = SY; icons[icoIndex].IconTOBJ = icon; icons[icoIndex].NameTOBJ = name; icons[icoIndex].Animation = MexMenuAnimationGenerator.FromAnimJoint(stage.PositionAnimation.Children[childIndex], icons[icoIndex].Joint); icoIndex++; Y -= 5.6f; Z = 0; icon = tex0_extra[texunswizzle[i] + 2]; name = nameTOBJs[(int)nameTOBJsAnim[texunswizzle[i]].Value * 2 + 1]; } icons[icoIndex].X = keys[keys.Count - 1].Value; icons[icoIndex].Y = Y; icons[icoIndex].Z = Z; icons[icoIndex].Joint.SX = SX; icons[icoIndex].Joint.SY = SY; icons[icoIndex].IconTOBJ = icon; icons[icoIndex].NameTOBJ = name; icons[icoIndex].Animation = MexMenuAnimationGenerator.FromAnimJoint(stage.PositionAnimation.Children[childIndex], icons[icoIndex].Joint); icoIndex++; } }
/// <summary> /// /// </summary> /// <param name="stage"></param> /// <returns></returns> public static MEX_mexMapData LoadIconDataFromVanilla(SBM_MnSelectStageDataTable stage) { List <HSD_TOBJ> nameTags = new List <HSD_TOBJ>(); List <HSD_TOBJ> iconTOBJs = new List <HSD_TOBJ>(); HSD_JOBJ root = new HSD_JOBJ() { SX = 1, SY = 1, SZ = 1, Flags = JOBJ_FLAG.CLASSICAL_SCALING }; HSD_AnimJoint animRoot = new HSD_AnimJoint(); var tex0 = stage.IconDoubleMatAnimJoint.Child.Next.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex0_extra = stage.IconDoubleMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex1 = stage.IconLargeMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var tex2 = stage.IconSpecialMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); var nameTOBJs = stage.StageNameMatAnimJoint.Child.Child.MaterialAnimation.TextureAnimation.ToTOBJs(); var nameTOBJsAnim = stage.StageNameMatAnimJoint.Child.Child.MaterialAnimation.TextureAnimation.AnimationObject.FObjDesc.GetDecodedKeys(); var positionAnimation = new List <HSD_AnimJoint>(); foreach (var c in stage.PositionAnimation.Children) { var pos = new HSD_AnimJoint(); pos.AOBJ = HSDAccessor.DeepClone <HSD_AOBJ>(c.AOBJ); positionAnimation.Add(pos); } var g1 = tex0.Length - 2; var g2 = tex0.Length - 2 + tex1.Length - 2; var g3 = tex0.Length - 2 + tex1.Length - 2 + tex2.Length - 2; for (int i = 0; i < stage.PositionModel.Children.Length; i++) { var childIndex = i; if (unswizzle.ContainsKey(i)) { childIndex = unswizzle[i]; } HSD_TOBJ icon = null; HSD_TOBJ name = null; var keys = positionAnimation[childIndex].AOBJ.FObjDesc.GetDecodedKeys(); var Y = stage.PositionModel.Children[childIndex].TY; var Z = stage.PositionModel.Children[childIndex].TZ; var SX = 1f; var SY = 1f; if (i >= g3) { //RandomIcon name = nameTOBJs[(int)nameTOBJsAnim[nameTOBJsAnim.Count - 1].Value]; } else if (i >= g2) { name = nameTOBJs[(int)nameTOBJsAnim[24 + (i - g2)].Value]; icon = tex2[i - g2 + 2]; SX = 0.8f; SY = 0.8f; } else if (i >= g1) { name = nameTOBJs[(int)nameTOBJsAnim[22 + texunswizzle[i - g1]].Value]; icon = tex1[i - g1 + 2]; SY = 1.1f; } else { icon = tex0[texunswizzle[i] + 2]; name = nameTOBJs[(int)nameTOBJsAnim[texunswizzle[i]].Value * 2]; root.AddChild(new HSD_JOBJ() { TX = keys[keys.Count - 1].Value, TY = Y, TZ = Z, SX = SX, SY = SY, SZ = 1, Flags = JOBJ_FLAG.CLASSICAL_SCALING }); iconTOBJs.Add(icon); nameTags.Add(name); animRoot.AddChild(HSDAccessor.DeepClone <HSD_AnimJoint>(positionAnimation[childIndex])); Y -= 5.6f; Z = 0; icon = tex0_extra[texunswizzle[i] + 2]; name = nameTOBJs[(int)nameTOBJsAnim[texunswizzle[i]].Value * 2 + 1]; } root.AddChild(new HSD_JOBJ() { TX = keys[keys.Count - 1].Value, TY = Y, TZ = Z, SX = SX, SY = SY, SZ = 1, Flags = JOBJ_FLAG.CLASSICAL_SCALING }); iconTOBJs.Add(icon); nameTags.Add(name); animRoot.AddChild(HSDAccessor.DeepClone <HSD_AnimJoint>(positionAnimation[childIndex])); } var extraIcons = stage.IconLargeMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.ToTOBJs(); iconTOBJs.Insert(0, extraIcons[0]); iconTOBJs.Insert(0, extraIcons[1]); iconTOBJs.Add(extraIcons[0]); var iconJOBJ = HSDAccessor.DeepClone <HSD_JOBJ>(stage.IconDoubleModel); iconJOBJ.Child = iconJOBJ.Child.Next; var iconAnimJoint = HSDAccessor.DeepClone <HSD_AnimJoint>(stage.IconDoubleAnimJoint); iconAnimJoint.Child = iconAnimJoint.Child.Next; var iconMatAnimJoint = HSDAccessor.DeepClone <HSD_MatAnimJoint>(stage.IconDoubleMatAnimJoint); iconMatAnimJoint.Child = iconMatAnimJoint.Child.Next; iconMatAnimJoint.Child.MaterialAnimation.Next.TextureAnimation.FromTOBJs(iconTOBJs.ToArray(), true); var mapdata = new MEX_mexMapData(); mapdata.IconModel = iconJOBJ; mapdata.IconAnimJoint = iconAnimJoint; mapdata.IconMatAnimJoint = iconMatAnimJoint; mapdata.PositionModel = root; mapdata.PositionAnimJoint = animRoot; mapdata.StageNameMaterialAnimation = HSDAccessor.DeepClone <HSD_MatAnimJoint>(stage.StageNameMatAnimJoint); mapdata.StageNameMaterialAnimation.Child.Child.MaterialAnimation.TextureAnimation.FromTOBJs(nameTags, true); return(mapdata); }
public TextureContainer(HSD_TOBJ t) { TOBJ = t; Text = t.Flags.ToString(); }
public SBDobjAttachment() { Text = "DOBJ List"; Dock = DockStyle.Fill; //this.s ApplicationSettings.SkinControl(this); dobjList = new SBTreeView(); dobjList.Dock = DockStyle.Top; dobjList.Size = new Size(200, 200); dobjList.CheckBoxes = true; dobjList.HideSelection = false; dobjList.AfterCheck += (sender, args) => { if (args.Node != null && args.Node.Tag is SBHsdMesh mesh) { mesh.Visible = args.Node.Checked; } }; dobjList.AfterSelect += (sender, args) => { foreach (TreeNode v in dobjList.Nodes) { if (v.Tag is SBHsdMesh mesh) { mesh.Selected = false; } } propertyGrid.SelectedObject = null; if (dobjList.SelectedNode != null) { if (dobjList.SelectedNode.Tag is SBHsdMesh mesh) { mesh.Selected = true; } propertyGrid.SelectedObject = dobjList.SelectedNode.Tag; } }; propertyGrid = new PropertyGrid(); propertyGrid.Dock = DockStyle.Fill; propertyGrid.Size = new Size(200, 400); propertyGrid.SelectedObjectsChanged += (sender, args) => { removeTexture.Visible = propertyGrid.SelectedObject is HSD_TOBJ; exportTexture.Visible = propertyGrid.SelectedObject is HSD_TOBJ; importTexture.Visible = propertyGrid.SelectedObject != null; }; clearTextures = new SBButton("Clear Textures"); clearTextures.Dock = DockStyle.Top; clearTextures.Click += (sender, args) => { if (scene != null) { if (scene.HasMaterialAnimations) { MessageBox.Show("Eror: DATs with material animations must keep their textures intact"); } else { if (MessageBox.Show("Are you sure? This cannot be undone", "Clear Textures", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } //TODO: //scene.ClearMaterialAnimations(); //return; foreach (SBHsdMesh m in scene.GetMeshObjects()) { m.ClearTextures(); } RefreshList(); } } }; optionPanel = new GroupBox(); optionPanel.Text = "Options"; ApplicationSettings.SkinControl(optionPanel); optionPanel.Dock = DockStyle.Top; //AutoScroll = true; exportTexture = new SBButton("Export Texture"); exportTexture.Dock = DockStyle.Top; exportTexture.Visible = false; optionPanel.Controls.Add(exportTexture); exportTexture.Click += (sender, args) => { string filePath; if (propertyGrid.SelectedObject is HSD_TOBJ tobj) { var filter = "PNG (*.png)|*.png;"; //if (tobj.ImageData != null && tobj.ImageData.Format == GXTexFmt.CMP) // filter = "DDS (*.dds)|*.dds;"; if (FileTools.TrySaveFile(out filePath, filter)) { //TODO: dds export / import /*if (tobj.ImageData != null && tobj.ImageData.Format == GXTexFmt.CMP) * { * SBSurface s = new SBSurface(); * s.Width = tobj.ImageData.Width; * s.Height = tobj.ImageData.Height; * s.InternalFormat = OpenTK.Graphics.OpenGL.InternalFormat.CompressedRgbaS3tcDxt1Ext; * s.Arrays.Add(new MipArray() { Mipmaps = new List<byte[]>() { HSDRaw.Tools.TPLConv.ToCMP(tobj.ImageData.ImageData, tobj.ImageData.Width, tobj.ImageData.Height) } }); * * IO_DDS.Export(filePath, s); * } * else*/ { FileTools.WriteBitmapFile(filePath, tobj.ImageData.Width, tobj.ImageData.Height, tobj.GetDecodedImageData()); } } } }; removeTexture = new SBButton("Remove Texture"); removeTexture.Dock = DockStyle.Top; removeTexture.Visible = false; optionPanel.Controls.Add(removeTexture); removeTexture.Click += (sender, args) => { if (dobjList.SelectedNode.Tag is HSD_TOBJ tobj) { // remove tobj from list var mobj = (HSD_MOBJ)dobjList.SelectedNode.Parent.Tag; HSD_TOBJ prevTexture = null; if (mobj.Textures != null) { foreach (var tex in mobj.Textures.List) { if (tex._s == tobj._s) { if (prevTexture == null) { mobj.Textures = tex.Next; } else { prevTexture.Next = tex.Next; } // update texture and flag stuff break; } prevTexture = tex; } } FixMOBJTexIDs(mobj); var root = dobjList.SelectedNode.Parent.Parent; root.Nodes.Clear(); root.Nodes.Add(CreateMOBJNode(mobj)); scene.RefreshRendering(); } }; importTexture = new SBButton("Import Texture"); importTexture.Dock = DockStyle.Top; importTexture.Visible = false; optionPanel.Controls.Add(importTexture); importTexture.Click += (sender, args) => { // select texture HSD_MOBJ mobj = null; SBTreeNode root = null; if (dobjList.SelectedNode.Tag is SBHsdMesh mesh) { if (mesh.DOBJ.Mobj != null) { mobj = mesh.DOBJ.Mobj; root = (SBTreeNode)dobjList.SelectedNode; } } if (dobjList.SelectedNode.Tag is HSD_MOBJ m) { mobj = m; root = (SBTreeNode)dobjList.SelectedNode.Parent; } if (dobjList.SelectedNode.Tag is HSD_TOBJ) { if (dobjList.SelectedNode.Parent.Tag is HSD_MOBJ mo) { mobj = mo; root = (SBTreeNode)dobjList.SelectedNode.Parent.Parent; } } if (mobj == null) { return; } string filePath; if (FileTools.TryOpenFile(out filePath, "Supported Formats (*.png*.dds)|*.png;*.dds")) { var settings = new TOBJImportSettings(); // select textue import options using (SBCustomDialog d = new SBCustomDialog(settings)) { if (d.ShowDialog() == DialogResult.OK) { // create tobj and attach to selected mobj HSD_TOBJ tobj = new HSD_TOBJ(); tobj.MagFilter = GXTexFilter.GX_LINEAR; tobj.HScale = 1; tobj.WScale = 1; tobj.WrapS = GXWrapMode.REPEAT; tobj.WrapT = GXWrapMode.REPEAT; tobj.SX = 1; tobj.SY = 1; tobj.SZ = 1; if (System.IO.Path.GetExtension(filePath.ToLower()) == ".dds") { var dxtsurface = IO_DDS.Import(filePath); if (dxtsurface.InternalFormat != OpenTK.Graphics.OpenGL.InternalFormat.CompressedRgbaS3tcDxt1Ext) { throw new NotSupportedException("DDS format " + dxtsurface.InternalFormat.ToString() + " not supported"); } tobj.EncodeImageData(dxtsurface.Arrays[0].Mipmaps[0], dxtsurface.Width, dxtsurface.Height, GXTexFmt.CMP, GXTlutFmt.IA8); } else { var bmp = new Bitmap(filePath); var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var length = bitmapData.Stride * bitmapData.Height; byte[] bytes = new byte[length]; Marshal.Copy(bitmapData.Scan0, bytes, 0, length); bmp.UnlockBits(bitmapData); tobj.EncodeImageData(bytes, bmp.Width, bmp.Height, settings.ImageFormat, settings.PaletteFormat); bmp.Dispose(); } if (settings.UseBlending && mobj.PEDesc == null) { mobj.PEDesc = new HSD_PEDesc(); } //TODO: set flags for texture types if (settings.TextureType == TOBJTextureType.Diffuse) { mobj.RenderFlags |= RENDER_MODE.DIFFUSE; tobj.Flags |= TOBJ_FLAGS.LIGHTMAP_DIFFUSE; } if (settings.TextureType == TOBJTextureType.Specular) { mobj.RenderFlags |= RENDER_MODE.SPECULAR; tobj.Flags |= TOBJ_FLAGS.LIGHTMAP_SPECULAR; } if (settings.TextureType == TOBJTextureType.Bump) { tobj.Flags |= TOBJ_FLAGS.BUMP; } switch (settings.UVType) { case TOBJUVType.Sphere: tobj.Flags |= TOBJ_FLAGS.COORD_REFLECTION; break; case TOBJUVType.TextureCoord: tobj.Flags |= TOBJ_FLAGS.COORD_UV; break; } if (mobj.Textures == null) { mobj.Textures = tobj; tobj.Flags |= TOBJ_FLAGS.COLORMAP_REPLACE; } else { tobj.Flags |= TOBJ_FLAGS.COLORMAP_BLEND; } propertyGrid.SelectedObject = tobj; FixMOBJTexIDs(mobj); root.Nodes.Clear(); root.Nodes.Add(CreateMOBJNode(mobj)); scene.RefreshRendering(); } } } }; propertyPanel = new GroupBox(); propertyPanel.Text = "Properties"; propertyPanel.Dock = DockStyle.Top; propertyPanel.Controls.Add(propertyGrid); propertyPanel.Height = 300; ApplicationSettings.SkinControl(propertyPanel); Controls.Add(propertyPanel); Controls.Add(new Splitter() { Dock = DockStyle.Top, Height = 10, BackColor = ApplicationSettings.BGColor2 }); Controls.Add(optionPanel); Controls.Add(new Splitter() { Dock = DockStyle.Top, Height = 10, BackColor = ApplicationSettings.BGColor2 }); Controls.Add(dobjList); Controls.Add(clearTextures); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addPackage_Click(object sender, EventArgs e) { using (OpenFileDialog d = new OpenFileDialog()) { d.Filter = "Costume DAT or Package (*.dat, *.zip)|*.dat;*.zip;"; if (d.ShowDialog() == DialogResult.OK) { switch (Path.GetExtension(d.FileName).ToLower()) { case ".zip": using (FileStream zipToOpen = new FileStream(d.FileName, FileMode.Open)) using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read)) { string fileName = ""; byte[] datFile = null; HSD_TOBJ csp = null; HSD_TOBJ stc = null; foreach (var v in archive.Entries) { Console.WriteLine(v.FullName); if (v.Name.EndsWith(".dat")) { using (var s = v.Open()) using (var deom = new MemoryStream()) { s.CopyTo(deom); datFile = deom.ToArray(); fileName = v.Name; } } if (v.Name.Equals("stc.png")) { using (var s = v.Open()) using (var img = new Bitmap(s)) stc = img.ToTOBJ(HSDRaw.GX.GXTexFmt.CI4, HSDRaw.GX.GXTlutFmt.RGB5A3); } if (v.Name.Equals("csp.png")) { using (var s = v.Open()) using (var img = new Bitmap(s)) csp = img.ToTOBJ(HSDRaw.GX.GXTexFmt.CI8, HSDRaw.GX.GXTlutFmt.RGB5A3); } } if (datFile != null) { var costume = ToCostume(fileName, new HSDRawFile(datFile), stc, csp); MEX.ImageResource.AddFile(fileName, datFile); _costumes.Add(costume); } } break; case ".dat": { var costume = ToCostume(Path.GetFileName(d.FileName), new HSDRawFile(d.FileName), null, null); MEX.ImageResource.AddFile(Path.GetFileName(d.FileName), d.FileName); _costumes.Add(costume); } break; } } } }
/// <summary> /// /// </summary> /// <param name="tobj"></param> /// <param name="bmp"></param> /// <param name="imgFormat"></param> /// <param name="palFormat"></param> public static void InjectBitmap(Bitmap bmp, HSD_TOBJ tobj, GXTexFmt imgFormat, GXTlutFmt palFormat) { tobj.EncodeImageData(bmp.GetBGRAData(), bmp.Width, bmp.Height, imgFormat, palFormat); }