AVProVideo-Skybox-Sphere.shader 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Shader "AVProVideo/Skybox/Sphere"
  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. inline float2 ToRadialCoords(float3 coords)
  49. {
  50. const float CONST_PI = 3.14159265359f;
  51. float3 normalizedCoords = normalize(coords);
  52. float latitude = acos(normalizedCoords.y);
  53. float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
  54. float2 sphereCoords = float2(longitude, latitude) * float2(0.5/CONST_PI, 1.0/CONST_PI);
  55. float2 radial = float2(0.5,1.0) - sphereCoords;
  56. radial.x += 0.25;
  57. radial.x = fmod(radial.x, 1.0);
  58. return radial;
  59. }
  60. struct appdata_t {
  61. float4 vertex : POSITION;
  62. float2 texcoord : TEXCOORD0;
  63. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  64. UNITY_VERTEX_INPUT_INSTANCE_ID
  65. #endif
  66. };
  67. struct v2f {
  68. float4 vertex : SV_POSITION;
  69. float3 texcoord : TEXCOORD0;
  70. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  71. float4 scaleOffset : TEXCOORD1;
  72. #if STEREO_DEBUG
  73. float4 tint : COLOR;
  74. #endif
  75. #endif
  76. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  77. UNITY_VERTEX_OUTPUT_STEREO
  78. #endif
  79. };
  80. v2f sb_vert(appdata_t v)
  81. {
  82. v2f o;
  83. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  84. 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
  85. UNITY_INITIALIZE_OUTPUT(v2f, o); // initializes all v2f values to 0
  86. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // tells the GPU which eye in the texture array it should render to
  87. #endif
  88. float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
  89. o.vertex = XFormObjectToClip(float4(rotated, 0.0));
  90. o.texcoord = v.vertex.xyz;
  91. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  92. o.scaleOffset = GetStereoScaleOffset(IsStereoEyeLeft(), _MainTex_ST.y < 0.0);
  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. float2 tc = ToRadialCoords(i.texcoord);
  102. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  103. tc.xy *= i.scaleOffset.xy;
  104. tc.xy += i.scaleOffset.zw;
  105. #endif
  106. tc = TRANSFORM_TEX(tc, _MainTex);
  107. half4 tex;
  108. #if USE_YPCBCR
  109. tex = SampleYpCbCr(_MainTex, _ChromaTex, tc, _YpCbCrTransform);
  110. #else
  111. tex = SampleRGBA(_MainTex, tc);
  112. #endif
  113. half3 c = tex;
  114. //c = DecodeHDR(tex, _MainTex_HDR);
  115. //c = c * _Tint.rgb;
  116. ///c = c * unity_ColorSpaceDouble.rgb;
  117. c *= _Exposure;
  118. #if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
  119. #if STEREO_DEBUG
  120. c *= i.tint;
  121. #endif
  122. #endif
  123. return half4(c, 1.0);
  124. }
  125. ENDCG
  126. Pass // Pass
  127. {
  128. CGPROGRAM
  129. #pragma vertex vert
  130. #pragma fragment frag
  131. #pragma target 2.0
  132. v2f vert(appdata_t v)
  133. {
  134. return sb_vert(v);
  135. }
  136. ENDCG
  137. }
  138. }
  139. }