浏览代码

Merge pull request #1674 from Unity-Technologies/decals/v2/per_channel_mask_new_ui

Decals/v2/per channel mask new ui
/main
GitHub 6 年前
当前提交
b4a3f127
共有 21 个文件被更改,包括 873 次插入141 次删除
  1. 1
      com.unity.render-pipelines.high-definition/CHANGELOG.md
  2. 130
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Decal/DecalUI.cs
  3. 1
      com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/Settings/GlobalDecalSettingsUI.cs
  4. 2
      com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/Settings/SerializedGlobalDecalSettings.cs
  5. 22
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DBufferManager.cs
  6. 23
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs
  7. 42
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs.hlsl
  8. 129
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.hlsl
  9. 422
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  10. 42
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalData.hlsl
  11. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalProjectorComponent.cs
  12. 32
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalSystem.cs
  13. 103
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalUtilities.hlsl
  14. 1
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/GlobalDecalSettings.cs
  15. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLit.shader
  16. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLitTessellation.shader
  17. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader
  18. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitTessellation.shader
  19. 4
      com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader
  20. 26
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
  21. 20
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs

1
com.unity.render-pipelines.high-definition/CHANGELOG.md


- Exposed shadow budget parameters in HDRP asset
- Add an option to generate an emissive mesh for area lights (currently rectangle light only). The mesh fits the size, intensity and color of the light.
- Add an option to the HDRP asset to increase the resolution of volumetric lighting.
- Add an decal mask map channel selection mode.
- Add additional ligth unit support for punctual light (Lumens, Candela) and area lights (Lumens, Luminance)
- Add dedicated Gizmo for the box Influence volume of HDReflectionProbe / PlanarReflectionProbe

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


using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.Rendering;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{

public static GUIContent AlbedoModeText = new GUIContent("Albedo contribution", "Base color + Blend, Blend only");
}
enum BlendSource
{
AlbedoMapAlpha,
MaskMapBlue
}
protected string[] blendSourceNames = Enum.GetNames(typeof(BlendSource));
protected string[] blendModeNames = Enum.GetNames(typeof(BlendMode));
protected MaterialProperty baseColorMap = new MaterialProperty();
protected const string kBaseColorMap = "_BaseColorMap";

protected MaterialProperty albedoMode = new MaterialProperty();
protected const string kAlbedoMode = "_AlbedoMode";
protected MaterialProperty normalBlendSrc = new MaterialProperty();
protected const string kNormalBlendSrc = "_NormalBlendSrc";
protected MaterialProperty maskBlendSrc = new MaterialProperty();
protected const string kMaskBlendSrc = "_MaskBlendSrc";
protected MaterialProperty maskBlendMode = new MaterialProperty();
protected const string kMaskBlendMode = "_MaskBlendMode";
protected MaterialProperty maskmapMetal = new MaterialProperty();
protected const string kMaskmapMetal = "_MaskmapMetal";
protected MaterialProperty maskmapAO = new MaterialProperty();
protected const string kMaskmapAO = "_MaskmapAO";
protected MaterialProperty maskmapSmoothness = new MaterialProperty();
protected const string kMaskmapSmoothness = "_MaskmapSmoothness";
protected MaterialEditor m_MaterialEditor;

maskMap = FindProperty(kMaskMap, props);
decalBlend = FindProperty(kDecalBlend, props);
albedoMode = FindProperty(kAlbedoMode, props);
normalBlendSrc = FindProperty(kNormalBlendSrc, props);
maskBlendSrc = FindProperty(kMaskBlendSrc, props);
maskBlendMode = FindProperty(kMaskBlendMode, props);
maskmapMetal = FindProperty(kMaskmapMetal, props);
maskmapAO = FindProperty(kMaskmapAO, props);
maskmapSmoothness = FindProperty(kMaskmapSmoothness, props);
// always instanced
SerializedProperty instancing = m_MaterialEditor.serializedObject.FindProperty("m_EnableInstancingVariants");
instancing.boolValue = true;

static public void SetupMaterialKeywordsAndPass(Material material)
{
Decal.MaskBlendFlags blendMode = (Decal.MaskBlendFlags)material.GetFloat(kMaskBlendMode);
CoreUtils.SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap));
CoreUtils.SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap));
CoreUtils.SetKeyword(material, "_NORMAL_BLEND_SRC_B", material.GetFloat(kNormalBlendSrc) == 1.0f);
CoreUtils.SetKeyword(material, "_MASK_BLEND_SRC_B", material.GetFloat(kMaskBlendSrc) == 1.0f);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecals3RTStr, true);
switch (blendMode)
{
case Decal.MaskBlendFlags.Metal:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, true);
break;
case Decal.MaskBlendFlags.AO:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, true);
break;
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.AO:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOStr, true);
break;
case Decal.MaskBlendFlags.Smoothness:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, true);
break;
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.Smoothness:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, true);
break;
case Decal.MaskBlendFlags.AO | Decal.MaskBlendFlags.Smoothness:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOSStr, true);
break;
case Decal.MaskBlendFlags.Metal | Decal.MaskBlendFlags.AO | Decal.MaskBlendFlags.Smoothness:
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOSStr, true);
break;
}
}
protected void SetupMaterialKeywordsAndPassInternal(Material material)

{
// Use default labelWidth
EditorGUIUtility.labelWidth = 0f;
float normalBlendSrcValue = normalBlendSrc.floatValue;
float maskBlendSrcValue = maskBlendSrc.floatValue;
Decal.MaskBlendFlags maskBlendFlags = (Decal.MaskBlendFlags)maskBlendMode.floatValue;
HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
bool perChannelMask = hdrp.renderPipelineSettings.decalSettings.perChannelMask;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();

m_MaterialEditor.TexturePropertySingleLine(Styles.baseColorText2, baseColorMap, baseColor);
}
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalMap);
if (material.GetTexture(kNormalMap))
{
normalBlendSrcValue = EditorGUILayout.Popup( "Normal blend source", (int)normalBlendSrcValue, blendSourceNames);
}
if (material.GetTexture(kMaskMap))
{
maskBlendSrcValue = EditorGUILayout.Popup("Mask blend source", (int) maskBlendSrcValue, blendSourceNames);
if (perChannelMask)
{
EditorGUILayout.HelpBox(
"Disabling all mask map channels defaults to smoothness only.\n" +
"To disable all channels remove the mask map from the shader.\n" +
"Mask map channel selection mode can be disabled in pipeline asset, when disabled only smoothness is active.\n" +
"Disabling mask map channel selection mode will improve performance.",
MessageType.Info);
}
else
{
EditorGUILayout.HelpBox(
"For better control of mask map channels enable mask map channel selection mode in pipeline asset.\nEnabling this feature incurs a performance cost.",
MessageType.Info);
}
if (perChannelMask)
{
m_MaterialEditor.ShaderProperty(maskmapMetal, "Metal");
m_MaterialEditor.ShaderProperty(maskmapAO, "AO");
m_MaterialEditor.ShaderProperty(maskmapSmoothness, "Smoothness");
if ((maskmapMetal.floatValue == 0.0f) && (maskmapAO.floatValue == 0.0f) &&
(maskmapSmoothness.floatValue == 0.0f))
{
maskmapSmoothness.floatValue = 1.0f;
}
if (maskmapMetal.floatValue == 1.0f)
maskBlendFlags |= Decal.MaskBlendFlags.Metal;
if (maskmapAO.floatValue == 1.0f)
maskBlendFlags |= Decal.MaskBlendFlags.AO;
if (maskmapSmoothness.floatValue == 1.0f)
maskBlendFlags |= Decal.MaskBlendFlags.Smoothness;
}
}
m_MaterialEditor.ShaderProperty(decalBlend, Styles.decalBlendText);
EditorGUI.indentLevel--;
}

normalBlendSrc.floatValue = normalBlendSrcValue;
maskBlendSrc.floatValue = maskBlendSrcValue;
maskBlendMode.floatValue = (float) maskBlendFlags;
foreach (var obj in m_MaterialEditor.targets)
SetupMaterialKeywordsAndPassInternal((Material)obj);
}

1
com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/Settings/GlobalDecalSettingsUI.cs


EditorGUILayout.PropertyField(d.drawDistance, _.GetContent("Draw Distance"));
EditorGUILayout.PropertyField(d.atlasWidth, _.GetContent("Atlas Width"));
EditorGUILayout.PropertyField(d.atlasHeight, _.GetContent("Atlas Height"));
EditorGUILayout.PropertyField(d.perChannelMask, _.GetContent("Mask map channel selection"));
--EditorGUI.indentLevel;
}
}

2
com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/Settings/SerializedGlobalDecalSettings.cs


public SerializedProperty drawDistance;
public SerializedProperty atlasWidth;
public SerializedProperty atlasHeight;
public SerializedProperty perChannelMask;
public SerializedGlobalDecalSettings(SerializedProperty root)
{

atlasWidth = root.Find((GlobalDecalSettings s) => s.atlasWidth);
atlasHeight = root.Find((GlobalDecalSettings s) => s.atlasHeight);
perChannelMask = root.Find((GlobalDecalSettings s) => s.perChannelMask);
}
}
}

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


RTHandles.Release(m_HTile);
}
public void ClearTargets(CommandBuffer cmd, HDCamera camera)
public void ClearAndSetTargets(CommandBuffer cmd, HDCamera camera, bool rtCount4, RTHandleSystem.RTHandle cameraDepthStencilBuffer)
RenderTargetIdentifier[] RTIDs = new RenderTargetIdentifier[rtCount4 ? 4 :3];
// this clears the targets
Color clearColorAOSBlend = new Color(1.0f, 1.0f, 1.0f, 1.0f);
// names IDs have to be set every frame, because they can change
RTIDs[0] = m_RTs[0].nameID;
RTIDs[1] = m_RTs[1].nameID;
RTIDs[2] = m_RTs[2].nameID;
if (rtCount4)
{
HDUtils.SetRenderTarget(cmd, camera, m_RTs[3], ClearFlag.Color, clearColorAOSBlend);
RTIDs[3] = m_RTs[3].nameID;
}
}
public void SetHTile(int bindSlot, CommandBuffer cmd)
{
cmd.SetRandomWriteTarget(bindSlot, m_HTile);
// this actually sets the MRTs and HTile RWTexture, this is done separately because we do not have an api to clear MRTs to different colors
HDUtils.SetRenderTarget(cmd, camera, RTIDs, cameraDepthStencilBuffer); // do not clear anymore
cmd.SetRandomWriteTarget(rtCount4 ? 4 : 3, m_HTile);
}
public void UnSetHTile(CommandBuffer cmd)

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


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

public Vector4 normalWS;
[SurfaceDataAttributes("Mask", true)]
public Vector4 mask;
[SurfaceDataAttributes("AOSBlend", true)]
public Vector2 MAOSBlend;
[SurfaceDataAttributes("HTileMask")]
public uint HTileMask;
};

{
// Note: This count doesn't include the velocity buffer. On shader and csharp side the velocity buffer will be added by the framework
Count = 3
{
Count = 4
};
[GenerateHLSL(PackingRules.Exact)]

// should this be combined into common class shared with Lit.cs???
static public int GetMaterialDBufferCount() { return (int)DBufferMaterial.Count; }
static RenderTextureFormat[] m_RTFormat = { RenderTextureFormat.ARGB32, RenderTextureFormat.ARGB32, RenderTextureFormat.ARGB32 };
static bool[] m_sRGBFlags = { true, false, false };
static RenderTextureFormat[] m_RTFormat = { RenderTextureFormat.ARGB32, RenderTextureFormat.ARGB32, RenderTextureFormat.ARGB32, RenderTextureFormat.RG16 };
static bool[] m_sRGBFlags = { true, false, false, false };
static public void GetMaterialDBufferDescription(out RenderTextureFormat[] RTFormat, out bool[] 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

public Vector4 normalScaleBias;
public Vector4 maskScaleBias;
public Vector4 baseColor;
public Vector3 blendParams; // x normal blend source, y mask blend source, z mask blend mode
};
}

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


#define DEBUGVIEW_DECAL_DECALSURFACEDATA_BASE_COLOR (200)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_NORMAL (201)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_MASK (202)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_HTILE_MASK (203)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_AOSBLEND (203)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_HTILE_MASK (204)
#define DBUFFERMATERIAL_COUNT (3)
#define DBUFFERMATERIAL_COUNT (4)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Decal+DBufferHTileBit: static fields

float4 baseColor;
float4 normalWS;
float4 mask;
float2 MAOSBlend;
uint HTileMask;
};

float4 normalScaleBias;
float4 maskScaleBias;
float4 baseColor;
float3 blendParams;
// Accessors for UnityEngine.Experimental.Rendering.HDPipeline.DecalData
//
float4x4 GetWorldToDecal(DecalData value)
{
return value.worldToDecal;
}
float4x4 GetNormalToWorld(DecalData value)
{
return value.normalToWorld;
}
float4 GetDiffuseScaleBias(DecalData value)
{
return value.diffuseScaleBias;
}
float4 GetNormalScaleBias(DecalData value)
{
return value.normalScaleBias;
}
float4 GetMaskScaleBias(DecalData value)
{
return value.maskScaleBias;
}
float4 GetBaseColor(DecalData value)
{
return value.baseColor;
}
float3 GetBlendParams(DecalData value)
{
return value.blendParams;
}
//
// Debug functions
//
void GetGeneratedDecalSurfaceDataDebug(uint paramId, DecalSurfaceData decalsurfacedata, inout float3 result, inout bool needLinearToSRGB)

break;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_MASK:
result = decalsurfacedata.mask.xyz;
break;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_AOSBLEND:
result = float3(decalsurfacedata.MAOSBlend, 0.0);
break;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_HTILE_MASK:
result = GetIndexColor(decalsurfacedata.HTileMask);

129
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.hlsl


#define DBufferType0 float4
#define DBufferType1 float4
#define DBufferType2 float4
#define DBufferType3 float4
#ifdef DBUFFERMATERIAL_COUNT
#ifdef _DECALS_4RT
#define DBufferType3 float2
#if DBUFFERMATERIAL_COUNT == 1
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType1 MERGE_NAME(NAME, 1) : SV_Target1, \
out DBufferType2 MERGE_NAME(NAME, 2) : SV_Target2, \
out DBufferType3 MERGE_NAME(NAME, 3) : SV_Target3
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1)); \
TEXTURE2D(MERGE_NAME(NAME, 2)); \
TEXTURE2D(MERGE_NAME(NAME, 3));
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0));
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2); \
DBufferType1 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), unCoord2); \
DBufferType2 MERGE_NAME(NAME, 2) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 2), unCoord2); \
DBufferType3 MERGE_NAME(NAME, 3) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 3), unCoord2);
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2);
#define ENCODE_INTO_DBUFFER(DECAL_SURFACE_DATA, NAME) EncodeIntoDBuffer(DECAL_SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2), MERGE_NAME(NAME,3))
#define DECODE_FROM_DBUFFER(NAME, DECAL_SURFACE_DATA) DecodeFromDBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2), MERGE_NAME(NAME,3), DECAL_SURFACE_DATA)
#define ENCODE_INTO_DBUFFER(DECAL_SURFACE_DATA, NAME) EncodeIntoDBuffer(DECAL_SURFACE_DATA, MERGE_NAME(NAME,0))
#define DECODE_FROM_DBUFFER(NAME, DECAL_SURFACE_DATA) DecodeFromDBuffer(MERGE_NAME(NAME,0), DECAL_SURFACE_DATA)
#else
#elif DBUFFERMATERIAL_COUNT == 2
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType1 MERGE_NAME(NAME, 1) : SV_Target1
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1));
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2); \
DBufferType1 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), unCoord2);
#define ENCODE_INTO_DBUFFER(DECAL_SURFACE_DATA, NAME) EncodeIntoDBuffer(DECAL_SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1))
#define DECODE_FROM_DBUFFER(NAME, DECAL_SURFACE_DATA) DecodeFromDBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), DECAL_SURFACE_DATA)
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType1 MERGE_NAME(NAME, 1) : SV_Target1, \
out DBufferType2 MERGE_NAME(NAME, 2) : SV_Target2
#elif DBUFFERMATERIAL_COUNT == 3
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1)); \
TEXTURE2D(MERGE_NAME(NAME, 2));
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType1 MERGE_NAME(NAME, 1) : SV_Target1, \
out DBufferType2 MERGE_NAME(NAME, 2) : SV_Target2
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1)); \
TEXTURE2D(MERGE_NAME(NAME, 2));
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2); \
DBufferType1 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), unCoord2); \
DBufferType2 MERGE_NAME(NAME, 2) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 2), unCoord2);
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2); \
DBufferType1 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), unCoord2); \
DBufferType2 MERGE_NAME(NAME, 2) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 2), unCoord2);
#elif DBUFFERMATERIAL_COUNT == 4
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType1 MERGE_NAME(NAME, 1) : SV_Target1, \
out DBufferType2 MERGE_NAME(NAME, 2) : SV_Target2, \
out DBufferType3 MERGE_NAME(NAME, 3) : SV_Target3
#define DECLARE_DBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1)); \
TEXTURE2D(MERGE_NAME(NAME, 2)); \
TEXTURE2D(MERGE_NAME(NAME, 3));
#define FETCH_DBUFFER(NAME, TEX, unCoord2) \
DBufferType0 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), unCoord2); \
DBufferType1 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), unCoord2); \
DBufferType2 MERGE_NAME(NAME, 2) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 2), unCoord2); \
DBufferType3 MERGE_NAME(NAME, 3) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 3), unCoord2);
#define ENCODE_INTO_DBUFFER(DECAL_SURFACE_DATA, NAME) EncodeIntoDBuffer(DECAL_SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2), MERGE_NAME(NAME,3))
#define DECODE_FROM_DBUFFER(NAME, DECAL_SURFACE_DATA) DecodeFromDBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2), MERGE_NAME(NAME,3), DECAL_SURFACE_DATA)
#endif // #ifdef DBUFFERMATERIAL_COUNT
CBUFFER_START(UnityDecalParameters)
uint _EnableDBuffer;

SAMPLER(_trilinear_clamp_sampler_DecalAtlas2D);
// Must be in sync with RT declared in HDRenderPipeline.cs ::Rebuild
void EncodeIntoDBuffer( DecalSurfaceData surfaceData,
out DBufferType0 outDBuffer0,
out DBufferType1 outDBuffer1,
out DBufferType2 outDBuffer2
void EncodeIntoDBuffer( DecalSurfaceData surfaceData
, out DBufferType0 outDBuffer0
, out DBufferType1 outDBuffer1
, out DBufferType2 outDBuffer2
#ifdef _DECALS_4RT
, out DBufferType3 outDBuffer3
#endif
#ifdef _DECALS_4RT
outDBuffer3 = surfaceData.MAOSBlend;
#endif
DBufferType0 inDBuffer0,
DBufferType1 inDBuffer1,
DBufferType2 inDBuffer2,
out DecalSurfaceData surfaceData
DBufferType0 inDBuffer0
, DBufferType1 inDBuffer1
, DBufferType2 inDBuffer2
#ifdef _DECALS_4RT
, DBufferType3 inDBuffer3
#endif
, out DecalSurfaceData surfaceData
)
{
ZERO_INITIALIZE(DecalSurfaceData, surfaceData);

surfaceData.mask = inDBuffer2;
#ifdef _DECALS_4RT
surfaceData.MAOSBlend = inDBuffer3;
#else
surfaceData.MAOSBlend = float2(surfaceData.mask.w, surfaceData.mask.w);
#endif
}

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


_MaskMap("MaskMap", 2D) = "white" {}
_DecalBlend("_DecalBlend", Range(0.0, 1.0)) = 0.5
[ToggleUI] _AlbedoMode("_AlbedoMode", Range(0.0, 1.0)) = 1.0
[HideInInspector] _NormalBlendSrc("_NormalBlendSrc", Float) = 0.0
[HideInInspector] _MaskBlendSrc("_MaskBlendSrc", Float) = 1.0
[HideInInspector] _MaskBlendMode("_MaskBlendMode", Float) = 4.0 // smoothness 3RT default
[ToggleUI] _MaskmapMetal("_MaskmapMetal", Range(0.0, 1.0)) = 0.0
[ToggleUI] _MaskmapAO("_MaskmapAO", Range(0.0, 1.0)) = 0.0
[ToggleUI] _MaskmapSmoothness("_MaskmapSmoothness", Range(0.0, 1.0)) = 1.0
}
HLSLINCLUDE

#pragma shader_feature _MASKMAP
#pragma shader_feature _ALBEDOCONTRIBUTION
#pragma shader_feature _NORMAL_BLEND_SRC_B
#pragma shader_feature _MASK_BLEND_SRC_B
#pragma multi_compile _ _DECALS_4RT
//-------------------------------------------------------------------------------------
// Define
//-------------------------------------------------------------------------------------

{
Tags{ "RenderPipeline" = "HDRenderPipeline"}
// 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,
// AO = 1 << 1,
// Smoothness = 1 << 2,
//}
// Projectors
//
// 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
Tags{"LightMode" = "DBufferProjector_M"} // Metalness
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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
Blend 3 Zero OneMinusSrcColor
ColorMask R 2 // metal
ColorMask R 3 // metal alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferProjector_AO" // Name is not used
Tags{"LightMode" = "DBufferProjector_AO"} // AO only
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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
Blend 3 Zero OneMinusSrcColor
ColorMask G 2 // ao
ColorMask G 3 // ao alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferProjector_MAO" // Name is not used
Tags{"LightMode" = "DBufferProjector_MAO"} // AO + Metalness
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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
Blend 3 Zero OneMinusSrcColor
ColorMask RG 2 // metalness + ao
ColorMask RG 3 // metalness alpha + ao alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferProjector_S" // Name is not used
Tags{"LightMode" = "DBufferProjector_S"} // Smoothness
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferProjector_MS" // Name is not used
Tags{"LightMode" = "DBufferProjector_MS"} // Smoothness and Metalness
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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
Blend 3 Zero OneMinusSrcColor
ColorMask RBA 2 // metal/smoothness/smoothness alpha
ColorMask R 3 // metal alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Pass
{
Name "DBufferProjector_AOS" // Name is not used
Tags{"LightMode" = "DBufferProjector_AOS"} // AO + Smoothness
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off
ZTest Greater
// 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
Blend 3 Zero OneMinusSrcColor
ColorMask GBA 2 // ao, smoothness, smoothness alpha
ColorMask G 3 // ao alpha
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER_PROJECTOR
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
Name "DBufferProjector" // Name is not used
Tags { "LightMode" = "DBufferProjector" } // This will be only for opaque object based on the RenderQueue index
Name "DBufferProjector_MAOS" // Name is not used
Tags { "LightMode" = "DBufferProjector_MAOS" } // Metalness AO and Smoothness
Blend SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 1 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 3 Zero OneMinusSrcColor
HLSLPROGRAM

ENDHLSL
}
// Mesh
// 8 - Metal
// 9 - AO
// 10 - Metal + AO
// 11 - Smoothness
// 12 - Metal + Smoothness
// 13 - AO + Smoothness
// 14 - Metal + AO + Smoothness
Name "DBufferMesh" // Name is not used
Tags{"LightMode" = "DBufferMesh"} // This will be only for opaque object based on the RenderQueue index
Cull Back
Name "DBufferMesh_M" // Name is not used
Tags{"LightMode" = "DBufferMesh_M"} // Metalness
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
Blend 3 Zero OneMinusSrcColor
ColorMask R 2 // metal
ColorMask R 3 // metal 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
}
Pass
{
Name "DBufferMesh_AO" // Name is not used
Tags{"LightMode" = "DBufferMesh_AO"} // AO only
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
Blend 3 Zero OneMinusSrcColor
ColorMask G 2 // ao
ColorMask G 3 // ao 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
}
Pass
{
Name "DBufferMesh_MAO" // Name is not used
Tags{"LightMode" = "DBufferMesh_MAO"} // AO + Metalness
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
Blend 3 Zero OneMinusSrcColor
ColorMask RG 2 // metalness + ao
ColorMask RG 3 // metalness alpha + ao 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
}
Pass
{
Name "DBufferMesh_S" // Name is not used
Tags{"LightMode" = "DBufferMesh_S"} // 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
}
Pass
{
Name "DBufferMesh_MS" // Name is not used
Tags{"LightMode" = "DBufferMesh_MS"} // Smoothness and Metalness
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
Blend 3 Zero OneMinusSrcColor
ColorMask RBA 2 // metal/smoothness/smoothness alpha
ColorMask R 3 // metal 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
}
Pass
{
Name "DBufferMesh_AOS" // Name is not used
Tags{"LightMode" = "DBufferMesh_AOS"} // AO + Smoothness
Blend SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 1 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero OneMinusSrcAlpha
Blend 3 Zero OneMinusSrcColor
ColorMask GBA 2 // ao, smoothness, smoothness alpha
ColorMask G 3 // ao alpha
HLSLPROGRAM

ENDHLSL
}
}
Pass
{
Name "DBufferMesh_MAOS" // Name is not used
Tags{"LightMode" = "DBufferMesh_MAOS"} // Metalness AO and 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
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
}
}
CustomEditor "Experimental.Rendering.HDPipeline.DecalUI"
}

42
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalData.hlsl


surfaceData.baseColor = _BaseColor;
surfaceData.normalWS = float4(0,0,0,0);
surfaceData.mask = float4(0,0,0,0);
surfaceData.MAOSBlend = float2(0, 0);
float totalBlend = clamp(normalToWorld[0][3], 0.0f, 1.0f);
float albedoMapBlend = clamp(normalToWorld[0][3], 0.0f, 1.0f);
float totalBlend = _DecalBlend;
float albedoMapBlend = _DecalBlend;
float2 texCoords = input.texCoord0;
#endif

surfaceData.baseColor.w *= totalBlend;
totalBlend = surfaceData.baseColor.w; // base alpha affects all other channels;
surfaceData.baseColor.w *= albedoMapBlend;
albedoMapBlend = surfaceData.baseColor.w;
// outside _COLORMAP because we still have base color
#if _ALBEDOCONTRIBUTION
surfaceData.HTileMask |= DBUFFERHTILEBIT_DIFFUSE;

float maskMapBlend;
#if _MASKMAP
surfaceData.mask = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, texCoords);
maskMapBlend = surfaceData.mask.z * _DecalBlend; // store before overwriting with smoothness
surfaceData.mask.z = surfaceData.mask.w;
surfaceData.HTileMask |= DBUFFERHTILEBIT_MASK;
#if _MASK_BLEND_SRC_B
surfaceData.mask.w = maskMapBlend;
#else
surfaceData.mask.w = albedoMapBlend;
#endif
#endif
// needs to be after mask, because blend source could be in the mask map blue
#if _NORMALMAP
float3 normalTS = UnpackNormalmapRGorAG(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, texCoords));
#if (SHADERPASS == SHADERPASS_DBUFFER_PROJECTOR)

GetNormalWS(input, 0, normalTS, normalWS);
#endif
surfaceData.normalWS.xyz = normalWS * 0.5f + 0.5f;
surfaceData.normalWS.w = totalBlend;
surfaceData.HTileMask |= DBUFFERHTILEBIT_NORMAL;
surfaceData.normalWS.xyz = normalWS * 0.5f + 0.5f;
surfaceData.HTileMask |= DBUFFERHTILEBIT_NORMAL;
#if _NORMAL_BLEND_SRC_B
surfaceData.normalWS.w = maskMapBlend;
#else
surfaceData.normalWS.w = albedoMapBlend;
#endif
#if _MASKMAP
surfaceData.mask = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, texCoords);
surfaceData.mask.z = surfaceData.mask.w;
surfaceData.mask.w = totalBlend;
surfaceData.HTileMask |= DBUFFERHTILEBIT_MASK;
#endif
surfaceData.MAOSBlend.xy = float2(surfaceData.mask.w, surfaceData.mask.w);
}

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


}
}
public void Update()
public void LateUpdate()
{
if (m_Handle != null)
{

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


}
}
public bool perChannelMask
{
get
{
HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
if (hdrp != null)
{
return hdrp.renderPipelineSettings.decalSettings.perChannelMask;
}
return false;
}
}
public Camera CurrentCamera
{
get

m_Blend = m_Material.GetFloat("_DecalBlend");
m_AlbedoContribution = m_Material.GetFloat("_AlbedoMode");
m_BaseColor = m_Material.GetVector("_BaseColor");
m_BlendParams = new Vector3(m_Material.GetFloat("_NormalBlendSrc"), m_Material.GetFloat("_MaskBlendSrc"), m_Material.GetFloat("_MaskBlendMode"));
}
public DecalSet(Material material)

Vector3 cameraPos = instance.CurrentCamera.transform.position;
Matrix4x4 worldToView = LightLoop.WorldToCamera(instance.CurrentCamera);
bool perChannelMask = instance.perChannelMask;
for (int resultIndex = 0; resultIndex < m_NumResults; resultIndex++)
{
int decalIndex = m_ResultIndices[resultIndex];

m_DecalDatas[m_DecalDatasCount].worldToDecal = decalToWorldBatch[instanceCount].inverse;
m_DecalDatas[m_DecalDatasCount].normalToWorld = normalToWorldBatch[instanceCount];
m_DecalDatas[m_DecalDatasCount].baseColor = m_BaseColor;
m_DecalDatas[m_DecalDatasCount].blendParams = m_BlendParams;
if(!perChannelMask)
{
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
m_DiffuseTextureScaleBias[m_DecalDatasCount] = m_Diffuse;
m_NormalTextureScaleBias[m_DecalDatasCount] = m_Normal;

return;
int batchIndex = 0;
int totalToDraw = m_NumResults;
HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
bool perChannelMask = hdrp.renderPipelineSettings.decalSettings.perChannelMask;
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
cmd.DrawMeshInstanced(m_DecalMesh, 0, KeyMaterial, 0, m_DecalToWorld[batchIndex], kDrawIndexedBatchSize, m_PropertyBlock);
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Material, shaderPass, m_DecalToWorld[batchIndex], kDrawIndexedBatchSize, m_PropertyBlock);
totalToDraw -= kDrawIndexedBatchSize;
}

cmd.DrawMeshInstanced(m_DecalMesh, 0, KeyMaterial, 0, m_DecalToWorld[batchIndex], totalToDraw, m_PropertyBlock);
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Material, shaderPass, m_DecalToWorld[batchIndex], totalToDraw, m_PropertyBlock);
}
}

private float m_Blend = 0;
private float m_AlbedoContribution = 0;
private Vector4 m_BaseColor;
private Vector3 m_BlendParams;
TextureScaleBias m_Diffuse = new TextureScaleBias();
TextureScaleBias m_Normal = new TextureScaleBias();

103
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalUtilities.hlsl


matMask |= mapMask;
}
void ApplyBlendMask(inout float4 dst, inout int matMask, float2 texCoords, int mapMask, float blend, float lod)
// albedoBlend is overall decal blend combined with distance fade and albedo alpha
// decalBlend is decal blend with distance fade to be able to construct normal and mask blend if they come from mask map blue channel
// normalBlend is calculated in this function and used later to blend the normal
// blendParams are material settings to determing blend source and mode for normal and mask map
void ApplyBlendMask(inout float4 dbuffer2, inout float2 dbuffer3, inout int matMask, float2 texCoords, int mapMask, float albedoBlend, float lod, float decalBlend, inout float normalBlend, float3 blendParams) // too many blends!!!
src.z = src.w;
src.w = blend;
dst.xyz = src.xyz * src.w + dst.xyz * (1.0f - src.w);
dst.w = dst.w * (1.0f - src.w);
float maskBlend;
if (blendParams.x == 1.0f) // normal blend source is mask blue channel
normalBlend = src.z * decalBlend;
else
normalBlend = albedoBlend; // normal blend source is albedo alpha
if (blendParams.y == 1.0f) // mask blend source is mask blue channel
maskBlend = src.z * decalBlend;
else
maskBlend = albedoBlend; // mask blend siurce is albedo alpha
src.z = src.w; // remap so smoothness goes to blue and mask blend goes to alpha
src.w = maskBlend;
float4 dbuffer2Mask;
float2 dbuffer3Mask;
if (blendParams.z == 0)
{
dbuffer2Mask = float4(1, 1, 1, 1); // M, AO, S, S alpha
dbuffer3Mask = float2(1, 1); // M alpha, AO alpha
}
else if (blendParams.z == 1)
{
dbuffer2Mask = float4(1, 0, 0, 0); // M, _, _, _
dbuffer3Mask = float2(1, 0); // M alpha, _
}
else if (blendParams.z == 2)
{
dbuffer2Mask = float4(0, 1, 0, 0); // _, AO, _, _
dbuffer3Mask = float2(0, 1); // _, AO alpha
}
else if (blendParams.z == 3)
{
dbuffer2Mask = float4(1, 1, 0, 0); // M, AO, _, _
dbuffer3Mask = float2(1, 1); // M Alpha, AO alpha
}
else if (blendParams.z == 4)
{
dbuffer2Mask = float4(0, 0, 1, 1); // _, _, S, S alpha
dbuffer3Mask = float2(0, 0); // _, _
}
else if (blendParams.z == 5)
{
dbuffer2Mask = float4(1, 0, 1, 1); // M, _, S, S alpha
dbuffer3Mask = float2(1, 0); // M alpha, _
}
else if (blendParams.z == 6)
{
dbuffer2Mask = float4(0, 1, 1, 1); // _, AO, S, S alpha
dbuffer3Mask = float2(0, 1); // _, AO alpha
}
else if (blendParams.z == 7)
{
dbuffer2Mask = float4(1, 1, 1, 1); // M, AO, S, S alpha
dbuffer3Mask = float2(1, 1); // M alpha, AO alpha
}
dbuffer2.xyz = (dbuffer2Mask.xyz == 1) ? src.xyz * src.w + dbuffer2.xyz * (1.0f - src.w) : dbuffer2.xyz;
dbuffer2.w = (dbuffer2Mask.w == 1) ? dbuffer2.w * (1.0f - src.w) : dbuffer2.w;
dbuffer3.xy = (dbuffer3Mask.xy == 1) ? dbuffer3.xy * (1.0f - src.w) : dbuffer3.xy;
matMask |= mapMask;
}

