CubeToLatlong.shader 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Shader "Hidden/LookDev/CubeToLatlong"
  2. {
  3. Properties
  4. {
  5. [NoScaleOffset] _MainTex ("Cubemap", Any) = "grey" {}
  6. _CubeToLatLongParams ("Parameters", Vector) = (0.0, 0.0, 0.0, 0.0)
  7. _WindowParams("Window params", Vector) = (0.0, 0.0, 0.0, 0.0)
  8. }
  9. CGINCLUDE
  10. #include "UnityCG.cginc"
  11. uniform float4 _MainTex_HDR;
  12. uniform float4 _MainTex_ST;
  13. UNITY_DECLARE_TEXCUBE(_MainTex);
  14. uniform float4 _CubeToLatLongParams; // x angle offset, y alpha, z intensity w lod to use
  15. uniform float4 _WindowParams; // x Editor windows height, y Environment windows posY, z margin (constant of 2), w PixelsPerPoint
  16. uniform bool _ManualTex2SRGB;
  17. #define OutputAlpha _CubeToLatLongParams.y
  18. #define Intensity _CubeToLatLongParams.z
  19. #define CurrentLOD _CubeToLatLongParams.w
  20. struct appdata_t
  21. {
  22. float4 vertex : POSITION;
  23. float2 texcoord : TEXCOORD0;
  24. };
  25. struct v2f
  26. {
  27. float2 texcoord : TEXCOORD0;
  28. float4 vertex : SV_POSITION;
  29. };
  30. v2f vert(appdata_t IN)
  31. {
  32. v2f OUT;
  33. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  34. OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
  35. return OUT;
  36. }
  37. float4 frag( float2 texcoord : TEXCOORD0,
  38. UNITY_VPOS_TYPE vpos : VPOS
  39. ) : SV_Target
  40. {
  41. float2 texCoord = texcoord.xy;
  42. float theta = texCoord.y * UNITY_PI;
  43. float phi = (texCoord.x * 2.f * UNITY_PI - UNITY_PI*0.5f) - _CubeToLatLongParams.x;
  44. float cosTheta = cos(theta);
  45. float sinTheta = sqrt(1.0f - min(1.0f, cosTheta*cosTheta));
  46. float cosPhi = cos(phi);
  47. float sinPhi = sin(phi);
  48. float3 direction = float3(sinTheta*cosPhi, cosTheta, sinTheta*sinPhi);
  49. direction.xy *= -1.0;
  50. float4 ret = float4(DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, direction, CurrentLOD), _MainTex_HDR) * Intensity, OutputAlpha);
  51. if (_ManualTex2SRGB)
  52. ret.rgb = LinearToGammaSpace(ret.rgb);
  53. // Clip outside of the library window
  54. // Editor windows is like this:
  55. //------
  56. // Margin (2)
  57. // Scene - Game - Asset Store <= What we call tab size
  58. //------
  59. // Settings - Views <= what we call menu size
  60. //----
  61. // View size with Environment windows)
  62. //
  63. // _WindowParams.x contain the height of the editor windows
  64. // _WindowParams.y contain the start of the windows environment in the windows editor, i.e the menu size + tab size
  65. // _WindowParams.z contain a constant margin of 2 (don't know how to retrieve that)
  66. // _WindowParams.w is PixelsPerPoin (To handle retina display on OSX))
  67. // We use VPOS register to clip, VPOS is dependent on the API. It is reversed in openGL.
  68. // There is no need to clip when y is above height because the editor windows will clip it
  69. // vertex.y is relative to editor windows
  70. #if UNITY_UV_STARTS_AT_TOP
  71. if ((vpos.y / _WindowParams.w) < (_WindowParams.y + _WindowParams.z))
  72. #else
  73. // vertex.y is reversed (start from bottom of the editor windsows)
  74. vpos.y = _WindowParams.x - (vpos.y / _WindowParams.w);
  75. if (vpos.y < _WindowParams.z)
  76. #endif
  77. {
  78. clip(-1);
  79. }
  80. return ret;
  81. }
  82. ENDCG
  83. SubShader
  84. {
  85. Tags
  86. {
  87. "ForceSupported"="True"
  88. }
  89. Lighting Off
  90. Cull Off
  91. ZTest Always
  92. ZWrite Off
  93. Pass
  94. {
  95. Blend One Zero
  96. CGPROGRAM
  97. #pragma fragment frag
  98. #pragma vertex vert
  99. #pragma target 3.0
  100. ENDCG
  101. }
  102. Pass
  103. {
  104. Blend SrcAlpha OneMinusSrcAlpha
  105. CGPROGRAM
  106. #pragma fragment frag
  107. #pragma vertex vert
  108. #pragma target 3.0
  109. ENDCG
  110. }
  111. }
  112. }