浏览代码

Unified all variants of standard shader in one converter.

/main
Remy 7 年前
当前提交
ab4675c0
共有 7 个文件被更改,包括 298 次插入45 次删除
  1. 43
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs
  2. 35
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader
  3. 52
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs
  4. 16
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs
  5. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/UpgradeStandardShaderMaterials.cs
  6. 181
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs
  7. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs.meta

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


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
});
Texture2D tex = new Texture2D(1,1, TextureFormat.ARGB32, false, true);
tex.SetPixel(0,0,color);
tex.Apply();
singleColorTextures[color] = tex;

}
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)

private Texture m_aSource;
// Chanels are : r=0, g=1, b=2, a=3, greyscale from rgb = 4
// If negative, the chanel is inverted
// Chanels remaping
private Vector4[] m_remapings = new Vector4[]{
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f)
};
private bool m_bilinearFilter;
private Dictionary<Texture, Texture> m_RawTextures;

m_bilinearFilter = bilinearFilter;
}
public void SetRemapping( int channel, float min, float max)
{
if (channel > 3 || channel < 0) return;
m_remapings[channel].x = min;
m_remapings[channel].y = max;
}
public Texture2D Combine( string savePath )
{
int xMin = int.MaxValue;

combinerMaterial.SetFloat("_GChannel", m_gChanel);
combinerMaterial.SetFloat("_BChannel", m_bChanel);
combinerMaterial.SetFloat("_AChannel", m_aChanel);
combinerMaterial.SetVector("_RRemap", m_remapings[0]);
combinerMaterial.SetVector("_GRemap", m_remapings[1]);
combinerMaterial.SetVector("_BRemap", m_remapings[2]);
combinerMaterial.SetVector("_ARemap", m_remapings[3]);
RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.sRGB);

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


Properties
{
// Chanels are : r=0, g=1, b=2, a=3, greyscale from rgb = 4
// If the chanel value is negative, we invert the value
_RRemap ("R Remap", Vector) = (0, 1, 0, 0)
_GRemap ("G Remap", Vector) = (0, 1, 0, 0)
_BRemap ("B Remap", Vector) = (0, 1, 0, 0)
_ARemap ("A Remap", Vector) = (0, 1, 0, 0)
}
SubShader
{

sampler2D _RSource, _GSource, _BSource, _ASource;
float _RChannel, _GChannel, _BChannel, _AChannel;
float4 _RRemap, _GRemap, _BRemap, _ARemap;
v2f vert (appdata v)
{

return o;
}
float PlotSourcetoChanel(float4 source, float param)
float PlotSourcetoChanel(float4 source, float param, float2 remap)
if (param < 0 )
{
param = -param;
source = float4(1,1,1,1) - source;
}
float o;
return source.r * 0.3 + source.g * 0.59 + source.b * 0.11; // Photoshop desaturation : G*.59+R*.3+B*.11
o = source.r * 0.3 + source.g * 0.59 + source.b * 0.11; // Photoshop desaturation : G*.59+R*.3+B*.11
return source[param];
o = source[param];
return o * ( remap.y - remap.x) + remap.x ;
}
float PlotSourcetoChanel(float4 source, float param)
{
return PlotSourcetoChanel(source, param, float2(0,1) )
}
float4 frag (v2f i) : SV_Target

col.r = PlotSourcetoChanel( tex2D(_RSource, i.uv), _RChannel );
col.g = PlotSourcetoChanel( tex2D(_GSource, i.uv), _GChannel );
col.b = PlotSourcetoChanel( tex2D(_BSource, i.uv), _BChannel );
col.a = PlotSourcetoChanel( tex2D(_ASource, i.uv), _AChannel );
col.r = PlotSourcetoChanel( tex2D(_RSource, i.uv), _RChannel, _RRemap );
col.g = PlotSourcetoChanel( tex2D(_GSource, i.uv), _GChannel, _GRemap );
col.b = PlotSourcetoChanel( tex2D(_BSource, i.uv), _BChannel, _BRemap );
col.a = PlotSourcetoChanel( tex2D(_ASource, i.uv), _AChannel, _ARemap );
return col;
}

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


RenameTexture("_MainTex", "_BaseColorMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_Glossiness", "_Smoothness");
RenameFloat("_GlossMapScale", "SmoothnessRemapMax");
RenameTexture("_DetailAlbedoMap", "DetailMap");
RenameFloat("_UVSec", "_UVDetail");
SetFloat("_LinkDetailsWithBase", 0);
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);

//@TODO: Find a good way of setting up keywords etc from properties.
// Code should be shared with material UI code.
if ( srcMaterial.GetTexture("_MetallicGlossMap") != null ||
srcMaterial.GetTexture("_OcclusionMap") != null ||
if ( srcMaterial.GetTexture("_OcclusionMap") != null ||
// if there is a specular map, change the specular color to white
if (srcMaterial.GetTexture("_SpecGlossMap") != null ) dstMaterial.SetColor("_SpecularColor", Color.white);
// Get the Smoothness value that will be passed to the map.
string smoothnessTextureChannel = ( srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)?"_SpecGlossMap" : "_MainTex";
Texture2D smoothnessSource = (Texture2D) srcMaterial.GetTexture( smoothnessTextureChannel );
if (smoothnessSource == null || !TextureCombiner.TextureHasAlpha(smoothnessSource))
{
smoothnessSource = TextureCombiner.TextureFromColor(Color.white * srcMaterial.GetFloat("_Glossiness"));
}
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
Texture2D.blackTexture, 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white), 4,
smoothnessSource, 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
maskMapPath = maskMapPath.Remove(maskMapPath.Length-4) + "_MaskMap.png";

dstMaterial.SetFloat("_AORemapMin", 1f - srcMaterial.GetFloat("_OcclusionStrength"));
if ( srcMaterial.GetTexture("_DetailAlbedoMap") != null ||
srcMaterial.GetTexture("_DetailNormalMap") != null

TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", Color.grey), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 0
Debug.Log("Coucou");
dstMaterial.SetTexture("_DetailMap", detailMap);
}

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);
}

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


RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameTexture("_EmissionMap", "_EmissiveColorMap");
RenameTexture("_DetailAlbedoMap", "DetailMap");
RenameFloat("_UVSec", "_UVDetail");
SetFloat("_LinkDetailsWithBase", 0);
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");

SetFloat("_MaterialID", 1f);
RenameTexture("_DetailAlbedoMap", "_DetailMap");
// Moved to convert function

}
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", Color.white), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white), 4,
smoothnessSource, 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);

