CopyStdFromDoubleWide.shader 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Shader "Hidden/PostProcessing/CopyStdFromDoubleWide"
  2. {
  3. //Blit from single-pass double-wide texture. Similar to CopyStd but with stereo texture as source
  4. //and samples from double-wide. Having separate shader is cleaner than multiple #if in the code.
  5. Properties
  6. {
  7. _MainTex ("", 2D) = "white" {}
  8. }
  9. CGINCLUDE
  10. struct Attributes
  11. {
  12. float4 vertex : POSITION;
  13. float2 texcoord : TEXCOORD0;
  14. };
  15. struct Varyings
  16. {
  17. float4 vertex : SV_POSITION;
  18. float2 texcoord : TEXCOORD0;
  19. };
  20. sampler2D _MainTex;
  21. float4 _UVScaleOffset;
  22. Varyings Vert(Attributes v)
  23. {
  24. Varyings o;
  25. o.vertex = float4(v.vertex.xy * 2.0 - 1.0, 0.0, 1.0);
  26. o.texcoord = v.texcoord;
  27. #if UNITY_UV_STARTS_AT_TOP
  28. o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
  29. #endif
  30. o.texcoord = o.texcoord * _UVScaleOffset.xy + _UVScaleOffset.zw;
  31. return o;
  32. }
  33. float4 Frag(Varyings i) : SV_Target
  34. {
  35. float4 color = tex2D(_MainTex, i.texcoord);
  36. return color;
  37. }
  38. //>>> We don't want to include StdLib.hlsl in this file so let's copy/paste what we need
  39. bool IsNan(float x)
  40. {
  41. return (x < 0.0 || x > 0.0 || x == 0.0) ? false : true;
  42. }
  43. bool AnyIsNan(float4 x)
  44. {
  45. return IsNan(x.x) || IsNan(x.y) || IsNan(x.z) || IsNan(x.w);
  46. }
  47. //<<<
  48. float4 FragKillNaN(Varyings i) : SV_Target
  49. {
  50. float4 color = tex2D(_MainTex, i.texcoord);
  51. if (AnyIsNan(color))
  52. {
  53. color = (0.0).xxxx;
  54. }
  55. return color;
  56. }
  57. ENDCG
  58. SubShader
  59. {
  60. Cull Off ZWrite Off ZTest Always
  61. // 0 - Copy
  62. Pass
  63. {
  64. CGPROGRAM
  65. #pragma vertex Vert
  66. #pragma fragment Frag
  67. ENDCG
  68. }
  69. // 1 - Copy + NaN killer
  70. Pass
  71. {
  72. CGPROGRAM
  73. #pragma vertex Vert
  74. #pragma fragment FragKillNaN
  75. ENDCG
  76. }
  77. }
  78. }