浏览代码

add decals to stacklit material

decals correctly affect normal buffer in forward and for stacklit in deferred
/main
Paul Melamed 6 年前
当前提交
26eb1cae
共有 9 个文件被更改,包括 96 次插入38 次删除
  1. 7
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Decal/DecalUI.cs
  2. 6
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/StackLit/StackLitUI.cs
  3. 63
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  4. 12
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalNormalBuffer.shader
  5. 6
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalUtilities.hlsl
  6. 10
      com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader
  7. 5
      com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLitData.hlsl
  8. 23
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
  9. 2
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs

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


protected MaterialProperty drawOrder = new MaterialProperty();
protected const string kDrawOrder = "_DrawOrder";
protected const string kDecalStencilWriteMask = "_DecalStencilWriteMask";
protected const string kDecalStencilRef = "_DecalStencilRef";
protected MaterialEditor m_MaterialEditor;
// This is call by the inspector

CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
CoreUtils.SetKeyword(material, "_MASKMAP", material.GetTexture(kMaskMap));
material.SetInt(HDShaderIDs._StencilRef, (int)HDRenderPipeline.StencilBitMask.Decals);
material.SetInt(HDShaderIDs._StencilMask, (int)HDRenderPipeline.StencilBitMask.Decals);
material.SetInt(kDecalStencilWriteMask, (int)HDRenderPipeline.StencilBitMask.Decals);
material.SetInt(kDecalStencilRef, (int)HDRenderPipeline.StencilBitMask.Decals);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsAOStr, false);
material.SetShaderPassEnabled(HDShaderPassNames.s_MeshDecalsMAOStr, false);

6
com.unity.render-pipelines.high-definition/HDRP/Editor/Material/StackLit/StackLitUI.cs


protected const string kStencilWriteMask = "_StencilWriteMask";
protected const string kStencilRefMV = "_StencilRefMV";
protected const string kStencilWriteMaskMV = "_StencilWriteMaskMV";
protected const string kStencilDepthPrepassRef = "_StencilDepthPrepassRef";
protected const string kStencilDepthPrepassWriteMask = "_StencilDepthPrepassWriteMask";
protected const string k_GeometricNormalFilteringEnabled = "_GeometricNormalFilteringEnabled";
protected const string k_TextureNormalFilteringEnabled = "_TextureNormalFilteringEnabled";

material.SetInt(kStencilWriteMask, (int)HDRenderPipeline.StencilBitMask.LightingMask);
material.SetInt(kStencilRefMV, (int)HDRenderPipeline.StencilBitMask.ObjectVelocity);
material.SetInt(kStencilWriteMaskMV, (int)HDRenderPipeline.StencilBitMask.ObjectVelocity);
// for depth only pass to be used in decal to normal buffer compositing
material.SetInt(kStencilDepthPrepassRef, (int)HDRenderPipeline.StencilBitMask.StackLit);
material.SetInt(kStencilDepthPrepassWriteMask, (int)HDRenderPipeline.StencilBitMask.StackLit);
}
}
} // namespace UnityEditor

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


[ToggleUI] _MaskmapSmoothness("_MaskmapSmoothness", Range(0.0, 1.0)) = 1.0
[HideInInspector] _DecalMeshDepthBias("_DecalMeshDepthBias", Float) = 0.0
[HideInInspector] _DrawOrder("_DrawOrder", Int) = 0
// Stencil state
[HideInInspector] _DecalStencilRef("_DecalStencilRef", Int) = 8
[HideInInspector] _DecalStencilWriteMask("_DecalStencilWriteMask", Int) = 8
}
HLSLINCLUDE

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

// back faces with zfail, for cases when camera is inside the decal volume
Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

// back faces with zfail, for cases when camera is inside the decal volume
Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

Stencil
{
WriteMask[_StencilMask]
Ref[_StencilRef]
WriteMask[_DecalStencilWriteMask]
Ref[_DecalStencilRef]
Comp Always
Pass Replace
}

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


Shader "Hidden/HDRenderPipeline/Material/Decal/DecalNormalBuffer"
{
Properties
{
// Stencil state
[HideInInspector] _DecalNormalBufferStencilRef("_DecalNormalBufferStencilRef", Int) = 0 // set at runtime
[HideInInspector] _DecalNormalBufferStencilReadMask("_DecalNormalBufferStencilReadMask", Int) = 0 // set at runtime
}
HLSLINCLUDE
#pragma target 4.5

Stencil
{
ReadMask[_StencilMask]
Ref[_StencilRef]
ReadMask[_DecalNormalBufferStencilReadMask]
Ref[_DecalNormalBufferStencilRef]
Comp Equal
Pass Zero // doesn't really matter, but clear to 0 for debugging
}

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


surfaceData.metallic = surfaceData.metallic * decalSurfaceData.MAOSBlend.x + decalSurfaceData.mask.x;
surfaceData.ambientOcclusion = surfaceData.ambientOcclusion * decalSurfaceData.MAOSBlend.y + decalSurfaceData.mask.y;
#endif
#if defined(UNITY_MATERIAL_STACKLIT)
surfaceData.perceptualSmoothnessA = surfaceData.perceptualSmoothnessA * decalSurfaceData.mask.w + decalSurfaceData.mask.z;
surfaceData.perceptualSmoothnessB = surfaceData.perceptualSmoothnessB * decalSurfaceData.mask.w + decalSurfaceData.mask.z;
surfaceData.coatPerceptualSmoothness = surfaceData.coatPerceptualSmoothness * decalSurfaceData.mask.w + decalSurfaceData.mask.z;
#else
#endif
}
}
}

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