{
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", Color.grey), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 0
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
detailMapPath = detailMapPath.Remove(detailMapPath.Length-4) + "_DetailMap.png";

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/UpgradeStandardShaderMaterials.cs


static List<MaterialUpgrader> GetHDUpgraders()
{
var upgraders = new List<MaterialUpgrader>();
upgraders.Add(new StandardToHDLitMaterialUpgrader("Standard", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass));
upgraders.Add(new StandardSpecularToHDLitMaterialUpgrader("Standard (Specular setup)", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard", "HDRenderPipeline/Lit"));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard (Specular setup)", "HDRenderPipeline/Lit"));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard (Roughness setup)", "HDRenderPipeline/Lit"));
return upgraders;
}

181
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs


using UnityEngine;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class StandardsToHDLitMaterialUpgrader : MaterialUpgrader
{
static readonly string Standard = "Standard";
static readonly string Standard_Spec = "Standard (Specular setup)";
static readonly string Standard_Rough = "Standard (Roughness setup)";
public StandardsToHDLitMaterialUpgrader(string sourceShaderName, string destShaderName, MaterialFinalizer finalizer = null)
{
RenameShader(sourceShaderName, destShaderName, finalizer);
RenameTexture("_MainTex", "_BaseColorMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_Glossiness", "_Smoothness");
RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameTexture("_EmissionMap", "_EmissiveColorMap");
RenameTexture("_DetailAlbedoMap", "_DetailMap");
RenameFloat("_UVSec", "_UVDetail");
SetFloat("_LinkDetailsWithBase", 0);
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);
if (sourceShaderName == Standard)
{
SetFloat("_MaterialID", 1f);
}
if (sourceShaderName == Standard_Rough)
{
SetFloat("_MaterialID", 4f);
RenameColor("_SpecColor", "_SpecularColor");
RenameTexture("_SpecGlossMap", "_SpecularColorMap");
}
}
public override void Convert(Material srcMaterial, Material dstMaterial)
{
dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
base.Convert(srcMaterial, dstMaterial);
// ---------- Mask Map ----------
// Metallic
bool hasMetallic = false;
Texture metallicMap;
if ( (srcMaterial.shader.name == Standard) || (srcMaterial.shader.name == Standard_Rough) )
{
hasMetallic = srcMaterial.GetTexture("_MetallicGlossMap") != null;
if (hasMetallic) metallicMap = TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", Color.white);
}
else
metallicMap = Texture2D.blackTexture;
// Occlusion
bool hasOcclusion = srcMaterial.GetTexture("_OcclusionMap") != null;
Texture occlusionMap;
if (hasOcclusion) occlusionMap = TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white);
// Detail Mask
bool hasDetailMask = srcMaterial.GetTexture("_DetailMask") != null;
Texture detailMaskMap;
if (hasDetailMask) detailMaskMap = TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white);
// Build the mask map
if ( hasMetallic || hasOcclusion || hasDetailMask )
{
Texture2D maskMap;
// Get the Smoothness value that will be passed to the map.
Texture2D smoothnessSource = TextureCombiner.TextureFromColor(Color.grey);
string smoothnessTextureChannel = "_MainTex";
if ( (srcMaterial.shader.name != Standard_Rough) && ( srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0 ) ) // Standard Rough doesn't have the smoothness source selector
{
if (srcMaterial.shader.name == Standard) smoothnessTextureChannel = "_MetallicGlossMap";
if (srcMaterial.shader.name == Standard_Spec) smoothnessTextureChannel = "_SpecGlossMap";
}
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", Color.white), 4, // Metallic
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white), 4, // Occlusion
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white), 4, // Detail Mask
smoothnessSource, (srcMaterial.shader.name == Standard_Rough)?-4:3 // Smoothness
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
maskMapPath = maskMapPath.Remove(maskMapPath.Length-4) + "_MaskMap.png";
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
}
dstMaterial.SetFloat("_AORemapMin", 1f - srcMaterial.GetFloat("_OcclusionStrength"));
// Specular Setup Specific
if (srcMaterial.shader.name == Standard_Spec)
{
// if there is a specular map, change the specular color to white
if (srcMaterial.GetTexture("_SpecGlossMap") != null ) dstMaterial.SetColor("_SpecularColor", Color.white);
}
// ---------- Detail Map ----------
bool hasDetailAlbedo = srcMaterial.GetTexture("_DetailAlbedoMap") != null;
bool hasDetailNormal = srcMaterial.GetTexture("_DetailNormalMap") != null;
if ( hasDetailAlbedo || hasDetailNormal )
{
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", Color.grey), 4, // Albedo (overlay)
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 1, // Normal Y
TextureCombiner.midGrey, 1, // Smoothness
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 0 // Normal X
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
detailMapPath = detailMapPath.Remove(detailMapPath.Length-4) + "_DetailMap.png";
if (hasDetailAlbedo) detailCombiner.SetRemapping(0, 0.5f, 1f); // Remap albedo from [0;1] to [0.5;1] to compensate the change from "Lerp to white" to "Overlay" blend mode
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);
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs.meta


fileFormatVersion: 2
guid: 3892a7f1ec73e554fb1bd8be6a6d3400
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存