浏览代码

refactor to address all comments by Sebastien in slack conversation.

/Add-support-for-light-specular-color-tint
Paul Melamed 7 年前
当前提交
d9d7b2cb
共有 15 个文件被更改,包括 312 次插入260 次删除
  1. 53
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  2. 19
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  3. 12
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs
  4. 25
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl
  5. 108
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl
  6. 80
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader
  7. 15
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl
  8. 11
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs
  9. 22
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
  10. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl
  11. 21
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl
  12. 7
      ScriptableRenderPipeline/HDRenderPipeline/ShaderVariables.hlsl
  13. 110
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs
  14. 13
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalUtilities.hlsl
  15. 68
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs

53
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


}
}
internal HashSet<DecalComponent> m_Decals = new HashSet<DecalComponent>();
internal HashSet<DecalProjectorComponent> m_Decals = new HashSet<DecalProjectorComponent>();
Mesh m_CubeMesh;
private static readonly int m_WorldToDecal = Shader.PropertyToID("_WorldToDecal");

int[] triangles = new int[36];
triangles[0] = 0; triangles[1] = 1; triangles[2] = 2;
triangles[3] = 0; triangles[4] = 2; triangles[5] = 3;
triangles[6] = 1; triangles[7] = 5; triangles[8] = 6;
triangles[9] = 1; triangles[10] = 6; triangles[11] = 2;
triangles[12] = 5; triangles[13] = 4; triangles[14] = 7;
triangles[15] = 5; triangles[16] = 7; triangles[17] = 6;
triangles[18] = 4; triangles[19] = 0; triangles[20] = 3;
triangles[21] = 4; triangles[22] = 3; triangles[23] = 7;
triangles[24] = 3; triangles[25] = 2; triangles[26] = 6;
triangles[27] = 3; triangles[28] = 6; triangles[29] = 7;
triangles[30] = 4; triangles[31] = 5; triangles[32] = 1;
triangles[33] = 4; triangles[34] = 1; triangles[35] = 0;
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;
public void AddDecal(DecalComponent d)
public void AddDecal(DecalProjectorComponent d)
{
if (d.m_Material.GetTexture("_BaseColorMap") || d.m_Material.GetTexture("_NormalMap"))
{

}
public void RemoveDecal(DecalComponent d)
public void RemoveDecal(DecalProjectorComponent d)
{
m_Decals.Remove(d);
}

if (m_CubeMesh == null)
CreateCubeMesh();
Matrix4x4 CRWStoAWS = new Matrix4x4();
if (ShaderConfig.s_CameraRelativeRendering == 1)
{
Vector4 worldSpaceCameraPos = Shader.GetGlobalVector(HDShaderIDs._WorldSpaceCameraPos);
CRWStoAWS = Matrix4x4.Translate(worldSpaceCameraPos);
}
else
{
CRWStoAWS = Matrix4x4.identity;
}
Matrix4x4 final = decal.transform.localToWorldMatrix;
Matrix4x4 decalToWorldR = Matrix4x4.Rotate(decal.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;
cmd.SetGlobalMatrix(m_WorldToDecal, worldToDecal * CRWStoAWS);
cmd.SetGlobalMatrix(m_DecalToWorldR, decalToWorldR);
//DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int submeshIndex, int shaderPass);
cmd.DrawMesh(m_CubeMesh, final, decal.m_Material, 0, 0);
if (decal.transform.hasChanged)
{
decal.UpdatePropertyBlock();
decal.transform.hasChanged = false;
}
cmd.DrawMesh(m_CubeMesh, decal.transform.localToWorldMatrix, decal.m_Material, 0, 0, decal.GetPropertyBlock());
}
}
}

19
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


RenderDepthPrepass(m_CullResults, camera, renderContext, cmd, true);
using (new ProfilingSample(cmd, "Decals"))
{
CoreUtils.SetRenderTarget(cmd, m_DbufferManager.GetGBuffers(), m_CameraDepthStencilBufferRT);
cmd.SetGlobalTexture(HDShaderIDs._CameraDepthTexture, GetDepthTexture());
DecalSystem.instance.Render(renderContext, camera, cmd);
cmd.SetGlobalTexture(HDShaderIDs._DBufferTexture0, m_DbufferManager.GetGBuffers()[0]);
cmd.SetGlobalTexture(HDShaderIDs._DBufferTexture1, m_DbufferManager.GetGBuffers()[1]);
}
RenderDBuffer(camera, renderContext, cmd);
RenderGBuffer(m_CullResults, camera, renderContext, cmd);

