Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Matrix4x4 v = renderingData.cameraData.GetViewMatrix();
- Matrix4x4 p = renderingData.cameraData.GetProjectionMatrix();
- v.SetColumn(3, new Vector4(0, 0, 0, 1));
- Matrix4x4 vpInv = (p * v).inverse;
- Vector3 topLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
- Vector3 topRightCorner = vpInv.MultiplyPoint(new Vector3(1, -1, -1));
- Vector3 bottomLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
- _ssrMat.SetVector(CamPosToNearLeftUpperVec, topLeftCorner);
- _ssrMat.SetVector(NearTopLeftToTopRightVec, topRightCorner - topLeftCorner);
- _ssrMat.SetVector(NearTopLeftToBottomLeftVec, bottomLeftCorner - topLeftCorner);
- Pass
- {
- HLSLPROGRAM
- #pragma vertex Vert
- #pragma fragment frag
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
- #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
- CBUFFER_START(UnityPerMaterial)
- int _MaxMarchStep;
- float _MarchStepLength;
- float _MarchThreshold;
- float3 _CamPosToNearLeftUpperVec;
- float3 _NearTopLeftToTopRightVec;
- float3 _NearTopLeftToBottomLeftVec;
- CBUFFER_END
- float3 ReconstructPositionWSRelativeToCamera(float2 uv)
- {
- float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
- float3 positionWSIntersectWithNear =
- _CamPosToNearLeftUpperVec
- + uv.x * _NearTopLeftToTopRightVec
- + uv.y * _NearTopLeftToBottomLeftVec;
- // scalene triangle
- return depth / _ProjectionParams.y * positionWSIntersectWithNear;
- }
- void ReconstructUVAndDepth(float3 positionWS, out float2 uv, out float depth)
- {
- float4 positionCS = TransformWorldToHClip(positionWS);
- uv = float2(positionCS.x, positionCS.y * _ProjectionParams.x) / positionCS.w * 0.5 + 0.5;
- depth = positionCS.w;
- }
- float3 frag(Varyings IN) : SV_Target
- {
- float3 positionWSSurfaceRel = ReconstructPositionWSRelativeToCamera(IN.texcoord);
- float3 reflectDirWS = SafeNormalize(reflect(SafeNormalize(positionWSSurfaceRel), SampleSceneNormals(IN.texcoord)));
- UNITY_LOOP
- for (int step = 0; step < _MaxMarchStep; ++step)
- {
- float3 positionWSRel = positionWSSurfaceRel + step * _MarchStepLength * reflectDirWS;
- float2 uv;
- float rayDepth;
- ReconstructUVAndDepth(positionWSRel + _WorldSpaceCameraPos, uv, rayDepth);
- float surfaceDepth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
- if (rayDepth > surfaceDepth && rayDepth < surfaceDepth + _MarchThreshold)
- return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, uv);
- }
- return 0;
- // return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, IN.texcoord);
- }
- ENDHLSL
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement