AVProVideo-Skybox-Cube3x2.shader 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Shader "AVProVideo/Skybox/3x2 Cube"
  2. {
  3. Properties
  4. {
  5. _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
  6. [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
  7. _Rotation ("Rotation", Range(0, 360)) = 0
  8. [NoScaleOffset] _MainTex ("MainTex (HDR)", 2D) = "grey" { }
  9. [NoScaleOffset] _ChromaTex ("Chroma", 2D) = "grey" { }
  10. [KeywordEnum(None, Top_Bottom, Left_Right, Custom_UV)] Stereo ("Stereo Mode", Float) = 0
  11. [Toggle(STEREO_DEBUG)] _StereoDebug ("Stereo Debug Tinting", 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 { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
  18. Cull Off ZWrite Off
  19. CGINCLUDE
  20. // TODO: replace use multi_compile_local instead (Unity 2019.1 feature)
  21. #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT STEREO_CUSTOM_UV
  22. #pragma multi_compile FORCEEYE_NONE FORCEEYE_LEFT FORCEEYE_RIGHT
  23. #pragma multi_compile __ STEREO_DEBUG
  24. #pragma multi_compile __ APPLY_GAMMA
  25. #pragma multi_compile __ USE_YPCBCR
  26. #include "UnityCG.cginc"
  27. #include "AVProVideo.cginc"
  28. half4 _Tint;
  29. half _Exposure;
  30. float _Rotation;
  31. sampler2D _MainTex;
  32. float4 _MainTex_TexelSize;
  33. half4 _MainTex_HDR;
  34. float4 _MainTex_ST;
  35. #if USE_YPCBCR
  36. sampler2D _ChromaTex;
  37. float4x4 _YpCbCrTransform;
  38. #endif
  39. float3 RotateAroundYInDegrees (float3 vertex, float degrees)
  40. {
  41. const float CONST_PI = 3.14159265359f;
  42. float alpha = degrees * CONST_PI / 180.0;
  43. float sina, cosa;
  44. sincos(alpha, sina, cosa);
  45. float2x2 m = float2x2(cosa, -sina, sina, cosa);
  46. return float3(mul(m, vertex.xz), vertex.y).xzy;
  47. }
  48. struct appdata_t {
  49. float4 vertex : POSITION;
  50. float2 texcoord : TEXCOORD0;
  51. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  52. UNITY_VERTEX_INPUT_INSTANCE_ID
  53. #endif
  54. };
  55. struct v2f {
  56. float4 vertex : SV_POSITION;
  57. float2 texcoord : TEXCOORD0;
  58. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  59. UNITY_VERTEX_OUTPUT_STEREO
  60. #endif
  61. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  62. #if STEREO_DEBUG
  63. float4 tint : COLOR;
  64. #endif
  65. #endif
  66. };
  67. static const float onehalf = 1.0 / 2.0;
  68. static const float onethird = 1.0 / 3.0;
  69. static const float twothird = 2.0 / 3.0;
  70. static const float exp_coeff = 1.01;
  71. static const float2 face_scale = float2(onethird, onehalf);
  72. v2f sb_vert(appdata_t v, float2 face_offset)
  73. {
  74. v2f o;
  75. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  76. UNITY_SETUP_INSTANCE_ID(v);
  77. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  78. #endif
  79. float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
  80. o.vertex = XFormObjectToClip(float4(rotated, 0.0));
  81. // Remap texcoords for the cubemap face with the offset passed in.
  82. float2 t = _MainTex_TexelSize.wz * face_scale;
  83. float2 p = (floor((t * exp_coeff) - t) / t) * 0.5;
  84. float2 hp = p * 0.5;
  85. float2 of = face_offset + hp;
  86. float2 s = face_scale - p;
  87. float2 uv = v.texcoord * s + of;
  88. o.texcoord = TRANSFORM_TEX(uv, _MainTex);
  89. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  90. float4 scaleOffset = GetStereoScaleOffset(IsStereoEyeLeft(), _MainTex_ST.y < 0.0);
  91. o.texcoord *= scaleOffset.xy;
  92. o.texcoord += scaleOffset.zw;
  93. #if STEREO_DEBUG
  94. o.tint = GetStereoDebugTint(IsStereoEyeLeft());
  95. #endif
  96. #endif
  97. return o;
  98. }
  99. half4 frag(v2f i) : SV_Target
  100. {
  101. half4 tex;
  102. #if USE_YPCBCR
  103. tex = SampleYpCbCr(_MainTex, _ChromaTex, i.texcoord, _YpCbCrTransform);
  104. #else
  105. tex = SampleRGBA(_MainTex, i.texcoord);
  106. #endif
  107. half3 c = tex.rgb;
  108. //c = DecodeHDR(tex, _MainTex_HDR);
  109. //c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
  110. c *= _Exposure;
  111. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  112. #if STEREO_DEBUG
  113. c *= i.tint;
  114. #endif
  115. #endif
  116. return half4(c, 1.0);
  117. }
  118. ENDCG
  119. Pass // Front
  120. {
  121. CGPROGRAM
  122. #pragma vertex vert
  123. #pragma fragment frag
  124. #pragma target 2.0
  125. v2f vert(appdata_t v)
  126. {
  127. return sb_vert(v, float2(onethird, 0));
  128. }
  129. ENDCG
  130. }
  131. Pass // Back
  132. {
  133. CGPROGRAM
  134. #pragma vertex vert
  135. #pragma fragment frag
  136. #pragma target 2.0
  137. v2f vert(appdata_t v)
  138. {
  139. return sb_vert(v, float2(twothird, 0));
  140. }
  141. ENDCG
  142. }
  143. Pass // Right
  144. {
  145. CGPROGRAM
  146. #pragma vertex vert
  147. #pragma fragment frag
  148. #pragma target 2.0
  149. v2f vert(appdata_t v)
  150. {
  151. return sb_vert(v, float2(0, onehalf));
  152. }
  153. ENDCG
  154. }
  155. Pass // Left
  156. {
  157. CGPROGRAM
  158. #pragma vertex vert
  159. #pragma fragment frag
  160. #pragma target 2.0
  161. v2f vert(appdata_t v)
  162. {
  163. return sb_vert(v, float2(onethird, onehalf));
  164. }
  165. ENDCG
  166. }
  167. Pass // Top
  168. {
  169. CGPROGRAM
  170. #pragma vertex vert
  171. #pragma fragment frag
  172. #pragma target 2.0
  173. v2f vert(appdata_t v)
  174. {
  175. return sb_vert(v, float2(twothird, onehalf));
  176. }
  177. ENDCG
  178. }
  179. Pass // Bottom
  180. {
  181. CGPROGRAM
  182. #pragma vertex vert
  183. #pragma fragment frag
  184. #pragma target 2.0
  185. v2f vert(appdata_t v)
  186. {
  187. return sb_vert(v, float2(0, 0));
  188. }
  189. ENDCG
  190. }
  191. }
  192. }