common.glsl
7.2 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
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
// COMMON SHADERS
// ----------------- UNIFORM AND ATTRIBUTES -----------
@export ecgl.common.transformUniforms
uniform mat4 worldViewProjection : WORLDVIEWPROJECTION;
uniform mat4 worldInverseTranspose : WORLDINVERSETRANSPOSE;
uniform mat4 world : WORLD;
@end
@export ecgl.common.attributes
attribute vec3 position : POSITION;
attribute vec2 texcoord : TEXCOORD_0;
attribute vec3 normal : NORMAL;
@end
@export ecgl.common.uv.header
uniform vec2 uvRepeat : [1.0, 1.0];
uniform vec2 uvOffset : [0.0, 0.0];
uniform vec2 detailUvRepeat : [1.0, 1.0];
uniform vec2 detailUvOffset : [0.0, 0.0];
varying vec2 v_Texcoord;
varying vec2 v_DetailTexcoord;
@end
@export ecgl.common.uv.main
v_Texcoord = texcoord * uvRepeat + uvOffset;
v_DetailTexcoord = texcoord * detailUvRepeat + detailUvOffset;
@end
@export ecgl.common.uv.fragmentHeader
varying vec2 v_Texcoord;
varying vec2 v_DetailTexcoord;
@end
// ----------------- albedo -----------
@export ecgl.common.albedo.main
vec4 albedoTexel = vec4(1.0);
#ifdef DIFFUSEMAP_ENABLED
albedoTexel = texture2D(diffuseMap, v_Texcoord);
#ifdef SRGB_DECODE
albedoTexel = sRGBToLinear(albedoTexel);
#endif
#endif
#ifdef DETAILMAP_ENABLED
vec4 detailTexel = texture2D(detailMap, v_DetailTexcoord);
#ifdef SRGB_DECODE
detailTexel = sRGBToLinear(detailTexel);
#endif
albedoTexel.rgb = mix(albedoTexel.rgb, detailTexel.rgb, detailTexel.a);
albedoTexel.a = detailTexel.a + (1.0 - detailTexel.a) * albedoTexel.a;
#endif
@end
// -----------------WIREFRAME -----------
@export ecgl.common.wireframe.vertexHeader
#ifdef WIREFRAME_QUAD
attribute vec4 barycentric;
varying vec4 v_Barycentric;
#elif defined(WIREFRAME_TRIANGLE)
attribute vec3 barycentric;
varying vec3 v_Barycentric;
#endif
@end
@export ecgl.common.wireframe.vertexMain
#if defined(WIREFRAME_QUAD) || defined(WIREFRAME_TRIANGLE)
v_Barycentric = barycentric;
#endif
@end
@export ecgl.common.wireframe.fragmentHeader
uniform float wireframeLineWidth : 1;
uniform vec4 wireframeLineColor: [0, 0, 0, 0.5];
#ifdef WIREFRAME_QUAD
varying vec4 v_Barycentric;
float edgeFactor () {
vec4 d = fwidth(v_Barycentric);
vec4 a4 = smoothstep(vec4(0.0), d * wireframeLineWidth, v_Barycentric);
return min(min(min(a4.x, a4.y), a4.z), a4.w);
}
#elif defined(WIREFRAME_TRIANGLE)
varying vec3 v_Barycentric;
float edgeFactor () {
vec3 d = fwidth(v_Barycentric);
vec3 a3 = smoothstep(vec3(0.0), d * wireframeLineWidth, v_Barycentric);
return min(min(a3.x, a3.y), a3.z);
}
#endif
@end
@export ecgl.common.wireframe.fragmentMain
#if defined(WIREFRAME_QUAD) || defined(WIREFRAME_TRIANGLE)
if (wireframeLineWidth > 0.) {
vec4 lineColor = wireframeLineColor;
#ifdef SRGB_DECODE
lineColor = sRGBToLinear(lineColor);
#endif
gl_FragColor.rgb = mix(gl_FragColor.rgb, lineColor.rgb, (1.0 - edgeFactor()) * lineColor.a);
}
#endif
@end
// ----------------- Bumpmap and normal map -----------
@export ecgl.common.bumpMap.header
#ifdef BUMPMAP_ENABLED
uniform sampler2D bumpMap;
uniform float bumpScale : 1.0;
// Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
// http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
// Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
vec3 bumpNormal(vec3 surfPos, vec3 surfNormal, vec3 baseNormal)
{
vec2 dSTdx = dFdx(v_Texcoord);
vec2 dSTdy = dFdy(v_Texcoord);
float Hll = bumpScale * texture2D(bumpMap, v_Texcoord).x;
float dHx = bumpScale * texture2D(bumpMap, v_Texcoord + dSTdx).x - Hll;
float dHy = bumpScale * texture2D(bumpMap, v_Texcoord + dSTdy).x - Hll;
vec3 vSigmaX = dFdx(surfPos);
vec3 vSigmaY = dFdy(surfPos);
vec3 vN = surfNormal;
vec3 R1 = cross(vSigmaY, vN);
vec3 R2 = cross(vN, vSigmaX);
float fDet = dot(vSigmaX, R1);
vec3 vGrad = sign(fDet) * (dHx * R1 + dHy * R2);
return normalize(abs(fDet) * baseNormal - vGrad);
}
#endif
@end
@export ecgl.common.normalMap.vertexHeader
#ifdef NORMALMAP_ENABLED
attribute vec4 tangent : TANGENT;
varying vec3 v_Tangent;
varying vec3 v_Bitangent;
#endif
@end
@export ecgl.common.normalMap.vertexMain
#ifdef NORMALMAP_ENABLED
if (dot(tangent, tangent) > 0.0) {
v_Tangent = normalize((worldInverseTranspose * vec4(tangent.xyz, 0.0)).xyz);
v_Bitangent = normalize(cross(v_Normal, v_Tangent) * tangent.w);
}
#endif
@end
@export ecgl.common.normalMap.fragmentHeader
#ifdef NORMALMAP_ENABLED
uniform sampler2D normalMap;
varying vec3 v_Tangent;
varying vec3 v_Bitangent;
#endif
@end
@export ecgl.common.normalMap.fragmentMain
#ifdef NORMALMAP_ENABLED
if (dot(v_Tangent, v_Tangent) > 0.0) {
vec3 normalTexel = texture2D(normalMap, v_DetailTexcoord).xyz;
if (dot(normalTexel, normalTexel) > 0.0) { // Valid normal map
N = normalTexel * 2.0 - 1.0;
mat3 tbn = mat3(v_Tangent, v_Bitangent, v_Normal);
N = normalize(tbn * N);
}
}
#endif
@end
//----------- Vertex animation ---------
@export ecgl.common.vertexAnimation.header
#ifdef VERTEX_ANIMATION
attribute vec3 prevPosition;
attribute vec3 prevNormal;
uniform float percent;
#endif
@end
@export ecgl.common.vertexAnimation.main
#ifdef VERTEX_ANIMATION
vec3 pos = mix(prevPosition, position, percent);
vec3 norm = mix(prevNormal, normal, percent);
#else
vec3 pos = position;
vec3 norm = normal;
#endif
@end
//---------- SSAO MAP -------
@export ecgl.common.ssaoMap.header
#ifdef SSAOMAP_ENABLED
uniform sampler2D ssaoMap;
uniform vec4 viewport : VIEWPORT;
#endif
@end
@export ecgl.common.ssaoMap.main
float ao = 1.0;
#ifdef SSAOMAP_ENABLED
ao = texture2D(ssaoMap, (gl_FragCoord.xy - viewport.xy) / viewport.zw).r;
#endif
@end
//----------- Layers ---------
@export ecgl.common.diffuseLayer.header
#if (LAYER_DIFFUSEMAP_COUNT > 0)
uniform float layerDiffuseIntensity[LAYER_DIFFUSEMAP_COUNT];
uniform sampler2D layerDiffuseMap[LAYER_DIFFUSEMAP_COUNT];
#endif
@end
@export ecgl.common.emissiveLayer.header
#if (LAYER_EMISSIVEMAP_COUNT > 0)
uniform float layerEmissionIntensity[LAYER_EMISSIVEMAP_COUNT];
uniform sampler2D layerEmissiveMap[LAYER_EMISSIVEMAP_COUNT];
#endif
@end
@export ecgl.common.layers.header
@import ecgl.common.diffuseLayer.header
@import ecgl.common.emissiveLayer.header
@end
@export ecgl.common.diffuseLayer.main
#if (LAYER_DIFFUSEMAP_COUNT > 0)
for (int _idx_ = 0; _idx_ < LAYER_DIFFUSEMAP_COUNT; _idx_++) {{
float intensity = layerDiffuseIntensity[_idx_];
vec4 texel2 = texture2D(layerDiffuseMap[_idx_], v_Texcoord);
#ifdef SRGB_DECODE
texel2 = sRGBToLinear(texel2);
#endif
// source-over blend
albedoTexel.rgb = mix(albedoTexel.rgb, texel2.rgb * intensity, texel2.a);
albedoTexel.a = texel2.a + (1.0 - texel2.a) * albedoTexel.a;
}}
#endif
@end
@export ecgl.common.emissiveLayer.main
#if (LAYER_EMISSIVEMAP_COUNT > 0)
for (int _idx_ = 0; _idx_ < LAYER_EMISSIVEMAP_COUNT; _idx_++)
{{
vec4 texel2 = texture2D(layerEmissiveMap[_idx_], v_Texcoord) * layerEmissionIntensity[_idx_];
#ifdef SRGB_DECODE
texel2 = sRGBToLinear(texel2);
#endif
float intensity = layerEmissionIntensity[_idx_];
gl_FragColor.rgb += texel2.rgb * texel2.a * intensity;
}}
#endif
@end