[HideInInspector] _StencilWriteMask("_StencilWriteMask", Int) = 7 // StencilMask.Lighting (fixed at compile time)
[HideInInspector] _StencilRefMV("_StencilRefMV", Int) = 128 // StencilLightingUsage.RegularLighting (fixed at compile time)
[HideInInspector] _StencilWriteMaskMV("_StencilWriteMaskMV", Int) = 128 // StencilMask.ObjectsVelocity (fixed at compile time)
[HideInInspector] _StencilDepthPrepassRef("_StencilDepthPrepassRef", Int) = 16
[HideInInspector] _StencilDepthPrepassWriteMask("_StencilDepthPrepassWriteMask", Int) = 16
// Blending state
[HideInInspector] _SurfaceType("__surfacetype", Float) = 0.0

Cull[_CullMode]
ZWrite On
Stencil
{
WriteMask[_StencilDepthPrepassWriteMask]
Ref[_StencilDepthPrepassRef]
Comp Always
Pass Replace
}
HLSLPROGRAM

5
com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLitData.hlsl


#include "CoreRP/ShaderLibrary/Sampling/SampleUVMapping.hlsl"
#include "HDRP/Material/BuiltinUtilities.hlsl"
#include "HDRP/Material/MaterialUtilities.hlsl"
#include "HDRP/Material/Decal/DecalUtilities.hlsl"
//-----------------------------------------------------------------------------
// Texture Mapping

surfaceData.coatNormalWS = SurfaceGradientResolveNormal(input.worldToTangent[2], coatGradient.xyz);
surfaceData.tangentWS = Orthonormalize(surfaceData.tangentWS, surfaceData.normalWS);
#if HAVE_DECALS
AddDecalContribution(posInput, surfaceData, alpha);
#endif
if ((_GeometricNormalFilteringEnabled + _TextureNormalFilteringEnabled) > 0.0)
{

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


Clear = 0, // 0x0
LightingMask = 7, // 0x7 - 3 bit
Decals = 8, // 0x8 - 1 bit
StackLit = 16, // 0x10 - 1 bit
ObjectVelocity = 128, // 0x80 - 1 bit
All = 255 // 0xFF - 8 bit
}

m_CopyStencilForNoLighting.SetInt(HDShaderIDs._StencilMask, (int)StencilBitMask.LightingMask);
m_CameraMotionVectorsMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.cameraMotionVectors);
m_DecalNormalBufferMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.decalNormalBuffer);
m_DecalNormalBufferMaterial.SetInt(HDShaderIDs._StencilRef, (int)StencilBitMask.Decals);
m_DecalNormalBufferMaterial.SetInt(HDShaderIDs._StencilMask, (int)StencilBitMask.Decals);
m_CopyDepth = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.copyDepthBuffer);

// We can now bind the normal buffer to be use by any effect
m_NormalBufferManager.BindNormalBuffers(cmd);
if ((hdCamera.frameSettings.enableForwardRenderingOnly) && (!hdCamera.frameSettings.enableMSAA)) // MSAA not supported
if (!hdCamera.frameSettings.enableMSAA) // MSAA not supported
int stencilMask;
int stencilRef;
if (hdCamera.frameSettings.enableForwardRenderingOnly) // in forward rendering all pixels that decals wrote into have to be composited
{
stencilMask = (int)StencilBitMask.Decals;
stencilRef = (int)StencilBitMask.Decals;
}
else // in deferred rendering only pixels affected by both forward materials and decals need to be composited
{
stencilMask = (int)StencilBitMask.Decals | (int)StencilBitMask.StackLit;
stencilRef = (int)StencilBitMask.Decals | (int)StencilBitMask.StackLit;
}
m_DecalNormalBufferMaterial.SetInt(HDShaderIDs._DecalNormalBufferStencilReadMask, stencilMask);
m_DecalNormalBufferMaterial.SetInt(HDShaderIDs._DecalNormalBufferStencilRef, stencilRef);
cmd.SetRandomWriteTarget(1, m_NormalBufferManager.GetNormalBuffer(0));
cmd.SetRandomWriteTarget(1, m_NormalBufferManager.GetNormalBuffer(0));
cmd.DrawProcedural(Matrix4x4.identity, m_DecalNormalBufferMaterial, 0, MeshTopology.Triangles, 3, 1);
cmd.ClearRandomWriteTargets();
}

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


public static readonly int _DecalIndexShift = Shader.PropertyToID("_DecalIndexShift");
public static readonly int _DecalCount = Shader.PropertyToID("_DecalCount");
public static readonly int _DecalDatas = Shader.PropertyToID("_DecalDatas");
public static readonly int _DecalNormalBufferStencilReadMask = Shader.PropertyToID("_DecalNormalBufferStencilReadMask");
public static readonly int _DecalNormalBufferStencilRef = Shader.PropertyToID("_DecalNormalBufferStencilRef");
public static readonly int _WorldSpaceCameraPos = Shader.PropertyToID("_WorldSpaceCameraPos");
public static readonly int _ViewMatrix = Shader.PropertyToID("_ViewMatrix");

正在加载...
取消
保存