浏览代码

Merge pull request #888 from Unity-Technologies/LW-ScreenSpaceShadows-WPositionReconstruction

Lw screen space shadows w position reconstruction
/main
GitHub 7 年前
当前提交
f565d43a
共有 5 个文件被更改,包括 17 次插入69 次删除
  1. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightConstantBuffer.cs
  2. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  3. 19
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineUtils.cs
  4. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl
  5. 63
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightScreenSpaceShadows.shader

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightConstantBuffer.cs


public static int _ShadowOffset2;
public static int _ShadowOffset3;
public static int _ShadowmapSize;
public static int _FrustumCorners;
}
}

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


ShadowConstantBuffer._ShadowOffset2 = Shader.PropertyToID("_ShadowOffset2");
ShadowConstantBuffer._ShadowOffset3 = Shader.PropertyToID("_ShadowOffset3");
ShadowConstantBuffer._ShadowmapSize = Shader.PropertyToID("_ShadowmapSize");
ShadowConstantBuffer._FrustumCorners = Shader.PropertyToID("_FrustumCorners");
m_ShadowMapRTID = Shader.PropertyToID("_ShadowMap");
m_ScreenSpaceShadowMapRTID = Shader.PropertyToID("_ScreenSpaceShadowMap");

cmd.SetGlobalVector(ShadowConstantBuffer._ShadowOffset2, new Vector4(-invHalfShadowResolution, invHalfShadowResolution, 0.0f, 0.0f));
cmd.SetGlobalVector(ShadowConstantBuffer._ShadowOffset3, new Vector4(invHalfShadowResolution, invHalfShadowResolution, 0.0f, 0.0f));
cmd.SetGlobalVector(ShadowConstantBuffer._ShadowmapSize, new Vector4(invShadowResolution, invShadowResolution, m_Asset.ShadowAtlasResolution, m_Asset.ShadowAtlasResolution));
cmd.SetGlobalVectorArray(ShadowConstantBuffer._FrustumCorners, LightweightUtils.GetFarPlaneCorners(m_CurrCamera));
}
private void SetShaderKeywords(CommandBuffer cmd, ref LightData lightData, List<VisibleLight> visibleLights)

19
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineUtils.cs


// Remaining light types don't support cookies
}
public static Vector4[] GetFarPlaneCorners(Camera camera)
{
Vector4[] corners = new Vector4[4];
float farH = 2.0f * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad) * camera.farClipPlane;
float farW = farH * camera.aspect;
Vector3 fc = Vector3.forward * camera.farClipPlane;
Vector3 up = Vector3.up;
Vector3 right = Vector3.right;
corners[0] = fc - (up * farH / 2) - (right * farW / 2);
corners[1] = fc + (up * farH / 2) - (right * farW / 2);
corners[2] = fc + (up * farH / 2) + (right * farW / 2);
corners[3] = fc - (up * farH / 2) + (right * farW / 2);
return corners;
}
public static bool IsSupportedShadowType(LightType lightType)
{
return lightType == LightType.Directional || lightType == LightType.Spot;

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl


half4 _ShadowOffset3;
half4 _ShadowData; // (x: shadowStrength)
float4 _ShadowmapSize; // (xy: 1/width and 1/height, zw: width and height)
float4 _FrustumCorners[4];
CBUFFER_END
#if UNITY_REVERSED_Z

63
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightScreenSpaceShadows.shader


struct VertexInput
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
uint id : SV_VertexID;
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
//Perspective Case
float3 ray : TEXCOORD1;
//Orthographic Case
float3 orthoPosNear : TEXCOORD2;
float3 orthoPosFar : TEXCOORD3;
half4 pos : SV_POSITION;
half4 texcoord : TEXCOORD0;
float3 ComputeViewSpacePositionGeometric(Interpolators i)
{
float zDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);
float depth = Linear01Depth(zDepth, _ZBufferParams);
#if UNITY_REVERSED_Z
zDepth = 1 - zDepth;
#endif
//Perspective Case
float3 vposPersp = i.ray * depth;
//Orthographics Case
float3 vposOrtho = lerp(i.orthoPosNear, i.orthoPosFar, zDepth);
return lerp(vposPersp, vposOrtho, unity_OrthoParams.w);
}
Interpolators Vertex(VertexInput i)
{
Interpolators o;

o.pos = TransformObjectToHClip(i.vertex.xyz);
o.uv = i.uv;
//Perspective Case
o.ray = _FrustumCorners[i.id].xyz;
float4 projPos = o.pos * 0.5;
projPos.xy = projPos.xy + projPos.w;
//Orthographic Case
float4 clipPos = o.pos;
clipPos.y *= _ProjectionParams.x;
float3 orthoPosNear = mul(unity_CameraInvProjection, float4(clipPos.x, clipPos.y, -1, 1)).xyz;
float3 orthoPosFar = mul(unity_CameraInvProjection, float4(clipPos.x, clipPos.y, 1, 1)).xyz;
orthoPosNear.z *= -1;
orthoPosFar.z *= -1;
o.orthoPosNear = orthoPosNear;
o.orthoPosFar = orthoPosFar;
o.texcoord.xy = i.texcoord;
o.texcoord.zw = projPos.xy;
return o;
}

UNITY_SETUP_INSTANCE_ID(i);
//Reconstruct the world position.
float3 vpos = ComputeViewSpacePositionGeometric(i); //TODO: Profile against unprojection method in core library.
float deviceDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord.xy);
#if UNITY_REVERSED_Z
deviceDepth = 1 - deviceDepth;
#endif
deviceDepth = 2 * deviceDepth - 1; //NOTE: Currently must massage depth before computing CS position.
float3 vpos = ComputeViewSpacePosition(i.texcoord.zw, deviceDepth, unity_CameraInvProjection);
//Fetch shadow coordinates.
//Fetch shadow coordinates for cascade.
float4 coords = ComputeShadowCoord(wpos);
return SampleShadowmap(coords);

正在加载...
取消
保存