BlueGit

timfa frameworkv3

edits

timfa Tim Falken · pushed by timfa

Authored 2026-08-13 · 3 months ago

Hash
6d353738c4ac5a9010eafad1d220c1359c3d1576
Parent
291668f6
3 files changed +92 −44
framework/engine/gl/builtinShaders.js +25 −18
@@ -11,10 +11,14 @@ Shader.Builtin.BLOOM_BRIGHT_FRAG = [
11 11 "varying vec2 v_texCoord;",
12 12 "void main() {",
13 13 " vec4 color = texture2D(u_texture, v_texCoord);",
14 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
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;",
15 16 " 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);",
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);",
18 22 "}"
19 23 ].join("\n");
20 24
@@ -86,10 +90,11 @@ Shader.Builtin.BLOOM_COMBINE_FRAG = [
86 90 " vec4 scene = texture2D(u_scene, v_texCoord);",
87 91 " vec4 bloom = texture2D(u_texture, v_texCoord);",
88 92 " 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 " 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);",
93 98 " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));",
94 99 " bloomCol *= u_intensity;",
95 100 // Soft add: keeps punchy color instead of clipping hard to white/gray.
@@ -110,7 +115,7 @@ Shader.Builtin.BLOOM_COMBINE_FRAG = [
110 115 * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets)
111 116 * - dilate: explicit dilate radius in texels (overrides smallSources default)
112 117 * - 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
118 * - halfRes: run dilate + blur at half resolution when true (default true)
114 119 */
115 120 Shader.getBloomPostChain = function (options)
116 121 {
@@ -127,16 +132,19 @@ Shader.getBloomPostChain = function (options)
127 132 if (options.dilate != null)
128 133 { dilateRadius = options.dilate; }
129 134 else if (smallSources)
130 { dilateRadius = 2.5; }
135 { dilateRadius = 4.5; } // fatter hotspots so tiny bullets bloom closer to large ones
131 136
132 137 const scales = options.scales != null
133 138 ? options.scales
134 139 : (smallSources ? 2 : 1);
135 140
136 // Blur at half resolution (~4× less fill). Bright/dilate/combine stay full-res.
141 // Dilate + blur at half-res (~4× less fill). Bright extract + combine stay full-res.
137 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;
138 146
139 if (!Shader._bloomBright)
147 if (!Shader._bloomBright || Shader._bloomBright.fragSource !== Shader.Builtin.BLOOM_BRIGHT_FRAG)
140 148 {
141 149 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
142 150 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
@@ -150,25 +158,24 @@ Shader.getBloomPostChain = function (options)
150 158
151 159 if (dilateRadius > 0)
152 160 {
153 chain.push({
161 chain.push(Object.assign({
154 162 shader: Shader._bloomDilate,
155 uniforms: { u_radius: dilateRadius }
156 });
163 uniforms: { u_radius: dilateRadius * resScale }
164 }, halfPass));
157 165 }
158 166
159 167 const scaleFactors = [1, 2.2, 4.5, 8];
160 168 for (let i = 0; i < scales; i++)
161 169 {
162 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)];
163 const blurPass = halfResBlur ? { scale: 0.5 } : {};
170 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)] * resScale;
164 171 chain.push(Object.assign({
165 172 shader: Shader._bloomBlur,
166 173 uniforms: { u_direction: [1, 0], u_blurSize: size }
167 }, blurPass));
174 }, halfPass));
168 175 chain.push(Object.assign({
169 176 shader: Shader._bloomBlur,
170 177 uniforms: { u_direction: [0, 1], u_blurSize: size }
171 }, blurPass));
178 }, halfPass));
172 179 }
173 180
174 181 chain.push({
@@ -11,10 +11,14 @@ Shader.Builtin.BLOOM_BRIGHT_FRAG = [
11 "varying vec2 v_texCoord;",
11 "varying vec2 v_texCoord;",
12 "void main() {",
12 "void main() {",
13 " vec4 color = texture2D(u_texture, v_texCoord);",
13 " vec4 color = texture2D(u_texture, v_texCoord);",
14 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
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;",
15 " float knee = max(u_softKnee, 0.001);",
16 " float knee = max(u_softKnee, 0.001);",
16 " float soft = smoothstep(u_threshold, u_threshold + knee, lum);",
17 " float soft = smoothstep(u_threshold, u_threshold + knee, peak);",
17 " gl_FragColor = vec4(color.rgb * soft, 1.0);",
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);",
18 "}"
22 "}"
19].join("\n");
23].join("\n");
20
24
@@ -86,10 +90,11 @@ Shader.Builtin.BLOOM_COMBINE_FRAG = [
86 " vec4 scene = texture2D(u_scene, v_texCoord);",
90 " vec4 scene = texture2D(u_scene, v_texCoord);",
87 " vec4 bloom = texture2D(u_texture, v_texCoord);",
91 " vec4 bloom = texture2D(u_texture, v_texCoord);",
88 " vec3 bloomCol = bloom.rgb;",
92 " vec3 bloomCol = bloom.rgb;",
89 " float bloomLum = dot(bloomCol, vec3(0.299, 0.587, 0.114));",
93 " float bloomPeak = max(bloomCol.r, max(bloomCol.g, bloomCol.b));",
90 " bloomCol = mix(vec3(bloomLum), bloomCol, u_saturation);",
94 " bloomCol = mix(vec3(bloomPeak), bloomCol, u_saturation);",
91 " float sceneLum = dot(scene.rgb, vec3(0.299, 0.587, 0.114));",
95 // Hue from scene peak (not luma) so red bullets keep chroma under colorPreserve.
92 " vec3 sceneHue = scene.rgb / max(sceneLum, 0.04);",
96 " float scenePeak = max(scene.r, max(scene.g, scene.b));",
97 " vec3 sceneHue = scene.rgb / max(scenePeak, 0.04);",
93 " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));",
98 " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));",
94 " bloomCol *= u_intensity;",
99 " bloomCol *= u_intensity;",
95 // Soft add: keeps punchy color instead of clipping hard to white/gray.
100 // Soft add: keeps punchy color instead of clipping hard to white/gray.
@@ -110,7 +115,7 @@ Shader.Builtin.BLOOM_COMBINE_FRAG = [
110 * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets)
115 * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets)
111 * - dilate: explicit dilate radius in texels (overrides smallSources default)
116 * - dilate: explicit dilate radius in texels (overrides smallSources default)
112 * - scales: number of H/V blur scale pairs (default 2 with smallSources, else 1)
117 * - 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
118 * - halfRes: run dilate + blur at half resolution when true (default true)
114 */
119 */
115Shader.getBloomPostChain = function (options)
120Shader.getBloomPostChain = function (options)
116{
121{
@@ -127,16 +132,19 @@ Shader.getBloomPostChain = function (options)
127 if (options.dilate != null)
132 if (options.dilate != null)
128 { dilateRadius = options.dilate; }
133 { dilateRadius = options.dilate; }
129 else if (smallSources)
134 else if (smallSources)
130 { dilateRadius = 2.5; }
135 { dilateRadius = 4.5; } // fatter hotspots so tiny bullets bloom closer to large ones
131
136
132 const scales = options.scales != null
137 const scales = options.scales != null
133 ? options.scales
138 ? options.scales
134 : (smallSources ? 2 : 1);
139 : (smallSources ? 2 : 1);
135
140
136 // Blur at half resolution (~4× less fill). Bright/dilate/combine stay full-res.
141 // Dilate + blur at half-res (~4× less fill). Bright extract + combine stay full-res.
137 const halfResBlur = options.halfRes !== false;
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;
138
146
139 if (!Shader._bloomBright)
147 if (!Shader._bloomBright || Shader._bloomBright.fragSource !== Shader.Builtin.BLOOM_BRIGHT_FRAG)
140 {
148 {
141 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
149 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
142 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
150 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
@@ -150,25 +158,24 @@ Shader.getBloomPostChain = function (options)
150
158
151 if (dilateRadius > 0)
159 if (dilateRadius > 0)
152 {
160 {
153 chain.push({
161 chain.push(Object.assign({
154 shader: Shader._bloomDilate,
162 shader: Shader._bloomDilate,
155 uniforms: { u_radius: dilateRadius }
163 uniforms: { u_radius: dilateRadius * resScale }
156 });
164 }, halfPass));
157 }
165 }
158
166
159 const scaleFactors = [1, 2.2, 4.5, 8];
167 const scaleFactors = [1, 2.2, 4.5, 8];
160 for (let i = 0; i < scales; i++)
168 for (let i = 0; i < scales; i++)
161 {
169 {
162 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)];
170 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)] * resScale;
163 const blurPass = halfResBlur ? { scale: 0.5 } : {};
164 chain.push(Object.assign({
171 chain.push(Object.assign({
165 shader: Shader._bloomBlur,
172 shader: Shader._bloomBlur,
166 uniforms: { u_direction: [1, 0], u_blurSize: size }
173 uniforms: { u_direction: [1, 0], u_blurSize: size }
167 }, blurPass));
174 }, halfPass));
168 chain.push(Object.assign({
175 chain.push(Object.assign({
169 shader: Shader._bloomBlur,
176 shader: Shader._bloomBlur,
170 uniforms: { u_direction: [0, 1], u_blurSize: size }
177 uniforms: { u_direction: [0, 1], u_blurSize: size }
171 }, blurPass));
178 }, halfPass));
172 }
179 }
173
180
174 chain.push({
181 chain.push({
framework/engine/gl/glContext.js +28 −2
@@ -68,6 +68,8 @@ class GLContext
68 68 this._lastHeight = 0;
69 69 this._halfWidth = 0;
70 70 this._halfHeight = 0;
71 this._postUniforms = {};
72 this._resUniform = [0, 0];
71 73 this.setSize(canvas.width, canvas.height);
72 74 }
73 75
@@ -246,8 +248,32 @@ class GLContext
246 248 gl.clearColor(0, 0, 0, 0);
247 249 gl.clear(gl.COLOR_BUFFER_BIT);
248 250
249 const uniforms = Object.assign({}, builtins, pass.uniforms || {});
250 uniforms.u_resolution = [writeW, writeH];
251 const uniforms = this._postUniforms;
252 for (const key in uniforms)
253 {
254 if (Object.prototype.hasOwnProperty.call(uniforms, key))
255 { delete uniforms[key]; }
256 }
257 if (builtins)
258 {
259 for (const key in builtins)
260 {
261 if (Object.prototype.hasOwnProperty.call(builtins, key))
262 { uniforms[key] = builtins[key]; }
263 }
264 }
265 const passUniforms = pass.uniforms;
266 if (passUniforms)
267 {
268 for (const key in passUniforms)
269 {
270 if (Object.prototype.hasOwnProperty.call(passUniforms, key))
271 { uniforms[key] = passUniforms[key]; }
272 }
273 }
274 this._resUniform[0] = writeW;
275 this._resUniform[1] = writeH;
276 uniforms.u_resolution = this._resUniform;
251 277 this.drawFullscreen(pass.shader, this.colorTextures[read], uniforms, sceneTexture);
252 278
253 279 if (!isLast || !toCanvas)
@@ -68,6 +68,8 @@ class GLContext
68 this._lastHeight = 0;
68 this._lastHeight = 0;
69 this._halfWidth = 0;
69 this._halfWidth = 0;
70 this._halfHeight = 0;
70 this._halfHeight = 0;
71 this._postUniforms = {};
72 this._resUniform = [0, 0];
71 this.setSize(canvas.width, canvas.height);
73 this.setSize(canvas.width, canvas.height);
72 }
74 }
73
75
@@ -246,8 +248,32 @@ class GLContext
246 gl.clearColor(0, 0, 0, 0);
248 gl.clearColor(0, 0, 0, 0);
247 gl.clear(gl.COLOR_BUFFER_BIT);
249 gl.clear(gl.COLOR_BUFFER_BIT);
248
250
249 const uniforms = Object.assign({}, builtins, pass.uniforms || {});
251 const uniforms = this._postUniforms;
250 uniforms.u_resolution = [writeW, writeH];
252 for (const key in uniforms)
253 {
254 if (Object.prototype.hasOwnProperty.call(uniforms, key))
255 { delete uniforms[key]; }
256 }
257 if (builtins)
258 {
259 for (const key in builtins)
260 {
261 if (Object.prototype.hasOwnProperty.call(builtins, key))
262 { uniforms[key] = builtins[key]; }
263 }
264 }
265 const passUniforms = pass.uniforms;
266 if (passUniforms)
267 {
268 for (const key in passUniforms)
269 {
270 if (Object.prototype.hasOwnProperty.call(passUniforms, key))
271 { uniforms[key] = passUniforms[key]; }
272 }
273 }
274 this._resUniform[0] = writeW;
275 this._resUniform[1] = writeH;
276 uniforms.u_resolution = this._resUniform;
251 this.drawFullscreen(pass.shader, this.colorTextures[read], uniforms, sceneTexture);
277 this.drawFullscreen(pass.shader, this.colorTextures[read], uniforms, sceneTexture);
252
278
253 if (!isLast || !toCanvas)
279 if (!isLast || !toCanvas)
framework/engine/image.js +39 −24
@@ -40,9 +40,6 @@ class image
40 40 if (dx < -pad || dy < -pad || dx > scene.real_size.x + pad || dy > scene.real_size.y + pad)
41 41 { return; }
42 42
43 const subImageData = this.getSubImage(subImage);
44 const imgW = this.g_image.width;
45 const imgH = this.g_image.height;
46 43 let glTextures = null;
47 44
48 45 if (textures)
@@ -64,27 +61,45 @@ class image
64 61 }
65 62 }
66 63
67 scene.glContext.drawSprite({
68 texture: scene.glContext.getTexture(this.g_image),
69 x: dx,
70 y: dy,
71 w: dw,
72 h: dh,
73 rotationDeg: rotation,
74 mirrorX: mirrorX,
75 mirrorY: mirrorY,
76 alpha: alpha,
77 uvRect: {
78 u0: subImageData.sx / imgW,
79 v0: subImageData.sy / imgH,
80 u1: (subImageData.sx + subImageData.sw) / imgW,
81 v1: (subImageData.sy + subImageData.sh) / imgH
82 },
83 shader: shader || scene.glContext.defaultShader,
84 uniforms: uniforms,
85 textures: glTextures,
86 lights: lights
87 });
64 const opts = this._drawOpts || (this._drawOpts = {});
65 opts.texture = scene.glContext.getTexture(this.g_image);
66 opts.x = dx;
67 opts.y = dy;
68 opts.w = dw;
69 opts.h = dh;
70 opts.rotationDeg = rotation;
71 opts.mirrorX = mirrorX;
72 opts.mirrorY = mirrorY;
73 opts.alpha = alpha;
74 opts.uvRect = this.getUvRect(subImage);
75 opts.shader = shader || scene.glContext.defaultShader;
76 opts.uniforms = uniforms;
77 opts.textures = glTextures;
78 opts.lights = lights;
79 scene.glContext.drawSprite(opts);
80 }
81
82 getUvRect (subImage)
83 {
84 if (!this._uvRects)
85 { this._uvRects = []; }
86
87 const index = subImage | 0;
88 let rect = this._uvRects[index];
89 if (rect)
90 { return rect; }
91
92 const subImageData = this.getSubImage(index);
93 const imgW = this.g_image.width;
94 const imgH = this.g_image.height;
95 rect = {
96 u0: subImageData.sx / imgW,
97 v0: subImageData.sy / imgH,
98 u1: (subImageData.sx + subImageData.sw) / imgW,
99 v1: (subImageData.sy + subImageData.sh) / imgH
100 };
101 this._uvRects[index] = rect;
102 return rect;
88 103 }
89 104
90 105 hasAnimationCycle (label)
@@ -40,9 +40,6 @@ class image
40 if (dx < -pad || dy < -pad || dx > scene.real_size.x + pad || dy > scene.real_size.y + pad)
40 if (dx < -pad || dy < -pad || dx > scene.real_size.x + pad || dy > scene.real_size.y + pad)
41 { return; }
41 { return; }
42
42
43 const subImageData = this.getSubImage(subImage);
44 const imgW = this.g_image.width;
45 const imgH = this.g_image.height;
46 let glTextures = null;
43 let glTextures = null;
47
44
48 if (textures)
45 if (textures)
@@ -64,27 +61,45 @@ class image
64 }
61 }
65 }
62 }
66
63
67 scene.glContext.drawSprite({
64 const opts = this._drawOpts || (this._drawOpts = {});
68 texture: scene.glContext.getTexture(this.g_image),
65 opts.texture = scene.glContext.getTexture(this.g_image);
69 x: dx,
66 opts.x = dx;
70 y: dy,
67 opts.y = dy;
71 w: dw,
68 opts.w = dw;
72 h: dh,
69 opts.h = dh;
73 rotationDeg: rotation,
70 opts.rotationDeg = rotation;
74 mirrorX: mirrorX,
71 opts.mirrorX = mirrorX;
75 mirrorY: mirrorY,
72 opts.mirrorY = mirrorY;
76 alpha: alpha,
73 opts.alpha = alpha;
77 uvRect: {
74 opts.uvRect = this.getUvRect(subImage);
78 u0: subImageData.sx / imgW,
75 opts.shader = shader || scene.glContext.defaultShader;
79 v0: subImageData.sy / imgH,
76 opts.uniforms = uniforms;
80 u1: (subImageData.sx + subImageData.sw) / imgW,
77 opts.textures = glTextures;
81 v1: (subImageData.sy + subImageData.sh) / imgH
78 opts.lights = lights;
82 },
79 scene.glContext.drawSprite(opts);
83 shader: shader || scene.glContext.defaultShader,
80 }
84 uniforms: uniforms,
81
85 textures: glTextures,
82 getUvRect (subImage)
86 lights: lights
83 {
87 });
84 if (!this._uvRects)
85 { this._uvRects = []; }
86
87 const index = subImage | 0;
88 let rect = this._uvRects[index];
89 if (rect)
90 { return rect; }
91
92 const subImageData = this.getSubImage(index);
93 const imgW = this.g_image.width;
94 const imgH = this.g_image.height;
95 rect = {
96 u0: subImageData.sx / imgW,
97 v0: subImageData.sy / imgH,
98 u1: (subImageData.sx + subImageData.sw) / imgW,
99 v1: (subImageData.sy + subImageData.sh) / imgH
100 };
101 this._uvRects[index] = rect;
102 return rect;
88 }
103 }
89
104
90 hasAnimationCycle (label)
105 hasAnimationCycle (label)