Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Matrix4x4 v = renderingData.cameraData.GetViewMatrix();
- Matrix4x4 p = renderingData.cameraData.GetProjectionMatrix();
- // set translation to 0
- v.SetColumn(3, new Vector4(0, 0, 0, 1));
- Matrix4x4 vpInv = Matrix4x4.Inverse(p * v);
- Vector3 topLeft = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
- Vector3 topRight = vpInv.MultiplyPoint(new Vector3(1, 1, -1));
- Vector3 bottomLeft = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
- // these are 3 ids, corresponding to the property _xxx
- Shader.SetGlobalVector(CamPosToNearLeftUpperVec, topLeft);
- Shader.SetGlobalVector(NearTopLeftToTopRightVec, topRight - topLeft);
- Shader.SetGlobalVector(NearTopLeftToBottomLeftVec, bottomLeft - topLeft);
- 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.core/Runtime/Utilities/Blit.hlsl"
- float3 _CamPosToNearLeftUpperVec;
- float3 _NearTopLeftToTopRightVec;
- float3 _NearTopLeftToBottomLeftVec;
- float ReconstructWorldPosition(float2 uv)
- {
- // uv.y = 1 - uv.y;
- float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
- float3 positionWSIntersectWithNear =
- _CamPosToNearLeftUpperVec
- + uv.x * _NearTopLeftToTopRightVec
- + uv.y * _NearTopLeftToBottomLeftVec;
- // scalene triangle
- return depth / _ProjectionParams.y * positionWSIntersectWithNear + _WorldSpaceCameraPos;
- }
- float3 frag(Varyings IN) : SV_Target
- {
- return ReconstructWorldPosition(IN.texcoord);
- }
- ENDHLSL
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement