Advertisement
443eb9

Untitled

Sep 29th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 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.  
  15. Pass
  16. {
  17. HLSLPROGRAM
  18. #pragma vertex Vert
  19. #pragma fragment frag
  20. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  21. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  22. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
  23. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  24.  
  25. CBUFFER_START(UnityPerMaterial)
  26. int _MaxMarchStep;
  27. float _MarchStepLength;
  28. float _MarchThreshold;
  29. float3 _CamPosToNearLeftUpperVec;
  30. float3 _NearTopLeftToTopRightVec;
  31. float3 _NearTopLeftToBottomLeftVec;
  32. CBUFFER_END
  33.  
  34. float3 ReconstructPositionWSRelativeToCamera(float2 uv)
  35. {
  36. float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  37. float3 positionWSIntersectWithNear =
  38. _CamPosToNearLeftUpperVec
  39. + uv.x * _NearTopLeftToTopRightVec
  40. + uv.y * _NearTopLeftToBottomLeftVec;
  41. // scalene triangle
  42. return depth / _ProjectionParams.y * positionWSIntersectWithNear;
  43. }
  44.  
  45. void ReconstructUVAndDepth(float3 positionWS, out float2 uv, out float depth)
  46. {
  47. float4 positionCS = TransformWorldToHClip(positionWS);
  48. uv = float2(positionCS.x, positionCS.y * _ProjectionParams.x) / positionCS.w * 0.5 + 0.5;
  49. depth = positionCS.w;
  50. }
  51.  
  52. float3 frag(Varyings IN) : SV_Target
  53. {
  54. float3 positionWSSurfaceRel = ReconstructPositionWSRelativeToCamera(IN.texcoord);
  55. float3 reflectDirWS = normalize(reflect(normalize(positionWSSurfaceRel), SampleSceneNormals(IN.texcoord)));
  56. UNITY_LOOP
  57. for (int step = 0; step < _MaxMarchStep; ++step)
  58. {
  59. float3 positionWSRel = positionWSSurfaceRel + step * _MarchStepLength * reflectDirWS;
  60. float2 uv;
  61. float rayDepth;
  62. ReconstructUVAndDepth(positionWSRel + _WorldSpaceCameraPos, uv, rayDepth);
  63. float surfaceDepth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  64. if (rayDepth > surfaceDepth && rayDepth < surfaceDepth + _MarchThreshold)
  65. return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, uv);
  66. // return abs(positionWSRel + _WorldSpaceCameraPos);
  67. }
  68. // return abs(positionWSSurfaceRel + _WorldSpaceCameraPos);
  69. return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, IN.texcoord);
  70. }
  71. ENDHLSL
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement