CustomMotionVectorTexture.shader 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Shader "Post Processing/Custom Motion Vector Texture"
  2. {
  3. Properties
  4. {
  5. _MotionTex ("Motion Vector Texture", 2D) = "black" {}
  6. _MotionAmount ("Motion Vector Multiplier", range (-0.25, 0.25)) = 0
  7. }
  8. SubShader
  9. {
  10. Pass
  11. {
  12. Name "Motion Vectors"
  13. Tags { "LightMode" = "MotionVectors" }
  14. ZTest LEqual Cull Back ZWrite On
  15. CGPROGRAM
  16. #pragma vertex vert
  17. #pragma fragment FragMotionVectors
  18. #include "UnityCG.cginc"
  19. float4 _MotionValue;
  20. sampler2D _MotionTex;
  21. float4 _MotionTex_ST;
  22. float _MotionAmount;
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. float3 normal : NORMAL;
  28. float4 tangent : TANGENT;
  29. };
  30. struct v2f
  31. {
  32. float2 uv : TEXCOORD0;
  33. float4 vertex : SV_POSITION;
  34. float3 normal : NORMAL;
  35. float4 tangent : TANGENT;
  36. float4 transposedTangent : TEXCOORD1;
  37. };
  38. v2f vert (appdata v)
  39. {
  40. v2f o;
  41. o.vertex = UnityObjectToClipPos(v.vertex);
  42. o.uv = TRANSFORM_TEX(v.uv, _MotionTex);
  43. o.normal = UnityObjectToClipPos(v.normal);
  44. o.normal = o.normal * 0.5 + 0.5;
  45. o.tangent = mul(UNITY_MATRIX_MV, v.tangent);
  46. o.transposedTangent = (mul(UNITY_MATRIX_IT_MV, v.tangent)) * 0.5 + 0.5;
  47. return o;
  48. }
  49. float4 FragMotionVectors(v2f i) : SV_Target
  50. {
  51. half4 c = tex2D(_MotionTex, i.uv);
  52. c.rg = (c.rg * 2.0 - 1.0) * _MotionAmount; // Using color texture so need to make 0.5 neutral
  53. half4 t1 = i.tangent * 0.005; // Sides of tire
  54. half4 t2 = c * float4(i.transposedTangent.r * 2.0, i.transposedTangent.g * 2.0, 0.0, 1.0); // Front of tire
  55. half4 t3 = lerp(t2, t1, c.b); // Lerp between front and side of tire
  56. return t3 * _MotionAmount;
  57. }
  58. ENDCG
  59. }
  60. }
  61. }