浏览代码

Fixed cascade index selection.

/vr_sandbox
Felipe Lira 8 年前
当前提交
a749dbac
共有 2 个文件被更改,包括 15 次插入20 次删除
  1. 20
      Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader
  2. 15
      Assets/LowEndRenderLoop/LowEndRenderPipeline.cs

20
Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader


float _Glossiness;
float4x4 _WorldToShadow[MAX_SHADOW_CASCADES];
float _PSSMDistances[MAX_SHADOW_CASCADES - 1];
float4 _PSSMDistances;
struct LightInput
{

inline int ComputeCascadeIndex(float eyeZ)
{
for (int index = 0; index < MAX_SHADOW_CASCADES - 1; ++index)
{
if (eyeZ < _PSSMDistances[index])
return index;
}
return MAX_SHADOW_CASCADES - 2;
// PSSMDistance is set to infinity for non active cascades. This way the comparison for unavailable cascades will always be zero.
half3 cascadeCompare = step(_PSSMDistances, half3(eyeZ, eyeZ, eyeZ));
return dot(cascadeCompare, cascadeCompare);
}
struct VertexInput

o.positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
o.normalWS = UnityObjectToWorldNormal(v.normal);
half eyePosZ = mul(UNITY_MATRIX_V, float4(o.positionWS, 1.0)).z;
half eyePosZ = -UnityObjectToViewPos(v.vertex).z;
int cascadeIndex = ComputeCascadeIndex(eyePosZ);
o.shadowCoord = mul(_WorldToShadow[3], float4(o.positionWS, 1.0));
int cascadeIndex = ComputeCascadeIndex(eyePosZ);
o.shadowCoord = mul(_WorldToShadow[cascadeIndex], float4(o.positionWS, 1.0));
for (int lightIndex = globalLightCount.x; lightIndex < globalLightCount.y; ++lightIndex)
{

{
Tags { "Lightmode" = "ShadowCaster" }
ZWrite On ZTest LEqual
ZWrite On ZTest LEqual Cull Front
CGPROGRAM

15
Assets/LowEndRenderLoop/LowEndRenderPipeline.cs


void SetupShadowShaderVariables(ShadowOutput shadowOutput, ScriptableRenderContext context, float shadowNear, float shadowFar)
{
// PSSM distance settings
float shadowFrustumDepth = shadowNear - shadowFar;
float shadowFrustumDepth = shadowFar - shadowNear;
// TODO: check z buffer direction to invert eye space depths
float[] PSSMDistances =
{
// We set PSSMDistance to infinity for non active cascades so the comparison test always fails for unavailable cascades
Vector4 PSSMDistances = new Vector4(
shadowNear + shadowSplitRatio.y * shadowFrustumDepth,
shadowNear + shadowSplitRatio.z * shadowFrustumDepth,
};
(shadowSplitRatio.y > 0.0f) ? shadowNear + shadowSplitRatio.y * shadowFrustumDepth : Mathf.Infinity,
(shadowSplitRatio.z > 0.0f) ? shadowNear + shadowSplitRatio.z * shadowFrustumDepth : Mathf.Infinity,
Mathf.Infinity);
Matrix4x4[] shadowMatrices =
{

var setupShadow = new CommandBuffer() { name = "SetupShadowShaderConstants" };
setupShadow.SetGlobalMatrixArray("_WorldToShadow", shadowMatrices);
setupShadow.SetGlobalFloatArray("_PSSMDistances", PSSMDistances);
setupShadow.SetGlobalVector("_PSSMDistances", PSSMDistances);
context.ExecuteCommandBuffer(setupShadow);
setupShadow.Dispose();
}

正在加载...
取消
保存