-
Notifications
You must be signed in to change notification settings - Fork 103
/
CubicVR_Core.vs
341 lines (248 loc) · 8.07 KB
/
CubicVR_Core.vs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
attribute vec3 vertexPosition;
attribute vec3 vertexNormal;
attribute vec2 vertexTexCoord;
#if VERTEX_COLOR
attribute vec3 vertexColor;
varying vec3 vertexColorOut;
#endif
#if VERTEX_MORPH
attribute vec3 vertexMorphPosition;
attribute vec3 vertexMorphNormal;
uniform float materialMorphWeight;
#endif
#if POINT_SIZE||POINT_SPRITE
uniform float pointSize;
#endif
#if POINT_SIZE && !POINT_SPRITE && POINT_CIRCLE
varying float ptSize;
#if POINT_CIRCLE
varying vec2 sPos;
uniform vec3 viewPort;
#endif
#endif
varying vec2 vertexTexCoordOut;
uniform vec2 materialTexOffset;
#if !LIGHT_PERPIXEL
#if LIGHT_IS_POINT||LIGHT_IS_DIRECTIONAL||LIGHT_IS_SPOT||LIGHT_IS_AREA
uniform vec3 lightDirection[LIGHT_COUNT];
uniform vec3 lightPosition[LIGHT_COUNT];
uniform vec3 lightSpecular[LIGHT_COUNT];
uniform vec3 lightDiffuse[LIGHT_COUNT];
uniform float lightIntensity[LIGHT_COUNT];
uniform float lightDistance[LIGHT_COUNT];
#if LIGHT_IS_SPOT
uniform float lightCutOffAngle[LIGHT_COUNT];
#endif
varying vec3 lightColorOut;
varying vec3 lightSpecularOut;
#endif
uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform float materialShininess;
#endif
// #if TEXTURE_COLOR||TEXTURE_BUMP||TEXTURE_NORMAL||TEXTURE_AMBIENT||hasSpecularMap||hasAlphaMap
// #endif
uniform mat4 matrixModelView;
uniform mat4 matrixProjection;
uniform mat4 matrixObject;
uniform mat3 matrixNormal;
varying vec3 vertexNormalOut;
varying vec4 vertexPositionOut;
#if !LIGHT_DEPTH_PASS
#if LIGHT_SHADOWED
varying vec4 lightProjectionOut[LIGHT_COUNT];
uniform mat4 lightShadowMatrix[LIGHT_COUNT];
#endif
#if TEXTURE_ENVSPHERE
#if TEXTURE_NORMAL
varying vec3 envTexCoordOut;
#else
varying vec2 envTexCoordOut;
#endif
#endif
#if TEXTURE_BUMP||TEXTURE_NORMAL
varying vec3 envEyeVectorOut;
#endif
#endif // !LIGHT_DEPTH_PASS
void cubicvr_normalMap() {
#if !LIGHT_DEPTH_PASS
#if TEXTURE_BUMP||TEXTURE_NORMAL
vec3 tangent;
vec3 binormal;
vec3 c1 = cross( vertexNormal, vec3(0.0, 0.0, 1.0) );
vec3 c2 = cross( vertexNormal, vec3(0.0, 1.0, 0.0) );
if ( length(c1) > length(c2) ) {
tangent = c1;
} else {
tangent = c2;
}
tangent = normalize(tangent);
binormal = cross(vertexNormal, tangent);
binormal = normalize(binormal);
mat4 uMVOMatrix = matrixModelView * matrixObject;
mat3 TBNMatrix = mat3( (vec3 (uMVOMatrix * vec4 (tangent, 0.0))),
(vec3 (uMVOMatrix * vec4 (binormal, 0.0))),
(vec3 (uMVOMatrix * vec4 (vertexNormal, 0.0)))
);
envEyeVectorOut = vec3(uMVOMatrix * vec4(vertexPosition,1.0)) * TBNMatrix;
#endif
#endif
}
void cubicvr_environmentMap() {
#if !LIGHT_DEPTH_PASS
#if TEXTURE_ENVSPHERE
#if TEXTURE_NORMAL
envTexCoordOut = normalize( vertexPositionOut.xyz );
#else
vec3 ws = (matrixModelView * vec4(vertexPosition,1.0)).xyz;
vec3 r = reflect(ws, vertexNormalOut );
float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
envTexCoordOut.s = r.x/m + 0.5;
envTexCoordOut.t = r.y/m + 0.5;
#endif
#endif
#if VERTEX_COLOR
vertexColorOut = vertexColor;
#endif
#endif
}
void cubicvr_shadowMap() {
#if (LIGHT_IS_SPOT||LIGHT_IS_AREA) && LIGHT_SHADOWED
for (int i = 0; i < LIGHT_COUNT; i++)
{
#if LIGHT_SHADOWED
#if VERTEX_MORPH
lightProjectionOut[i] = lightShadowMatrix[i] * (matrixObject * vec4(vertexPosition+(vertexMorphPosition-vertexPosition)*materialMorphWeight, 1.0));
#else
lightProjectionOut[i] = lightShadowMatrix[i] * (matrixObject * vec4(vertexPosition, 1.0));
#endif
#endif
}
#endif
}
void cubicvr_lighting() {
#if !LIGHT_PERPIXEL
#if LIGHT_IS_POINT
vec3 specTotal = vec3(0.0,0.0,0.0);
vec3 accum = vec3(0.0,0.0,0.0);
for (int i = 0; i < LIGHT_COUNT; i++) {
vec3 lightDirection = lightPosition[i]-vertexPositionOut.xyz;
float dist = length(lightDirection);
vec3 halfVector = normalize(vec3(0.0,0.0,1.0)+lightDirection);
float NdotL = max(dot(normalize(lightDirection),vertexNormalOut),0.0);
if (NdotL > 0.0) {
// basic diffuse
float att = clamp(((lightDistance[i]-dist)/lightDistance[i]), 0.0, 1.0)*lightIntensity[i];
accum += att * NdotL * lightDiffuse[i] * materialDiffuse;
float NdotHV = max(dot(vertexNormalOut, halfVector),0.0);
vec3 spec2 = lightSpecular[i] * materialSpecular * pow(NdotHV,materialShininess);
specTotal += spec2;
}
}
lightColorOut = accum;
lightSpecularOut = specTotal;
#endif
#if LIGHT_IS_DIRECTIONAL
float NdotL;
float NdotHV = 0.0;
vec3 specTotal = vec3(0.0,0.0,0.0);
vec3 spec2 = vec3(0.0,0.0,0.0);
vec3 accum = vec3(0.0,0.0,0.0);
vec3 halfVector;
for (int i = 0; i < LIGHT_COUNT; i++) {
halfVector = normalize(vec3(0.0,0.0,1.0)-lightDirection[i]);
NdotL = max(dot(normalize(-lightDirection[i]),vertexNormalOut),0.0);
if (NdotL > 0.0) {
accum += lightIntensity[i] * materialDiffuse * lightDiffuse[i] * NdotL;
NdotHV = max(dot(vertexNormalOut, halfVector),0.0);
spec2 = lightSpecular[i] * materialSpecular * pow(NdotHV,materialShininess);
specTotal += spec2;
}
}
lightColorOut = accum;
lightSpecularOut = specTotal;
#endif
#if LIGHT_IS_SPOT
vec3 specTotal = vec3(0.0,0.0,0.0);
vec3 spec2 = vec3(0.0,0.0,0.0);
vec3 accum = vec3(0.0,0.0,0.0);
vec3 halfVector;
float spotEffect;
float spotDot;
float power;
for (int i = 0; i < LIGHT_COUNT; i++) {
vec3 l = lightPosition[i]-vertexPositionOut.xyz;
float dist = length(l);
float att = clamp(((lightDistance[i]-dist)/lightDistance[i]), 0.0, 1.0)*lightIntensity[i];
att = clamp(att,0.0,1.0);
spotDot = dot(normalize(-l), normalize(lightDirection[i]));
if ( spotDot < cos((lightCutOffAngle[i]/2.0)*(3.14159/180.0)) ) {
spotEffect = 0.0;
}
else {
spotEffect = pow(spotDot, 1.0);
}
att *= spotEffect;
vec3 v = normalize(-vertexPositionOut.xyz);
vec3 h = normalize(l + v);
float NdotL = max(0.0, dot(vertexNormalOut, normalize(l)));
float NdotH = max(0.0, dot(vertexNormalOut, h));
if (NdotL > 0.0) {
power = pow(NdotH, materialShininess);
}
else {
power = 0.0;
}
accum += att * lightDiffuse[i] * materialDiffuse * NdotL;
spec2 = lightSpecular[i] * materialSpecular * power;
specTotal += spec2*spotEffect;
}
lightColorOut = accum;
lightSpecularOut = specTotal;
#endif
#endif // !LIGHT_PERPIXEL
cubicvr_normalMap();
cubicvr_shadowMap();
cubicvr_environmentMap();
}
vec2 cubicvr_texCoord() {
return vertexTexCoord + materialTexOffset;
}
vec4 cubicvr_transform() {
#if LIGHT_DEPTH_PASS
vertexNormalOut = vec3(0.0,0.0,0.0);
#endif
#if VERTEX_MORPH
vec4 vPos = matrixObject * vec4(vertexPosition+(vertexMorphPosition-vertexPosition)*materialMorphWeight, 1.0);
#else
vec4 vPos = matrixObject * vec4(vertexPosition, 1.0);
#endif
vertexPositionOut = matrixModelView * vPos;
#if POINT_SIZE||POINT_SPRITE
float d = length(vertexPositionOut);
gl_PointSize = pointSize * sqrt( 1.0/(1.0 + d*d) );
#if !POINT_SPRITE && POINT_CIRCLE
ptSize = gl_PointSize;
vec4 screenPos = vec4(matrixProjection * vertexPositionOut);
sPos = (screenPos.xy/screenPos.w)*vec2(viewPort.x/2.0,viewPort.y/2.0)+vec2(viewPort.x/2.0+0.5,viewPort.y/2.0+0.5);
#endif
#endif
return vPos;
}
vec3 cubicvr_normal() {
#if VERTEX_MORPH
return normalize(matrixObject*vec4(vertexNormal+(vertexMorphNormal-vertexNormal)*materialMorphWeight,0.0)).xyz;
#else
return normalize(matrixObject*vec4(vertexNormal,0.0)).xyz;
#endif
}
#define customShader_splice 1
void main(void)
{
vertexTexCoordOut = cubicvr_texCoord();
gl_Position = matrixProjection * matrixModelView * cubicvr_transform();
#if !LIGHT_DEPTH_PASS // not needed if shadowing
vertexNormalOut = matrixNormal * cubicvr_normal();
cubicvr_lighting();
#endif // !LIGHT_DEPTH_PASS
}