void DumpMaterial(AxFService.AxFFile.Material _material, System.IO.DirectoryInfo _targetDirectory) { System.IO.DirectoryInfo fullTargetDirectory = new System.IO.DirectoryInfo(System.IO.Path.Combine(_targetDirectory.FullName, _material.Name)); if (!fullTargetDirectory.Exists) { fullTargetDirectory.Create(); } AxFService.AxFFile.Material.Texture[] textures = _material.Textures; TEXTURE_TYPE[] textureTypes = new TEXTURE_TYPE[textures.Length]; string[] GUIDs = new string[textures.Length]; bool allGUIDsValid = true; for (int textureIndex = 0; textureIndex < textures.Length; textureIndex++) { AxFService.AxFFile.Material.Texture texture = textures[textureIndex]; TEXTURE_TYPE textureType = TEXTURE_TYPE.UNKNOWN; switch (texture.Name.ToLower()) { case "diffusecolor": textureType = TEXTURE_TYPE.DIFFUSE_COLOR; break; case "specularcolor": textureType = TEXTURE_TYPE.SPECULAR_COLOR; break; case "normal": textureType = TEXTURE_TYPE.NORMAL; break; case "fresnel": textureType = TEXTURE_TYPE.FRESNEL; break; case "specularlobe": textureType = TEXTURE_TYPE.SPECULAR_LOBE; break; case "anisorotation": textureType = TEXTURE_TYPE.ANISOTROPY_ANGLE; break; case "height": textureType = TEXTURE_TYPE.HEIGHT; break; case "opacity": textureType = TEXTURE_TYPE.OPACITY; break; case "clearcoatcolor": textureType = TEXTURE_TYPE.CLEARCOAT_COLOR; break; case "clearcoatnormal": textureType = TEXTURE_TYPE.CLEARCOAT_NORMAL; break; case "clearcoatior": textureType = TEXTURE_TYPE.CLEARCOAT_IOR; break; // Car Paint case "brdfcolors": textureType = TEXTURE_TYPE.BRDF_COLOR; break; case "btfflakes": textureType = TEXTURE_TYPE.BTF_FLAKES; break; default: throw new Exception("Unsupported texture type \"" + texture.Name + "\"!"); } textureTypes[textureIndex] = textureType; bool sRGB = ((int)textureType & (int)TEXTURE_TYPE.FLAG_sRGB) != 0; bool isNormalMap = ((int)textureType & (int)TEXTURE_TYPE.FLAG_NORMAL) != 0; bool isIOR = ((int)textureType & (int)TEXTURE_TYPE.FLAG_IOR) != 0; bool isAngle = ((int)textureType & (int)TEXTURE_TYPE.FLAG_ANGLE) != 0; bool isArray = ((int)textureType & (int)TEXTURE_TYPE.FLAG_2DARRAY) != 0; bool scale = ((int)textureType & (int)TEXTURE_TYPE.FLAG_SCALE_BY_MAX) != 0 && texture.MaxValue > 1; System.IO.FileInfo targetTextureFileName = new System.IO.FileInfo(System.IO.Path.Combine(fullTargetDirectory.FullName, texture.Name + ".png")); //* // // Dump as DDS // texture.Images.DDSSaveFile( new System.IO.FileInfo( @"D:\Workspaces\Unity Labs\AxF\AxF Shader\Assets\AxF Materials\X-Rite_14-LTH_Red_GoatLeather_4405_2479\" + texture.Name + ".dds" ), texture.ComponentFormat ); // Individual dump as RGBA8 files // ImageUtility.ImageFile source = texture.Images[0][0][0]; // ImageUtility.ImageFile temp = new ImageUtility.ImageFile(); // //temp.ConvertFrom( source, ImageUtility.PIXEL_FORMAT.BGRA8 ); // temp.ToneMapFrom( source, ( float3 _HDR, ref float3 _LDR ) => { _LDR =_HDR; } ); // temp.Save( new System.IO.FileInfo( @"D:\Workspaces\Unity Labs\AxF\AxF Shader\Assets\AxF Materials\X-Rite_14-LTH_Red_GoatLeather_4405_2479\" + texture.Name + ".png" ), ImageUtility.ImageFile.FILE_FORMAT.PNG ); uint mipsCount = texture.Images[0].MipLevelsCount; for (uint mipIndex = 0; mipIndex < mipsCount; mipIndex++) { // Individual dump as RGBA16 files ImageUtility.ImageFile source = texture.Images[0][mipIndex][0]; float factor = 1.0f; if (scale) { factor = 1.0f / texture.MaxValue; // Apply scale } // if ( textureType == TEXTURE_TYPE.BRDF_COLOR ) { // Random R = new Random(); // source.ReadWritePixels( ( uint _X, uint _Y, ref float4 _color ) => { // _color.x = (0.5f+_X) / 63.0f; // _color.y = (0.5f+_X) / 63.0f; // _color.z = (0.5f+_X) / 63.0f; // _color.w = (float) R.NextDouble(); // // // Apply sRGB // _color.x = Mathf.Pow( Math.Max( 0.0f, _color.x ), 1.0f / 2.2f ); // _color.y = Mathf.Pow( Math.Max( 0.0f, _color.y ), 1.0f / 2.2f ); // _color.z = Mathf.Pow( Math.Max( 0.0f, _color.z ), 1.0f / 2.2f ); // // } ); // } else if (sRGB) { source.ReadWritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = Mathf.Pow(Math.Max(0.0f, factor * _color.x), 1.0f / 2.2f); _color.y = Mathf.Pow(Math.Max(0.0f, factor * _color.y), 1.0f / 2.2f); _color.z = Mathf.Pow(Math.Max(0.0f, factor * _color.z), 1.0f / 2.2f); // _color.w = 1.0f; }); } if (isNormalMap) { source.ReadWritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = 0.5f * (1.0f + _color.x); _color.y = 0.5f * (1.0f + _color.y); _color.z = 0.5f * (1.0f + _color.z); }); } if (isIOR) { // Transform into F0 source.ReadWritePixels((uint _X, uint _Y, ref float4 _color) => { if (float.IsNaN(_color.x)) { _color.x = 1.2f; } if (float.IsNaN(_color.y)) { _color.y = 1.2f; } if (float.IsNaN(_color.z)) { _color.z = 1.2f; } _color.x = (_color.x - 1.0f) / (_color.x + 1.0f); // We apply the square below, during the sRGB conversion _color.y = (_color.y - 1.0f) / (_color.y + 1.0f); _color.z = (_color.z - 1.0f) / (_color.z + 1.0f); _color.x = Mathf.Pow(Mathf.Max(0.0f, _color.x), 2.0f / 2.2f); // <= Notice the 2/2.2 here! _color.y = Mathf.Pow(Mathf.Max(0.0f, _color.y), 2.0f / 2.2f); _color.z = Mathf.Pow(Mathf.Max(0.0f, _color.z), 2.0f / 2.2f); }); sRGB = true; // Also encoded as sRGB now } if (isAngle) { // Renormalize source.ReadWritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = 0.5f * (1.0f + _color.x * Mathf.INVPI); _color.y = 0.5f * (1.0f + _color.y * Mathf.INVPI); _color.z = 0.5f * (1.0f + _color.z * Mathf.INVPI); }); } ImageUtility.ImageFile temp = new ImageUtility.ImageFile(); // if ( texture.Name.ToLower() == "diffusecolor" ) // temp.ToneMapFrom( source, ( float3 _HDR, ref float3 _LDR ) => { _LDR =_HDR; } ); // 8-bits for diffuse otherwise unity doesn't like it... :'( // else temp.ConvertFrom(source, ImageUtility.PIXEL_FORMAT.RGBA16); System.IO.FileInfo targetMipTextureFileName = mipIndex > 0 ? new System.IO.FileInfo(System.IO.Path.Combine(fullTargetDirectory.FullName, texture.Name + "_mip" + mipIndex + ".png")) : targetTextureFileName; temp.Save(targetMipTextureFileName, ImageUtility.ImageFile.FILE_FORMAT.PNG); // System.IO.FileInfo targetMipTextureFileName = new System.IO.FileInfo( System.IO.Path.Combine( fullTargetDirectory.FullName, texture.Name + ".tif" ) ); // temp.Save( targetMipTextureFileName, ImageUtility.ImageFile.FILE_FORMAT.TIFF ); // Generate or read meta file string GUID = GenerateMeta(targetMipTextureFileName, checkBoxGenerateMeta.Checked, checkBoxOverwriteExistingMeta.Checked, sRGB, isNormalMap, isIOR, isArray); if (mipIndex == 0) { GUIDs[textureIndex] = GUID; allGUIDsValid &= GUID != null; } } //*/ } if (!checkBoxGenerateMat.Checked) { return; } if (!allGUIDsValid) { throw new Exception("Not all texture GUIDs are valid! Can't generate material file!"); } GenerateMaterial(_material, _targetDirectory, textureTypes, GUIDs); }