AVProVideo-VR-InsideSphere.shader 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Shader "AVProVideo/VR/InsideSphere Unlit (stereo+fog)"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "black" {}
  6. _ChromaTex("Chroma", 2D) = "white" {}
  7. [KeywordEnum(None, Top_Bottom, Left_Right, Custom_UV)] Stereo ("Stereo Mode", Float) = 0
  8. [KeywordEnum(None, Left, Right)] ForceEye ("Force Eye Mode", Float) = 0
  9. [Toggle(STEREO_DEBUG)] _StereoDebug ("Stereo Debug Tinting", Float) = 0
  10. [KeywordEnum(None, EquiRect180)] Layout("Layout", Float) = 0
  11. [Toggle(HIGH_QUALITY)] _HighQuality ("High Quality", Float) = 0
  12. [Toggle(APPLY_GAMMA)] _ApplyGamma("Apply Gamma", Float) = 0
  13. [Toggle(USE_YPCBCR)] _UseYpCbCr("Use YpCbCr", Float) = 0
  14. }
  15. SubShader
  16. {
  17. Tags { "RenderType"="Opaque" "IgnoreProjector" = "True" "Queue" = "Background" }
  18. ZWrite On
  19. //ZTest Always
  20. Cull Front
  21. Lighting Off
  22. Pass
  23. {
  24. CGPROGRAM
  25. #include "UnityCG.cginc"
  26. #include "AVProVideo.cginc"
  27. #if HIGH_QUALITY || APPLY_GAMMA
  28. #pragma target 3.0
  29. #endif
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. //#define STEREO_DEBUG 1
  33. //#define HIGH_QUALITY 1
  34. #pragma multi_compile_fog
  35. // TODO: replace use multi_compile_local instead (Unity 2019.1 feature)
  36. #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
  37. #pragma multi_compile FORCEEYE_NONE FORCEEYE_LEFT FORCEEYE_RIGHT
  38. #pragma multi_compile __ STEREO_DEBUG
  39. #pragma multi_compile __ HIGH_QUALITY
  40. #pragma multi_compile __ APPLY_GAMMA
  41. #pragma multi_compile __ USE_YPCBCR
  42. #pragma multi_compile __ LAYOUT_EQUIRECT180
  43. struct appdata
  44. {
  45. float4 vertex : POSITION; // vertex position
  46. #if HIGH_QUALITY
  47. float3 normal : NORMAL;
  48. #else
  49. float2 uv : TEXCOORD0; // texture coordinate
  50. #if STEREO_CUSTOM_UV
  51. float2 uv2 : TEXCOORD1; // Custom uv set for right eye (left eye is in TEXCOORD0)
  52. #endif
  53. #endif
  54. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  55. UNITY_VERTEX_INPUT_INSTANCE_ID
  56. #endif
  57. };
  58. struct v2f
  59. {
  60. float4 vertex : SV_POSITION; // clip space position
  61. #if HIGH_QUALITY
  62. float3 normal : TEXCOORD0;
  63. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  64. float4 scaleOffset : TEXCOORD1; // texture coordinate
  65. UNITY_FOG_COORDS(2)
  66. #else
  67. UNITY_FOG_COORDS(1)
  68. #endif
  69. #else
  70. float2 uv : TEXCOORD0; // texture coordinate
  71. UNITY_FOG_COORDS(1)
  72. #endif
  73. #if STEREO_DEBUG
  74. float4 tint : COLOR;
  75. #endif
  76. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  77. UNITY_VERTEX_OUTPUT_STEREO
  78. #endif
  79. };
  80. uniform sampler2D _MainTex;
  81. #if USE_YPCBCR
  82. uniform sampler2D _ChromaTex;
  83. uniform float4x4 _YpCbCrTransform;
  84. #endif
  85. uniform float4 _MainTex_ST;
  86. v2f vert (appdata v)
  87. {
  88. v2f o;
  89. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  90. UNITY_SETUP_INSTANCE_ID(v); // calculates and sets the built-n unity_StereoEyeIndex and unity_InstanceID Unity shader variables to the correct values based on which eye the GPU is currently rendering
  91. UNITY_INITIALIZE_OUTPUT(v2f, o); // initializes all v2f values to 0
  92. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // tells the GPU which eye in the texture array it should render to
  93. #endif
  94. o.vertex = XFormObjectToClip(v.vertex);
  95. #if !HIGH_QUALITY
  96. o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
  97. #if LAYOUT_EQUIRECT180
  98. o.uv.x = ((o.uv.x - 0.5) * 2.0) + 0.5;
  99. #endif
  100. o.uv.xy = float2(1.0-o.uv.x, o.uv.y);
  101. #endif
  102. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  103. float4 scaleOffset = GetStereoScaleOffset(IsStereoEyeLeft(), _MainTex_ST.y < 0.0);
  104. #if !HIGH_QUALITY
  105. o.uv.xy *= scaleOffset.xy;
  106. o.uv.xy += scaleOffset.zw;
  107. #else
  108. o.scaleOffset = scaleOffset;
  109. #endif
  110. #elif STEREO_CUSTOM_UV && !HIGH_QUALITY
  111. if (!IsStereoEyeLeft())
  112. {
  113. o.uv.xy = TRANSFORM_TEX(v.uv2, _MainTex);
  114. o.uv.xy = float2(1.0 - o.uv.x, o.uv.y);
  115. }
  116. #endif
  117. #if HIGH_QUALITY
  118. o.normal = v.normal;
  119. #endif
  120. #if STEREO_DEBUG
  121. o.tint = GetStereoDebugTint(IsStereoEyeLeft());
  122. #endif
  123. UNITY_TRANSFER_FOG(o, o.vertex);
  124. return o;
  125. }
  126. fixed4 frag (v2f i) : SV_Target
  127. {
  128. float2 uv;
  129. #if HIGH_QUALITY
  130. float3 n = normalize(i.normal);
  131. float M_1_PI = 1.0 / 3.1415926535897932384626433832795;
  132. float M_1_2PI = 1.0 / 6.283185307179586476925286766559;
  133. uv.x = 0.5 - atan2(n.z, n.x) * M_1_2PI;
  134. uv.y = 0.5 - asin(-n.y) * M_1_PI;
  135. uv.x += 0.75;
  136. uv.x = fmod(uv.x, 1.0);
  137. //uv.x = uv.x % 1.0;
  138. uv.xy = TRANSFORM_TEX(uv, _MainTex);
  139. #if LAYOUT_EQUIRECT180
  140. uv.x = ((uv.x - 0.5) * 2.0) + 0.5;
  141. #endif
  142. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  143. uv.xy *= i.scaleOffset.xy;
  144. uv.xy += i.scaleOffset.zw;
  145. #endif
  146. #else
  147. uv = i.uv;
  148. #endif
  149. fixed4 col;
  150. #if USE_YPCBCR
  151. col = SampleYpCbCr(_MainTex, _ChromaTex, uv, _YpCbCrTransform);
  152. #else
  153. col = SampleRGBA(_MainTex, uv);
  154. #endif
  155. #if STEREO_DEBUG
  156. col *= i.tint;
  157. #endif
  158. UNITY_APPLY_FOG(i.fogCoord, col);
  159. return fixed4(col.rgb, 1.0);
  160. }
  161. ENDCG
  162. }
  163. }
  164. }