浏览代码

Merge pull request #645 from Unity-Technologies/prototype/decals

Prototype/decals
/namespace
GitHub 7 年前
当前提交
bb442f25
共有 40 个文件被更改,包括 881 次插入9 次删除
  1. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDCustomSamplerId.cs
  2. 62
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  3. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitData.hlsl
  5. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
  6. 1
      ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl
  7. 3
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs
  8. 1
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs.hlsl
  9. 8
      ScriptableRenderPipeline/HDRenderPipeline/Decal.meta
  10. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal.meta
  11. 33
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl
  12. 9
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl.meta
  13. 104
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs
  14. 88
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  15. 11
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs.meta
  16. 11
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs.meta
  17. 17
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl
  18. 44
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl
  19. 110
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl
  20. 24
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl
  21. 13
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalUtilities.hlsl
  22. 40
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs
  23. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl.meta
  24. 11
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.meta
  25. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl.meta
  26. 76
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader
  27. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader.meta
  28. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl.meta
  29. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl.meta
  30. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalUtilities.hlsl.meta
  31. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor.meta
  32. 98
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs
  33. 11
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs.meta
  34. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass.meta
  35. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl
  36. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl.meta

1
ScriptableRenderPipeline/HDRenderPipeline/HDCustomSamplerId.cs


DepthPrepass,
TransparentDepthPrepass,
GBuffer,
DBuffer,
DisplayDebugViewMaterial,
DebugViewMaterialGBuffer,
BlitDebugViewMaterialDebug,

62
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


}
}
public class DBufferManager
{
public const int k_MaxDbuffer = 4;
public int dbufferCount { get; set; }
RenderTargetIdentifier[] m_ColorMRTs;
RenderTargetIdentifier[] m_RTIDs = new RenderTargetIdentifier[k_MaxDbuffer];
public void InitDBuffers(int width, int height, CommandBuffer cmd)
{
dbufferCount = Decal.GetMaterialDBufferCount();
RenderTextureFormat[] rtFormat;
RenderTextureReadWrite[] rtReadWrite;
Decal.GetMaterialDBufferDescription(out rtFormat, out rtReadWrite);
for (int dbufferIndex = 0; dbufferIndex < dbufferCount; ++dbufferIndex)
{
cmd.ReleaseTemporaryRT(HDShaderIDs._DBufferTexture[dbufferIndex]);
cmd.GetTemporaryRT(HDShaderIDs._DBufferTexture[dbufferIndex], width, height, 0, FilterMode.Point, rtFormat[dbufferIndex], rtReadWrite[dbufferIndex]);
m_RTIDs[dbufferIndex] = new RenderTargetIdentifier(HDShaderIDs._DBufferTexture[dbufferIndex]);
}
}
public RenderTargetIdentifier[] GetDBuffers()
{
if (m_ColorMRTs == null || m_ColorMRTs.Length != dbufferCount)
m_ColorMRTs = new RenderTargetIdentifier[dbufferCount];
for (int index = 0; index < dbufferCount; index++)
{
m_ColorMRTs[index] = m_RTIDs[index];
}
return m_ColorMRTs;
}
}
public partial class HDRenderPipeline : RenderPipeline
{
enum ForwardPass

readonly List<RenderPipelineMaterial> m_MaterialList = new List<RenderPipelineMaterial>();
readonly GBufferManager m_GbufferManager = new GBufferManager();
readonly DBufferManager m_DbufferManager = new DBufferManager();
readonly SubsurfaceScatteringManager m_SSSBufferManager = new SubsurfaceScatteringManager();
// Renderer Bake configuration can vary depends on if shadow mask is enabled or no

}
ConfigureForShadowMask(enableBakeShadowMask, cmd);
InitAndClearBuffer(hdCamera, enableBakeShadowMask, cmd);
InitAndClearBuffer(hdCamera, enableBakeShadowMask, cmd);
RenderDepthPrepass(m_CullResults, hdCamera, renderContext, cmd);
RenderDepthPrepass(m_CullResults, hdCamera, renderContext, cmd, true);
RenderDBuffer(hdCamera.cameraPos, renderContext, cmd);
RenderGBuffer(m_CullResults, hdCamera, renderContext, cmd);