DBuffer0 = float4(0.0f, 0.0f, 0.0f, 1.0f);
DBuffer1 = float4(0.5f, 0.5f, 0.5f, 1.0f);
DBuffer2 = float4(0.0f, 0.0f, 0.0f, 1.0f);
#ifdef _DECALS_4RT
DBuffer3 = float2(1.0f, 1.0f);
#else
float2 DBuffer3 = float2(1.0f, 1.0f);
#endif
#ifdef LIGHTLOOP_TILE_PASS
GetCountAndStart(posInput, LIGHTCATEGORY_DECAL, decalStart, decalCount);

float2 sampleMaskDdy = positionDSDdy.xz * decalData.maskScaleBias.xy;
float lodMask = ComputeTextureLOD(sampleMaskDdx, sampleMaskDdy, _DecalAtlasResolution);
float decalBlend = decalData.normalToWorld[0][3];
float albedoBlend = decalData.normalToWorld[0][3];
ApplyBlendDiffuse(DBuffer0, mask, sampleDiffuse, src, DBUFFERHTILEBIT_DIFFUSE, decalBlend, lodDiffuse, diffuseTextureBound);
alpha = alpha < decalBlend ? decalBlend : alpha; // use decal alpha if it is higher than transparent alpha
ApplyBlendDiffuse(DBuffer0, mask, sampleDiffuse, src, DBUFFERHTILEBIT_DIFFUSE, albedoBlend, lodDiffuse, diffuseTextureBound);
alpha = alpha < albedoBlend ? albedoBlend : alpha; // use decal alpha if it is higher than transparent alpha
float albedoContribution = decalData.normalToWorld[1][3];
if (albedoContribution == 0.0f)

float normalBlend = albedoBlend;
if ((decalData.maskScaleBias.x > 0) && (decalData.maskScaleBias.y > 0))
{
ApplyBlendMask(DBuffer2, DBuffer3, mask, sampleMask, DBUFFERHTILEBIT_MASK, albedoBlend, lodMask, decalData.normalToWorld[0][3], normalBlend, decalData.blendParams);
}
ApplyBlendNormal(DBuffer1, mask, sampleNormal, DBUFFERHTILEBIT_NORMAL, (float3x3)decalData.normalToWorld, decalBlend, lodNormal);
}
if ((decalData.maskScaleBias.x > 0) && (decalData.maskScaleBias.y > 0))
{
ApplyBlendMask(DBuffer2, mask, sampleMask, DBUFFERHTILEBIT_MASK, decalBlend, lodMask);
ApplyBlendNormal(DBuffer1, mask, sampleNormal, DBUFFERHTILEBIT_NORMAL, (float3x3)decalData.normalToWorld, normalBlend, lodNormal);
}
}
}

}
if(mask & DBUFFERHTILEBIT_MASK)
{
surfaceData.metallic = surfaceData.metallic * decalSurfaceData.mask.w + decalSurfaceData.mask.x;
surfaceData.ambientOcclusion = surfaceData.ambientOcclusion * decalSurfaceData.mask.w + decalSurfaceData.mask.y;
#ifdef _DECALS_4RT // only smoothness in 3RT mode
surfaceData.metallic = surfaceData.metallic * decalSurfaceData.MAOSBlend.x + decalSurfaceData.mask.x;
surfaceData.ambientOcclusion = surfaceData.ambientOcclusion * decalSurfaceData.MAOSBlend.y + decalSurfaceData.mask.y;
#endif
surfaceData.perceptualSmoothness = surfaceData.perceptualSmoothness * decalSurfaceData.mask.w + decalSurfaceData.mask.z;
}
}

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


public int drawDistance = 1000;
public int atlasWidth = 4096;
public int atlasHeight = 4096;
public bool perChannelMask = false;
}
}

3
com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLit.shader


// enable dithering LOD crossfade
#pragma multi_compile _ LOD_FADE_CROSSFADE
// decal 3RT or 4RT toggle
#pragma multi_compile _ _DECALS_4RT
//enable GPU instancing support
#pragma multi_compile_instancing

3
com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLitTessellation.shader


// enable dithering LOD crossfade
#pragma multi_compile _ LOD_FADE_CROSSFADE
// decal 3RT or 4RT toggle
#pragma multi_compile _ _DECALS_4RT
// enable GPU instancing
#pragma multi_compile_instancing

3
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader


// enable dithering LOD crossfade
#pragma multi_compile _ LOD_FADE_CROSSFADE
// decal 3RT or 4RT toggle
#pragma multi_compile _ _DECALS_4RT
//enable GPU instancing support
#pragma multi_compile_instancing

