1 Shader "Hidden/CubeToPano" {
3 _SrcBlend ("", Float) = 1
4 _DstBlend ("", Float) = 1
24 #include "UnityCG.cginc"
26 UNITY_DECLARE_TEXCUBE(_srcCubeTexture);
28 uniform int _cubeMipLvl;
32 float4 vertex : SV_POSITION;
33 float2 texcoord : TEXCOORD0;
36 v2f vert (float4 vertex : POSITION, float2 texcoord : TEXCOORD0)
39 o.vertex = UnityObjectToClipPos(vertex);
40 o.texcoord = texcoord.xy;
44 half2 DirectionToSphericalTexCoordinate(half3 dir_in) // use this for the lookup
46 half3 dir = normalize(dir_in);
47 // coordinate frame is (-Z,X) meaning negative Z is primary axis and X is secondary axis.
48 float recipPi = 1.0/3.1415926535897932384626433832795;
49 return half2( 1.0-0.5*recipPi*atan2(dir.x, -dir.z), asin(dir.y)*recipPi+0.5 );
52 half3 SphericalTexCoordinateToDirection(half2 sphTexCoord)
54 float pi = 3.1415926535897932384626433832795;
55 float theta = (1-sphTexCoord.x) * (pi*2);
56 float phi = (sphTexCoord.y-0.5) * pi;
58 float csTh, siTh, csPh, siPh;
59 sincos(theta, siTh, csTh);
60 sincos(phi, siPh, csPh);
62 // theta is 0 at negative Z (backwards). Coordinate frame is (-Z,X) meaning negative Z is primary axis and X is secondary axis.
63 return float3(siTh*csPh, siPh, -csTh*csPh);
66 half4 frag (v2f i) : SV_Target
68 uint2 pixCoord = ((uint2) i.vertex.xy);
70 half3 dir = SphericalTexCoordinateToDirection(i.texcoord.xy);
72 return (half4) UNITY_SAMPLE_TEXCUBE_LOD(_srcCubeTexture, dir, (float) _cubeMipLvl);