Frédéric Vauchelles
7 年前
当前提交
f84484dc
共有 12 个文件被更改,包括 252 次插入 和 151 次删除
-
13ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
-
1ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl
-
11ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs
-
9ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs.hlsl
-
7ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebugPanel.cs
-
1ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
-
1ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
-
118ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
-
131ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl
-
9ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl.meta
-
93ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl
-
9ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl.meta
|
|||
#ifndef UNITY_SCREEN_SPACE_RAYMARCHING_INCLUDED |
|||
#define UNITY_SCREEN_SPACE_RAYMARCHING_INCLUDED |
|||
|
|||
struct ScreenSpaceRayHit |
|||
{ |
|||
float distance; |
|||
float linearDepth; |
|||
float2 positionSS; |
|||
|
|||
#ifdef DEBUG_DISPLAY |
|||
float3 debugOutput; |
|||
#endif |
|||
}; |
|||
|
|||
bool ScreenSpaceRaymarch( |
|||
float3 startPositionVS, |
|||
float startLinearDepth, |
|||
float3 dirVS, |
|||
float4x4 projectionMatrix, |
|||
int2 bufferSize, |
|||
int minLevel, |
|||
int maxLevel, |
|||
out ScreenSpaceRayHit hit) |
|||
{ |
|||
// dirVS must be normalized |
|||
|
|||
const float2 CROSS_OFFSET = float2(1, 1); |
|||
const int MAX_ITERATIONS = 32; |
|||
|
|||
float4 startPositionCS = mul(projectionMatrix, float4(startPositionVS, 1)); |
|||
float4 dirCS = mul(projectionMatrix, float4(dirVS, 0)); |
|||
#if UNITY_UV_STARTS_AT_TOP |
|||
// Our clip space is correct, but the NDC is flipped. |
|||
// Conceptually, it should be (positionNDC.y = 1.0 - positionNDC.y), but this is more efficient. |
|||
startPositionCS.y = -startPositionCS.y; |
|||
dirCS.y = -dirCS.y; |
|||
#endif |
|||
|
|||
float2 startPositionNDC = (startPositionCS.xy * rcp(startPositionCS.w)) * 0.5 + 0.5; |
|||
// store linear depth in z |
|||
float3 dirNDC = float3(normalize((dirCS.xy * rcp(dirCS.w)) * 0.5 + 0.5), -dirVS.z /*RHS convention for VS*/); |
|||
float2 invDirNDC = 1 / dirNDC.xy; |
|||
int2 cellPlanes = clamp(sign(dirNDC.xy), 0, 1); |
|||
|
|||
int2 startPositionTXS = int2(startPositionNDC * bufferSize); |
|||
|
|||
ZERO_INITIALIZE(ScreenSpaceRayHit, hit); |
|||
|
|||
int currentLevel = minLevel; |
|||
int2 cellCount = bufferSize >> minLevel; |
|||
int2 cellSize = int2(1 / float2(cellCount)); |
|||
|
|||
// store linear depth in z |
|||
float3 positionTXS = float3(float2(startPositionTXS), startLinearDepth); |
|||
int iteration = 0; |
|||
|
|||
bool hitSuccessful = true; |
|||
|
|||
while (currentLevel >= minLevel) |
|||
{ |
|||
if (++iteration < MAX_ITERATIONS) |
|||
{ |
|||
hitSuccessful = false; |
|||
break; |
|||
} |
|||
|
|||
// 1. Calculate hit in this HiZ cell |
|||
int2 cellId = int2(positionTXS.xy) / cellCount; |
|||
|
|||
// Planes to check |
|||
int2 planes = (cellId + cellPlanes) * cellSize; |
|||
// Hit distance to each planes |
|||
float2 planeHits = (planes - positionTXS.xy) * invDirNDC; |
|||
|
|||
float rayOffsetTestLength = min(planeHits.x, planeHits.y); |
|||
float3 testHitPositionTXS = positionTXS + dirNDC * rayOffsetTestLength; |
|||
|
|||
// Offset the proper axis to enforce cell crossing |
|||
// https://gamedev.autodesk.com/blogs/1/post/5866685274515295601 |
|||
testHitPositionTXS.xy += (planeHits.x < planeHits.y) ? float2(CROSS_OFFSET.x, 0) : float2(0, CROSS_OFFSET.y); |
|||
|
|||
if (any(testHitPositionTXS.xy > bufferSize) |
|||
|| any(testHitPositionTXS.xy < 0)) |
|||
{ |
|||
hitSuccessful = false; |
|||
break; |
|||
} |
|||
|
|||
// 2. Sample the HiZ cell |
|||
float pyramidDepth = LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, int2(testHitPositionTXS.xy) >> currentLevel, currentLevel).r; |
|||
float hiZLinearDepth = LinearEyeDepth(pyramidDepth, _ZBufferParams); |
|||
|
|||
if (hiZLinearDepth > testHitPositionTXS.z) |
|||
{ |
|||
currentLevel = min(maxLevel, currentLevel + 1); |
|||
positionTXS = testHitPositionTXS; |
|||
hit.distance += rayOffsetTestLength; |
|||
} |
|||
else |
|||
{ |
|||
float rayOffsetLength = (hiZLinearDepth - positionTXS.z) / dirNDC.z; |
|||
positionTXS += dirNDC * rayOffsetLength; |
|||
hit.distance += rayOffsetLength; |
|||
} |
|||
} |
|||
|
|||
hit.linearDepth = positionTXS.z; |
|||
hit.positionSS = float2(positionTXS.xy) / float2(bufferSize); |
|||
|
|||
#ifdef DEBUG_DISPLAY |
|||
if (_DebugLightingMode == DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION) |
|||
{ |
|||
switch (_DebugLightingSubMode) |
|||
{ |
|||
case DEBUGSCREENSPACETRACING_POSITION_VS: |
|||
hit.debugOutput = float3(startPositionNDC.xy, 0); |
|||
break; |
|||
case DEBUGSCREENSPACETRACING_HIT_DISTANCE: |
|||
hit.debugOutput = hit.distance; |
|||
break; |
|||
case DEBUGSCREENSPACETRACING_HIT_DEPTH: |
|||
hit.debugOutput = hit.linearDepth; |
|||
break; |
|||
} |
|||
} |
|||
#endif |
|||
|
|||
return hitSuccessful; |
|||
} |
|||
|
|||
#endif |
|
|||
fileFormatVersion: 2 |
|||
guid: 3fe9e9e55bdb5dd409b968b50307325a |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
#ifndef UNITY_DEPTH_RAYMARCHING_INCLUDED |
|||
#define UNITY_DEPTH_RAYMARCHING_INCLUDED |
|||
|
|||
bool RaymarchDepthBuffer( |
|||
float3 startPositionVS, |
|||
float startLinearDepth, |
|||
float3 dirVS, |
|||
float4x4 projectionMatrix, |
|||
int2 bufferSize, |
|||
int minLevel, |
|||
int maxLevel, |
|||
out float hitDistance, |
|||
out float hitDepth) |
|||
{ |
|||
// dirVS must be normalized |
|||
|
|||
const float2 CROSS_OFFSET = float2(1, 1); |
|||
const int MAX_ITERATIONS = 32; |
|||
|
|||
float4 startPositionCS = mul(projectionMatrix, float4(startPositionVS, 1)); |
|||
float4 dirCS = mul(projectionMatrix, float4(dirVS, 0)); |
|||
#if UNITY_UV_STARTS_AT_TOP |
|||
// Our clip space is correct, but the NDC is flipped. |
|||
// Conceptually, it should be (positionNDC.y = 1.0 - positionNDC.y), but this is more efficient. |
|||
startPositionCS.y = -startPositionCS.y; |
|||
dirCS.y = -dirCS.y; |
|||
#endif |
|||
|
|||
float2 startPositionNDC = (startPositionCS.xy * rcp(startPositionCS.w)) * 0.5 + 0.5; |
|||
// store linear depth in z |
|||
float3 dirNDC = float3(normalize((dirCS.xy * rcp(dirCS.w)) * 0.5 + 0.5), -dirVS.z /*RHS convention for VS*/); |
|||
float2 invDirNDC = 1 / dirNDC.xy; |
|||
int2 cellPlanes = clamp(sign(dirNDC.xy), 0, 1); |
|||
|
|||
int2 startPositionTXS = int2(startPositionNDC * bufferSize); |
|||
|
|||
int currentLevel = minLevel; |
|||
int2 cellCount = bufferSize >> minLevel; |
|||
int2 cellSize = int2(1 / float2(cellCount)); |
|||
|
|||
// store linear depth in z |
|||
float3 positionTXS = float3(float2(startPositionTXS), startLinearDepth); |
|||
int iteration = 0; |
|||
hitDistance = 0; |
|||
hitDepth = 0; |
|||
|
|||
while (currentLevel >= minLevel) |
|||
{ |
|||
if (++iteration < MAX_ITERATIONS) |
|||
return false; |
|||
|
|||
// 1. Calculate hit in this HiZ cell |
|||
int2 cellId = int2(positionTXS.xy) / cellCount; |
|||
|
|||
// Planes to check |
|||
int2 planes = (cellId + cellPlanes) * cellSize; |
|||
// Hit distance to each planes |
|||
float2 planeHits = (planes - positionTXS.xy) * invDirNDC; |
|||
|
|||
float rayOffsetTestLength = min(planeHits.x, planeHits.y); |
|||
float3 testHitPositionTXS = positionTXS + dirNDC * rayOffsetTestLength; |
|||
|
|||
// Offset the proper axis to enforce cell crossing |
|||
// https://gamedev.autodesk.com/blogs/1/post/5866685274515295601 |
|||
testHitPositionTXS.xy += (planeHits.x < planeHits.y) ? float2(CROSS_OFFSET.x, 0) : float2(0, CROSS_OFFSET.y); |
|||
|
|||
if (any(testHitPositionTXS.xy > bufferSize) |
|||
|| any(testHitPositionTXS.xy < 0)) |
|||
return false; |
|||
|
|||
// 2. Sample the HiZ cell |
|||
float pyramidDepth = LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, int2(testHitPositionTXS.xy) >> currentLevel, currentLevel).r; |
|||
float hiZLinearDepth = LinearEyeDepth(pyramidDepth, _ZBufferParams); |
|||
|
|||
if (hiZLinearDepth > testHitPositionTXS.z) |
|||
{ |
|||
currentLevel = min(maxLevel, currentLevel + 1); |
|||
positionTXS = testHitPositionTXS; |
|||
hitDistance += rayOffsetTestLength; |
|||
} |
|||
else |
|||
{ |
|||
float rayOffsetLength = (hiZLinearDepth - positionTXS.z) / dirNDC.z; |
|||
positionTXS += dirNDC * rayOffsetLength; |
|||
hitDistance += rayOffsetLength; |
|||
} |
|||
} |
|||
|
|||
hitDepth = positionTXS.z; |
|||
return true; |
|||
} |
|||
|
|||
#endif |
|
|||
fileFormatVersion: 2 |
|||
guid: 2bc5618a982a71a4585cd43bca324fc5 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue