BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / gl / builtinShaders.js

builtinShaders.js

builtinShaders.js 13.6 KiB Download Raw
1// Built-in screen-space / effect shaders. Games can use these without fetching files.
2// Expand this set over time into the default framework shader bundle.
3
4Shader.Builtin = Shader.Builtin || {};
5
6Shader.Builtin.BLOOM_BRIGHT_FRAG = [
7 "precision mediump float;",
8 "uniform sampler2D u_texture;",
9 "uniform float u_threshold;",
10 "uniform float u_softKnee;",
11 "varying vec2 v_texCoord;",
12 "void main() {",
13 " vec4 color = texture2D(u_texture, v_texCoord);",
14 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
15 " float knee = max(u_softKnee, 0.001);",
16 " float soft = smoothstep(u_threshold, u_threshold + knee, lum);",
17 " gl_FragColor = vec4(color.rgb * soft, 1.0);",
18 "}"
19].join("\n");
20
21// Max-filter dilate: fattens 1–4px hotspots so later blur has something to spread.
22Shader.Builtin.BLOOM_DILATE_FRAG = [
23 "precision mediump float;",
24 "uniform sampler2D u_texture;",
25 "uniform vec2 u_resolution;",
26 "uniform float u_radius;",
27 "varying vec2 v_texCoord;",
28 "void main() {",
29 " vec2 px = u_radius / max(u_resolution, vec2(1.0));",
30 " vec4 c = texture2D(u_texture, v_texCoord);",
31 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, 0.0)));",
32 " c = max(c, texture2D(u_texture, v_texCoord - vec2(px.x, 0.0)));",
33 " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px.y)));",
34 " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px.y)));",
35 " c = max(c, texture2D(u_texture, v_texCoord + px));",
36 " c = max(c, texture2D(u_texture, v_texCoord - px));",
37 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, -px.y)));",
38 " c = max(c, texture2D(u_texture, v_texCoord + vec2(-px.x, px.y)));",
39 " vec2 px2 = px * 2.0;",
40 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px2.x, 0.0)));",
41 " c = max(c, texture2D(u_texture, v_texCoord - vec2(px2.x, 0.0)));",
42 " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px2.y)));",
43 " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px2.y)));",
44 " gl_FragColor = c;",
45 "}"
46].join("\n");
47
48// Wider separable blur (13-tap). u_direction (1,0)/(0,1); u_blurSize in texels.
49Shader.Builtin.BLOOM_BLUR_FRAG = [
50 "precision mediump float;",
51 "uniform sampler2D u_texture;",
52 "uniform vec2 u_resolution;",
53 "uniform vec2 u_direction;",
54 "uniform float u_blurSize;",
55 "varying vec2 v_texCoord;",
56 "void main() {",
57 " vec2 step = u_direction * u_blurSize / max(u_resolution, vec2(1.0));",
58 " vec4 c = vec4(0.0);",
59 " c += texture2D(u_texture, v_texCoord - step * 6.0) * 0.009;",
60 " c += texture2D(u_texture, v_texCoord - step * 5.0) * 0.019;",
61 " c += texture2D(u_texture, v_texCoord - step * 4.0) * 0.038;",
62 " c += texture2D(u_texture, v_texCoord - step * 3.0) * 0.071;",
63 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.111;",
64 " c += texture2D(u_texture, v_texCoord - step) * 0.147;",
65 " c += texture2D(u_texture, v_texCoord) * 0.160;",
66 " c += texture2D(u_texture, v_texCoord + step) * 0.147;",
67 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.111;",
68 " c += texture2D(u_texture, v_texCoord + step * 3.0) * 0.071;",
69 " c += texture2D(u_texture, v_texCoord + step * 4.0) * 0.038;",
70 " c += texture2D(u_texture, v_texCoord + step * 5.0) * 0.019;",
71 " c += texture2D(u_texture, v_texCoord + step * 6.0) * 0.009;",
72 " gl_FragColor = c;",
73 "}"
74].join("\n");
75
76// Combine: add bloom while keeping / boosting chroma (avoids gray wash at high intensity).
77Shader.Builtin.BLOOM_COMBINE_FRAG = [
78 "precision mediump float;",
79 "uniform sampler2D u_texture;",
80 "uniform sampler2D u_scene;",
81 "uniform float u_intensity;",
82 "uniform float u_saturation;",
83 "uniform float u_colorPreserve;",
84 "varying vec2 v_texCoord;",
85 "void main() {",
86 " vec4 scene = texture2D(u_scene, v_texCoord);",
87 " vec4 bloom = texture2D(u_texture, v_texCoord);",
88 " vec3 bloomCol = bloom.rgb;",
89 " float bloomLum = dot(bloomCol, vec3(0.299, 0.587, 0.114));",
90 " bloomCol = mix(vec3(bloomLum), bloomCol, u_saturation);",
91 " float sceneLum = dot(scene.rgb, vec3(0.299, 0.587, 0.114));",
92 " vec3 sceneHue = scene.rgb / max(sceneLum, 0.04);",
93 " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));",
94 " bloomCol *= u_intensity;",
95 // Soft add: keeps punchy color instead of clipping hard to white/gray.
96 " vec3 rgb = scene.rgb + bloomCol * (1.0 - scene.rgb * 0.35);",
97 " float bloomA = max(bloomCol.r, max(bloomCol.g, bloomCol.b));",
98 " float a = max(scene.a, min(bloomA, 1.0));",
99 " gl_FragColor = vec4(rgb, a);",
100 "}"
101].join("\n");
102
103/**
104 * Multi-pass bloom for a RenderLayer / scene.postShaders.
105 *
106 * options:
107 * - threshold, softKnee, intensity, blurSize
108 * - saturation: bloom chroma boost (1 = neutral, >1 = juicier)
109 * - colorPreserve: 0–1 tint bloom with the original sprite hue
110 * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets)
111 * - dilate: explicit dilate radius in texels (overrides smallSources default)
112 * - scales: number of H/V blur scale pairs (default 3 with smallSources, else 2)
113 */
114Shader.getBloomPostChain = function (options)
115{
116 options = options || {};
117 const threshold = options.threshold != null ? options.threshold : 0.4;
118 const softKnee = options.softKnee != null ? options.softKnee : 0.3;
119 const intensity = options.intensity != null ? options.intensity : 1.25;
120 const blurSize = options.blurSize != null ? options.blurSize : 3.0;
121 const saturation = options.saturation != null ? options.saturation : 1.0;
122 const colorPreserve = options.colorPreserve != null ? options.colorPreserve : 0.0;
123 const smallSources = !!options.smallSources;
124
125 let dilateRadius = 0;
126 if (options.dilate != null)
127 { dilateRadius = options.dilate; }
128 else if (smallSources)
129 { dilateRadius = 2.5; }
130
131 const scales = options.scales != null
132 ? options.scales
133 : (smallSources ? 3 : 2);
134
135 if (!Shader._bloomBright)
136 {
137 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
138 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
139 Shader._bloomBlur = Shader.screenFragment(Shader.Builtin.BLOOM_BLUR_FRAG);
140 Shader._bloomCombine = Shader.screenFragment(Shader.Builtin.BLOOM_COMBINE_FRAG);
141 }
142
143 const chain = [
144 { shader: Shader._bloomBright, uniforms: { u_threshold: threshold, u_softKnee: softKnee } }
145 ];
146
147 if (dilateRadius > 0)
148 {
149 chain.push({
150 shader: Shader._bloomDilate,
151 uniforms: { u_radius: dilateRadius }
152 });
153 }
154
155 const scaleFactors = [1, 2.2, 4.5, 8];
156 for (let i = 0; i < scales; i++)
157 {
158 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)];
159 chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [1, 0], u_blurSize: size } });
160 chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [0, 1], u_blurSize: size } });
161 }
162
163 chain.push({
164 shader: Shader._bloomCombine,
165 uniforms: {
166 u_intensity: intensity,
167 u_saturation: saturation,
168 u_colorPreserve: colorPreserve
169 }
170 });
171 return chain;
172};
173
174// --- Sprite effect builtins -------------------------------------------------
175
176Shader.Builtin.BUMPMAP_FRAG = [
177 "precision mediump float;",
178 "uniform sampler2D u_texture;",
179 "uniform sampler2D u_bump;",
180 "uniform float u_alpha;",
181 "uniform vec2 u_worldCenter;",
182 "uniform vec2 u_worldSize;",
183 "uniform float u_rotationRad;",
184 "uniform int u_lightCount;",
185 "uniform vec2 u_lightPos[16];",
186 "uniform vec3 u_lightColor[16];",
187 "uniform float u_ambient;",
188 "uniform float u_specular;",
189 "varying vec2 v_texCoord;",
190 "void main() {",
191 " vec4 albedo = texture2D(u_texture, v_texCoord);",
192 " float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
193 " float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
194 " float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
195 " float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
196 " vec3 normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
197 " vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
198 " float c = cos(u_rotationRad);",
199 " float s = sin(u_rotationRad);",
200 " vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",
201 " vec3 lighting = vec3(u_ambient);",
202 " for (int i = 0; i < 16; i++) {",
203 " if (i >= u_lightCount) { break; }",
204 " vec2 toLight = u_lightPos[i] - world;",
205 " float dist = max(length(toLight), 0.001);",
206 " vec3 lightDir = normalize(vec3(toLight / dist, 0.65));",
207 " float diff = max(dot(normal, lightDir), 0.0);",
208 " float atten = 1.0 / (1.0 + dist * 0.008 + dist * dist * 0.00005);",
209 " float spec = pow(diff, 16.0) * u_specular;",
210 " lighting += u_lightColor[i] * (diff + spec) * atten;",
211 " }",
212 " gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",
213 "}"
214].join("\n");
215
216// Replace pure R/G/B paint channel with u_replaceColor.
217Shader.Builtin.COLOUR_REPLACE_FRAG = [
218 "precision mediump float;",
219 "uniform sampler2D u_texture;",
220 "uniform float u_alpha;",
221 "uniform float u_channel;",
222 "uniform vec3 u_replaceColor;",
223 "varying vec2 v_texCoord;",
224 "void main() {",
225 " vec4 color = texture2D(u_texture, v_texCoord);",
226 " float ch = color.r;",
227 " float other = max(color.g, color.b);",
228 " if (u_channel > 0.5 && u_channel < 1.5) { ch = color.g; other = max(color.r, color.b); }",
229 " if (u_channel >= 1.5) { ch = color.b; other = max(color.r, color.g); }",
230 " float mask = clamp((ch - other) * 1.75, 0.0, 1.0);",
231 " vec3 rgb = mix(color.rgb, u_replaceColor, mask);",
232 " gl_FragColor = vec4(rgb, color.a * u_alpha);",
233 "}"
234].join("\n");
235
236// Clip sprite where secondary map luminance < u_amount (0 = full, 1 = gone).
237Shader.Builtin.DISINTEGRATE_FRAG = [
238 "precision mediump float;",
239 "uniform sampler2D u_texture;",
240 "uniform sampler2D u_secondary;",
241 "uniform float u_alpha;",
242 "uniform float u_amount;",
243 "varying vec2 v_texCoord;",
244 "void main() {",
245 " vec4 color = texture2D(u_texture, v_texCoord);",
246 " float ref = texture2D(u_secondary, v_texCoord).r;",
247 " if (ref < u_amount) { discard; }",
248 " float edge = smoothstep(u_amount, u_amount + 0.08, ref);",
249 " gl_FragColor = vec4(color.rgb, color.a * u_alpha * edge);",
250 "}"
251].join("\n");
252
253Shader.getBumpmapShader = function ()
254{
255 if (!Shader._bumpmap)
256 { Shader._bumpmap = Shader.fragment(Shader.Builtin.BUMPMAP_FRAG); }
257 return Shader._bumpmap;
258};
259
260Shader.getColourReplaceShader = function ()
261{
262 if (!Shader._colourReplace)
263 { Shader._colourReplace = Shader.fragment(Shader.Builtin.COLOUR_REPLACE_FRAG); }
264 return Shader._colourReplace;
265};
266
267Shader.getDisintegrateShader = function ()
268{
269 if (!Shader._disintegrate)
270 { Shader._disintegrate = Shader.fragment(Shader.Builtin.DISINTEGRATE_FRAG); }
271 return Shader._disintegrate;
272};
273
274// --- Screen-space effect builtins ------------------------------------------
275
276Shader.Builtin.DIRECTIONAL_BLUR_FRAG = [
277 "precision mediump float;",
278 "uniform sampler2D u_texture;",
279 "uniform vec2 u_resolution;",
280 "uniform vec2 u_direction;",
281 "uniform float u_length;",
282 "varying vec2 v_texCoord;",
283 "void main() {",
284 " vec2 dir = u_direction;",
285 " float len = length(dir);",
286 " if (len > 0.0001) { dir /= len; } else { dir = vec2(1.0, 0.0); }",
287 " vec2 step = dir * u_length / max(u_resolution, vec2(1.0));",
288 " vec4 c = vec4(0.0);",
289 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.12;",
290 " c += texture2D(u_texture, v_texCoord - step) * 0.23;",
291 " c += texture2D(u_texture, v_texCoord) * 0.30;",
292 " c += texture2D(u_texture, v_texCoord + step) * 0.23;",
293 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.12;",
294 " gl_FragColor = c;",
295 "}"
296].join("\n");
297
298Shader.Builtin.RADIAL_BLUR_FRAG = [
299 "precision mediump float;",
300 "uniform sampler2D u_texture;",
301 "uniform vec2 u_center;",
302 "uniform float u_amount;",
303 "varying vec2 v_texCoord;",
304 "void main() {",
305 " vec2 toCenter = u_center - v_texCoord;",
306 " vec4 c = vec4(0.0);",
307 " for (int i = 0; i < 8; i++) {",
308 " float t = float(i) / 7.0;",
309 " vec2 uv = v_texCoord + toCenter * t * u_amount;",
310 " c += texture2D(u_texture, uv);",
311 " }",
312 " gl_FragColor = c / 8.0;",
313 "}"
314].join("\n");
315
316Shader.Builtin.COLOR_ADJUST_FRAG = [
317 "precision mediump float;",
318 "uniform sampler2D u_texture;",
319 "uniform float u_saturation;",
320 "uniform float u_contrast;",
321 "uniform float u_brightness;",
322 "varying vec2 v_texCoord;",
323 "void main() {",
324 " vec4 color = texture2D(u_texture, v_texCoord);",
325 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
326 " vec3 rgb = mix(vec3(lum), color.rgb, u_saturation);",
327 " rgb = (rgb - 0.5) * u_contrast + 0.5 + u_brightness;",
328 " gl_FragColor = vec4(rgb, color.a);",
329 "}"
330].join("\n");
331
332Shader.getDirectionalBlurPostChain = function (options)
333{
334 options = options || {};
335 const direction = options.direction || [1, 0];
336 const length = options.length != null ? options.length : 8;
337
338 if (!Shader._directionalBlur)
339 { Shader._directionalBlur = Shader.screenFragment(Shader.Builtin.DIRECTIONAL_BLUR_FRAG); }
340
341 return [{
342 shader: Shader._directionalBlur,
343 uniforms: {
344 u_direction: [direction[0], direction[1]],
345 u_length: length
346 }
347 }];
348};
349
350Shader.getRadialBlurPostChain = function (options)
351{
352 options = options || {};
353 const amount = options.amount != null ? options.amount : 0.15;
354 const center = options.center || [0.5, 0.5];
355
356 if (!Shader._radialBlur)
357 { Shader._radialBlur = Shader.screenFragment(Shader.Builtin.RADIAL_BLUR_FRAG); }
358
359 return [{
360 shader: Shader._radialBlur,
361 uniforms: {
362 u_amount: amount,
363 u_center: [center[0], center[1]]
364 }
365 }];
366};
367
368Shader.getColorAdjustPostChain = function (options)
369{
370 options = options || {};
371 const saturation = options.saturation != null ? options.saturation : 1.0;
372 const contrast = options.contrast != null ? options.contrast : 1.0;
373 const brightness = options.brightness != null ? options.brightness : 0.0;
374
375 if (!Shader._colorAdjust)
376 { Shader._colorAdjust = Shader.screenFragment(Shader.Builtin.COLOR_ADJUST_FRAG); }
377
378 return [{
379 shader: Shader._colorAdjust,
380 uniforms: {
381 u_saturation: saturation,
382 u_contrast: contrast,
383 u_brightness: brightness
384 }
385 }];
386};