-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBlitCubemapToEquirect.shader
More file actions
208 lines (171 loc) · 4.51 KB
/
BlitCubemapToEquirect.shader
File metadata and controls
208 lines (171 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "NewChromantics/BlitCubemapToEquirect"
{
Properties
{
CubemapLeft ("CubemapLeft", 2D) = "white" {}
CubemapRight ("CubemapRight", 2D) = "white" {}
CubemapFront ("CubemapFront", 2D) = "white" {}
CubemapBack ("CubemapBack", 2D) = "white" {}
CubemapTop ("CubemapTop", 2D) = "white" {}
CubemapBottom ("CubemapBottom", 2D) = "white" {}
Cubemap("Cubemap", CUBE ) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "PopCommon.cginc"
// if neither keyword set, first is used
#pragma multi_compile USE_MULTIFACES USE_CUBEMAP
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D CubemapLeft;
sampler2D CubemapRight;
sampler2D CubemapFront;
sampler2D CubemapBack;
sampler2D CubemapTop;
sampler2D CubemapBottom;
samplerCUBE Cubemap;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
// gr: const int's were all 0 on DX
#define TOP 0
#define BOTTOM 1
#define LEFT 2
#define RIGHT 3
#define FRONT 4
#define BACK 5
float2 STtoUV(float2 st)
{
st += float2(1,1);
st /= float2(2,2);
return st;
}
float2 UVtoST(float2 uv)
{
uv *= float2(2,2);
uv -= float2(1,1);
return uv;
}
// returns index and xy sample
int GetCubemapIndex(float3 View,out float2 st)
{
// gr: if this appears to be getting the wrong angle (left/right/forward/back), it's because the screens need to face the same direction as the actor
View = normalize(View);
float x = View.x;
float y = View.y;
float z = View.z;
float ax = abs(x);
float ay = abs(y);
float az = abs(z);
if (ax >ay && ax > az && x>=0)
{
st.x = -z / ax;
st.y = -y / ax;
return RIGHT;
}
else if (ax >ay && ax > az && x<0)
{
st.x = (z / ax);
st.y = -y / ax;
return LEFT;
}
else if (ay > ax && ay > az && y>=0 )
{
st.x = x / ay;
st.y = z / ay;
return TOP;
}
else if (ay > ax && ay > az && y<0 )
{
st.x = x / ay;
st.y = -(z / ay);
return BOTTOM;
}
else if ( z >= 0 )
{
st.x = x / az;
st.y = -y / az;
return FRONT;
}
else
{
st.x = -(x / az);
st.y = -y / az;
return BACK;
}
return -1;
}
float4 SampleTexCube(float3 View)
{
float2 st;
View = normalize(View);
int TextureIndex = GetCubemapIndex( View, st );
//return float4(View.x, View.y,View.z,1);
//return float4(st.x,st.y,0,1);
st = STtoUV(st);
st.y = 1 - st.y;
//st.x = 1 - st.x;
bool RenderFaceDebug = false;
if ( RenderFaceDebug )
{
if ( TextureIndex == TOP ) return float4( 1, st.y, 0, 1 );//tex2D( CubemapTop, st );
if ( TextureIndex == BOTTOM ) return float4( 0, 1, 1-st.y, 1 );//tex2D( CubemapBottom, st );
if ( TextureIndex == LEFT ) return float4( st.y, 0, 1, 1 );//tex2D( CubemapLeft, st );
if ( TextureIndex == RIGHT ) return float4( 1, 1, st.y, 1 );//tex2D( CubemapRight, st );
if ( TextureIndex == FRONT ) return float4( st.y, 1, 1, 1 );//tex2D( CubemapFront, st );
if ( TextureIndex == BACK ) return float4( 1, st.y, 1, 1 );//tex2D( CubemapBack, st );
}
if ( TextureIndex == TOP ) return tex2D( CubemapTop, st );
if ( TextureIndex == BOTTOM ) return tex2D( CubemapBottom, st );
if ( TextureIndex == LEFT ) return tex2D( CubemapLeft, st );
if ( TextureIndex == RIGHT ) return tex2D( CubemapRight, st );
if ( TextureIndex == FRONT ) return tex2D( CubemapFront, st );
if ( TextureIndex == BACK ) return tex2D( CubemapBack, st );
return float4( 1, 0, 1, 1 );
}
fixed4 frag (v2f i) : SV_Target
{
// rotate uv (??)
float2 uv = i.uv.xy;
float2 LatLon;
//uv.y *= 2;
uv.x *= 2;
uv.y *= 1;
LatLon.x = lerp( -UNITY_PI/2, UNITY_PI/2, uv.y );
LatLon.y = lerp( -UNITY_PI/2, UNITY_PI/2, uv.x );
float3 View = LatLonToView( LatLon );
#if defined(USE_CUBEMAP)
//return float4( View, 1 );
float4 Rgba = texCUBE( Cubemap, View );
Rgba.a = 1;
return Rgba;
#elif USE_MULTIFACES
return SampleTexCube( View );
#endif
return float4(1,0,0,1);
}
ENDCG
}
}
}