OculusMotionVectorCore.hlsl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  2. struct Attributes
  3. {
  4. float4 positionOS : POSITION;
  5. float3 previousPositionOS : TEXCOORD4;
  6. UNITY_VERTEX_INPUT_INSTANCE_ID
  7. };
  8. struct Varyings
  9. {
  10. float4 positionCS : SV_POSITION;
  11. float4 curPositionCS : TEXCOORD8;
  12. float4 prevPositionCS : TEXCOORD9;
  13. UNITY_VERTEX_OUTPUT_STEREO
  14. };
  15. bool IsSmoothRotation(float3 prevAxis1, float3 prevAxis2, float3 currAxis1, float3 currAxis2)
  16. {
  17. float angleThreshold = 0.984f; // cos(10 degrees)
  18. float2 angleDot = float2(dot(normalize(prevAxis1), normalize(currAxis1)), dot(normalize(prevAxis2), normalize(currAxis2)));
  19. return all(angleDot > angleThreshold);
  20. }
  21. // Best effort for particles, we'll just take into account the camera motion.
  22. Varyings vertParticles(Attributes input)
  23. {
  24. Varyings output = (Varyings)0;
  25. UNITY_SETUP_INSTANCE_ID(input);
  26. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  27. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  28. output.positionCS = vertexInput.positionCS;
  29. float3 curWS = TransformObjectToWorld(input.positionOS.xyz);
  30. output.curPositionCS = TransformWorldToHClip(curWS);
  31. output.prevPositionCS = TransformWorldToPrevHClip(curWS);
  32. return output;
  33. }
  34. Varyings vert(Attributes input)
  35. {
  36. Varyings output = (Varyings)0;
  37. UNITY_SETUP_INSTANCE_ID(input);
  38. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  39. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  40. output.positionCS = vertexInput.positionCS;
  41. float3 curWS = TransformObjectToWorld(input.positionOS.xyz);
  42. output.curPositionCS = TransformWorldToHClip(curWS);
  43. if (unity_MotionVectorsParams.y == 0.0)
  44. {
  45. output.prevPositionCS = float4(0.0, 0.0, 0.0, 1.0);
  46. }
  47. else
  48. {
  49. bool hasDeformation = unity_MotionVectorsParams.x > 0.0;
  50. float3 effectivePositionOS = (hasDeformation ? input.previousPositionOS : input.positionOS.xyz);
  51. float3 previousWS = TransformPreviousObjectToWorld(effectivePositionOS);
  52. float4x4 previousOTW = GetPrevObjectToWorldMatrix();
  53. float4x4 currentOTW = GetObjectToWorldMatrix();
  54. if (!IsSmoothRotation(previousOTW._11_21_31, previousOTW._12_22_32, currentOTW._11_21_31, currentOTW._12_22_32))
  55. {
  56. output.prevPositionCS = output.curPositionCS;
  57. }
  58. else
  59. {
  60. output.prevPositionCS = TransformWorldToPrevHClip(previousWS);
  61. }
  62. }
  63. return output;
  64. }
  65. half4 frag(Varyings i) : SV_Target
  66. {
  67. float3 screenPos = i.curPositionCS.xyz / i.curPositionCS.w;
  68. float3 screenPosPrev = i.prevPositionCS.xyz / i.prevPositionCS.w;
  69. half4 color = (1);
  70. color.xyz = screenPos - screenPosPrev;
  71. return color;
  72. }