浏览代码

Material upgrader working quite fine now :)

/main
Remy 7 年前
当前提交
fa8d58d8
共有 4 个文件被更改,包括 288 次插入57 次删除
  1. 145
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs
  2. 8
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader
  3. 74
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs
  4. 118
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs

145
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs


get
{
if (_midGrey == null)
{
_midGrey = new Texture2D(4, 4, TextureFormat.ARGB32, false, false);
_midGrey.SetPixels(new Color[] {
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray
});
_midGrey.Apply();
}
_midGrey = TextureFromColor(Color.grey);
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, int fallback = 0)
private static Dictionary<Color, Texture2D> singleColorTextures = new Dictionary<Color, Texture2D>();
public static Texture2D TextureFromColor(Color color)
{
if (color == Color.white) return Texture2D.whiteTexture;
if (color == Color.black) return Texture2D.blackTexture;
bool makeTexture = !singleColorTextures.ContainsKey(color);
if (!makeTexture)
makeTexture = (singleColorTextures[color] == null);
if (makeTexture)
{
Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, false, true);
tex.SetPixels(new Color[] {
color, color, color, color,
color, color, color, color,
color, color, color, color,
color, color, color, color
});
tex.Apply();
singleColorTextures[color] = tex;
}
return singleColorTextures[color];
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, int fallback)
switch(fallback)
{
case 0: return GetTextureSafe( srcMaterial, propertyName, Texture2D.whiteTexture );
case 1: return GetTextureSafe( srcMaterial, propertyName, Texture2D.blackTexture );
case 2: return GetTextureSafe( srcMaterial, propertyName, TextureCombiner.midGrey );
}
return null;
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, Color fallback)
{
return GetTextureSafe( srcMaterial, propertyName, TextureFromColor(fallback) );
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, Texture fallback)
{
if (!srcMaterial.HasProperty(propertyName))
return fallback;
return fallback;
else
return tex;
}
public static TextureFormat[] TextureFormatsWithouthAlpha = new TextureFormat[]{
TextureFormat.ASTC_RGB_10x10 ,
TextureFormat.ASTC_RGB_12x12 ,
TextureFormat.ASTC_RGB_4x4 ,
TextureFormat.ASTC_RGB_5x5 ,
TextureFormat.ASTC_RGB_6x6 ,
TextureFormat.ASTC_RGB_8x8 ,
TextureFormat.BC4 ,
TextureFormat.BC5 ,
TextureFormat.DXT1 ,
TextureFormat.DXT1Crunched ,
TextureFormat.EAC_R ,
TextureFormat.EAC_R_SIGNED ,
TextureFormat.EAC_RG ,
TextureFormat.EAC_RG_SIGNED ,
TextureFormat.ETC2_RGB ,
TextureFormat.ETC_RGB4 ,
TextureFormat.ETC_RGB4_3DS ,
TextureFormat.ETC_RGB4Crunched ,
TextureFormat.PVRTC_RGB2 ,
TextureFormat.PVRTC_RGB4 ,
TextureFormat.R16 ,
TextureFormat.R8 ,
TextureFormat.RFloat ,
TextureFormat.RG16 ,
TextureFormat.RGB24 ,
TextureFormat.RGB565 ,
TextureFormat.RGB9e5Float ,
TextureFormat.RGFloat ,
TextureFormat.RGHalf ,
TextureFormat.RHalf ,
TextureFormat.YUY2
};
public static bool TextureHasAlpha ( Texture2D tex )
{
if (tex == null) return false;
bool o = true;
int i=0;
while ( i < TextureFormatsWithouthAlpha.Length && o)
switch(fallback)
{
case 0: tex = Texture2D.whiteTexture; break;
case 1: tex = Texture2D.blackTexture; break;
case 2: tex = TextureCombiner.midGrey; break;
}
o = tex.format != TextureFormatsWithouthAlpha[i];
++i;
return tex;
return o;
}
private Texture m_rSource;

private Dictionary<Texture, Texture> m_RawTextures;
public TextureCombiner( Texture rSource, int rChanel, Texture gSource, int gChanel, Texture bSource, int bChanel, Texture aSource, int achanel, bool bilinearFilter = true )
public TextureCombiner( Texture rSource, int rChanel, Texture gSource, int gChanel, Texture bSource, int bChanel, Texture aSource, int aChanel, bool bilinearFilter = true )
{
m_rSource = rSource;
m_gSource = gSource;

m_gChanel = gChanel;
m_bChanel = bChanel;
m_aChanel = achanel;
m_aChanel = aChanel;
m_bilinearFilter = bilinearFilter;
}

combinerMaterial.SetFloat("_BChannel", m_bChanel);
combinerMaterial.SetFloat("_AChannel", m_aChanel);
RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.sRGB);
Graphics.Blit(Texture2D.whiteTexture, combinedRT, combinerMaterial);

combined.Apply();
RenderTexture.active = previousActive;
byte[] bytes = ImageConversion.EncodeToEXR(combined);
byte[] bytes = new byte[0];
if (savePath.EndsWith("png"))
bytes = ImageConversion.EncodeToPNG(combined);
if (savePath.EndsWith("exr"))
bytes = ImageConversion.EncodeToEXR(combined);
if (savePath.EndsWith("jpg"))
bytes = ImageConversion.EncodeToJPG(combined);
string systemPath = Path.Combine(Application.dataPath.Remove(Application.dataPath.Length-6), savePath);
File.WriteAllBytes(systemPath, bytes);

combinedImporter.sRGBTexture = false;
combinedImporter.SaveAndReimport();
if (savePath.EndsWith("exr"))
{
// The options for the platform string are: "Standalone", "iPhone", "Android", "WebGL", "Windows Store Apps", "PSP2", "PS4", "XboxOne", "Nintendo 3DS", "WiiU", "tvOS".
combinedImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings(){name = "Standalone", format = TextureImporterFormat.DXT5, overridden = true });
}
combined = AssetDatabase.LoadAssetAtPath<Texture2D>(savePath);
//cleanup "raw" textures

return combined;
}
private Texture GetRawTexture (Texture original)
private Texture GetRawTexture (Texture original, bool sRGB = false)
{
if (m_RawTextures == null) m_RawTextures = new Dictionary<Texture, Texture>();
if (!m_RawTextures.ContainsKey(original))

Debug.Log("Import raw texture: "+rawPath);
TextureImporter rawImporter = (TextureImporter) TextureImporter.GetAtPath(rawPath);
rawImporter.textureType = TextureImporterType.Default;
rawImporter.sRGBTexture = sRGB;
rawImporter.maxTextureSize = 8192;
rawImporter.textureType = TextureImporterType.Default;
rawImporter.SaveAndReimport();

8
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader


{
// Chanels are : r=0, g=1, b=2, a=3, greyscale from rgb = 4
[NoScaleOffset] _RSource ("R Source", 2D) = "white" {}
[Linear][NoScaleOffset] _RSource ("R Source", 2D) = "white" {}
[NoScaleOffset] _GSource ("G Source", 2D) = "white" {}
[Linear][NoScaleOffset] _GSource ("G Source", 2D) = "white" {}
[NoScaleOffset] _BSource ("B Source", 2D) = "white" {}
[Linear][NoScaleOffset] _BSource ("B Source", 2D) = "white" {}
[NoScaleOffset] _ASource ("A Source", 2D) = "white" {}
[Linear][NoScaleOffset] _ASource ("A Source", 2D) = "white" {}
_AChannel ("A Channel", float) = 3
}
SubShader

74
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs


RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameColor("_EmissionColor", "_EmissiveColor");
RenameTexture("_EmissionMap", "_EmissiveColorMap");
SetFloat("_MaterialID", 4f);
RenameTexture("_DetailNormalMap", "_DetailMap");
//RenameFloat("_SpecColor", ...);
RenameColor("_SpecColor", "_SpecularColor");
RenameTexture("_SpecGlossMap", "_SpecularColorMap");
//@TODO: Seb. Why do we multiply color by intensity
// in shader when we can just store a color?

public override void Convert(Material srcMaterial, Material dstMaterial)
{
dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
if ( srcMaterial.GetTexture("_MetallicGlossMap") != null ||
srcMaterial.GetTexture("_OcclusionMap") != null ||
srcMaterial.GetTexture("_DetailMask") != null)
{
Texture2D maskMap;
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", 1), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, (srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)?"_SpecGlossMap": "_MainTex", 2), 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
maskMapPath = maskMapPath.Remove(maskMapPath.Length-4) + "_MaskMap.png";
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
}
if ( srcMaterial.GetTexture("_DetailAlbedoMap") != null ||
srcMaterial.GetTexture("_DetailNormalMap") != null
)
{
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.midGrey, 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
detailMapPath = detailMapPath.Remove(detailMapPath.Length-4) + "_DetailMap.png";
detailMap = detailCombiner.Combine( detailMapPath );
Debug.Log("Coucou");
dstMaterial.SetTexture("_DetailMap", detailMap);
}
// Blend Mode
int previousBlendMode = srcMaterial.GetInt("_Mode");
switch (previousBlendMode)
{
case 0: // Opaque
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
case 1: // Cutout
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 1);
break;
case 2: // Fade -> Alpha
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
case 3: // Transparent -> alpha pre-multiply
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 4);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
}
HDEditorUtils.ResetMaterialKeywords(dstMaterial);
}
}
}

118
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs


RenameFloat("_Glossiness", "_Smoothness");
RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameColor("_EmissionColor", "_EmissiveColor");
RenameTexture("_EmissionMap", "_EmissiveColorMap");
RenameFloat("_UVSec", "_UVDetail");
SetFloat("_LinkDetailsWithBase", 0);
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);

// Metallic uses [Gamma] attribute in standard shader but not in Lit.
// @Seb: Should we convert?
RenameFloat("_Metallic", "_Metallic");
// RenameFloat("_Metallic", "_Metallic");
//@TODO: Seb. Why do we multiply color by intensity
// in shader when we can just store a color?

//@TODO: Find a good way of setting up keywords etc from properties.
// Code should be shared with material UI code.
//*
Texture2D maskMap;
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", 1), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, (srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)?"_MetallicGlossMap": "_MainTex", 2), 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial).Replace(".mat", "_MaskMap.exr");
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
// */
if ( srcMaterial.GetTexture("_MetallicGlossMap") != null ||
srcMaterial.GetTexture("_OcclusionMap") != null ||
srcMaterial.GetTexture("_DetailMask") != null)
{
Texture2D maskMap;
// Get the Smoothness value that will be passed to the map.
string smoothnessTextureChannel = ( srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)?"_MetallicGlossMap" : "_MainTex";
Texture2D smoothnessSource = (Texture2D) srcMaterial.GetTexture( smoothnessTextureChannel );
if (smoothnessSource == null || !TextureCombiner.TextureHasAlpha(smoothnessSource))
{
smoothnessSource = TextureCombiner.TextureFromColor(Color.white * srcMaterial.GetFloat("_Glossiness"));
}
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", 0), 4,
smoothnessSource, 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
maskMapPath = maskMapPath.Remove(maskMapPath.Length-4) + "_MaskMap.png";
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
}
//*
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.midGrey, 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial).Replace(".mat", "_DetailMap.exr");
detailMap = detailCombiner.Combine( detailMapPath );
Debug.Log("Coucou");
dstMaterial.SetTexture("_DetailMap", detailMap);
//*/
dstMaterial.SetFloat("_AORemapMin", 1f - srcMaterial.GetFloat("_OcclusionStrength"));
if ( srcMaterial.GetTexture("_DetailAlbedoMap") != null ||
srcMaterial.GetTexture("_DetailNormalMap") != null
)
{
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.midGrey, 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
detailMapPath = detailMapPath.Remove(detailMapPath.Length-4) + "_DetailMap.png";
detailMap = detailCombiner.Combine( detailMapPath );
dstMaterial.SetTexture("_DetailMap", detailMap);
}
// Blend Mode
int previousBlendMode = srcMaterial.GetInt("_Mode");
switch (previousBlendMode)
{
case 0: // Opaque
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
case 1: // Cutout
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 1);
break;
case 2: // Fade -> Alpha
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
case 3: // Transparent -> alpha pre-multiply
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 4);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
break;
}
// Emission: Convert the HDR emissive color to ldr color + intensity
Color hdrEmission = srcMaterial.GetColor("_EmissionColor");
float intensity = Mathf.Max(hdrEmission.r, Mathf.Max(hdrEmission.g, hdrEmission.b));
if (intensity > 1f)
{
hdrEmission.r /= intensity;
hdrEmission.g /= intensity;
hdrEmission.b /= intensity;
}
else
intensity = 1f;
dstMaterial.SetColor("_EmissiveColor", hdrEmission);
dstMaterial.SetFloat("_EmissiveIntensity", intensity);
HDEditorUtils.ResetMaterialKeywords(dstMaterial);
}
正在加载...
取消
保存