CopyStdFromTexArray.shader 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Shader "Hidden/PostProcessing/CopyStdFromTexArray"
  2. {
  3. //Blit from texture array slice. Similar to CopyStd but with texture array as source
  4. //and sampling from texture array. Having separate shader is cleaner than multiple #if in the code.
  5. Properties
  6. {
  7. _MainTex ("", 2DArray) = "white" {}
  8. }
  9. CGINCLUDE
  10. #pragma target 3.5
  11. struct Attributes
  12. {
  13. float3 vertex : POSITION;
  14. };
  15. struct Varyings
  16. {
  17. float4 vertex : SV_POSITION;
  18. float3 texcoord : TEXCOORD0;
  19. };
  20. Texture2DArray _MainTex;
  21. SamplerState sampler_MainTex;
  22. float _DepthSlice;
  23. float2 TransformTriangleVertexToUV(float2 vertex)
  24. {
  25. float2 uv = (vertex + 1.0) * 0.5;
  26. return uv;
  27. }
  28. Varyings Vert(Attributes v)
  29. {
  30. Varyings o;
  31. o.vertex = float4(v.vertex.xy, 0.0, 1.0);
  32. o.texcoord.xy = TransformTriangleVertexToUV(v.vertex.xy);
  33. #if UNITY_UV_STARTS_AT_TOP
  34. o.texcoord.xy = o.texcoord.xy * float2(1.0, -1.0) + float2(0.0, 1.0);
  35. #endif
  36. o.texcoord.z = _DepthSlice;
  37. return o;
  38. }
  39. float4 Frag(Varyings i) : SV_Target
  40. {
  41. float4 color = _MainTex.Sample(sampler_MainTex, i.texcoord);
  42. return color;
  43. }
  44. bool IsNan(float x)
  45. {
  46. return (x < 0.0 || x > 0.0 || x == 0.0) ? false : true;
  47. }
  48. bool AnyIsNan(float4 x)
  49. {
  50. return IsNan(x.x) || IsNan(x.y) || IsNan(x.z) || IsNan(x.w);
  51. }
  52. float4 FragKillNaN(Varyings i) : SV_Target
  53. {
  54. float4 color = _MainTex.Sample(sampler_MainTex, i.texcoord);
  55. if (AnyIsNan(color))
  56. {
  57. color = (0.0).xxxx;
  58. }
  59. return color;
  60. }
  61. ENDCG
  62. SubShader
  63. {
  64. Cull Off ZWrite Off ZTest Always
  65. // 0 - Copy
  66. Pass
  67. {
  68. CGPROGRAM
  69. #pragma vertex Vert
  70. #pragma fragment Frag
  71. ENDCG
  72. }
  73. // 0 - Copy + NaN killer
  74. Pass
  75. {
  76. CGPROGRAM
  77. #pragma vertex Vert
  78. #pragma fragment FragKillNaN
  79. ENDCG
  80. }
  81. }
  82. }