CubemapToEquirectangular.shader 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Originally By James O'Hare, from his Gist: https://gist.github.com/Farfarer/5664694
  3. // This takes in the cubemap generated by your cubemap camera and feeds back out an equirectangular image.
  4. // Create a new material and give it this shader. Then give that material to the "cubemapToEquirectangularMateral" property of the dynamicAmbient.js script in this gist.
  5. // You could probably abstract this to C#/JS code and feed it in a pre-baked cubemap to sample and then spit out an equirectangular map if you don't have render textures.
  6. Shader "Hidden/CubemapToEquirectangular"
  7. {
  8. Properties
  9. {
  10. _MainTex ("Cubemap (RGB)", CUBE) = "" {}
  11. }
  12. CGINCLUDE
  13. #include "UnityCG.cginc"
  14. struct appdata
  15. {
  16. float4 vertex : POSITION;
  17. float2 texcoord : TEXCOORD0;
  18. };
  19. struct v2f
  20. {
  21. float4 pos : SV_POSITION;
  22. float2 uv : TEXCOORD0;
  23. };
  24. uniform samplerCUBE _MainTex;
  25. uniform float _FlipX;
  26. #define PI 3.141592653589793
  27. #define HALFPI 1.57079632679
  28. v2f vert(appdata v )
  29. {
  30. v2f o;
  31. o.pos = UnityObjectToClipPos(v.vertex);
  32. #if STEREOPACK_TOP
  33. o.pos.y = (o.pos.y / 2.0) - 0.5;
  34. #elif STEREOPACK_BOTTOM
  35. o.pos.y = (o.pos.y / 2.0) + 0.5;
  36. #elif STEREOPACK_LEFT
  37. o.pos.x = (o.pos.x / 2.0) - 0.5;
  38. #elif STEREOPACK_RIGHT
  39. o.pos.x = (o.pos.x / 2.0) + 0.5;
  40. #endif
  41. float2 uv = v.texcoord.xy;
  42. if (_FlipX > 0.5)
  43. {
  44. uv.x = 1.0 - uv.x;
  45. }
  46. uv = uv * 2.0 - float2(0.5, 1.0);
  47. uv *= float2(PI, HALFPI);
  48. o.uv = uv;
  49. return o;
  50. }
  51. fixed4 frag(v2f i) : COLOR
  52. {
  53. float cosy = cos(i.uv.y);
  54. float3 normal = float3(0.0, 0.0, 0.0);
  55. normal.x = cos(i.uv.x) * cosy;
  56. normal.y = i.uv.y;
  57. normal.z = cos(i.uv.x - HALFPI) * cosy;
  58. return texCUBE(_MainTex, normalize(normal));
  59. }
  60. ENDCG
  61. Subshader
  62. {
  63. ZTest Always
  64. Cull Off
  65. ZWrite Off
  66. Fog{ Mode off }
  67. Pass
  68. {
  69. CGPROGRAM
  70. #pragma multi_compile __ STEREOPACK_TOP STEREOPACK_BOTTOM STEREOPACK_LEFT STEREOPACK_RIGHT
  71. #pragma vertex vert
  72. #pragma fragment frag
  73. ENDCG
  74. }
  75. }
  76. Fallback Off
  77. }