浏览代码

(wip) Screen Space raymarching for hit projection

/feature-ScreenSpaceProjection
Frédéric Vauchelles 7 年前
当前提交
8d36c467
共有 4 个文件被更改,包括 449 次插入35 次删除
  1. 354
      SampleScenes/HDTest/PlanarReflectionTests.unity
  2. 28
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  3. 93
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl
  4. 9
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl.meta

354
SampleScenes/HDTest/PlanarReflectionTests.unity


m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!1 &156769327
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 156769328}
- component: {fileID: 156769330}
- component: {fileID: 156769329}
m_Layer: 0
m_Name: Cube (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &156769328
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 156769327}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 6.791, y: 2.3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 402761694}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &156769329
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 156769327}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 2100000, guid: 19eca733d53dfd64f873fc8ff6fc10e6, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &156769330
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 156769327}
m_Mesh: {fileID: 4300000, guid: b075fe1ac5e67cb448c2a126c8e36a4a, type: 3}
--- !u!1 &241156468
GameObject:
m_ObjectHideFlags: 0

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 394595280}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &402761693
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 402761694}
m_Layer: 0
m_Name: Refraction
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &402761694
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 402761693}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 737980738}
- {fileID: 2110476610}
- {fileID: 156769328}
- {fileID: 715341231}
m_Father: {fileID: 1306845494}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &443104619
GameObject:
m_ObjectHideFlags: 0

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 713763938}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &715341230
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 715341231}
- component: {fileID: 715341233}
- component: {fileID: 715341232}
m_Layer: 0
m_Name: Sphere (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &715341231
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 715341230}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 8.049, y: 2.3, z: 0.026}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 402761694}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &715341232
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 715341230}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 2100000, guid: 8ebe34a210701364a81834f22173aaa2, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &715341233
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 715341230}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &737980737
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 737980738}
- component: {fileID: 737980740}
- component: {fileID: 737980739}
m_Layer: 0
m_Name: Sphere
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &737980738
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 737980737}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.859, y: 2.3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 402761694}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &737980739
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 737980737}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 2100000, guid: 8c6442ce9ad0de9428c7adb28112f20a, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &737980740
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 737980737}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &743933947
GameObject:
m_ObjectHideFlags: 0

- {fileID: 1179256903}
- {fileID: 1596124444}
- {fileID: 713763939}
- {fileID: 402761694}
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

m_RefreshMode: 1
m_CustomTexture: {fileID: 2800000, guid: 624b8c3a613121244b071ae2f92c971b, type: 3}
m_BakedTexture: {fileID: 2800000, guid: 78df8455ea6df3949940f01d0907a72c, type: 2}
m_RealtimeTexture: {fileID: 1830406945}
m_FrameSettings:
enableShadow: 1
enableContactShadows: 1

m_LookSpeedController: 120
m_LookSpeedMouse: 10
m_MoveSpeed: 50
m_MoveSpeedIncrement: 2.5
m_Turbo: 10
--- !u!114 &1828470167
MonoBehaviour:

m_EditorClassIdentifier:
renderPipelineAsset: {fileID: 11400000, guid: 1fa8c4c8604deee4e99ff30888538ca5,
type: 2}
--- !u!84 &1830406945
RenderTexture:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: PlanarProbeRT Planar Reflection Probe
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
m_Width: 512
m_Height: 512
m_AntiAliasing: 1
m_DepthFormat: 0
m_ColorFormat: 2
m_MipMap: 1
m_GenerateMips: 0
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 1
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
--- !u!1 &1882799006
GameObject:
m_ObjectHideFlags: 0

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2077996852}
m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &2110476609
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 2110476610}
- component: {fileID: 2110476612}
- component: {fileID: 2110476611}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2110476610
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2110476609}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 5.401, y: 2.3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 402761694}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &2110476611
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2110476609}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 2100000, guid: 07f76a948898a7f45bc29f1f24d6034f, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &2110476612
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2110476609}
m_Mesh: {fileID: 4300000, guid: b075fe1ac5e67cb448c2a126c8e36a4a, type: 3}
--- !u!1 &2125068291
GameObject:
m_ObjectHideFlags: 0

28
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


//-----------------------------------------------------------------------------
#if HAS_REFRACTION
# include "CoreRP/ShaderLibrary/DepthRaymarching.hlsl"
# include "CoreRP/ShaderLibrary/Refraction.hlsl"
# if defined(_REFRACTION_PLANE)

// a. Get the corresponding color depending on the roughness from the gaussian pyramid of the color buffer
// b. Multiply by the transmittance for absorption (depends on the optical depth)
float3 refractedBackPointWS = EstimateRaycast(V, posInput, preLightData.transparentPositionWS, preLightData.transparentRefractV);
float3 positionVS = mul(UNITY_MATRIX_V, float4(preLightData.transparentPositionWS, 1)).xyz;
float transparentRefractVVS = mul(UNITY_MATRIX_V, float4(preLightData.transparentRefractV, 0));
uint2 depthSize = uint2(_PyramidDepthMipSize.xy);
float hitDistance = 0;
float refractedBackPointDepth = 0;
if (!RaymarchDepthBuffer(
positionVS,
posInput.linearDepth,
transparentRefractVVS,
UNITY_MATRIX_P,
depthSize,
0,
int(_PyramidDepthMipSize.z),
hitDistance,
refractedBackPointDepth
))
return lighting;
//float3 refractedBackPointWS = EstimateRaycast(V, posInput, preLightData.transparentPositionWS, preLightData.transparentRefractV);
float3 refractedBackPointWS = preLightData.transparentPositionWS + preLightData.transparentRefractV * hitDistance;
uint2 depthSize = uint2(_PyramidDepthMipSize.xy);
float refractedBackPointDepth = LinearEyeDepth(LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, refractedBackPointNDC * depthSize, 0).r, _ZBufferParams);
//float refractedBackPointDepth = LinearEyeDepth(LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, refractedBackPointNDC * depthSize, 0).r, _ZBufferParams);
// Exit if texel is out of color buffer
// Or if the texel is from an object in front of the object

93
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl


#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

9
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/DepthRaymarching.hlsl.meta


fileFormatVersion: 2
guid: 2bc5618a982a71a4585cd43bca324fc5
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存