// Built-in screen-space / effect shaders. Games can use these without fetching files. // Expand this set over time into the default framework shader bundle. Shader.Builtin = Shader.Builtin || {}; Shader.Builtin.BLOOM_BRIGHT_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform float u_threshold;", "uniform float u_softKnee;", "varying vec2 v_texCoord;", "void main() {", " vec4 color = texture2D(u_texture, v_texCoord);", " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));", " float knee = max(u_softKnee, 0.001);", " float soft = smoothstep(u_threshold, u_threshold + knee, lum);", " gl_FragColor = vec4(color.rgb * soft, 1.0);", "}" ].join("\n"); // Max-filter dilate: fattens 1–4px hotspots so later blur has something to spread. Shader.Builtin.BLOOM_DILATE_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform vec2 u_resolution;", "uniform float u_radius;", "varying vec2 v_texCoord;", "void main() {", " vec2 px = u_radius / max(u_resolution, vec2(1.0));", " vec4 c = texture2D(u_texture, v_texCoord);", " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, 0.0)));", " c = max(c, texture2D(u_texture, v_texCoord - vec2(px.x, 0.0)));", " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px.y)));", " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px.y)));", " c = max(c, texture2D(u_texture, v_texCoord + px));", " c = max(c, texture2D(u_texture, v_texCoord - px));", " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, -px.y)));", " c = max(c, texture2D(u_texture, v_texCoord + vec2(-px.x, px.y)));", " vec2 px2 = px * 2.0;", " c = max(c, texture2D(u_texture, v_texCoord + vec2(px2.x, 0.0)));", " c = max(c, texture2D(u_texture, v_texCoord - vec2(px2.x, 0.0)));", " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px2.y)));", " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px2.y)));", " gl_FragColor = c;", "}" ].join("\n"); // Wider separable blur (13-tap). u_direction (1,0)/(0,1); u_blurSize in texels. Shader.Builtin.BLOOM_BLUR_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform vec2 u_resolution;", "uniform vec2 u_direction;", "uniform float u_blurSize;", "varying vec2 v_texCoord;", "void main() {", " vec2 step = u_direction * u_blurSize / max(u_resolution, vec2(1.0));", " vec4 c = vec4(0.0);", " c += texture2D(u_texture, v_texCoord - step * 6.0) * 0.009;", " c += texture2D(u_texture, v_texCoord - step * 5.0) * 0.019;", " c += texture2D(u_texture, v_texCoord - step * 4.0) * 0.038;", " c += texture2D(u_texture, v_texCoord - step * 3.0) * 0.071;", " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.111;", " c += texture2D(u_texture, v_texCoord - step) * 0.147;", " c += texture2D(u_texture, v_texCoord) * 0.160;", " c += texture2D(u_texture, v_texCoord + step) * 0.147;", " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.111;", " c += texture2D(u_texture, v_texCoord + step * 3.0) * 0.071;", " c += texture2D(u_texture, v_texCoord + step * 4.0) * 0.038;", " c += texture2D(u_texture, v_texCoord + step * 5.0) * 0.019;", " c += texture2D(u_texture, v_texCoord + step * 6.0) * 0.009;", " gl_FragColor = c;", "}" ].join("\n"); // Combine: add bloom while keeping / boosting chroma (avoids gray wash at high intensity). Shader.Builtin.BLOOM_COMBINE_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform sampler2D u_scene;", "uniform float u_intensity;", "uniform float u_saturation;", "uniform float u_colorPreserve;", "varying vec2 v_texCoord;", "void main() {", " vec4 scene = texture2D(u_scene, v_texCoord);", " vec4 bloom = texture2D(u_texture, v_texCoord);", " vec3 bloomCol = bloom.rgb;", " float bloomLum = dot(bloomCol, vec3(0.299, 0.587, 0.114));", " bloomCol = mix(vec3(bloomLum), bloomCol, u_saturation);", " float sceneLum = dot(scene.rgb, vec3(0.299, 0.587, 0.114));", " vec3 sceneHue = scene.rgb / max(sceneLum, 0.04);", " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));", " bloomCol *= u_intensity;", // Soft add: keeps punchy color instead of clipping hard to white/gray. " vec3 rgb = scene.rgb + bloomCol * (1.0 - scene.rgb * 0.35);", " float bloomA = max(bloomCol.r, max(bloomCol.g, bloomCol.b));", " float a = max(scene.a, min(bloomA, 1.0));", " gl_FragColor = vec4(rgb, a);", "}" ].join("\n"); /** * Multi-pass bloom for a RenderLayer / scene.postShaders. * * options: * - threshold, softKnee, intensity, blurSize * - saturation: bloom chroma boost (1 = neutral, >1 = juicier) * - colorPreserve: 0–1 tint bloom with the original sprite hue * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets) * - dilate: explicit dilate radius in texels (overrides smallSources default) * - scales: number of H/V blur scale pairs (default 3 with smallSources, else 2) */ Shader.getBloomPostChain = function (options) { options = options || {}; const threshold = options.threshold != null ? options.threshold : 0.4; const softKnee = options.softKnee != null ? options.softKnee : 0.3; const intensity = options.intensity != null ? options.intensity : 1.25; const blurSize = options.blurSize != null ? options.blurSize : 3.0; const saturation = options.saturation != null ? options.saturation : 1.0; const colorPreserve = options.colorPreserve != null ? options.colorPreserve : 0.0; const smallSources = !!options.smallSources; let dilateRadius = 0; if (options.dilate != null) { dilateRadius = options.dilate; } else if (smallSources) { dilateRadius = 2.5; } const scales = options.scales != null ? options.scales : (smallSources ? 3 : 2); if (!Shader._bloomBright) { Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG); Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG); Shader._bloomBlur = Shader.screenFragment(Shader.Builtin.BLOOM_BLUR_FRAG); Shader._bloomCombine = Shader.screenFragment(Shader.Builtin.BLOOM_COMBINE_FRAG); } const chain = [ { shader: Shader._bloomBright, uniforms: { u_threshold: threshold, u_softKnee: softKnee } } ]; if (dilateRadius > 0) { chain.push({ shader: Shader._bloomDilate, uniforms: { u_radius: dilateRadius } }); } const scaleFactors = [1, 2.2, 4.5, 8]; for (let i = 0; i < scales; i++) { const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)]; chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [1, 0], u_blurSize: size } }); chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [0, 1], u_blurSize: size } }); } chain.push({ shader: Shader._bloomCombine, uniforms: { u_intensity: intensity, u_saturation: saturation, u_colorPreserve: colorPreserve } }); return chain; };