3
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitTessellation.shader


// enable dithering LOD crossfade
#pragma multi_compile _ LOD_FADE_CROSSFADE
// decal 3RT or 4RT toggle
#pragma multi_compile _ _DECALS_4RT
//enable GPU instancing support
#pragma multi_compile_instancing

4
com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader


#pragma shader_feature _STACKLIT_DEBUG
// decal 3RT or 4RT toggle
#pragma multi_compile _ _DECALS_4RT
//enable GPU instancing support
#pragma multi_compile_instancing

26
com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs


// We need to copy depth buffer texture if we want to bind it at this stage
CopyDepthBufferIfNeeded(cmd);
bool rtCount4 = m_Asset.GetRenderPipelineSettings().decalSettings.perChannelMask;
m_DbufferManager.ClearTargets(cmd, hdCamera);
HDUtils.SetRenderTarget(cmd, hdCamera, m_DbufferManager.GetBuffersRTI(), m_CameraDepthStencilBuffer); // do not clear anymore
m_DbufferManager.SetHTile(m_DbufferManager.bufferCount, cmd);
m_DbufferManager.ClearAndSetTargets(cmd, hdCamera, rtCount4, m_CameraDepthStencilBuffer);
renderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();

sorting = { flags = SortFlags.CommonOpaque }
};
drawSettings.SetShaderPassName(0, HDShaderPassNames.s_MeshDecalsName);
if(rtCount4)
{
drawSettings.SetShaderPassName(0, HDShaderPassNames.s_MeshDecalsMName);
drawSettings.SetShaderPassName(1, HDShaderPassNames.s_MeshDecalsAOName);
drawSettings.SetShaderPassName(2, HDShaderPassNames.s_MeshDecalsMAOName);
drawSettings.SetShaderPassName(3, HDShaderPassNames.s_MeshDecalsSName);
drawSettings.SetShaderPassName(4, HDShaderPassNames.s_MeshDecalsMSName);
drawSettings.SetShaderPassName(5, HDShaderPassNames.s_MeshDecalsAOSName);
drawSettings.SetShaderPassName(6, HDShaderPassNames.s_MeshDecalsMAOSName);
}
else
{
drawSettings.SetShaderPassName(0, HDShaderPassNames.s_MeshDecals3RTName);
}
renderContext.DrawRenderers(cullResults.visibleRenderers, ref drawSettings, filterRenderersSettings);
CoreUtils.SetKeyword(cmd, "_DECALS_4RT", m_Asset.GetRenderPipelineSettings().decalSettings.perChannelMask);
renderContext.DrawRenderers(cullResults.visibleRenderers, ref drawSettings, filterRenderersSettings);
DecalSystem.instance.RenderIntoDBuffer(cmd);
m_DbufferManager.UnSetHTile(cmd);
m_DbufferManager.SetHTileTexture(cmd); // mask per 8x8 tile used for optimization when looking up dbuffer values

20
com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs


public static readonly string s_TransparentDepthPostpassStr = "TransparentDepthPostpass";
public static readonly string s_MetaStr = "Meta";
public static readonly string s_ShadowCasterStr = "ShadowCaster";
public static readonly string s_MeshDecalsStr = "DBufferMesh";
public static readonly string s_MeshDecalsMStr = "DBufferMesh_M";
public static readonly string s_MeshDecalsSStr = "DBufferMesh_S";
public static readonly string s_MeshDecalsMSStr = "DBufferMesh_MS";
public static readonly string s_MeshDecalsAOStr = "DBufferMesh_AO";
public static readonly string s_MeshDecalsMAOStr = "DBufferMesh_MAO";
public static readonly string s_MeshDecalsAOSStr = "DBufferMesh_AOS";
public static readonly string s_MeshDecalsMAOSStr = "DBufferMesh_MAOS";
public static readonly string s_MeshDecals3RTStr = "DBufferMesh_3RT";
// ShaderPass name
public static readonly ShaderPassName s_EmptyName = new ShaderPassName(s_EmptyStr);
public static readonly ShaderPassName s_ForwardName = new ShaderPassName(s_ForwardStr);

public static readonly ShaderPassName s_TransparentDepthPrepassName = new ShaderPassName(s_TransparentDepthPrepassStr);
public static readonly ShaderPassName s_TransparentBackfaceName = new ShaderPassName(s_TransparentBackfaceStr);
public static readonly ShaderPassName s_TransparentDepthPostpassName = new ShaderPassName(s_TransparentDepthPostpassStr);
public static readonly ShaderPassName s_MeshDecalsName = new ShaderPassName(s_MeshDecalsStr);
public static readonly ShaderPassName s_MeshDecalsMName = new ShaderPassName(s_MeshDecalsMStr);
public static readonly ShaderPassName s_MeshDecalsSName = new ShaderPassName(s_MeshDecalsSStr);
public static readonly ShaderPassName s_MeshDecalsMSName = new ShaderPassName(s_MeshDecalsMSStr);
public static readonly ShaderPassName s_MeshDecalsAOName = new ShaderPassName(s_MeshDecalsAOStr);
public static readonly ShaderPassName s_MeshDecalsMAOName = new ShaderPassName(s_MeshDecalsMAOStr);
public static readonly ShaderPassName s_MeshDecalsAOSName = new ShaderPassName(s_MeshDecalsAOSStr);
public static readonly ShaderPassName s_MeshDecalsMAOSName = new ShaderPassName(s_MeshDecalsMAOSStr);
public static readonly ShaderPassName s_MeshDecals3RTName = new ShaderPassName(s_MeshDecals3RTStr);
// Legacy name
public static readonly ShaderPassName s_AlwaysName = new ShaderPassName("Always");

正在加载...
取消
保存