Skybox.shader 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Derived from Unity built-in shader source.
  2. // https://raw.githubusercontent.com/chsxf/unity-built-in-shaders/0c7940740e75340009bbed453e2b198e294e4bab/Shaders/DefaultResourcesExtra/Skybox-Panoramic.shader
  3. Shader "Skybox/Dual Panoramic" {
  4. Properties{
  5. _Tint1("Tint Color 1", Color) = (.5, .5, .5, .5)
  6. _Tint2("Tint Color 2", Color) = (.5, .5, .5, .5)
  7. [Gamma] _Exposure1("Exposure 1", Range(0, 8)) = 1.0
  8. [Gamma] _Exposure2("Exposure 2", Range(0, 8)) = 1.0
  9. _Rotation1("Rotation1", Range(0, 360)) = 0
  10. _Rotation2("Rotation2", Range(0, 360)) = 0
  11. [NoScaleOffset] _Texture1("Texture 1", 2D) = "grey" {}
  12. [NoScaleOffset] _Texture2("Texture 2", 2D) = "grey" {}
  13. [Enum(360 Degrees, 0, 180 Degrees, 1)] _ImageType("Image Type", Float) = 0
  14. [Toggle] _MirrorOnBack("Mirror on Back", Float) = 0
  15. [Enum(None, 0, Side by Side, 1, Over Under, 2)] _Layout("3D Layout", Float) = 0
  16. _Blend("Blend", Range(0.0, 1.0)) = 0.0
  17. }
  18. SubShader{
  19. Tags { "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" }
  20. Cull Off ZWrite Off
  21. Pass {
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma fragment frag
  25. #pragma target 2.0
  26. #pragma multi_compile_local __ _MAPPING_6_FRAMES_LAYOUT
  27. #include "UnityCG.cginc"
  28. sampler2D _Texture1;
  29. sampler2D _Texture2;
  30. float4 _Texture1_TexelSize;
  31. half4 _Texture1_HDR;
  32. half4 _Texture2_HDR;
  33. half4 _Tint1;
  34. half4 _Tint2;
  35. half _Exposure1;
  36. half _Exposure2;
  37. float _Rotation1;
  38. float _Rotation2;
  39. float _Blend;
  40. bool _MirrorOnBack;
  41. int _ImageType;
  42. int _Layout;
  43. inline float2 ToRadialCoords(float3 coords)
  44. {
  45. float3 normalizedCoords = normalize(coords);
  46. float latitude = acos(normalizedCoords.y);
  47. float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
  48. float2 sphereCoords = float2(longitude, latitude) * float2(0.5 / UNITY_PI, 1.0 / UNITY_PI);
  49. return float2(0.5,1.0) - sphereCoords;
  50. }
  51. float3 RotateAroundYInDegrees(float3 vertex, float degrees)
  52. {
  53. float alpha = degrees * UNITY_PI / 180.0;
  54. float sina, cosa;
  55. sincos(alpha, sina, cosa);
  56. float2x2 m = float2x2(cosa, -sina, sina, cosa);
  57. return float3(mul(m, vertex.xz), vertex.y).xzy;
  58. }
  59. struct appdata_t {
  60. float4 vertex : POSITION;
  61. UNITY_VERTEX_INPUT_INSTANCE_ID
  62. };
  63. struct v2f {
  64. float4 vertex : SV_POSITION;
  65. float3 texcoord : TEXCOORD0;
  66. float2 image180ScaleAndCutoff : TEXCOORD1;
  67. float4 layout3DScaleAndOffset : TEXCOORD2;
  68. UNITY_VERTEX_OUTPUT_STEREO
  69. };
  70. v2f vert(appdata_t v)
  71. {
  72. v2f o;
  73. UNITY_SETUP_INSTANCE_ID(v);
  74. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  75. float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation1);
  76. o.vertex = UnityObjectToClipPos(rotated);
  77. o.texcoord = v.vertex.xyz;
  78. // Calculate constant horizontal scale and cutoff for 180 (vs 360) image type
  79. if (_ImageType == 0) // 360 degree
  80. o.image180ScaleAndCutoff = float2(1.0, 1.0);
  81. else // 180 degree
  82. o.image180ScaleAndCutoff = float2(2.0, _MirrorOnBack ? 1.0 : 0.5);
  83. // Calculate constant scale and offset for 3D layouts
  84. if (_Layout == 0) // No 3D layout
  85. o.layout3DScaleAndOffset = float4(0,0,1,1);
  86. else if (_Layout == 1) // Side-by-Side 3D layout
  87. o.layout3DScaleAndOffset = float4(unity_StereoEyeIndex,0,0.5,1);
  88. else // Over-Under 3D layout
  89. o.layout3DScaleAndOffset = float4(0, 1 - unity_StereoEyeIndex,1,0.5);
  90. return o;
  91. }
  92. fixed4 frag(v2f i) : SV_Target
  93. {
  94. float2 tc = ToRadialCoords(i.texcoord);
  95. if (tc.x > i.image180ScaleAndCutoff[1])
  96. return half4(0,0,0,1);
  97. tc.x = fmod(tc.x * i.image180ScaleAndCutoff[0], 1);
  98. tc = (tc + i.layout3DScaleAndOffset.xy) * i.layout3DScaleAndOffset.zw;
  99. half4 tex1 = tex2D(_Texture1, tc);
  100. tc.x = frac(tc.x + (_Rotation2 - _Rotation1) / 360.0);
  101. half4 tex2 = tex2D(_Texture2, tc);
  102. half3 c1 = DecodeHDR(tex1, _Texture1_HDR);
  103. half3 c2 = DecodeHDR(tex2, _Texture2_HDR);
  104. c1 = lerp(c1, c2, _Blend) * lerp(_Tint1.rgb, _Tint2.rgb, _Blend) * unity_ColorSpaceDouble.rgb * lerp(_Exposure1, _Exposure2, _Blend);
  105. return half4(c1, 1);
  106. }
  107. ENDCG
  108. }
  109. }
  110. //CustomEditor "SkyboxPanoramicShaderGUI"
  111. Fallback Off
  112. }