RenderOpaqueRenderList(cull, camera, renderContext, cmd, HDShaderPassNames.s_GBufferName, HDUtils.k_RendererConfigurationBakedLighting, RenderQueueRange.opaque, m_DepthStateOpaque);
}
}
}
}
void RenderDBuffer(Camera camera, ScriptableRenderContext renderContext, CommandBuffer cmd)
{
using (new ProfilingSample(cmd, "Decals"))
{
CoreUtils.SetRenderTarget(cmd, m_DbufferManager.GetGBuffers(), m_CameraDepthStencilBufferRT);
cmd.SetGlobalTexture(HDShaderIDs._CameraDepthTexture, GetDepthTexture());
DecalSystem.instance.Render(renderContext, camera, cmd);
}
}

12
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class Decal : RenderPipelineMaterial
public partial class Decal
[GenerateHLSL(PackingRules.Exact, false, true, 2000)]
public struct SurfaceData
[GenerateHLSL(PackingRules.Exact, false, true, 10000)]
public struct DecalSurfaceData
{
[SurfaceDataAttributes("Base Color", false, true)]
public Vector4 baseColor;

[GenerateHLSL(PackingRules.Exact, false, true, 2100)]
public struct BSDFData
[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
};
}
}

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


#ifndef DECAL_CS_HLSL
#define DECAL_CS_HLSL
//
// UnityEngine.Experimental.Rendering.HDPipeline.Decal+SurfaceData: static fields
// 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 DEBUGVIEW_DECAL_SURFACEDATA_BASE_COLOR (2000)
#define DEBUGVIEW_DECAL_SURFACEDATA_NORMAL_WS (2001)
#define DBUFFERMATERIAL_COUNT (2)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Decal+SurfaceData
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Decal+DecalSurfaceData
struct SurfaceData
struct DecalSurfaceData
{
float4 baseColor;
float4 normalWS;

// Debug functions
//
void GetGeneratedSurfaceDataDebug(uint paramId, SurfaceData surfacedata, inout float3 result, inout bool needLinearToSRGB)
void GetGeneratedDecalSurfaceDataDebug(uint paramId, DecalSurfaceData decalsurfacedata, inout float3 result, inout bool needLinearToSRGB)
case DEBUGVIEW_DECAL_SURFACEDATA_BASE_COLOR:
result = surfacedata.baseColor.xyz;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_BASE_COLOR:
result = decalsurfacedata.baseColor.xyz;
case DEBUGVIEW_DECAL_SURFACEDATA_NORMAL_WS:
result = surfacedata.normalWS.xyz;
case DEBUGVIEW_DECAL_DECALSURFACEDATA_NORMAL_WS:
result = decalsurfacedata.normalWS.xyz;
break;
}
}

108
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl


#include "Decal.cs.hlsl"
#define DBufferType0 float4
#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 = inDBuffer1 * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
}

80
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader


#pragma shader_feature _COLORMAP
#pragma shader_feature _NORMALMAP
/*
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _DEPTHOFFSET_ON
#pragma shader_feature _DOUBLESIDED_ON
#pragma shader_feature _ _VERTEX_DISPLACEMENT _PIXEL_DISPLACEMENT
#pragma shader_feature _VERTEX_DISPLACEMENT_LOCK_OBJECT_SCALE
#pragma shader_feature _DISPLACEMENT_LOCK_TILING_SCALE
#pragma shader_feature _PIXEL_DISPLACEMENT_LOCK_OBJECT_SCALE
#pragma shader_feature _VERTEX_WIND
#pragma shader_feature _ _REFRACTION_PLANE _REFRACTION_SPHERE
#pragma shader_feature _ _MAPPING_PLANAR _MAPPING_TRIPLANAR
#pragma shader_feature _NORMALMAP_TANGENT_SPACE
#pragma shader_feature _ _REQUIRE_UV2 _REQUIRE_UV3
#pragma shader_feature _NORMALMAP
#pragma shader_feature _MASKMAP
#pragma shader_feature _BENTNORMALMAP
#pragma shader_feature _EMISSIVE_COLOR_MAP
#pragma shader_feature _ENABLESPECULAROCCLUSION
#pragma shader_feature _HEIGHTMAP
#pragma shader_feature _TANGENTMAP
#pragma shader_feature _ANISOTROPYMAP
#pragma shader_feature _DETAIL_MAP
#pragma shader_feature _SUBSURFACE_RADIUS_MAP
#pragma shader_feature _THICKNESSMAP
#pragma shader_feature _SPECULARCOLORMAP
// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT
// MaterialId are used as shader feature to allow compiler to optimize properly
// Note _MATID_STANDARD is not define as there is always the default case "_". We assign default as _MATID_STANDARD, so we never test _MATID_STANDARD
#pragma shader_feature _ _MATID_SSS _MATID_ANISO _MATID_SPECULAR _MATID_CLEARCOAT
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED
#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
// enable dithering LOD crossfade
#pragma multi_compile _ LOD_FADE_CROSSFADE
// TODO: We should have this keyword only if VelocityInGBuffer is enable, how to do that ?
//#pragma multi_compile VELOCITYOUTPUT_OFF VELOCITYOUTPUT_ON
*/
#define UNITY_MATERIAL_DECAL // Need to be define before including Material.hlsl
/*
#define UNITY_MATERIAL_LIT // Need to be define before including Material.hlsl
// Use surface gradient normal mapping as it handle correctly triplanar normal mapping and multiple UVSet
#define SURFACE_GRADIENT
// This shader support vertex modification
#define HAVE_VERTEX_MODIFICATION
*/
#define UNITY_MATERIAL_DECAL // do we need this now that Material.hlsl is not getting included?
#include "../../../Core/ShaderLibrary/Common.hlsl"
#include "../../../Core/ShaderLibrary/Wind.hlsl"

Name "DBuffer" // Name is not used
Tags { "LightMode" = "dBuffer" } // This will be only for opaque object based on the RenderQueue index
Cull [_CullMode]
Cull Back
ZTest [_ZTestMode]
Stencil
{
Ref [_StencilRef]
Comp Always
Pass Replace
}
ZTest LEqual
// #include "../../ShaderVariables.hlsl"
// #include "../../Material/Material.hlsl"
// #include "ShaderPass/LitSharePass.hlsl"
// #include "LitData.hlsl"
// #include "../../ShaderPass/ShaderPassGBuffer.hlsl"
#include "../../Material/Material.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "Decal.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"

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


//-------------------------------------------------------------------------------------
// Fill SurfaceData/Builtin data function
//-------------------------------------------------------------------------------------
#include "../../../Core/ShaderLibrary/Packing.hlsl"
#include "../../../Core/ShaderLibrary/EntityLighting.hlsl"
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
void GetSurfaceData(float2 texCoordDS, out DecalSurfaceData surfaceData)
float3 positionWS = posInput.positionWS;
float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, positionDS.xz);
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, texCoordDS.xy);
ZERO_INITIALIZE(BuiltinData, builtinData);
texCoord.uv = positionDS.xz;
texCoord.uv = texCoordDS.xy;
surfaceData.normalWS.xyz = SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1) * 0.5f + 0.5f;
surfaceData.normalWS.xyz = mul((float3x3)_DecalToWorldR, SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1)) * 0.5f + 0.5f;
surfaceData.normalWS.w = totalBlend;
#endif
}

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


decalBlend = FindProperty(kDecalBlend, props);
}
static public void SetKeyword(Material m, string keyword, bool state)
{
if (state)
m.EnableKeyword(keyword);
else
m.DisableKeyword(keyword);
}
SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
CoreUtils.SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));
CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
}
protected void SetupMaterialKeywordsAndPassInternal(Material material)

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


//-------------------------------------------------------------------------------------
#include "../../../Core/ShaderLibrary/SampleUVMapping.hlsl"
#include "../MaterialUtilities.hlsl"
#include "../Decal/DecalUtilities.hlsl"
void DoAlphaTest(float alpha, float alphaCutoff)
{

#endif
}
void AddDecalBaseColor(float2 texCoords, inout float3 baseColor)
{
float4 decalBaseColor = SAMPLE_TEXTURE2D(_DBufferTexture0, sampler_DBufferTexture0, texCoords);
baseColor = lerp(baseColor, decalBaseColor.xyz, decalBaseColor.w);
}
void AddDecalNormal(float2 texCoords, inout float3 normalTS)
{
float4 decalNormalTS = SAMPLE_TEXTURE2D(_DBufferTexture1, sampler_DBufferTexture1, texCoords) * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
if(decalNormalTS.w > 0.0f)
normalTS = normalize(lerp(normalTS, decalNormalTS.xyz, decalNormalTS.w));
}
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group

float3 bentNormalTS;
float3 bentNormalWS;
float alpha = GetSurfaceData(input, layerTexCoord, surfaceData, normalTS, bentNormalTS);
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
AddDecalNormal(posInput.positionSS.xy, normalTS);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
surfaceData.specularOcclusion = 1.0;

// Caution: surfaceData must be fully initialize before calling GetBuiltinData
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);
AddDecalContribution(posInput.unPositionSS, surfaceData);
}
#else // if LAYERED_LIT_SHADER

surfaceData.atDistance = 1000000.0;
surfaceData.transmittanceMask = 0.0;
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
AddDecalNormal(posInput.positionSS.xy, normalTS);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
// If any layer use a bent normal map, then bentNormalTS contain the interpolated result of bentnormal and normalmap (in case no bent normal are available)

#endif
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);
AddDecalContribution(posInput.unPositionSS, surfaceData);
}
#endif // #ifndef LAYERED_LIT_SHADER

8
ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl


#include "Unlit/Unlit.hlsl"
#elif defined(UNITY_MATERIAL_IRIDESCENCE)
//#include "Iridescence/Iridescence.hlsl"
#elif defined(UNITY_MATERIAL_DECAL)
#include "Decal/Decal.hlsl"
#endif
//-----------------------------------------------------------------------------

#endif
#endif // #ifdef GBUFFERMATERIAL_COUNT
// define for dbuffer management
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType0 MERGE_NAME(NAME, 1) : SV_Target1 \
#endif // UNITY_MATERIAL_INCLUDED

21
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl


void Frag( PackedVaryingsToPS packedInput,
OUTPUT_DBUFFER(outDBuffer)
)
{
{
/*
ENCODE_INTO_GBUFFER(surfaceData, bakeDiffuseLighting, outGBuffer);
ENCODE_VELOCITY_INTO_GBUFFER(builtinData.velocity, outVelocityBuffer);
*/
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
outDBuffer0 = surfaceData.baseColor;
outDBuffer1 = surfaceData.normalWS;
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);
}

7
ScriptableRenderPipeline/HDRenderPipeline/ShaderVariables.hlsl


TEXTURE3D_FLOAT(unity_ProbeVolumeSH);
SAMPLER3D(samplerunity_ProbeVolumeSH);
// Decals textures
TEXTURE2D(_DBufferTexture0);
SAMPLER2D(sampler_DBufferTexture0);
TEXTURE2D(_DBufferTexture1);
SAMPLER2D(sampler_DBufferTexture1);
CBUFFER_START(UnityVelocityPass)
float4x4 unity_MatrixNonJitteredVP;
float4x4 unity_MatrixPreviousVP;

110
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();
UpdatePropertyBlock();
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 OnDrawGizmos()
{
//DrawGizmo(false);
}
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()
{
Matrix4x4 CRWStoAWS = new Matrix4x4();
if (ShaderConfig.s_CameraRelativeRendering == 1)
{
// Vector4 worldSpaceCameraPos = Shader.GetGlobalVector(HDShaderIDs._WorldSpaceCameraPos);
CRWStoAWS = Matrix4x4.Translate(Camera.main.transform.position);
}
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);
}
public MaterialPropertyBlock GetPropertyBlock()
{
return m_PropertyBlock;
}
}
}

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));
}

68
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs


using UnityEditor;
using UnityEngine;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[ExecuteInEditMode]
public class DecalComponent : MonoBehaviour
{
public enum Kind
{
DiffuseOnly,
NormalsOnly,
Both
}
public Kind m_Kind;
public Material m_Material;
public void OnEnable()
{
DecalSystem.instance.AddDecal(this);
}
public void Start()
{
DecalSystem.instance.AddDecal(this);
}
public void OnDisable()
{
DecalSystem.instance.RemoveDecal(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 OnDrawGizmos()
{
//DrawGizmo(false);
}
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<DecalComponent>();
// 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;
}
}
}
正在加载...
取消
保存