BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / gl / builtinShaders.js

builtinShaders.js

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