Example #1
0
        /// <summary>
        ///
        /// </summary>
        public static void ImportTextures(HSD_JOBJ jobj)
        {
            var folder = Tools.FileIO.OpenFolder();

            if (!string.IsNullOrEmpty(folder))
            {
                // get all tobjs
                Dictionary <int, HSD_TOBJ> hashToImage = new Dictionary <int, HSD_TOBJ>();

                // load all textures from file
                foreach (var tf in System.IO.Directory.GetFiles(folder))
                {
                    if (tf.ToLower().EndsWith(".png"))
                    {
                        var fn = System.IO.Path.GetFileNameWithoutExtension(tf);
                        if (fn.Length >= 8 &&
                            int.TryParse(fn.Substring(0, 8), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int hash) &&
                            TOBJConverter.FormatFromString(fn, out HSDRaw.GX.GXTexFmt texFmt, out HSDRaw.GX.GXTlutFmt tlutFmt))
                        {
                            hashToImage.Add(hash, TOBJConverter.ImportTOBJFromFile(tf, texFmt, tlutFmt));
                        }
                    }
                }

                // get all tobjs
                foreach (var j in jobj.BreathFirstList)
                {
                    if (j.Dobj != null)
                    {
                        foreach (var dobj in j.Dobj.List)
                        {
                            if (dobj.Mobj != null && dobj.Mobj.Textures != null)
                            {
                                foreach (var tobj in dobj.Mobj.Textures.List)
                                {
                                    // generate hashes and export textures and formatting

                                    var hash = ComputeHash(tobj.GetDecodedImageData());

                                    if (hashToImage.ContainsKey(hash))
                                    {
                                        var imgClone = HSDAccessor.DeepClone <HSD_TOBJ>(hashToImage[hash]);
                                        tobj.ImageData = imgClone.ImageData;
                                        tobj.TlutData  = imgClone.TlutData;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        private HSD_MOBJ GenerateMaterial(IOMaterial material)
        {
            // create blank mobj
            var Mobj = new HSD_MOBJ();

            Mobj.Material = new HSD_Material()
            {
                AMB_A         = 0xFF,
                AMB_R         = 0x7F,
                AMB_G         = 0x7F,
                AMB_B         = 0x7F,
                DiffuseColor  = System.Drawing.Color.White,
                SpecularColor = System.Drawing.Color.White,
                Shininess     = 50,
                Alpha         = 1
            };

            // detect and set flags
            if (Settings.ImportVertexColor)
            {
                Mobj.RenderFlags |= RENDER_MODE.VERTEX;
            }

            if (Settings.EnableDiffuse)
            {
                Mobj.RenderFlags |= RENDER_MODE.DIFFUSE;
            }

            if (Settings.EnableConstant)
            {
                Mobj.RenderFlags |= RENDER_MODE.CONSTANT;
            }

            // Properties
            if (material != null && Settings.ImportMaterialInfo)
            {
                Mobj.Material.Shininess = material.Shininess;
                Mobj.Material.Alpha     = material.Alpha;

                Mobj.Material.AMB_R = (byte)(material.AmbientColor.X * 255);
                Mobj.Material.AMB_G = (byte)(material.AmbientColor.Y * 255);
                Mobj.Material.AMB_B = (byte)(material.AmbientColor.Z * 255);
                Mobj.Material.AMB_A = (byte)(material.AmbientColor.W * 255);

                Mobj.Material.DIF_R = (byte)(material.DiffuseColor.X * 255);
                Mobj.Material.DIF_G = (byte)(material.DiffuseColor.Y * 255);
                Mobj.Material.DIF_B = (byte)(material.DiffuseColor.Z * 255);
                Mobj.Material.DIF_A = (byte)(material.DiffuseColor.W * 255);

                Mobj.Material.SPC_R = (byte)(material.SpecularColor.X * 255);
                Mobj.Material.SPC_G = (byte)(material.SpecularColor.Y * 255);
                Mobj.Material.SPC_B = (byte)(material.SpecularColor.Z * 255);
                Mobj.Material.SPC_A = (byte)(material.SpecularColor.W * 255);
            }

            // Textures
            if (material != null && Settings.ImportTexture)
            {
                if (material.DiffuseMap != null && !string.IsNullOrEmpty(material.DiffuseMap.FilePath))
                {
                    var texturePath = material.DiffuseMap.FilePath;

                    if (texturePath.Contains("file://"))
                    {
                        texturePath = texturePath.Replace("file://", "");
                    }

                    if (File.Exists(Path.Combine(_cache.FolderPath, texturePath)))
                    {
                        texturePath = Path.Combine(_cache.FolderPath, texturePath);
                    }

                    if (File.Exists(material.DiffuseMap.FilePath))
                    {
                        texturePath = material.DiffuseMap.FilePath;
                    }

                    if (File.Exists(texturePath + ".png"))
                    {
                        texturePath = texturePath + ".png";
                    }


                    var mobjPath = Path.Combine(Path.GetDirectoryName(texturePath), Path.GetFileNameWithoutExtension(texturePath)) + ".mobj";

                    if (Settings.ImportMOBJ && File.Exists(mobjPath))
                    {
                        var dat = new HSDRaw.HSDRawFile(mobjPath);
                        Mobj._s = dat.Roots[0].Data._s;
                        return(Mobj);
                    }
                    else
                    /// special mobj loading
                    if (Path.GetExtension(texturePath).ToLower() == ".mobj")
                    {
                        var dat = new HSDRaw.HSDRawFile(texturePath);
                        Mobj._s = dat.Roots[0].Data._s;
                        return(Mobj);
                    }
                    else
                    if (File.Exists(texturePath) && (texturePath.ToLower().EndsWith(".png") || texturePath.ToLower().EndsWith(".bmp")))
                    {
                        Mobj.RenderFlags |= RENDER_MODE.TEX0;

                        var tobj = TOBJConverter.ImportTOBJFromFile(texturePath, Settings.TextureFormat, Settings.PaletteFormat);
                        tobj.Flags = TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COORD_UV | TOBJ_FLAGS.COLORMAP_MODULATE;

                        tobj.GXTexGenSrc = 4;
                        tobj.TexMapID    = GXTexMapID.GX_TEXMAP0;

                        tobj.WrapS = ToGXWrapMode(material.DiffuseMap.WrapS);
                        tobj.WrapT = ToGXWrapMode(material.DiffuseMap.WrapT);

                        if (TOBJConverter.IsTransparent(tobj))
                        {
                            _cache.HasXLU     = true;
                            Mobj.RenderFlags |= RENDER_MODE.XLU;
                            tobj.Flags       |= TOBJ_FLAGS.ALPHAMAP_MODULATE;
                        }

                        Mobj.Textures = tobj;
                    }
                }
            }

            return(Mobj);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        private static HSD_MOBJ GenerateMaterial(ProcessingCache cache, ModelImportSettings settings, Material material)
        {
            var Mobj = new HSD_MOBJ();

            Mobj.Material = new HSD_Material();
            Mobj.Material.AmbientColorRGBA  = 0x7F7F7FFF;
            Mobj.Material.DiffuseColorRGBA  = 0xFFFFFFFF;
            Mobj.Material.SpecularColorRGBA = 0xFFFFFFFF;
            Mobj.Material.Shininess         = 1;
            Mobj.Material.Alpha             = 1;
            Mobj.RenderFlags = RENDER_MODE.ALPHA_COMPAT | RENDER_MODE.DIFFSE_VTX;

            // Properties
            if (settings.ImportMaterialInfo)
            {
                if (material.HasShininess)
                {
                    Mobj.Material.Shininess = material.Shininess;
                }

                if (material.HasColorAmbient)
                {
                    Mobj.Material.AMB_A = ColorFloatToByte(material.ColorAmbient.A);
                    Mobj.Material.AMB_R = ColorFloatToByte(material.ColorAmbient.R);
                    Mobj.Material.AMB_G = ColorFloatToByte(material.ColorAmbient.G);
                    Mobj.Material.AMB_B = ColorFloatToByte(material.ColorAmbient.B);
                }
                if (material.HasColorDiffuse)
                {
                    Mobj.Material.DIF_A = ColorFloatToByte(material.ColorDiffuse.A);
                    Mobj.Material.DIF_R = ColorFloatToByte(material.ColorDiffuse.R);
                    Mobj.Material.DIF_G = ColorFloatToByte(material.ColorDiffuse.G);
                    Mobj.Material.DIF_B = ColorFloatToByte(material.ColorDiffuse.B);
                }
                if (material.HasColorSpecular)
                {
                    Mobj.Material.SPC_A = ColorFloatToByte(material.ColorSpecular.A);
                    Mobj.Material.SPC_R = ColorFloatToByte(material.ColorSpecular.R);
                    Mobj.Material.SPC_G = ColorFloatToByte(material.ColorSpecular.G);
                    Mobj.Material.SPC_B = ColorFloatToByte(material.ColorSpecular.B);
                }
            }

            // Textures
            if (settings.ImportTexture)
            {
                if (material.HasTextureDiffuse)
                {
                    var texturePath = Path.Combine(cache.FolderPath, material.TextureDiffuse.FilePath);

                    if (File.Exists(material.TextureDiffuse.FilePath))
                    {
                        texturePath = material.TextureDiffuse.FilePath;
                    }

                    /// special mobj loading
                    if (Path.GetExtension(texturePath).ToLower() == ".mobj")
                    {
                        var dat = new HSDRaw.HSDRawFile(texturePath);
                        Mobj._s = dat.Roots[0].Data._s;
                        return(Mobj);
                    }
                    else
                    if (File.Exists(texturePath))
                    {
                        Mobj.RenderFlags |= RENDER_MODE.TEX0;

                        var tobj = TOBJConverter.ImportTOBJFromFile(texturePath, settings.TextureFormat, settings.PaletteFormat);
                        tobj.Flags = TOBJ_FLAGS.LIGHTMAP_DIFFUSE | TOBJ_FLAGS.COORD_UV;

                        if (settings.ShadingType == ShadingType.VertexColor || settings.ShadingType == ShadingType.Material)
                        {
                            tobj.Flags |= TOBJ_FLAGS.COLORMAP_MODULATE;
                        }
                        else
                        {
                            tobj.Flags |= TOBJ_FLAGS.COLORMAP_REPLACE;
                        }

                        tobj.GXTexGenSrc = 4;
                        tobj.TexMapID    = GXTexMapID.GX_TEXMAP0;

                        tobj.WrapS = ToGXWrapMode(material.TextureDiffuse.WrapModeU);
                        tobj.WrapT = ToGXWrapMode(material.TextureDiffuse.WrapModeV);

                        Mobj.Textures = tobj;
                    }
                }
            }

            return(Mobj);
        }