No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XRMotionVector.shader 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Shader "Hidden/Universal Render Pipeline/XR/XRMotionVector"
  2. {
  3. SubShader
  4. {
  5. Tags{ "RenderPipeline" = "UniversalPipeline" }
  6. Pass
  7. {
  8. Name "XR Camera MotionVectors"
  9. Cull Off
  10. ZWrite On
  11. ColorMask RGBA
  12. // Stencil test to only fill the pixels that doesn't have object motion data filled by the previous pass.
  13. Stencil
  14. {
  15. WriteMask 1
  16. ReadMask 1
  17. Ref 1
  18. Comp NotEqual
  19. // Fail Zero
  20. // Pass Zero
  21. }
  22. HLSLPROGRAM
  23. #pragma target 3.5
  24. #pragma vertex Vert
  25. #pragma fragment Frag
  26. // -------------------------------------
  27. // Includes
  28. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  29. // -------------------------------------
  30. // Structs
  31. struct Attributes
  32. {
  33. uint vertexID : SV_VertexID;
  34. UNITY_VERTEX_INPUT_INSTANCE_ID
  35. };
  36. struct Varyings
  37. {
  38. float4 position : SV_POSITION;
  39. float3 posWS : TEXCOORD0;
  40. UNITY_VERTEX_OUTPUT_STEREO
  41. };
  42. // -------------------------------------
  43. // Vertex
  44. Varyings Vert(Attributes input)
  45. {
  46. Varyings output;
  47. UNITY_SETUP_INSTANCE_ID(input);
  48. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  49. output.position = GetFullScreenTriangleVertexPosition(input.vertexID);
  50. float depth = 1 - UNITY_NEAR_CLIP_VALUE;
  51. output.position.z = depth;
  52. // Reconstruct world position
  53. output.posWS = ComputeWorldSpacePosition(output.position, depth, UNITY_MATRIX_I_VP);
  54. return output;
  55. }
  56. // -------------------------------------
  57. // Fragment
  58. half4 Frag(Varyings input) : SV_Target
  59. {
  60. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  61. // Multiply with current and previous non-jittered view projection
  62. float4 posCS = mul(_NonJitteredViewProjMatrix, float4(input.posWS.xyz, 1.0));
  63. float4 prevPosCS = mul(_PrevViewProjMatrix, float4(input.posWS.xyz, 1.0));
  64. // Non-uniform raster needs to keep the posNDC values in float to avoid additional conversions
  65. // since uv remap functions use floats
  66. float3 posNDC = posCS.xyz * rcp(posCS.w);
  67. float3 prevPosNDC = prevPosCS.xyz * rcp(prevPosCS.w);
  68. // Calculate forward velocity
  69. float3 velocity = (posNDC - prevPosNDC);
  70. return float4(velocity.xyz, 0);
  71. }
  72. ENDHLSL
  73. }
  74. }
  75. Fallback Off
  76. }