Advertisement
443eb9

Untitled

Sep 29th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. Matrix4x4 v = renderingData.cameraData.GetViewMatrix();
  2. Matrix4x4 p = renderingData.cameraData.GetProjectionMatrix();
  3. v.SetColumn(3, new Vector4(0, 0, 0, 1));
  4. Matrix4x4 vpInv = (p * v).inverse;
  5.  
  6. Vector3 topLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
  7. Vector3 topRightCorner = vpInv.MultiplyPoint(new Vector3(1, -1, -1));
  8. Vector3 bottomLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
  9.  
  10. _ssrMat.SetVector(CamPosToNearLeftUpperVec, topLeftCorner);
  11. _ssrMat.SetVector(NearTopLeftToTopRightVec, topRightCorner - topLeftCorner);
  12. _ssrMat.SetVector(NearTopLeftToBottomLeftVec, bottomLeftCorner - topLeftCorner);
  13.  
  14. Pass
  15. {
  16. HLSLPROGRAM
  17. #pragma vertex Vert
  18. #pragma fragment frag
  19. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  20. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  21. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
  22. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  23.  
  24. CBUFFER_START(UnityPerMaterial)
  25. int _MaxMarchStep;
  26. float _MarchStepLength;
  27. float _MarchThreshold;
  28. float3 _CamPosToNearLeftUpperVec;
  29. float3 _NearTopLeftToTopRightVec;
  30. float3 _NearTopLeftToBottomLeftVec;
  31. CBUFFER_END
  32.  
  33. float3 ReconstructPositionWSRelativeToCamera(float2 uv)
  34. {
  35. float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  36. float3 positionWSIntersectWithNear =
  37. _CamPosToNearLeftUpperVec
  38. + uv.x * _NearTopLeftToTopRightVec
  39. + uv.y * _NearTopLeftToBottomLeftVec;
  40. // scalene triangle
  41. return depth / _ProjectionParams.y * positionWSIntersectWithNear;
  42. }
  43.  
  44. void ReconstructUVAndDepth(float3 positionWS, out float2 uv, out float depth)
  45. {
  46. float4 positionCS = TransformWorldToHClip(positionWS);
  47. uv = float2(positionCS.x, positionCS.y * _ProjectionParams.x) / positionCS.w * 0.5 + 0.5;
  48. depth = positionCS.w;
  49. }
  50.  
  51. float3 frag(Varyings IN) : SV_Target
  52. {
  53. float3 positionWSSurfaceRel = ReconstructPositionWSRelativeToCamera(IN.texcoord);
  54. float3 reflectDirWS = SafeNormalize(reflect(SafeNormalize(positionWSSurfaceRel), SampleSceneNormals(IN.texcoord)));
  55. UNITY_LOOP
  56. for (int step = 0; step < _MaxMarchStep; ++step)
  57. {
  58. float3 positionWSRel = positionWSSurfaceRel + step * _MarchStepLength * reflectDirWS;
  59. float2 uv;
  60. float rayDepth;
  61. ReconstructUVAndDepth(positionWSRel + _WorldSpaceCameraPos, uv, rayDepth);
  62. float surfaceDepth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  63. if (rayDepth > surfaceDepth && rayDepth < surfaceDepth + _MarchThreshold)
  64. return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, uv);
  65. }
  66. return 0;
  67. // return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, IN.texcoord);
  68. }
  69. ENDHLSL
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement