Advertisement
443eb9

Untitled

Sep 30th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  2. {
  3. if (ReferenceEquals(_ssrMat, null))
  4. return;
  5.  
  6. _camColorHandle = renderingData.cameraData.renderer.cameraColorTargetHandle;
  7. var descriptor = renderingData.cameraData.cameraTargetDescriptor;
  8. descriptor.depthBufferBits = 0;
  9. RenderingUtils.ReAllocateIfNeeded(ref _tempHandle, descriptor, FilterMode.Bilinear);
  10.  
  11. Matrix4x4 v = renderingData.cameraData.GetViewMatrix();
  12. Matrix4x4 p = renderingData.cameraData.GetProjectionMatrix();
  13.  
  14. v.SetColumn(3, new Vector4(0, 0, 0, 1));
  15. Matrix4x4 vpInv = (p * v).inverse;
  16. Vector3 topLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
  17. Vector3 topRightCorner = vpInv.MultiplyPoint(new Vector3(1, -1, -1));
  18. Vector3 bottomLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
  19.  
  20. _ssrMat.SetVector(CamPosToNearLeftUpperVec, topLeftCorner);
  21. _ssrMat.SetVector(NearTopLeftToTopRightVec, topRightCorner - topLeftCorner);
  22. _ssrMat.SetVector(NearTopLeftToBottomLeftVec, bottomLeftCorner - topLeftCorner);
  23. }
  24.  
  25. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  26. {
  27. var component = VolumeManager.instance.stack.GetComponent<ScreenSpaceReflectionVolume>();
  28. if (ReferenceEquals(component, null) || !component.IsActive()) return;
  29.  
  30. _ssrMat.SetInt(MaxMarchStep, component.maxMarchStep.value);
  31. _ssrMat.SetFloat(MarchStepLength, component.marchStepLength.value);
  32. _ssrMat.SetFloat(MarchThreshold, component.marchThreshold.value);
  33.  
  34. CommandBuffer cmd = CommandBufferPool.Get("ScreenSpaceReflection");
  35.  
  36. using (new ProfilingScope(cmd, profilingSampler))
  37. {
  38. Blit(cmd, _camColorHandle, _tempHandle, _ssrMat);
  39. Blit(cmd, _tempHandle, _camColorHandle);
  40. }
  41.  
  42. context.ExecuteCommandBuffer(cmd);
  43. CommandBufferPool.Release(cmd);
  44. }
  45.  
  46.  
  47. Pass
  48. {
  49. HLSLPROGRAM
  50. #pragma vertex Vert
  51. #pragma fragment frag
  52. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  53. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  54. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
  55. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  56.  
  57. CBUFFER_START(UnityPerMaterial)
  58. int _MaxMarchStep;
  59. float _MarchStepLength;
  60. float _MarchThreshold;
  61. float3 _CamPosToNearLeftUpperVec;
  62. float3 _NearTopLeftToTopRightVec;
  63. float3 _NearTopLeftToBottomLeftVec;
  64. CBUFFER_END
  65.  
  66. float3 ReconstructPositionWSRelativeToCamera(float2 uv)
  67. {
  68. float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  69. float3 positionWSIntersectWithNear =
  70. _CamPosToNearLeftUpperVec
  71. + uv.x * _NearTopLeftToTopRightVec
  72. + uv.y * _NearTopLeftToBottomLeftVec;
  73. // scalene triangle
  74. return depth / _ProjectionParams.y * positionWSIntersectWithNear;
  75. }
  76.  
  77. void ReconstructUVAndDepth(float3 positionWS, out float2 uv, out float depth)
  78. {
  79. float4 positionCS = TransformWorldToHClip(positionWS);
  80. uv = float2(positionCS.x, positionCS.y * _ProjectionParams.x) / positionCS.w * 0.5 + 0.5;
  81. depth = positionCS.w;
  82. }
  83.  
  84. float3 frag(Varyings IN) : SV_Target
  85. {
  86. float3 positionWSSurfaceRel = ReconstructPositionWSRelativeToCamera(IN.texcoord);
  87. float3 reflectDirWS = normalize(reflect(normalize(positionWSSurfaceRel), SampleSceneNormals(IN.texcoord)));
  88.  
  89. UNITY_LOOP
  90. for (int step = 0; step < _MaxMarchStep; ++step)
  91. {
  92. float3 positionWSRel = positionWSSurfaceRel + step * _MarchStepLength * reflectDirWS;
  93. float2 uv;
  94. float rayDepth;
  95. ReconstructUVAndDepth(positionWSRel + _WorldSpaceCameraPos, uv, rayDepth);
  96. float surfaceDepth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  97. if (rayDepth > surfaceDepth && rayDepth < surfaceDepth + _MarchThreshold)
  98. return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, uv);
  99. // return abs(positionWSRel + _WorldSpaceCameraPos);
  100. }
  101. // return abs(positionWSSurfaceRel + _WorldSpaceCameraPos);
  102. return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, IN.texcoord);
  103. }
  104. ENDHLSL
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement