浏览代码

HDRenderLoop LayeredLit: Check consistency between layers correctly (input options and maps) and updated keywords accordingly

/main
Julien Ignace 8 年前
当前提交
d7662a20
共有 6 个文件被更改,包括 263 次插入65 次删除
  1. 213
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLitUI.cs
  2. 88
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LitUI.cs
  3. 2
      Assets/TestScenes/HDTest/EnlightenTest.unity
  4. 17
      Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Layered.mat
  5. 4
      Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Layered.mat.meta
  6. 4
      Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Red.mat

213
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLitUI.cs


materialImporter.userData = JsonUtility.ToJson(layersGUID);
}
bool CheckInputOptionConsistency(string optionName, string[] shortNames, ref string outValueNames)
{
bool result = true;
outValueNames = "";
for(int i = 0 ; i < m_MaterialLayers.Length ; ++i)
{
Material layer = m_MaterialLayers[i];
if (layer != null)
{
int currentValue = (int)layer.GetFloat(optionName); // All options are in fact enums
Debug.Assert(currentValue < shortNames.Length);
outValueNames += shortNames[currentValue] + " ";
for(int j = i + 1 ; j < m_MaterialLayers.Length ; ++j)
{
Material otherLayer = m_MaterialLayers[j];
if(otherLayer != null)
{
if(currentValue != (int)otherLayer.GetFloat(optionName))
{
result = false;
}
}
}
}
else
{
outValueNames += "X ";
}
}
return result;
}
bool CheckInputMapConsistency(string mapName, ref string outValueNames)
{
bool result = true;
outValueNames = "";
for (int i = 0; i < m_MaterialLayers.Length; ++i)
{
Material layer = m_MaterialLayers[i];
if (layer != null)
{
bool currentValue = layer.GetTexture(mapName) != null;
outValueNames += (currentValue ? "Y" : "N") + " ";
for (int j = i + 1; j < m_MaterialLayers.Length; ++j)
{
Material otherLayer = m_MaterialLayers[j];
if (otherLayer != null)
{
bool otherValue = otherLayer.GetTexture(mapName) != null;
if (currentValue != otherValue)
{
result = false;
}
}
}
}
else
{
outValueNames += "N ";
}
}
return result;
}
void CheckLayerConsistency()
{
string optionValueNames = "";
// We need to check consistency between all layers.
// Each input options and each input maps might can result in different #defines in the shader so all of them need to be consistent
// otherwise the result will be undetermined
// Input options consistency
string[] smoothnessSourceShortNames = { "Mask", "Albedo" };
string[] emissiveModeShortNames = { "Color", "Mask" };
string[] normalMapShortNames = { "Tan", "Obj" };
string[] heightMapShortNames = { "Parallax", "Disp" };
string warningInputOptions = "";
if (!CheckInputOptionConsistency(kSmoothnessTextureChannelProp, smoothnessSourceShortNames, ref optionValueNames))
{
warningInputOptions += "Smoothness Source: " + optionValueNames + "\n";
}
if (!CheckInputOptionConsistency(kEmissiveColorMode, emissiveModeShortNames, ref optionValueNames))
{
warningInputOptions += "Emissive Mode: " + optionValueNames + "\n";
}
if (!CheckInputOptionConsistency(kNormalMapSpace, normalMapShortNames, ref optionValueNames))
{
warningInputOptions += "Normal Map Space: " + optionValueNames + "\n";
}
if (!CheckInputOptionConsistency(kHeightMapMode, heightMapShortNames, ref optionValueNames))
{
warningInputOptions += "Height Map Mode: " + optionValueNames + "\n";
}
if (warningInputOptions != string.Empty)
{
warningInputOptions = "Input Option Consistency Error:\n" + warningInputOptions;
}
// Check input maps consistency
string warningInputMaps = "";
if (!CheckInputMapConsistency(kNormalMap, ref optionValueNames))
{
warningInputMaps += "Normal Map: " + optionValueNames + "\n";
}
if (!CheckInputMapConsistency(kMaskMap, ref optionValueNames))
{
warningInputMaps += "Mask Map: " + optionValueNames + "\n";
}
if (!CheckInputMapConsistency(kspecularOcclusionMap, ref optionValueNames))
{
warningInputMaps += "Specular Occlusion Map: " + optionValueNames + "\n";
}
if (!CheckInputMapConsistency(kEmissiveColorMap, ref optionValueNames))
{
warningInputMaps += "Emissive Color Map: " + optionValueNames + "\n";
}
if (!CheckInputMapConsistency(kHeightMap, ref optionValueNames))
{
warningInputMaps += "Height Map: " + optionValueNames + "\n";
}
if (warningInputMaps != string.Empty)
{
warningInputMaps = "Input Maps Consistency Error:\n" + warningInputMaps;
if (warningInputOptions != string.Empty)
warningInputMaps = "\n" + warningInputMaps;
}
string warning = warningInputOptions + warningInputMaps;
if (warning != string.Empty)
{
EditorGUILayout.HelpBox(warning, MessageType.Error);
}
}
void SynchronizeInputOptions()
{
Material material = m_MaterialEditor.target as Material;
// We synchronize input options with the firsts non null Layer (all layers should have consistent options)
Material firstLayer = null;
int i = 0;
while (i < m_MaterialLayers.Length && !(firstLayer = m_MaterialLayers[i])) ++i;
if(firstLayer != null)
{
material.SetFloat(kSmoothnessTextureChannelProp, firstLayer.GetFloat(kSmoothnessTextureChannelProp));
material.SetFloat(kEmissiveColorMode, firstLayer.GetFloat(kEmissiveColorMode));
material.SetFloat(kNormalMapSpace, firstLayer.GetFloat(kNormalMapSpace));
material.SetFloat(kHeightMapMode, firstLayer.GetFloat(kHeightMapMode));
}
}
bool saveMaterialLayers = false;
bool layerChanged = false;
EditorGUI.indentLevel++;
GUILayout.Label(styles.layers, EditorStyles.boldLabel);

{
Undo.RecordObject(materialImporter, "Change layer material");
SynchronizeLayerProperties(i);
saveMaterialLayers = true;
layerChanged = true;
layerChanged = true;
if (saveMaterialLayers)
EditorGUI.indentLevel--;
return layerChanged;
}
protected override void SetupKeywordsForInputMaps(Material material)
{
// Find first non null layer
int i = 0;
while (i < m_MaterialLayers.Length && (m_MaterialLayers[i] == null)) ++i;
if(i < m_MaterialLayers.Length)
SaveMaterialLayers(materialImporter);
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap + i));
SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap + i));
SetKeyword(material, "_SPECULAROCCLUSIONMAP", material.GetTexture(kspecularOcclusionMap + i));
SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap + i));
SetKeyword(material, "_HEIGHTMAP", material.GetTexture(kHeightMap + i));
EditorGUI.indentLevel--;
return saveMaterialLayers;
}
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)

m_MaterialEditor.serializedObject.Update();
Material material = materialEditor.target as Material;
Material material = m_MaterialEditor.target as Material;
bool optionsChanged = false;
EditorGUILayout.Space();
ShaderInputOptionsGUI();
foreach (var obj in m_MaterialEditor.targets)
MaterialChanged((Material)obj);
optionsChanged = true;
bool saveMaterialLayers = LayersGUI(materialImporter);
bool layerChanged = LayersGUI(materialImporter);
CheckLayerConsistency();
if (layerChanged || optionsChanged)
{
SynchronizeInputOptions();
foreach (var obj in m_MaterialEditor.targets)
SetupMaterial((Material)obj);
SaveMaterialLayers(materialImporter);
}
if (saveMaterialLayers)
if (layerChanged)
{
materialImporter.SaveAndReimport();
}

88
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LitUI.cs


// MaterialProperty subSurfaceRadius = null;
// MaterialProperty subSurfaceRadiusMap = null;
protected const string kSurfaceType = "_SurfaceType";
protected const string kBlendMode = "_BlendMode";
protected const string kAlphaCutoff = "_AlphaCutoff";
protected const string kAlphaCutoffEnabled = "_AlphaCutoffEnable";
protected const string kDoubleSidedMode = "_DoubleSidedMode";
protected const string kSmoothnessTextureChannelProp = "_SmoothnessTextureChannel";
protected const string kEmissiveColorMode = "_EmissiveColorMode";
protected const string kNormalMapSpace = "_NormalMapSpace";
protected const string kHeightMapMode = "_HeightMapMode";
protected const string kNormalMap = "_NormalMap";
protected const string kMaskMap = "_MaskMap";
protected const string kspecularOcclusionMap = "_SpecularOcclusionMap";
protected const string kEmissiveColorMap = "_EmissiveColorMap";
protected const string kHeightMap = "_HeightMap";
surfaceType = FindProperty("_SurfaceType", props);
blendMode = FindProperty("_BlendMode", props);
alphaCutoff = FindProperty("_AlphaCutoff", props);
alphaCutoffEnable = FindProperty("_AlphaCutoffEnable", props);
doubleSidedMode = FindProperty("_DoubleSidedMode", props);
smoothnessMapChannel = FindProperty("_SmoothnessTextureChannel", props);
emissiveColorMode = FindProperty("_EmissiveColorMode", props);
normalMapSpace = FindProperty("_NormalMapSpace", props);
heightMapMode = FindProperty("_HeightMapMode", props);
surfaceType = FindProperty(kSurfaceType, props);
blendMode = FindProperty(kBlendMode, props);
alphaCutoff = FindProperty(kAlphaCutoff, props);
alphaCutoffEnable = FindProperty(kAlphaCutoffEnabled, props);
doubleSidedMode = FindProperty(kDoubleSidedMode, props);
smoothnessMapChannel = FindProperty(kSmoothnessTextureChannelProp, props);
emissiveColorMode = FindProperty(kEmissiveColorMode, props);
normalMapSpace = FindProperty(kNormalMapSpace, props);
heightMapMode = FindProperty(kHeightMapMode, props);
}
public void FindInputProperties(MaterialProperty[] props)

metalic = FindProperty("_Metalic", props);
smoothness = FindProperty("_Smoothness", props);
maskMap = FindProperty("_MaskMap", props);
specularOcclusionMap = FindProperty("_SpecularOcclusionMap", props);
normalMap = FindProperty("_NormalMap", props);
heightMap = FindProperty("_HeightMap", props);
maskMap = FindProperty(kMaskMap, props);
specularOcclusionMap = FindProperty(kspecularOcclusionMap, props);
normalMap = FindProperty(kNormalMap, props);
heightMap = FindProperty(kHeightMap, props);
emissiveColorMap = FindProperty("_EmissiveColorMap", props);
emissiveColorMap = FindProperty(kEmissiveColorMap, props);
emissiveIntensity = FindProperty("_EmissiveIntensity", props);
}

if (EditorGUI.EndChangeCheck())
{
foreach (var obj in m_MaterialEditor.targets)
MaterialChanged((Material)obj);
SetupMaterial((Material)obj);
}
}

EditorGUI.showMixedValue = false;
}
protected virtual void SetupKeywordsForInputMaps(Material material)
{
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap));
SetKeyword(material, "_SPECULAROCCLUSIONMAP", material.GetTexture(kspecularOcclusionMap));
SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap));
SetKeyword(material, "_HEIGHTMAP", material.GetTexture(kHeightMap));
}
bool alphaTestEnable = material.GetFloat("_AlphaCutoffEnable") == 1.0;
SurfaceType surfaceType = (SurfaceType)material.GetFloat("_SurfaceType");
BlendMode blendMode = (BlendMode)material.GetFloat("_BlendMode");
DoubleSidedMode doubleSidedMode = (DoubleSidedMode)material.GetFloat("_DoubleSidedMode");
bool alphaTestEnable = material.GetFloat(kAlphaCutoffEnabled) == 1.0;
SurfaceType surfaceType = (SurfaceType)material.GetFloat(kSurfaceType);
BlendMode blendMode = (BlendMode)material.GetFloat(kBlendMode);
DoubleSidedMode doubleSidedMode = (DoubleSidedMode)material.GetFloat(kDoubleSidedMode);
if (surfaceType == SurfaceType.Opaque)
{

}
SetKeyword(material, "_ALPHATEST_ON", alphaTestEnable);
SetKeyword(material, "_NORMALMAP", material.GetTexture("_NormalMap"));
SetKeyword(material, "_NORMALMAP_TANGENT_SPACE", (NormalMapSpace)material.GetFloat("_NormalMapSpace") == NormalMapSpace.TangentSpace);
SetKeyword(material, "_MASKMAP", material.GetTexture("_MaskMap"));
SetKeyword(material, "_SPECULAROCCLUSIONMAP", material.GetTexture("_SpecularOcclusionMap"));
SetKeyword(material, "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A", ((SmoothnessMapChannel)material.GetFloat("_SmoothnessTextureChannel")) == SmoothnessMapChannel.AlbedoAlpha);
SetKeyword(material, "_EMISSIVE_COLOR", ((EmissiveColorMode)material.GetFloat("_EmissiveColorMode")) == EmissiveColorMode.UseEmissiveColor);
SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture("_EmissiveColorMap"));
SetKeyword(material, "_HEIGHTMAP", material.GetTexture("_HeightMap"));
SetKeyword(material, "_HEIGHTMAP_AS_DISPLACEMENT", (HeightmapMode)material.GetFloat("_HeightMapMode") == HeightmapMode.Displacement);
SetKeyword(material, "_NORMALMAP_TANGENT_SPACE", (NormalMapSpace)material.GetFloat(kNormalMapSpace) == NormalMapSpace.TangentSpace);
SetKeyword(material, "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A", ((SmoothnessMapChannel)material.GetFloat(kSmoothnessTextureChannelProp)) == SmoothnessMapChannel.AlbedoAlpha);
SetKeyword(material, "_EMISSIVE_COLOR", ((EmissiveColorMode)material.GetFloat(kEmissiveColorMode)) == EmissiveColorMode.UseEmissiveColor);
SetKeyword(material, "_HEIGHTMAP_AS_DISPLACEMENT", (HeightmapMode)material.GetFloat(kHeightMapMode) == HeightmapMode.Displacement);
SetupKeywordsForInputMaps(material);
/*
// Setup lightmap emissive flags

return true;
}
protected void MaterialChanged(Material material)
{
SetupMaterial(material);
}
static void SetKeyword(Material m, string keyword, bool state)
protected void SetKeyword(Material m, string keyword, bool state)
{
if (state)
m.EnableKeyword (keyword);

2
Assets/TestScenes/HDTest/EnlightenTest.unity


m_Name:
m_EditorClassIdentifier:
m_RenderLoop: {fileID: 11400000, guid: 2400b74f5ce370c4481e5dc417d03703, type: 2}
m_AssetVersion: 1
m_AssetVersion: 0
--- !u!1 &1893534339
GameObject:
m_ObjectHideFlags: 0

17
Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Layered.mat


m_Name: Layered
m_Shader: {fileID: 4800000, guid: 81d02e8644315b742b154842a3a2f98c, type: 3}
m_ShaderKeywords: _ALPHACUTOFFENABLE_OFF _DISTORTIONDEPTHTEST_OFF _DISTORTIONONLY_OFF
_EMISSION
_EMISSION _NORMALMAP_TANGENT_SPACE
m_CustomRenderQueue: -1
stringTagMap: {}
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
m_SavedProperties:
serializedVersion: 2
m_TexEnvs:

second: 0
- first:
name: _DstBlend
second: 0
second: 10
- first:
name: _EmissiveColorMode
second: 1

second: 1
- first:
name: _SrcBlend
second: 1
second: 5
second: 0
second: 1
second: 1
second: 0
m_Colors:
- first:
name: _BaseColor0

second: {r: 0.31185117, g: 0.49926907, b: 0.75735295, a: 0.672}
second: {r: 0, g: 1, b: 0, a: 1}
- first:
name: _BaseColor2
second: {r: 1, g: 0, b: 0, a: 1}

4
Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Layered.mat.meta


fileFormatVersion: 2
guid: 6e7fa39a7d1b15c4184c9f51d86eba22
timeCreated: 1476108738
timeCreated: 1476116432
userData: '{"GUIDArray":["c569253e641dc934db7c3595b31890da","6816da50441b49245843695729d8968c","","6816da50441b49245843695729d8968c"]}'
userData: '{"GUIDArray":["c569253e641dc934db7c3595b31890da","2a49749e551c49a44acc0033cbbf7f22","","6816da50441b49245843695729d8968c"]}'
assetBundleName:
assetBundleVariant:

4
Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/Red.mat


second: 0.02
- first:
name: _Smoothness
second: 0.5
second: 0.486
- first:
name: _SmoothnessTextureChannel
second: 0

m_Colors:
- first:
name: _BaseColor
second: {r: 1, g: 0, b: 0, a: 1}
second: {r: 1, g: 0, b: 0, a: 0.447}
- first:
name: _Color
second: {r: 1, g: 1, b: 1, a: 1}

正在加载...
取消
保存