浏览代码

HDRenderLoop LayeredLit : Added the possibility to choose the number of layers.

/main
Julien Ignace 8 年前
当前提交
d097419c
共有 3 个文件被更改,包括 109 次插入23 次删除
  1. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLit.shader
  2. 56
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLitTemplate.hlsl
  3. 73
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLitUI.cs

3
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLit.shader


// Material Id
[HideInInspector] _MaterialId("_MaterialId", FLoat) = 0
[HideInInspector] _LayerCount("__layerCount", Float) = 2.0
[Enum(Mask Alpha, 0, BaseColor Alpha, 1)] _SmoothnessTextureChannel("Smoothness texture channel", Float) = 1
[Enum(Use Emissive Color, 0, Use Emissive Mask, 1)] _EmissiveColorMode("Emissive color mode", Float) = 1
[Enum(None, 0, DoubleSided, 1, DoubleSidedLigthingFlip, 2, DoubleSidedLigthingMirror, 3)] _DoubleSidedMode("Double sided mode", Float) = 0

#pragma shader_feature _HEIGHTMAP
#pragma shader_feature _HEIGHTMAP_AS_DISPLACEMENT
#pragma shader_feature _LAYERMASKMAP
#pragma shader_feature _ _LAYEREDLIT_3_LAYERS _LAYEREDLIT_4_LAYERS
#include "LayeredLitTemplate.hlsl"

56
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLitTemplate.hlsl


#define PROP_BLEND_COLOR(name, mask) name = BlendLayeredColor(name##0, name##1, name##2, name##3, mask);
#define PROP_BLEND_SCALAR(name, mask) name = BlendLayeredScalar(name##0, name##1, name##2, name##3, mask);
#define _MAX_LAYER 4
#if defined(_LAYEREDLIT_4_LAYERS)
# define _LAYER_COUNT 4
#elif defined(_LAYEREDLIT_3_LAYERS)
# define _LAYER_COUNT 3
#else
# define _LAYER_COUNT 2
#endif
//-------------------------------------------------------------------------------------
// variable declaration
//-------------------------------------------------------------------------------------

float3 BlendLayeredColor(float3 rgb0, float3 rgb1, float3 rgb2, float3 rgb3, float weight[4])
{
return rgb0 * weight[0] + rgb1 * weight[1] + rgb2 * weight[2] + rgb3 * weight[3];
float3 result = float3(0.0, 0.0, 0.0);
result = rgb0 * weight[0] + rgb1 * weight[1];
#if _LAYER_COUNT >= 3
result += (rgb2 * weight[2]);
#endif
#if _LAYER_COUNT >= 4
result += rgb3 * weight[3];
#endif
return result;
float3 result = float3(0.0, 0.0, 0.0);
return normal0 * weight[0] + normal1 * weight[1] + normal2 * weight[2] + normal3 * weight[3];
result = normal0 * weight[0] + normal1 * weight[1];
#if _LAYER_COUNT >= 3
result += normal2 * weight[2];
#endif
#if _LAYER_COUNT >= 4
result += normal3 * weight[3];
#endif
return result;
return x0 * weight[0] + x1 * weight[1] + x2 * weight[2] + x3 * weight[3];
float result = 0.0;
result = x0 * weight[0] + x1 * weight[1];
#if _LAYER_COUNT >= 3
result += x2 * weight[2];
#endif
#if _LAYER_COUNT >= 4
result += x3 * weight[3];
#endif
return result;
#define MAX_LAYER 4
void ComputeMaskWeights(float4 inputMasks, out float outWeights[MAX_LAYER])
void ComputeMaskWeights(float4 inputMasks, out float outWeights[_MAX_LAYER])
float masks[MAX_LAYER];
float masks[_MAX_LAYER];
masks[0] = inputMasks.r;
masks[1] = inputMasks.g;
masks[2] = inputMasks.b;

float left = 1.0f;
// ATTRIBUTE_UNROLL
for (int i = MAX_LAYER - 1; i > 0; --i)
for (int i = _LAYER_COUNT - 1; i > 0; --i)
{
outWeights[i] = masks[i] * left;
left -= outWeights[i];

maskValues *= maskMap;
#endif
float weights[MAX_LAYER];
float weights[_MAX_LAYER];
ComputeMaskWeights(maskValues, weights);
PROP_DECL(float3, baseColor);

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


};
public readonly GUIContent syncButton = new GUIContent("Sync", "Re-synchronize this layer's properties with the referenced Material");
public readonly GUIContent layers = new GUIContent("Layers");
public readonly GUIContent layerMapMask = new GUIContent("LayerMask", "Layer mask (multiplied by vertex color)");
public readonly GUIContent layerMapMask = new GUIContent("Layer Mask", "Layer mask (multiplied by vertex color)");
public readonly GUIContent layerCount = new GUIContent("Layer Count", "Number of layers.");
}
static Styles s_Styles = null;

public string[] GUIDArray;
}
private int kSyncButtonWidth = 58;
private const int kMaxLayerCount = 4;
private const int kSyncButtonWidth = 58;
private string kLayerCount = "_LayerCount";
private Material[] m_MaterialLayers = new Material[4];
MaterialProperty layerMaskMap = null;
private Material[] m_MaterialLayers = new Material[kMaxLayerCount];
MaterialProperty layerCountProperty = null;
MaterialProperty layerMaskMapProperty = null;
int layerCount
{
set { layerCountProperty.floatValue = (float)value; }
get { return (int)layerCountProperty.floatValue; }
}
void SynchronizeLayerProperties(int layerIndex)
{

{
bool result = true;
outValueNames = "";
for(int i = 0 ; i < m_MaterialLayers.Length ; ++i)
for(int i = 0 ; i < layerCount ; ++i)
{
Material layer = m_MaterialLayers[i];
if (layer != null)

outValueNames += shortNames[currentValue] + " ";
for(int j = i + 1 ; j < m_MaterialLayers.Length ; ++j)
for (int j = i + 1; j < layerCount; ++j)
{
Material otherLayer = m_MaterialLayers[j];
if(otherLayer != null)

{
bool result = true;
outValueNames = "";
for (int i = 0; i < m_MaterialLayers.Length; ++i)
for (int i = 0; i < layerCount; ++i)
{
Material layer = m_MaterialLayers[i];
if (layer != null)

for (int j = i + 1; j < m_MaterialLayers.Length; ++j)
for (int j = i + 1; j < layerCount; ++j)
{
Material otherLayer = m_MaterialLayers[j];
if (otherLayer != null)

// 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;
while (i < layerCount && !(firstLayer = m_MaterialLayers[i])) ++i;
if(firstLayer != null)
{

bool LayersGUI(AssetImporter materialImporter)
{
Material material = m_MaterialEditor.target as Material;
m_MaterialEditor.TexturePropertySingleLine(styles.layerMapMask, layerMaskMap);
EditorGUI.BeginChangeCheck();
int newLayerCount = EditorGUILayout.IntSlider(styles.layerCount, (int)layerCountProperty.floatValue, 2, 4 );
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(material, "Change layer count");
layerCountProperty.floatValue = (float)newLayerCount;
layerChanged = true;
}
m_MaterialEditor.TexturePropertySingleLine(styles.layerMapMask, layerMaskMapProperty);
for (int i = 0; i < m_MaterialLayers.Length; i++)
for (int i = 0; i < layerCount; i++)
{
EditorGUILayout.BeginHorizontal();
{

{
// Find first non null layer
int i = 0;
while (i < m_MaterialLayers.Length && (m_MaterialLayers[i] == null)) ++i;
while (i < layerCount && (m_MaterialLayers[i] == null)) ++i;
if(i < m_MaterialLayers.Length)
if (i < layerCount)
{
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap + i));
SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap + i));

SetKeyword(material, "_LAYERMASKMAP", material.GetTexture(kLayerMaskMap));
}
void SetupMaterialForLayers(Material material)
{
if(layerCount == 4)
{
SetKeyword(material, "_LAYEREDLIT_4_LAYERS", true);
SetKeyword(material, "_LAYEREDLIT_3_LAYERS", false);
}
else if (layerCount == 3)
{
SetKeyword(material, "_LAYEREDLIT_4_LAYERS", false);
SetKeyword(material, "_LAYEREDLIT_3_LAYERS", true);
}
else
{
SetKeyword(material, "_LAYEREDLIT_4_LAYERS", false);
SetKeyword(material, "_LAYEREDLIT_3_LAYERS", false);
}
}
layerMaskMap = FindProperty(kLayerMaskMap, props);
layerMaskMapProperty = FindProperty(kLayerMaskMap, props);
layerCountProperty = FindProperty(kLayerCount, props);
m_MaterialEditor = materialEditor;

SynchronizeInputOptions();
foreach (var obj in m_MaterialEditor.targets)
{
SetupMaterialForLayers((Material)obj);
}
SaveMaterialLayers(materialImporter);
}

正在加载...
取消
保存