浏览代码

mesh decals work with some per channel masks

/main
Paul Melamed 7 年前
当前提交
de7b8a99
共有 9 个文件被更改,包括 217 次插入43 次删除
  1. 48
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Decal/DecalUI.cs
  2. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs
  3. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.cs.hlsl
  4. 4
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.hlsl
  5. 141
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  6. 43
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalData.hlsl
  7. 4
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalUtilities.hlsl
  8. 6
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
  9. 10
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs

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


Metal_AO_Smoothness,
Metal_Smoothness,
Metal,
AO,
Smoothness
Smoothness,
AO
}
protected string[] blendModeNames = Enum.GetNames(typeof(BlendMode));

CoreUtils.SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));
CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
CoreUtils.SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap));
CoreUtils.SetKeyword(material, "_NORMAL_BLEND_MASK_B", material.GetFloat(kNormalBlendSrc) == 1.0f);
CoreUtils.SetKeyword(material, "_MAOS_BLEND_MASK_B", material.GetFloat(kMaskBlendSrc) == 1.0f);
if (material.GetFloat(kMaskBlendMode) == 0.0f)
{
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsStr, true);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
}
else if (material.GetFloat(kMaskBlendMode) == 1.0f)
{
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, true);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
}
else if (material.GetFloat(kMaskBlendMode) == 2.0f)
{
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, true);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
}
else if (material.GetFloat(kMaskBlendMode) == 3.0f)
{
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, true);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
}
else if (material.GetFloat(kMaskBlendMode) == 4.0f)
{
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsSStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, true);
}
}
protected void SetupMaterialKeywordsAndPassInternal(Material material)

m_MaterialEditor.TexturePropertySingleLine(Styles.baseColorText2, baseColorMap, baseColor);
}
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalMap);
normalBlendSrcValue = EditorGUILayout.Popup( "Normal Per Pixel Blend", (int)normalBlendSrcValue, blendSourceNames);
normalBlendSrcValue = EditorGUILayout.Popup( "Normal blend source", (int)normalBlendSrcValue, blendSourceNames);
m_MaterialEditor.TexturePropertySingleLine(Styles.maskMapText, maskMap);
maskBlendSrcValue = EditorGUILayout.Popup( "Mask blend source", (int)maskBlendSrcValue, blendSourceNames);
maskBlendModeValue = EditorGUILayout.Popup( "Mask blend mode", (int)maskBlendModeValue, blendModeNames);

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


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

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


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

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


outDBuffer0 = surfaceData.baseColor;
outDBuffer1 = surfaceData.normalWS;
outDBuffer2 = surfaceData.mask;
outDBuffer3 = surfaceData.AOSBlend;
outDBuffer3 = surfaceData.MAOSBlend;
}
void DecodeFromDBuffer(

surfaceData.normalWS.xyz = inDBuffer1.xyz * 2.0f - 1.0f;
surfaceData.normalWS.w = inDBuffer1.w;
surfaceData.mask = inDBuffer2;
surfaceData.AOSBlend = inDBuffer3;
surfaceData.MAOSBlend = inDBuffer3;
}

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


#pragma shader_feature _MASKMAP
#pragma shader_feature _ALBEDOCONTRIBUTION
#pragma shader_feature _NORMAL_BLEND_ALBEDO_A
#pragma shader_feature _METALNESS_BLEND_ZERO
#pragma shader_feature _METALNESS_BLEND_ALBEDO_A
#pragma shader_feature _METALNESS_BLEND_MASK_B
#pragma shader_feature _AO_BLEND_ZERO
#pragma shader_feature _AO_BLEND_ALBEDO_A
#pragma shader_feature _AO_BLEND_MASK_B
#pragma shader_feature _SMOOTHNESS_BLEND_ZERO
#pragma shader_feature _SMOOTHNESS_BLEND_ALBEDO_A
#pragma shader_feature _SMOOTHNESS_BLEND_MASK_B
#pragma shader_feature _MAOS_BLEND_MASK_B
#pragma multi_compile_instancing
//-------------------------------------------------------------------------------------

Pass
{
Name "DBufferProjector" // Name is not used
Tags { "LightMode" = "DBufferProjector" } // This will be only for opaque object based on the RenderQueue index
Tags { "LightMode" = "DBufferProjector" } // pass 0 for decal projectors
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front

Pass
{
Name "DBufferMesh" // Name is not used
Tags{"LightMode" = "DBufferMesh"} // This will be only for opaque object based on the RenderQueue index
Name "DBufferMesh_MAOS" // Name is not used
Tags{"LightMode" = "DBufferMesh_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
#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"} // Metalness and smoothness
Cull Back
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_M" // Name is not used
Tags{"LightMode" = "DBufferMesh_M"} // Metalness only
Cull Back
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_S" // Name is not used
Tags{"LightMode" = "DBufferMesh_S"} // Metalness only
Cull Back
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 BA 2 // smoothness/smoothness alpha
ColorMask 0 3
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
Cull Back
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

43
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.AOSBlend = float2(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 _MAOS_BLEND_MASK_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_MASK_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);
}

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


}
if(mask & DBUFFERHTILEBIT_MASK)
{
surfaceData.metallic = surfaceData.metallic * decalSurfaceData.mask.w + decalSurfaceData.mask.x;
surfaceData.ambientOcclusion = surfaceData.ambientOcclusion * decalSurfaceData.mask.w + decalSurfaceData.mask.y;
surfaceData.metallic = surfaceData.metallic * decalSurfaceData.MAOSBlend.x + decalSurfaceData.mask.x;
surfaceData.ambientOcclusion = surfaceData.ambientOcclusion * decalSurfaceData.MAOSBlend.y + decalSurfaceData.mask.y;
surfaceData.perceptualSmoothness = surfaceData.perceptualSmoothness * decalSurfaceData.mask.w + decalSurfaceData.mask.z;
}
}

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


sorting = { flags = SortFlags.CommonOpaque }
};
drawSettings.SetShaderPassName(0, HDShaderPassNames.s_MeshDecalsName);
drawSettings.SetShaderPassName(1, HDShaderPassNames.s_MeshDecalsMSName);
drawSettings.SetShaderPassName(2, HDShaderPassNames.s_MeshDecalsMName);
drawSettings.SetShaderPassName(3, HDShaderPassNames.s_MeshDecalsSName);
drawSettings.SetShaderPassName(4, HDShaderPassNames.s_MeshDecalsAOName);
FilterRenderersSettings filterRenderersSettings = new FilterRenderersSettings(true)
{
renderQueueRange = HDRenderQueue.k_RenderQueue_AllOpaque

10
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_MeshDecalsStr = "DBufferMesh_MAOS";
public static readonly string s_MeshDecalsMSStr = "DBufferMesh_MS";
public static readonly string s_MeshDecalsMStr = "DBufferMesh_M";
public static readonly string s_MeshDecalsSStr = "DBufferMesh_S";
public static readonly string s_MeshDecalsAOStr = "DBufferMesh_AO";
// ShaderPass name
public static readonly ShaderPassName s_EmptyName = new ShaderPassName(s_EmptyStr);

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_MeshDecalsMSName = new ShaderPassName(s_MeshDecalsMSStr);
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_MeshDecalsAOName = new ShaderPassName(s_MeshDecalsAOStr);
// Legacy name
public static readonly ShaderPassName s_AlwaysName = new ShaderPassName("Always");

正在加载...
取消
保存