Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
- {
- if (ReferenceEquals(_ssrMat, null))
- return;
- _camColorHandle = renderingData.cameraData.renderer.cameraColorTargetHandle;
- var descriptor = renderingData.cameraData.cameraTargetDescriptor;
- descriptor.depthBufferBits = 0;
- RenderingUtils.ReAllocateIfNeeded(ref _tempHandle, descriptor, FilterMode.Bilinear);
- 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);
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- var component = VolumeManager.instance.stack.GetComponent<ScreenSpaceReflectionVolume>();
- if (ReferenceEquals(component, null) || !component.IsActive()) return;
- _ssrMat.SetInt(MaxMarchStep, component.maxMarchStep.value);
- _ssrMat.SetFloat(MarchStepLength, component.marchStepLength.value);
- _ssrMat.SetFloat(MarchThreshold, component.marchThreshold.value);
- CommandBuffer cmd = CommandBufferPool.Get("ScreenSpaceReflection");
- using (new ProfilingScope(cmd, profilingSampler))
- {
- Blit(cmd, _camColorHandle, _tempHandle, _ssrMat);
- Blit(cmd, _tempHandle, _camColorHandle);
- }
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- 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 = normalize(reflect(normalize(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 abs(positionWSRel + _WorldSpaceCameraPos);
- }
- // return abs(positionWSSurfaceRel + _WorldSpaceCameraPos);
- return SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearRepeat, IN.texcoord);
- }
- ENDHLSL
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement