Advertisement
443eb9

Untitled

Sep 29th, 2023 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. Matrix4x4 v = renderingData.cameraData.GetViewMatrix();
  2. Matrix4x4 p = renderingData.cameraData.GetProjectionMatrix();
  3.  
  4. v.SetColumn(3, new Vector4(0, 0, 0, 1));
  5. Matrix4x4 vpInv = (p * v).inverse;
  6. float near = renderingData.cameraData.camera.nearClipPlane;
  7.  
  8. Vector3 topLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
  9. Vector3 topRightCorner = vpInv.MultiplyPoint(new Vector3(1, 1, -1));
  10. Vector3 bottomLeftCorner = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
  11.  
  12. Shader.SetGlobalVector(CamPosToNearLeftUpperVec, topLeftCorner);
  13. Shader.SetGlobalVector(NearTopLeftToTopRightVec, topRightCorner - topLeftCorner);
  14. Shader.SetGlobalVector(NearTopLeftToBottomLeftVec, bottomLeftCorner - topLeftCorner);
  15.  
  16.  
  17. Pass
  18. {
  19. HLSLPROGRAM
  20. #pragma vertex Vert
  21. #pragma fragment frag
  22. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  23. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  24. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  25.  
  26. float3 _CamPosToNearLeftUpperVec;
  27. float3 _NearTopLeftToTopRightVec;
  28. float3 _NearTopLeftToBottomLeftVec;
  29.  
  30. float3 ReconstructPositionWS(float2 uv)
  31. {
  32. float depth = LinearEyeDepth(SampleSceneDepth(uv), _ZBufferParams);
  33. float3 positionWSIntersectWithNear =
  34. _CamPosToNearLeftUpperVec
  35. + uv.x * _NearTopLeftToTopRightVec
  36. + uv.y * _NearTopLeftToBottomLeftVec;
  37. // scalene triangle
  38. return depth / _ProjectionParams.y * positionWSIntersectWithNear + _WorldSpaceCameraPos;
  39. }
  40.  
  41. float3 frag(Varyings IN) : SV_Target
  42. {
  43. return abs(ReconstructPositionWS(IN.texcoord));
  44. }
  45. ENDHLSL
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement