浏览代码

cleanup refactor

/main
Paul Melamed 6 年前
当前提交
abdc7b2f
共有 4 个文件被更改,包括 68 次插入86 次删除
  1. 46
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Decal/DecalUI.cs
  2. 11
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs
  3. 81
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  4. 16
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalSystem.cs

46
com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Decal/DecalUI.cs


}
protected string[] blendSourceNames = Enum.GetNames(typeof(BlendSource));
// relies on the order shader passes are declared in decal.shader
enum BlendMode
{
None = 0,
Metal = 1,
AO = 2,
Metal_AO = 3,
Smoothness = 4,
Metal_Smoothness = 5,
AO_Smoothness = 6,
Metal_AO_Smoothness = 7
}
[Flags] enum MaskBlendFlags
{
Metal = 1 << 0,
AO = 1 << 1,
Smoothness = 1 << 2,
}
protected string[] blendModeNames = Enum.GetNames(typeof(BlendMode));
protected MaterialProperty baseColorMap = new MaterialProperty();

// All Setup Keyword functions must be static. It allow to create script to automatically update the shaders with a script if code change
static public void SetupMaterialKeywordsAndPass(Material material)
{
MaskBlendFlags blendMode = (MaskBlendFlags)material.GetFloat(kMaskBlendMode);
Decal.MaskBlendFlags blendMode = (Decal.MaskBlendFlags)material.GetFloat(kMaskBlendMode);
CoreUtils.SetKeyword(material, "_ALBEDOCONTRIBUTION", material.GetFloat(kAlbedoMode) == 1.0f);
CoreUtils.SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));

material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecals3RTStr, true);
switch (blendMode)
{
case MaskBlendFlags.Metal:
case Decal.MaskBlendFlags.Metal:
case MaskBlendFlags.AO:
case Decal.MaskBlendFlags.AO:
case MaskBlendFlags.Metal | MaskBlendFlags.AO:
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.AO:
case MaskBlendFlags.Smoothness:
case Decal.MaskBlendFlags.Smoothness:
case MaskBlendFlags.Metal |MaskBlendFlags.Smoothness:
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.Smoothness:
case MaskBlendFlags.AO | MaskBlendFlags.Smoothness:
case Decal.MaskBlendFlags.AO | Decal.MaskBlendFlags.Smoothness:
case MaskBlendFlags.Metal | MaskBlendFlags.AO | MaskBlendFlags.Smoothness:
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.AO | Decal.MaskBlendFlags.Smoothness:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOSStr, true);
break;
}

float normalBlendSrcValue = normalBlendSrc.floatValue;
float maskBlendSrcValue = maskBlendSrc.floatValue;
float maskBlendModeValue = maskBlendMode.floatValue;
MaskBlendFlags maskBlendFlags = (MaskBlendFlags) maskBlendModeValue;
Decal.MaskBlendFlags maskBlendFlags = (Decal.MaskBlendFlags) maskBlendModeValue;
HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
bool perChannelMask = hdrp.renderPipelineSettings.decalSettings.perChannelMask;

EditorGUILayout.HelpBox("Individual mask map channel blending mode can be enabled/disabled in pipeline asset.\nEnabling this feature incurs a performance cost.", MessageType.Info);
if(perChannelMask)
{
maskBlendFlags = (MaskBlendFlags)EditorGUILayout.EnumFlagsField( "Mask blend mode", maskBlendFlags);
maskBlendFlags = (Decal.MaskBlendFlags)EditorGUILayout.EnumFlagsField( "Mask blend mode", maskBlendFlags);
maskBlendFlags = MaskBlendFlags.Smoothness; // can not have nothing, to achieve this effect remove the mask map from shader
if (maskBlendFlags == (MaskBlendFlags)(-1)) // everything
maskBlendFlags = MaskBlendFlags.Metal | MaskBlendFlags.AO | MaskBlendFlags.Smoothness;
maskBlendFlags = Decal.MaskBlendFlags.Smoothness; // can not have nothing, to achieve this effect remove the mask map from shader
if (maskBlendFlags == (Decal.MaskBlendFlags)(-1)) // everything
maskBlendFlags = Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.AO | Decal.MaskBlendFlags.Smoothness;
}
m_MaterialEditor.ShaderProperty(decalBlend, Styles.decalBlendText);
EditorGUI.indentLevel--;

11
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs


using System;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline

RTFormat = m_RTFormat;
sRGBFlags = m_sRGBFlags;
}
// relies on the order shader passes are declared in decal.shader
[Flags]
public enum MaskBlendFlags
{
Metal = 1 << 0,
AO = 1 << 1,
Smoothness = 1 << 2,
}
}
// normal to world only uses 3x3 for actual matrix so some data is packed in the unused space

81
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader


// c# code relies on the order in which the passes are declared, any change will need to be reflected in DecalUI.cs
// pass 0 is mesh 3RT mode
Pass
{
Name "DBufferMesh_3RT" // Name is not used
Tags{"LightMode" = "DBufferMesh_3RT"} // Smoothness
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 1 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
ColorMask BA 2 // smoothness/smoothness alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_MESH
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
// enum MaskBlendFlags
//{
// Metal = 1 << 0,

// Projectors
//
// 0 - Metal
// 1 - AO
// 2 - Metal + AO
// 3 - Smoothness also 3RT
// 4 - Metal + Smoothness
// 5 - AO + Smoothness
// 6 - Metal + AO + Smoothness
// 1 - Metal
// 2 - AO
// 3 - Metal + AO
// 4 - Smoothness also 3RT
// 5 - Metal + Smoothness
// 6 - AO + Smoothness
// 7 - Metal + AO + Smoothness
Pass
{
Name "DBufferProjector_M" // Name is not used

}
// Mesh
// 7 - Metal
// 8 - AO
// 9 - Metal + AO
// 10 - Smoothness
// 11 - Metal + Smoothness
// 12 - AO + Smoothness
// 13 - Metal + AO + Smoothness
// 14 - 3RT
// 8 - Metal
// 9 - AO
// 10 - Metal + AO
// 11 - Smoothness
// 12 - Metal + Smoothness
// 13 - AO + Smoothness
// 14 - Metal + AO + Smoothness
Pass
{

Blend 1 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 3 Zero OneMinusSrcColor
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_MESH
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferMesh_3RT" // Name is not used
Tags{"LightMode" = "DBufferMesh_3RT"} // Smoothness
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 1 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
ColorMask BA 2 // smoothness/smoothness alpha
HLSLPROGRAM

16
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalSystem.cs


m_DecalDatas[m_DecalDatasCount].blendParams = m_BlendParams;
if(!perChannelMask)
{
m_DecalDatas[m_DecalDatasCount].blendParams.z = 4.0f; // smoothness
m_DecalDatas[m_DecalDatasCount].blendParams.z = (float)Decal.MaskBlendFlags.Smoothness;
}
// we have not allocated the textures in atlas yet, so only store references to them

int totalToDraw = m_NumResults;
HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
bool perChannelMask = hdrp.renderPipelineSettings.decalSettings.perChannelMask;
int shaderPass = perChannelMask ? (int)m_Material.GetFloat("_MaskBlendMode") : 4; // relies on the order shader passes are declared in decal.shader and decalUI.cs
shaderPass--; // shader passes are 0 based
// enum BlendMode
// {
// Metal_AO_Smoothness,
// Metal_Smoothness,
// Metal,
// Smoothness,
// AO
// }
int shaderPass = perChannelMask ? (int)m_Material.GetFloat("_MaskBlendMode") : (int)Decal.MaskBlendFlags.Smoothness; // relies on the order shader passes are declared in decal.shader and decalUI.cs
for (; batchIndex < m_NumResults / kDrawIndexedBatchSize; batchIndex++)
{
m_PropertyBlock.SetMatrixArray(HDShaderIDs._NormalToWorldID, m_NormalToWorld[batchIndex]);

正在加载...
取消
保存