Advertisement
443eb9

Untitled

Sep 28th, 2023
32
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. // set translation to 0
  5. v.SetColumn(3, new Vector4(0, 0, 0, 1));
  6. Matrix4x4 vpInv = Matrix4x4.Inverse(p * v);
  7. Vector3 topLeft = vpInv.MultiplyPoint(new Vector3(-1, 1, -1));
  8. Vector3 topRight = vpInv.MultiplyPoint(new Vector3(1, 1, -1));
  9. Vector3 bottomLeft = vpInv.MultiplyPoint(new Vector3(-1, -1, -1));
  10.  
  11. // these are 3 ids, corresponding to the property _xxx
  12. Shader.SetGlobalVector(CamPosToNearLeftUpperVec, topLeft);
  13. Shader.SetGlobalVector(NearTopLeftToTopRightVec, topRight - topLeft);
  14. Shader.SetGlobalVector(NearTopLeftToBottomLeftVec, bottomLeft - topLeft);
  15.  
  16.  
  17.  
  18. Pass
  19. {
  20. HLSLPROGRAM
  21. #pragma vertex Vert
  22. #pragma fragment frag
  23. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  24. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  25. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  26. float3 _CamPosToNearLeftUpperVec;
  27. float3 _NearTopLeftToTopRightVec;
  28. float3 _NearTopLeftToBottomLeftVec;
  29. float ReconstructWorldPosition(float2 uv)
  30. {
  31. // uv.y = 1 - uv.y;
  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. float3 frag(Varyings IN) : SV_Target
  41. {
  42. return ReconstructWorldPosition(IN.texcoord);
  43. }
  44. ENDHLSL
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement