浏览代码

Manually re-applied and fixed screen space shadow code after big merge of master that moved every files.

/Yibing-Project-2
Julien Ignace 7 年前
当前提交
6cf5f4d4
共有 10 个文件被更改,包括 204 次插入23 次删除
  1. 30
      SampleScenes/HDTest/ShadowsTest.unity
  2. 4
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Random.hlsl
  3. 8
      ScriptableRenderPipeline/Core/CoreRP/Shadow/AdditionalShadowData.cs
  4. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDLightEditor.Styles.cs
  5. 21
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDLightEditor.cs
  6. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.asset
  7. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  8. 113
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/DeferredDirectionalShadow.compute
  9. 13
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  10. 23
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl

30
SampleScenes/HDTest/ShadowsTest.unity


m_IsActive: 1
--- !u!114 &367594606
MonoBehaviour:
m_ObjectHideFlags: 0
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 367594605}

shadowResolution: 1024
shadowDimmer: 1
shadowFadeDistance: 10000
contactShadowLength: 0.2
contactShadowDistanceScaleFactor: 0.5
contactShadowMaxDistance: 25
contactShadowFadeDistance: 5
shadowCascadeCount: 4
shadowCascadeRatios:
- 0.007

- 0.272
- 0.356
shadowAlgorithm: 0
shadowVariant: 4
shadowVariant: 3
format: 4
data: d044583b0000803f
format: 3
data: 0bd7a33b0000803f
shadowDatas:
- format: 4
data: d044583b0000803f

data: 000000006f12833a0000803f0000803f02000000
- format: 25
data: ec51383da48cb83652499d3903000000
- format: 3
data: 0bd7a33b0000803f
m_ObjectHideFlags: 0
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 367594605}

m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Type: 1
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1

shadowResolution: 1024
shadowDimmer: 1
shadowFadeDistance: 10000
contactShadowLength: 0
contactShadowDistanceScaleFactor: 0.5
contactShadowMaxDistance: 50
contactShadowFadeDistance: 5
shadowCascadeCount: 4
shadowCascadeRatios:
- 0.05

shadowResolution: 512
shadowDimmer: 1
shadowFadeDistance: 10000
contactShadowLength: 0
contactShadowDistanceScaleFactor: 0.5
contactShadowMaxDistance: 50
contactShadowFadeDistance: 5
shadowCascadeCount: 4
shadowCascadeRatios:
- 0.05

shadowResolution: 512
shadowDimmer: 1
shadowFadeDistance: 10000
contactShadowLength: 0
contactShadowDistanceScaleFactor: 0.5
contactShadowMaxDistance: 50
contactShadowFadeDistance: 5
shadowCascadeCount: 4
shadowCascadeRatios:
- 0.05

4
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Random.hlsl


uint JenkinsHash(uint3 v)
{
return JenkinsHash(v.x ^ JenkinsHash(v.y) ^ JenkinsHash(v.z));
return JenkinsHash(v.x ^ JenkinsHash(v.yz));
return JenkinsHash(v.x ^ JenkinsHash(v.y) ^ JenkinsHash(v.z) ^ JenkinsHash(v.w));
return JenkinsHash(v.x ^ JenkinsHash(v.yzw));
}
// Construct a float with half-open range [0, 1) using low 23 bits.

8
ScriptableRenderPipeline/Core/CoreRP/Shadow/AdditionalShadowData.cs


return DefaultShadowResolution;
}
[Range(0.0F, 1.0F)]
[Range(0.0f, 1.0f)]
[Range(0.0f, 1.0f)]
public float contactShadowLength = 0.0f;
[Range(0.0f, 1.0f)]
public float contactShadowDistanceScaleFactor = 0.5f;
public float contactShadowMaxDistance = 50.0f;
public float contactShadowFadeDistance = 5.0f;
// shadow related parameters
[System.Serializable]

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDLightEditor.Styles.cs


public readonly GUIContent[] shapeNames;
// Additional shadow data
public readonly GUIContent shadowCascades = new GUIContent("Cascades", "");
public readonly GUIContent contactShadow = new GUIContent("Contact Shadows");
public readonly GUIContent contactShadowLength = new GUIContent("Length", "Length of rays used to gather contact shadows in world units.\nZero will disable the feature.");
public readonly GUIContent contactShadowDistanceScaleFactor = new GUIContent("Distance Scale Factor", "Contact Shadows are scaled up with distance. Use this parameter to dampen this effect.");
public readonly GUIContent contactShadowMaxDistance = new GUIContent("Max Distance", "Distance from the camera in world units at which contact shadows are faded out to zero.");
public readonly GUIContent contactShadowFadeDistance = new GUIContent("Fade Distance", "Distance in world units over which the contact shadows are faded out (see Max Distance).");
public Styles()
{

21
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDLightEditor.cs


public SerializedProperty cascadeRatios;
public SerializedProperty cascadeBorders;
public SerializedProperty resolution;
public SerializedProperty contactShadowLength;
public SerializedProperty contactShadowDistanceScaleFactor;
public SerializedProperty contactShadowMaxDistance;
public SerializedProperty contactShadowFadeDistance;
}
SerializedObject m_SerializedAdditionalLightData;

cascadeCount = o.Find("shadowCascadeCount"),
cascadeRatios = o.Find("shadowCascadeRatios"),
cascadeBorders = o.Find("shadowCascadeBorders"),
resolution = o.Find(x => x.shadowResolution)
resolution = o.Find(x => x.shadowResolution),
contactShadowLength = o.Find(x => x.contactShadowLength),
contactShadowDistanceScaleFactor = o.Find(x => x.contactShadowDistanceScaleFactor),
contactShadowMaxDistance = o.Find(x => x.contactShadowMaxDistance),
contactShadowFadeDistance = o.Find(x => x.contactShadowFadeDistance),
};
}

for (int i = 0; i < arraySize; i++)
EditorGUILayout.Slider(m_AdditionalShadowData.cascadeRatios.GetArrayElementAtIndex(i), 0f, 1f, s_Styles.shadowCascadeRatios[i]);
EditorGUI.indentLevel--;
EditorGUILayout.LabelField(s_Styles.contactShadow, EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_AdditionalShadowData.contactShadowLength, s_Styles.contactShadowLength);
bool disableContactShadowScope = m_AdditionalShadowData.contactShadowLength.hasMultipleDifferentValues || m_AdditionalShadowData.contactShadowLength.floatValue == 0.0f;
using (new EditorGUI.DisabledScope(disableContactShadowScope))
{
EditorGUILayout.PropertyField(m_AdditionalShadowData.contactShadowDistanceScaleFactor, s_Styles.contactShadowDistanceScaleFactor);
EditorGUILayout.PropertyField(m_AdditionalShadowData.contactShadowMaxDistance, s_Styles.contactShadowMaxDistance);
EditorGUILayout.PropertyField(m_AdditionalShadowData.contactShadowFadeDistance, s_Styles.contactShadowFadeDistance);
}
}
if (settings.isBakedOrMixed)

7
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.asset


m_EditorClassIdentifier:
m_RenderPipelineResources: {fileID: 11400000, guid: 3ce144cff5783da45aa5d4fdc2da14b7,
type: 2}
serializedFrameSettings:
m_FrameSettings:
enableSSSAndTransmission: 1
enableSubsurfaceScattering: 1
enableTransmission: 1
diffuseGlobalDimmer: 1
specularGlobalDimmer: 1
enableForwardRenderingOnly: 0

supportShadowMask: 1
supportSSR: 1
supportSSAO: 1
supportSSSAndTransmission: 1
supportSubsurfaceScattering: 1
supportDBuffer: 1
supportMSAA: 0
supportAsyncCompute: 0

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _DeferredShadowTexture = Shader.PropertyToID("_DeferredShadowTexture");
public static readonly int _DeferredShadowTextureUAV = Shader.PropertyToID("_DeferredShadowTextureUAV");
public static readonly int _DirectionalShadowIndex = Shader.PropertyToID("_DirectionalShadowIndex");
public static readonly int _DirectionalContactShadowParams = Shader.PropertyToID("_ScreenSpaceShadowsParameters");
public static readonly int _DirectionalLightDirection = Shader.PropertyToID("_LightDirection");
public static readonly int unity_OrthoParams = Shader.PropertyToID("unity_OrthoParams");
public static readonly int _ZBufferParams = Shader.PropertyToID("_ZBufferParams");

113
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/DeferredDirectionalShadow.compute


#include "../ShaderVariables.hlsl"
#include "Lighting.hlsl"
//#pragma enable_d3d11_debug_symbols
float _DirectionalShadowIndex;
CBUFFER_START(DeferredShadowParameters)
float _DirectionalShadowIndex;
float3 _LightDirection;
float4 _ScreenSpaceShadowsParameters;
CBUFFER_END
#define _ContactShadowLength _ScreenSpaceShadowsParameters.x
#define _ContactShadowDistanceScaleFactor _ScreenSpaceShadowsParameters.y
#define _ContactShadowFadeEnd _ScreenSpaceShadowsParameters.z
#define _ContactShadowFadeOneOverRange _ScreenSpaceShadowsParameters.w
// Return 1.0 if occluded 0.0 if not
float4 ScreenSpaceShadowRayCast(float3 positionWS, float3 rayDirection, float rayLength)
{
int stepCount = 8;
uint3 hashInput = uint3(abs(GetAbsolutePositionWS(positionWS)) * 1000);
// Dither pattern is shifted by 0.5 because we want to jitter the ray starting position backward and forward (so we need values between -0.5 and 0.5)
float ditherBias = 0.5;
float dither = GenerateHashedRandomFloat(hashInput) - ditherBias;
float3 rayStartWS = positionWS;
float3 rayEndWS = rayStartWS + rayDirection * rayLength;
float4 rayStartCS = TransformWorldToHClip(rayStartWS);
float4 rayEndCS = TransformWorldToHClip(rayEndWS);
// Here we compute a ray perpendicular to view space. This is the ray we use to compute the threshold for rejecting samples.
// This is done this way so that the threshold is less dependent of ray slope.
float4 rayOrthoViewSpace = rayStartCS + mul(GetViewToHClipMatrix(), float4(0, 0, rayLength, 0));
rayOrthoViewSpace = rayOrthoViewSpace / rayOrthoViewSpace.w;
rayStartCS.xyz = rayStartCS.xyz / rayStartCS.w;
rayEndCS.xyz = rayEndCS.xyz / rayEndCS.w;
// Pixel to light ray in clip space.
float3 rayCS = rayEndCS.xyz - rayStartCS.xyz;
// Depth at the start of the ray
float startDepth = rayStartCS.z;
// Depth range of the ray
float rayDepth = rayCS.z;
// Starting UV of the sampling loop
float2 startUV = rayStartCS.xy * 0.5f + 0.5f;
startUV.y = 1.0 - startUV.y;
// Pixel to light ray in
float2 rayUV = rayCS.xy * 0.5f;
rayUV.y = -rayUV.y;
float step = 1.0 / stepCount;
float compareThreshold = abs(rayOrthoViewSpace.z - rayStartCS.z) * step;
float occluded = 0.0f;
[unroll]
for (int i = 0; i < stepCount; i++)
{
// Step for this sample
float sampleStep = ((i + 1) * step + step * dither);
// UVs for the current sample
float2 sampleUV = startUV + rayUV * sampleStep;
// Ray depth for this sample
float raySampleDepth = startDepth + rayDepth * sampleStep;
// Depth buffer depth for this sample
float sampleDepth = SAMPLE_TEXTURE2D_LOD(_MainDepthTexture, sampler_MainDepthTexture, sampleUV, 0.0).x;
bool Hit = false;
float depthDiff = sampleDepth - raySampleDepth;
Hit = depthDiff < compareThreshold && depthDiff > 0.0;// 1e-4;
if (Hit)
occluded = 1.0f;
}
// Off screen masking
// We remove the occlusion if the ray is occluded and only if direction steps out of the screen
float2 vignette = max(6.0 * abs(rayStartCS.xy + rayCS.xy * occluded * 0.5) - 5.0, 0.0);
occluded *= saturate( 1.0 - dot(vignette, vignette) );
return occluded;
}
uint2 pixelCoord = groupId * DEFERRED_SHADOW_TILE_SIZE + groupThreadId;
uint2 tileCoord = groupId;
uint2 pixelCoord = groupId * DEFERRED_SHADOW_TILE_SIZE + groupThreadId;
uint2 tileCoord = groupId;
PositionInputs posInput = GetPositionInput(pixelCoord.xy, _ScreenSize.zw, tileCoord);
PositionInputs posInput = GetPositionInput(pixelCoord.xy, _ScreenSize.zw, tileCoord);
float depth = LOAD_TEXTURE2D(_MainDepthTexture, posInput.positionSS).x;
UpdatePositionInput(depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP, posInput);
float depth = LOAD_TEXTURE2D(_MainDepthTexture, posInput.positionSS).x;
UpdatePositionInput(depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP, posInput);
ShadowContext shadowContext = InitShadowContext();
float shadow = GetDirectionalShadowAttenuation(shadowContext, posInput.positionWS, float3(0.0, 0.0, 0.0), (uint)_DirectionalShadowIndex, float3(0.0, 0.0, 0.0));
ShadowContext shadowContext = InitShadowContext();
float shadow = GetDirectionalShadowAttenuation(shadowContext, posInput.positionWS, float3(0.0, 0.0, 0.0), (uint)_DirectionalShadowIndex, float3(0.0, 0.0, 0.0));
_DeferredShadowTextureUAV[pixelCoord] = float4(shadow.xxx, 0.0);
float contactShadow = 1.0f;
if (_ContactShadowLength > 0.0f)
{
float4 result = ScreenSpaceShadowRayCast(posInput.positionWS, normalize(_LightDirection), _ContactShadowLength * max(0.5, posInput.linearDepth * _ContactShadowDistanceScaleFactor));
contactShadow = 1.0 - result.x * saturate((_ContactShadowFadeEnd - posInput.linearDepth) * _ContactShadowFadeOneOverRange);
shadow *= contactShadow;
}
_DeferredShadowTextureUAV[pixelCoord] = float4(shadow, 0.0, 0.0, 0.0);
}

13
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


public void RenderDeferredDirectionalShadow(HDCamera hdCamera, RenderTargetIdentifier deferredShadowRT, RenderTargetIdentifier depthTexture, CommandBuffer cmd)
{
if (m_CurrentSunLight == null)
if (m_CurrentSunLight == null || m_CurrentSunLight.GetComponent<AdditionalShadowData>() == null)
return;
using (new ProfilingSample(cmd, "Deferred Directional Shadow", CustomSamplerId.TPDeferredDirectionalShadow.GetSampler()))

cmd.SetComputeFloatParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalShadowIndex, (float)m_CurrentSunLightShadowIndex);
AdditionalShadowData asd = m_CurrentSunLight.GetComponent<AdditionalShadowData>();
float contactShadowRange = Mathf.Clamp(asd.contactShadowFadeDistance, 0.0f, asd.contactShadowMaxDistance);
float contactShadowFadeEnd = asd.contactShadowMaxDistance;
float contactShadowOneOverFadeRange = 1.0f / (contactShadowRange);
Vector4 contactShadowParams = new Vector4(asd.contactShadowLength, asd.contactShadowDistanceScaleFactor, contactShadowFadeEnd, contactShadowOneOverFadeRange);
cmd.SetComputeVectorParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalContactShadowParams, contactShadowParams);
cmd.SetComputeVectorParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalLightDirection, -m_CurrentSunLight.transform.forward);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, s_deferredDirectionalShadowKernel, HDShaderIDs._DeferredShadowTextureUAV, deferredShadowRT);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, s_deferredDirectionalShadowKernel, HDShaderIDs._MainDepthTexture, depthTexture);

hdCamera.SetupComputeShader(deferredDirectionalShadowComputeShader, cmd);
// TODO: Update for stereo
cmd.DispatchCompute(deferredDirectionalShadowComputeShader, s_deferredDirectionalShadowKernel, numTilesX, numTilesY, 1);

23
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl


}
// Transform to homogenous clip space
float4x4 GetViewToHClipMatrix()
{
return UNITY_MATRIX_P;
}
float4x4 GetWorldToHClipMatrix()
{
return UNITY_MATRIX_VP;

float4 TransformWorldToHClip(float3 positionWS)
{
return mul(GetWorldToHClipMatrix(), float4(positionWS, 1.0));
}
// Tranforms vector from world space to homogenous space
float3 TransformWorldToHClipDir(float3 directionWS)
{
return mul((float3x3)GetWorldToHClipMatrix(), directionWS);
}
// Tranforms position from view space to homogenous space
float4 TransformWViewToHClip(float3 positionVS)
{
return mul(GetViewToHClipMatrix(), float4(positionVS, 1.0));
}
// Tranforms vector from world space to homogenous space
float3 TransformViewToHClipDir(float3 directionVS)
{
return mul((float3x3)GetViewToHClipMatrix(), directionVS);
}
float3 GetAbsolutePositionWS(float3 positionWS)

正在加载...
取消
保存