Graphics Reference
In-Depth Information
// Light Pixel shader
#if VOLUMES
// Clamp the view space position to the plane at Z = 1
float3 viewRay = float3(input.PositionVS.xy / input. PositionVS.z., 1.0f);
#elif QUADS
// For a directional light we already clamped in the vertex shader
float3 viewRay = input.ViewRay.xyz;
#endif
// Calculate our projection constants (this can be done in the application code
// and passed in a constant buffer)
ProjectionA = FarClipDistance / (FarClipDistance - NearClipDistance) ;
Projection!? = (-FarClipDistance * NearClipDistance)
/ (FarClipDistance - NearClipDistance);
// Sample the depth and convert to linear view space Z
float depth = DepthTexture.Sample(PointSampler, texCoord).x;
float linearDepth = Projection!? / (depth - ProjectionA);
float3 positionVS = viewRay * linearDepth;
Listing 11.12. Shader code for view space position reconstruction using a depth-stencil buffer.
If it's necessary to work in an arbitrary coordinate space, the linear depth value can
be used in conjunction with the first technique to reconstruct position. This can be done by
projecting the view ray onto the camera's local Z axis and using the result to figure out a
proper scaling value.
// -- Light Pixel Shader --
// Normalize the view ray
float3 viewRay = normalize(input. ViewRay );
// Sample the depth buffer and convert it to linear depth
float depth = DepthTexture.Sample(PointSampler, texCoord).x;
float linearDepth = ProjectionB / (depth - ProjectionA);
// Project the view ray onto the camera's z-axis
float viewZProj = dot(EyeZAxis, viewRay);
// Scale the view ray by the ratio of the linear z value to the projected
// view / ray
float3 positionWS = CameraPositionWS + viewRay * (linearDepth / viewZProj);
Listing 11.13. Shader code for position reconstruction from a depth-stencil buffer using an arbitrary coor-
dinate space.
When choosing a method for recovering position data, careful attention should be
paid to precision and error resulting from the storage format used. The Z/W value stored
Search WWH ::




Custom Search