// Forward only renderer: We always render everything
// Deferred renderer: We render a depth prepass only if engine request it. We can decide if we render everything or only opaque alpha tested object.
// Forward opaque with deferred renderer (DepthForwardOnly pass): We always render everything
void RenderDepthPrepass(CullResults cull, HDCamera hdCamera, ScriptableRenderContext renderContext, CommandBuffer cmd)
void RenderDepthPrepass(CullResults cull, HDCamera hdCamera, ScriptableRenderContext renderContext, CommandBuffer cmd, bool hasDecals)
{
// In case of deferred renderer, we can have forward opaque material. These materials need to be render in the depth buffer to correctly build the light list.
// And they will tag the stencil to not be lit during the deferred lighting pass.

using (new ProfilingSample(cmd, addAlphaTestedOnly ? "Depth Prepass alpha test" : "Depth Prepass", GetSampler(CustomSamplerId.DepthPrepass)))
{
CoreUtils.SetRenderTarget(cmd, m_CameraDepthStencilBufferRT);
if (addFullDepthPrepass && !addAlphaTestedOnly) // Always true in case of forward rendering, use in case of deferred rendering if requesting a full depth prepass
if (hasDecals || (addFullDepthPrepass && !addAlphaTestedOnly)) // Always true in case of forward rendering, use in case of deferred rendering if requesting a full depth prepass
{
// We render first the opaque object as opaque alpha tested are more costly to render and could be reject by early-z (but not Hi-z as it is disable with clip instruction)
// This is handled automatically with the RenderQueue value (OpaqueAlphaTested have a different value and thus are sorted after Opaque)

RenderOpaqueRenderList(cull, camera, renderContext, cmd, HDShaderPassNames.s_GBufferName, m_currentRendererConfigurationBakedLighting, RenderQueueRange.opaque, m_DepthStateOpaque);
}
}
}
}
void RenderDBuffer(Vector3 cameraPos, ScriptableRenderContext renderContext, CommandBuffer cmd)
{
using (new ProfilingSample(cmd, "Decals", GetSampler(CustomSamplerId.DBuffer)))
{
CoreUtils.SetRenderTarget(cmd, m_DbufferManager.GetDBuffers(), m_CameraDepthStencilBufferRT, ClearFlag.Color, CoreUtils.clearColorAllBlack);
cmd.SetGlobalTexture(HDShaderIDs._MainDepthTexture, GetDepthTexture());
DecalSystem.instance.Render(renderContext, cameraPos, cmd);
}
}

// We need to allocate target for SSS
m_SSSBufferManager.InitGBuffers(w, h, cmd);
}
m_DbufferManager.InitDBuffers(w, h, cmd);
CoreUtils.SetRenderTarget(cmd, m_CameraColorBufferRT, m_CameraDepthStencilBufferRT, ClearFlag.Depth);
}

10
ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs


Shader.PropertyToID("_GBufferTexture7")
};
public static readonly int[] _SSSBufferTexture =
public static readonly int[] _DBufferTexture =
{
Shader.PropertyToID("_DBufferTexture0"),
Shader.PropertyToID("_DBufferTexture1"),
Shader.PropertyToID("_DBufferTexture2"),
Shader.PropertyToID("_DBufferTexture3")
};
public static readonly int[] _SSSBufferTexture =
{
Shader.PropertyToID("_SSSBufferTexture0"),
Shader.PropertyToID("_SSSBufferTexture1"),

2
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitData.hlsl


surfaceData.specularOcclusion = 1.0;
#endif
AddDecalContribution(posInput.positionSS, surfaceData);
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);
}

8
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl


#include "ShaderLibrary/SampleUVMapping.hlsl"
#include "../MaterialUtilities.hlsl"
#include "ShaderLibrary/SampleUVMapping.hlsl"
#include "../MaterialUtilities.hlsl"
#include "../Decal/DecalUtilities.hlsl"
// TODO: move this function to commonLighting.hlsl once validated it work correctly
float GetSpecularOcclusionFromBentAO(float3 V, float3 bentNormalWS, SurfaceData surfaceData)

// This is use with anisotropic material
surfaceData.tangentWS = Orthonormalize(surfaceData.tangentWS, surfaceData.normalWS);
AddDecalContribution(posInput.positionSS, surfaceData);
// Caution: surfaceData must be fully initialize before calling GetBuiltinData
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);

1
ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl


#endif // #ifdef GBUFFERMATERIAL_COUNT
#endif // UNITY_MATERIAL_INCLUDED

3
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs


LightTransport,
Shadows,
SubsurfaceScattering,
VolumetricLighting
VolumetricLighting,
DBuffer
}
}

1
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs.hlsl


#define SHADERPASS_SHADOWS (7)
#define SHADERPASS_SUBSURFACE_SCATTERING (8)
#define SHADERPASS_VOLUMETRIC_LIGHTING (9)
#define SHADERPASS_DBUFFER (10)
#endif

8
ScriptableRenderPipeline/HDRenderPipeline/Decal.meta


fileFormatVersion: 2
guid: e23938c16411b694c9f86517a2ad5e5e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal.meta


fileFormatVersion: 2
guid: ce1e252deae73a04a96dc6d2014164ca
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

33
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl


#if SHADERPASS != SHADERPASS_DBUFFER
#error SHADERPASS_is_not_correctly_define
#endif
#include "VertMesh.hlsl"
PackedVaryingsType Vert(AttributesMesh inputMesh)
{
VaryingsType varyingsType;
varyingsType.vmesh = VertMesh(inputMesh);
return PackVaryingsType(varyingsType);
}
void Frag( PackedVaryingsToPS packedInput,
OUTPUT_DBUFFER(outDBuffer)
)
{
PositionInputs posInput = GetPositionInput(packedInput.vmesh.positionCS, _ScreenSize.zw);
float d = LOAD_TEXTURE2D(_MainDepthTexture, posInput.positionSS).x;
UpdatePositionInput(d, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP, posInput);
float3 positionWS = posInput.positionWS;
float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);
DecalSurfaceData surfaceData;
GetSurfaceData(positionDS.xz, surfaceData);
ENCODE_INTO_DBUFFER(surfaceData, outDBuffer);
}

9
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl.meta


fileFormatVersion: 2
guid: 3e465a3a5bd2869419f16363a863b82d
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

104
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs


using System;
using UnityEditor;
using UnityEngine;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[ExecuteInEditMode]
public class DecalProjectorComponent : MonoBehaviour
{
private static readonly int m_WorldToDecal = Shader.PropertyToID("_WorldToDecal");
private static readonly int m_DecalToWorldR = Shader.PropertyToID("_DecalToWorldR");
public Material m_Material;
private MaterialPropertyBlock m_PropertyBlock;
public void OnEnable()
{
DecalSystem.instance.AddDecal(this);
}
public void Start()
{
m_PropertyBlock = new MaterialPropertyBlock();
DecalSystem.instance.AddDecal(this);
}
public void OnDisable()
{
DecalSystem.instance.RemoveDecal(this);
}
public void OnValidate()
{
if (m_Material != null)
{
Shader shader = m_Material.shader;
if((shader != null) && (shader.name != "HDRenderPipeline/Decal"))
{
Debug.LogWarning("Decal projector component material is not using HDRenderPipeline/Decal shader.", this);
}
}
}
private void DrawGizmo(bool selected)
{
var col = new Color(0.0f, 0.7f, 1f, 1.0f);
col.a = selected ? 0.3f : 0.1f;
Gizmos.color = col;
Matrix4x4 offset = Matrix4x4.Translate(new Vector3(0.0f, -0.5f, 0.0f));
Gizmos.matrix = transform.localToWorldMatrix * offset;
Gizmos.DrawCube(Vector3.zero, Vector3.one);
col.a = selected ? 0.5f : 0.2f;
Gizmos.color = col;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
}
public void OnDrawGizmosSelected()
{
DrawGizmo(true);
}
[MenuItem("GameObject/Effects/Decal", false, 0)]
static void CreateDecal(MenuCommand menuCommand)
{
// Create a custom game object
GameObject go = new GameObject("Decal");
go.AddComponent<DecalProjectorComponent>();
// Ensure it gets re-parented if this was a context click (otherwise does nothing)
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// Register the creation in the undo system
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
Selection.activeObject = go;
}
public void UpdatePropertyBlock(Vector3 cameraPos)
{
Matrix4x4 CRWStoAWS = new Matrix4x4();
if (ShaderConfig.s_CameraRelativeRendering == 1)
{
CRWStoAWS = Matrix4x4.Translate(cameraPos);
}
else
{
CRWStoAWS = Matrix4x4.identity;
}
Matrix4x4 final = transform.localToWorldMatrix;
Matrix4x4 decalToWorldR = Matrix4x4.Rotate(transform.localRotation);
Matrix4x4 worldToDecal = Matrix4x4.Translate(new Vector3(0.5f, 0.0f, 0.5f)) * Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f)) * final.inverse;
if (m_PropertyBlock == null)
{
m_PropertyBlock = new MaterialPropertyBlock();
}
m_PropertyBlock.SetMatrix(m_DecalToWorldR, decalToWorldR);
m_PropertyBlock.SetMatrix(m_WorldToDecal, worldToDecal * CRWStoAWS);
}
public MaterialPropertyBlock GetPropertyBlock()
{
return m_PropertyBlock;
}
}
}

88
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


using System.Collections.Generic;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public class DecalSystem
{
static DecalSystem m_Instance;
static public DecalSystem instance
{
get
{
if (m_Instance == null)
m_Instance = new DecalSystem();
return m_Instance;
}
}
internal HashSet<DecalProjectorComponent> m_Decals = new HashSet<DecalProjectorComponent>();
Mesh m_CubeMesh;
public DecalSystem()
{
CreateCubeMesh();
}
void CreateCubeMesh()
{
m_CubeMesh = new Mesh();
Vector3[] vertices = new Vector3[8];
vertices[0] = new Vector3(-0.5f, -1.0f, -0.5f);
vertices[1] = new Vector3( 0.5f, -1.0f, -0.5f);
vertices[2] = new Vector3( 0.5f, 0.0f, -0.5f);
vertices[3] = new Vector3(-0.5f, 0.0f, -0.5f);
vertices[4] = new Vector3(-0.5f, -1.0f, 0.5f);
vertices[5] = new Vector3( 0.5f, -1.0f, 0.5f);
vertices[6] = new Vector3( 0.5f, 0.0f, 0.5f);
vertices[7] = new Vector3(-0.5f, 0.0f, 0.5f);
m_CubeMesh.vertices = vertices;
int[] triangles = new int[36];
triangles[0] = 0; triangles[1] = 2; triangles[2] = 1;
triangles[3] = 0; triangles[4] = 3; triangles[5] = 2;
triangles[6] = 1; triangles[7] = 6; triangles[8] = 5;
triangles[9] = 1; triangles[10] = 2; triangles[11] = 6;
triangles[12] = 5; triangles[13] = 7; triangles[14] = 4;
triangles[15] = 5; triangles[16] = 6; triangles[17] = 7;
triangles[18] = 4; triangles[19] = 3; triangles[20] = 0;
triangles[21] = 4; triangles[22] = 7; triangles[23] = 3;
triangles[24] = 3; triangles[25] = 6; triangles[26] = 2;
triangles[27] = 3; triangles[28] = 7; triangles[29] = 6;
triangles[30] = 4; triangles[31] = 1; triangles[32] = 5;
triangles[33] = 4; triangles[34] = 0; triangles[35] = 1;
m_CubeMesh.triangles = triangles;
}
public void AddDecal(DecalProjectorComponent d)
{
if (d.m_Material.GetTexture("_BaseColorMap") || d.m_Material.GetTexture("_NormalMap"))
{
RemoveDecal(d);
m_Decals.Add(d);
}
}
public void RemoveDecal(DecalProjectorComponent d)
{
m_Decals.Remove(d);
}
public void Render(ScriptableRenderContext renderContext, Vector3 cameraPos, CommandBuffer cmd)
{
if (m_CubeMesh == null)
CreateCubeMesh();
foreach (var decal in m_Decals)
{
decal.UpdatePropertyBlock(cameraPos);
cmd.DrawMesh(m_CubeMesh, decal.transform.localToWorldMatrix, decal.m_Material, 0, 0, decal.GetPropertyBlock());
}
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs.meta


fileFormatVersion: 2
guid: f19d9143a39eb3b46bc4563e9889cfbd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs.meta


fileFormatVersion: 2
guid: 3305c9249c40f8b43a953a8a6eb87a82
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

17
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl


#ifndef UNITY_DECALPROPERTIES_INCLUDED
#define UNITY_DECALPROPERTIES_INCLUDED
TEXTURE2D(_BaseColorMap);
SAMPLER2D(sampler_BaseColorMap);
TEXTURE2D(_NormalMap);
SAMPLER2D(sampler_NormalMap);
float _DecalBlend;
CBUFFER_START(Decal)
float4x4 _WorldToDecal;
float4x4 _DecalToWorldR;
CBUFFER_END
#endif

44
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl


//
// This file was automatically generated. Please don't edit by hand.
//
#ifndef DECAL_CS_HLSL
#define DECAL_CS_HLSL
//
// UnityEngine.Experimental.Rendering.HDPipeline.Decal+DecalSurfaceData: static fields
//
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_BASE_COLOR (10000)
#define DEBUGVIEW_DECAL_DECALSURFACEDATA_NORMAL_WS (10001)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Decal+DBufferMaterial: static fields
//
#define DBUFFERMATERIAL_COUNT (2)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Decal+DecalSurfaceData
// PackingRules = Exact
struct DecalSurfaceData
{
float4 baseColor;
float4 normalWS;
};
//
// Debug functions
//
void GetGeneratedDecalSurfaceDataDebug(uint paramId, DecalSurfaceData decalsurfacedata, inout float3 result, inout bool needLinearToSRGB)
{
switch (paramId)
{
case DEBUGVIEW_DECAL_DECALSURFACEDATA_BASE_COLOR:
result = decalsurfacedata.baseColor.xyz;
needLinearToSRGB = true;
break;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_NORMAL_WS:
result = decalsurfacedata.normalWS.xyz;
break;
}
}
#endif

110
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl


#include "Decal.cs.hlsl"
#define DBufferType0 float4
#define DBufferType1 float4
#define DBufferType2 float4
#define DBufferType3 float4
#ifdef DBUFFERMATERIAL_COUNT
#if DBUFFERMATERIAL_COUNT == 1
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0
#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);
#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)
#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)
#elif DBUFFERMATERIAL_COUNT == 3
#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 ENCODE_INTO_DBUFFER(DECAL_SURFACE_DATA, NAME) EncodeIntoDBuffer(DECAL_SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2))
#define DECODE_FROM_DBUFFER(NAME, DECAL_SURFACE_DATA) DecodeFromDBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2), DECAL_SURFACE_DATA)
#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
#endif // #ifdef DBUFFERMATERIAL_COUNT
// Must be in sync with RT declared in HDRenderPipeline.cs ::Rebuild
void EncodeIntoDBuffer( DecalSurfaceData surfaceData,
out DBufferType0 outDBuffer0,
out DBufferType1 outDBuffer1
)
{
outDBuffer0 = surfaceData.baseColor;
outDBuffer1 = surfaceData.normalWS;
}
void DecodeFromDBuffer(
DBufferType0 inDBuffer0,
DBufferType1 inDBuffer1,
out DecalSurfaceData surfaceData
)
{
ZERO_INITIALIZE(DecalSurfaceData, surfaceData);
surfaceData.baseColor = inDBuffer0;
surfaceData.normalWS.xyz = inDBuffer1.xyz * 2.0f - 1.0f;
surfaceData.normalWS.w = inDBuffer1.w;
}

24
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl


//-------------------------------------------------------------------------------------
// Fill SurfaceData/Builtin data function
//-------------------------------------------------------------------------------------
#include "ShaderLibrary/Packing.hlsl"
#include "ShaderLibrary/SampleUVMapping.hlsl"
void GetSurfaceData(float2 texCoordDS, out DecalSurfaceData surfaceData)
{
surfaceData.baseColor = float4(0,0,0,0);
surfaceData.normalWS = float4(0,0,0,0);
float totalBlend = _DecalBlend;
#if _COLORMAP
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, texCoordDS.xy);
totalBlend *= surfaceData.baseColor.w;
surfaceData.baseColor.w = totalBlend;
#endif
UVMapping texCoord;
ZERO_INITIALIZE(UVMapping, texCoord);
texCoord.uv = texCoordDS.xy;
#if _NORMALMAP
surfaceData.normalWS.xyz = mul((float3x3)_DecalToWorldR, SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1)) * 0.5f + 0.5f;
surfaceData.normalWS.w = totalBlend;
#endif
}

13
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalUtilities.hlsl


#include "Decal.hlsl"
DECLARE_DBUFFER_TEXTURE(_DBufferTexture);
void AddDecalContribution(uint2 unPositionSS, inout SurfaceData surfaceData)
{
FETCH_DBUFFER(DBuffer, _DBufferTexture, unPositionSS);
DecalSurfaceData decalSurfaceData;
DECODE_FROM_DBUFFER(DBuffer, decalSurfaceData);
surfaceData.baseColor.xyz = lerp(surfaceData.baseColor.xyz, decalSurfaceData.baseColor.xyz, decalSurfaceData.baseColor.w);
surfaceData.normalWS.xyz = normalize(lerp(surfaceData.normalWS.xyz, decalSurfaceData.normalWS.xyz, decalSurfaceData.normalWS.w));
}

40
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs


using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class Decal
{
// Main structure that store the user data (i.e user input of master node in material graph)
[GenerateHLSL(PackingRules.Exact, false, true, 10000)]
public struct DecalSurfaceData
{
[SurfaceDataAttributes("Base Color", false, true)]
public Vector4 baseColor;
[SurfaceDataAttributes("Normal", true)]
public Vector4 normalWS;
};
[GenerateHLSL(PackingRules.Exact)]
public enum DBufferMaterial
{
// Note: This count doesn't include the velocity buffer. On shader and csharp side the velocity buffer will be added by the framework
Count = 2
};
//-----------------------------------------------------------------------------
// DBuffer management
//-----------------------------------------------------------------------------
// 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};
static RenderTextureReadWrite[] m_RTReadWrite = { RenderTextureReadWrite.sRGB, RenderTextureReadWrite.Linear};
static public void GetMaterialDBufferDescription(out RenderTextureFormat[] RTFormat, out RenderTextureReadWrite[] RTReadWrite)
{
RTFormat = m_RTFormat;
RTReadWrite = m_RTReadWrite;
}
}
}

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl.meta


fileFormatVersion: 2
guid: 2f597db7b26e3364e96b55892e001ff8
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

11
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.meta


fileFormatVersion: 2
guid: 77304d3868098cb40af6328dba0bafda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl.meta


fileFormatVersion: 2
guid: 24e76285bfb1d4648aeaf39ced47c9ee
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

76
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader


Shader "HDRenderPipeline/Decal"
{
Properties
{
_BaseColorMap("BaseColorMap", 2D) = "white" {}
_NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map
_DecalBlend("_DecalBlend", Range(0.0, 1.0)) = 0.5
}
HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal
//#pragma enable_d3d11_debug_symbols
//-------------------------------------------------------------------------------------
// Variant
//-------------------------------------------------------------------------------------
#pragma shader_feature _COLORMAP
#pragma shader_feature _NORMALMAP
//-------------------------------------------------------------------------------------
// Define
//-------------------------------------------------------------------------------------
#define UNITY_MATERIAL_DECAL // do we need this now that Material.hlsl is not getting included?
//-------------------------------------------------------------------------------------
// Include
//-------------------------------------------------------------------------------------
#include "ShaderLibrary/Common.hlsl"
#include "ShaderLibrary/Wind.hlsl"
#include "../../ShaderPass/FragInputs.hlsl"
#include "../../ShaderPass/ShaderPass.cs.hlsl"
//-------------------------------------------------------------------------------------
// variable declaration
//-------------------------------------------------------------------------------------
#include "DecalProperties.hlsl"
// All our shaders use same name for entry point
#pragma vertex Vert
#pragma fragment Frag
ENDHLSL
SubShader
{
Pass
{
Name "DBuffer" // Name is not used
Tags { "LightMode" = "DBuffer" } // This will be only for opaque object based on the RenderQueue index
// need to optimize this and use proper Cull and ZTest modes for cases when decal geometry is clipped by camera
Cull Off
ZWrite Off
ZTest Always
HLSLPROGRAM
#define SHADERPASS SHADERPASS_DBUFFER
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}
}
CustomEditor "Experimental.Rendering.HDPipeline.DecalUI"
}

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader.meta


fileFormatVersion: 2
guid: 1d64af84bdc970c4fae0c1e06dd95b73
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl.meta


fileFormatVersion: 2
guid: 8d7b72155c1f8c74592263fbe5a557df
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl.meta


fileFormatVersion: 2
guid: 295b85a50f6edfe488511041c695c48a
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalUtilities.hlsl.meta


fileFormatVersion: 2
guid: 025b8dd135d8eac4da1b8178d944d4a6
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

8
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor.meta


fileFormatVersion: 2
guid: 63b8d3eb0967b4f41b631c98f7e9b027
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

98
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class DecalUI : ShaderGUI
{
protected static class Styles
{
public static string InputsText = "Inputs";
public static GUIContent baseColorText = new GUIContent("Base Color + Blend", "Albedo (RGB) and Blend Factor (A)");
public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map (BC7/BC5/DXT5(nm))");
public static GUIContent decalBlendText = new GUIContent("Decal Blend", "Combines with Base Color (A)");
}
protected MaterialProperty baseColorMap = new MaterialProperty();
protected const string kBaseColorMap = "_BaseColorMap";
protected MaterialProperty normalMap = new MaterialProperty();
protected const string kNormalMap = "_NormalMap";
protected MaterialProperty decalBlend = new MaterialProperty();
protected const string kDecalBlend = "_DecalBlend";
protected MaterialEditor m_MaterialEditor;
// This is call by the inspector
void FindMaterialProperties(MaterialProperty[] props)
{
baseColorMap = FindProperty(kBaseColorMap, props);
normalMap = FindProperty(kNormalMap, props);
decalBlend = FindProperty(kDecalBlend, props);
}
// 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)
{
CoreUtils.SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));
CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
}
protected void SetupMaterialKeywordsAndPassInternal(Material material)
{
SetupMaterialKeywordsAndPass(material);
}
public void ShaderPropertiesGUI(Material material)
{
// Use default labelWidth
EditorGUIUtility.labelWidth = 0f;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.LabelField(Styles.InputsText, EditorStyles.boldLabel);
EditorGUI.indentLevel++;
m_MaterialEditor.TexturePropertySingleLine(Styles.baseColorText, baseColorMap);
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalMap);
m_MaterialEditor.ShaderProperty(decalBlend, Styles.decalBlendText);
EditorGUI.indentLevel--;
}
if (EditorGUI.EndChangeCheck())
{
foreach (var obj in m_MaterialEditor.targets)
SetupMaterialKeywordsAndPassInternal((Material)obj);
}
}
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
{
m_MaterialEditor = materialEditor;
// We should always do this call at the beginning
m_MaterialEditor.serializedObject.Update();
FindMaterialProperties(props);
Material material = materialEditor.target as Material;
ShaderPropertiesGUI(material);
// We should always do this call at the end
m_MaterialEditor.serializedObject.ApplyModifiedProperties();
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs.meta


fileFormatVersion: 2
guid: bd489cfbe365c5749980ef35538739db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass.meta


fileFormatVersion: 2
guid: c95c191806c59d74ebecbd500cbed836
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl


#ifndef SHADERPASS
#error Undefine_SHADERPASS
#endif
// This include will define the various Attributes/Varyings structure
#include "../../ShaderPass/VaryingMesh.hlsl"

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl.meta


fileFormatVersion: 2
guid: 264d5aec19233224fa311299fb752e53
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存