BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / gl / builtinShaders.js

builtinShaders.js

engine optimisations
timfa Tim Falken · pushed by timfa · 3 months ago
74d8cdb3 History
builtinShaders.js 14.4 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 2 with smallSources, else 1)
113 * - halfRes: blur at half resolution when true (default true); tags blur passes with scale: 0.5
114 */
115Shader.getBloomPostChain = function (options)
116{
117 options = options || {};
118 const threshold = options.threshold != null ? options.threshold : 0.4;
119 const softKnee = options.softKnee != null ? options.softKnee : 0.3;
120 const intensity = options.intensity != null ? options.intensity : 1.25;
121 const blurSize = options.blurSize != null ? options.blurSize : 3.0;
122 const saturation = options.saturation != null ? options.saturation : 1.0;
123 const colorPreserve = options.colorPreserve != null ? options.colorPreserve : 0.0;
124 const smallSources = !!options.smallSources;
125
126 let dilateRadius = 0;
127 if (options.dilate != null)
128 { dilateRadius = options.dilate; }
129 else if (smallSources)
130 { dilateRadius = 2.5; }
131
132 const scales = options.scales != null
133 ? options.scales
134 : (smallSources ? 2 : 1);
135
136 // Blur at half resolution (~4× less fill). Bright/dilate/combine stay full-res.
137 const halfResBlur = options.halfRes !== false;
138
139 if (!Shader._bloomBright)
140 {
141 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
142 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
143 Shader._bloomBlur = Shader.screenFragment(Shader.Builtin.BLOOM_BLUR_FRAG);
144 Shader._bloomCombine = Shader.screenFragment(Shader.Builtin.BLOOM_COMBINE_FRAG);
145 }
146
147 const chain = [
148 { shader: Shader._bloomBright, uniforms: { u_threshold: threshold, u_softKnee: softKnee } }
149 ];
150
151 if (dilateRadius > 0)
152 {
153 chain.push({
154 shader: Shader._bloomDilate,
155 uniforms: { u_radius: dilateRadius }
156 });
157 }
158
159 const scaleFactors = [1, 2.2, 4.5, 8];
160 for (let i = 0; i < scales; i++)
161 {
162 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)];
163 const blurPass = halfResBlur ? { scale: 0.5 } : {};
164 chain.push(Object.assign({
165 shader: Shader._bloomBlur,
166 uniforms: { u_direction: [1, 0], u_blurSize: size }
167 }, blurPass));
168 chain.push(Object.assign({
169 shader: Shader._bloomBlur,
170 uniforms: { u_direction: [0, 1], u_blurSize: size }
171 }, blurPass));
172 }
173
174 chain.push({
175 shader: Shader._bloomCombine,
176 uniforms: {
177 u_intensity: intensity,
178 u_saturation: saturation,
179 u_colorPreserve: colorPreserve
180 }
181 });
182 return chain;
183};
184
185// --- Sprite effect builtins -------------------------------------------------
186
187Shader.Builtin.BUMPMAP_FRAG = [
188 "precision mediump float;",
189 "uniform sampler2D u_texture;",
190 "uniform sampler2D u_bump;",
191 "uniform float u_alpha;",
192 "uniform vec2 u_worldCenter;",
193 "uniform vec2 u_worldSize;",
194 "uniform float u_rotationRad;",
195 "uniform int u_lightCount;",
196 "uniform vec2 u_lightPos[16];",
197 "uniform vec3 u_lightColor[16];",
198 "uniform float u_ambient;",
199 "uniform float u_specular;",
200 // 0 = height map (R), 1 = tangent-space normal map (RGB)
201 "uniform float u_bumpMode;",
202 "varying vec2 v_texCoord;",
203 "void main() {",
204 " vec4 albedo = texture2D(u_texture, v_texCoord);",
205 " vec3 normal;",
206 " if (u_bumpMode < 0.5) {",
207 " float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
208 " float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
209 " float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
210 " float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
211 " normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
212 " } else {",
213 " vec3 nSample = texture2D(u_bump, v_texCoord).xyz * 2.0 - 1.0;",
214 " normal = normalize(nSample);",
215 " }",
216 " float c = cos(u_rotationRad);",
217 " float s = sin(u_rotationRad);",
218 " normal = normalize(vec3(c * normal.x - s * normal.y, s * normal.x + c * normal.y, normal.z));",
219 " vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
220 " vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",
221 " vec3 lighting = vec3(u_ambient);",
222 " for (int i = 0; i < 16; i++) {",
223 " if (i >= u_lightCount) { break; }",
224 " vec2 toLight = u_lightPos[i] - world;",
225 " float dist = max(length(toLight), 0.001);",
226 " vec3 lightDir = normalize(vec3(toLight / dist, 0.75));",
227 " float diff = max(dot(normal, lightDir), 0.0);",
228 " float atten = 1.0 / (1.0 + dist * 0.006 + dist * dist * 0.00004);",
229 " float spec = pow(diff, 24.0) * u_specular;",
230 " lighting += u_lightColor[i] * (diff + spec) * atten;",
231 " }",
232 " gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",
233 "}"
234].join("\n");
235
236// Replace pure R/G/B paint channel with u_replaceColor.
237Shader.Builtin.COLOUR_REPLACE_FRAG = [
238 "precision mediump float;",
239 "uniform sampler2D u_texture;",
240 "uniform float u_alpha;",
241 "uniform float u_channel;",
242 "uniform vec3 u_replaceColor;",
243 "varying vec2 v_texCoord;",
244 "void main() {",
245 " vec4 color = texture2D(u_texture, v_texCoord);",
246 " float ch = color.r;",
247 " float other = max(color.g, color.b);",
248 " if (u_channel > 0.5 && u_channel < 1.5) { ch = color.g; other = max(color.r, color.b); }",
249 " if (u_channel >= 1.5) { ch = color.b; other = max(color.r, color.g); }",
250 " float mask = clamp((ch - other) * 1.75, 0.0, 1.0);",
251 " vec3 rgb = mix(color.rgb, u_replaceColor, mask);",
252 " gl_FragColor = vec4(rgb, color.a * u_alpha);",
253 "}"
254].join("\n");
255
256// Clip sprite where secondary map luminance < u_amount (0 = full, 1 = gone).
257Shader.Builtin.DISINTEGRATE_FRAG = [
258 "precision mediump float;",
259 "uniform sampler2D u_texture;",
260 "uniform sampler2D u_secondary;",
261 "uniform float u_alpha;",
262 "uniform float u_amount;",
263 "varying vec2 v_texCoord;",
264 "void main() {",
265 " vec4 color = texture2D(u_texture, v_texCoord);",
266 " float ref = texture2D(u_secondary, v_texCoord).r;",
267 " if (ref < u_amount) { discard; }",
268 " float edge = smoothstep(u_amount, u_amount + 0.08, ref);",
269 " gl_FragColor = vec4(color.rgb, color.a * u_alpha * edge);",
270 "}"
271].join("\n");
272
273Shader.getBumpmapShader = function ()
274{
275 if (!Shader._bumpmap || Shader._bumpmap.fragSource !== Shader.Builtin.BUMPMAP_FRAG)
276 { Shader._bumpmap = Shader.fragment(Shader.Builtin.BUMPMAP_FRAG); }
277 return Shader._bumpmap;
278};
279
280Shader.getColourReplaceShader = function ()
281{
282 if (!Shader._colourReplace)
283 { Shader._colourReplace = Shader.fragment(Shader.Builtin.COLOUR_REPLACE_FRAG); }
284 return Shader._colourReplace;
285};
286
287Shader.getDisintegrateShader = function ()
288{
289 if (!Shader._disintegrate)
290 { Shader._disintegrate = Shader.fragment(Shader.Builtin.DISINTEGRATE_FRAG); }
291 return Shader._disintegrate;
292};
293
294// --- Screen-space effect builtins ------------------------------------------
295
296Shader.Builtin.DIRECTIONAL_BLUR_FRAG = [
297 "precision mediump float;",
298 "uniform sampler2D u_texture;",
299 "uniform vec2 u_resolution;",
300 "uniform vec2 u_direction;",
301 "uniform float u_length;",
302 "varying vec2 v_texCoord;",
303 "void main() {",
304 " vec2 dir = u_direction;",
305 " float len = length(dir);",
306 " if (len > 0.0001) { dir /= len; } else { dir = vec2(1.0, 0.0); }",
307 " vec2 step = dir * u_length / max(u_resolution, vec2(1.0));",
308 " vec4 c = vec4(0.0);",
309 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.12;",
310 " c += texture2D(u_texture, v_texCoord - step) * 0.23;",
311 " c += texture2D(u_texture, v_texCoord) * 0.30;",
312 " c += texture2D(u_texture, v_texCoord + step) * 0.23;",
313 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.12;",
314 " gl_FragColor = c;",
315 "}"
316].join("\n");
317
318Shader.Builtin.RADIAL_BLUR_FRAG = [
319 "precision mediump float;",
320 "uniform sampler2D u_texture;",
321 "uniform vec2 u_center;",
322 "uniform float u_amount;",
323 "varying vec2 v_texCoord;",
324 "void main() {",
325 " vec2 toCenter = u_center - v_texCoord;",
326 " vec4 c = vec4(0.0);",
327 " for (int i = 0; i < 8; i++) {",
328 " float t = float(i) / 7.0;",
329 " vec2 uv = v_texCoord + toCenter * t * u_amount;",
330 " c += texture2D(u_texture, uv);",
331 " }",
332 " gl_FragColor = c / 8.0;",
333 "}"
334].join("\n");
335
336Shader.Builtin.COLOR_ADJUST_FRAG = [
337 "precision mediump float;",
338 "uniform sampler2D u_texture;",
339 "uniform float u_saturation;",
340 "uniform float u_contrast;",
341 "uniform float u_brightness;",
342 "varying vec2 v_texCoord;",
343 "void main() {",
344 " vec4 color = texture2D(u_texture, v_texCoord);",
345 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
346 " vec3 rgb = mix(vec3(lum), color.rgb, u_saturation);",
347 " rgb = (rgb - 0.5) * u_contrast + 0.5 + u_brightness;",
348 " gl_FragColor = vec4(rgb, color.a);",
349 "}"
350].join("\n");
351
352Shader.getDirectionalBlurPostChain = function (options)
353{
354 options = options || {};
355 const direction = options.direction || [1, 0];
356 const length = options.length != null ? options.length : 8;
357
358 if (!Shader._directionalBlur)
359 { Shader._directionalBlur = Shader.screenFragment(Shader.Builtin.DIRECTIONAL_BLUR_FRAG); }
360
361 return [{
362 shader: Shader._directionalBlur,
363 uniforms: {
364 u_direction: [direction[0], direction[1]],
365 u_length: length
366 }
367 }];
368};
369
370Shader.getRadialBlurPostChain = function (options)
371{
372 options = options || {};
373 const amount = options.amount != null ? options.amount : 0.15;
374 const center = options.center || [0.5, 0.5];
375
376 if (!Shader._radialBlur)
377 { Shader._radialBlur = Shader.screenFragment(Shader.Builtin.RADIAL_BLUR_FRAG); }
378
379 return [{
380 shader: Shader._radialBlur,
381 uniforms: {
382 u_amount: amount,
383 u_center: [center[0], center[1]]
384 }
385 }];
386};
387
388Shader.getColorAdjustPostChain = function (options)
389{
390 options = options || {};
391 const saturation = options.saturation != null ? options.saturation : 1.0;
392 const contrast = options.contrast != null ? options.contrast : 1.0;
393 const brightness = options.brightness != null ? options.brightness : 0.0;
394
395 if (!Shader._colorAdjust)
396 { Shader._colorAdjust = Shader.screenFragment(Shader.Builtin.COLOR_ADJUST_FRAG); }
397
398 return [{
399 shader: Shader._colorAdjust,
400 uniforms: {
401 u_saturation: saturation,
402 u_contrast: contrast,
403 u_brightness: brightness
404 }
405 }];
406};