// 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; }; // --- Sprite effect builtins ------------------------------------------------- Shader.Builtin.BUMPMAP_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform sampler2D u_bump;", "uniform float u_alpha;", "uniform vec2 u_worldCenter;", "uniform vec2 u_worldSize;", "uniform float u_rotationRad;", "uniform int u_lightCount;", "uniform vec2 u_lightPos[16];", "uniform vec3 u_lightColor[16];", "uniform float u_ambient;", "uniform float u_specular;", // 0 = height map (R), 1 = tangent-space normal map (RGB) "uniform float u_bumpMode;", "varying vec2 v_texCoord;", "void main() {", " vec4 albedo = texture2D(u_texture, v_texCoord);", " vec3 normal;", " if (u_bumpMode < 0.5) {", " float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;", " float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;", " float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;", " float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;", " normal = normalize(vec3(hL - hR, hU - hD, 0.35));", " } else {", " vec3 nSample = texture2D(u_bump, v_texCoord).xyz * 2.0 - 1.0;", " normal = normalize(nSample);", " }", " float c = cos(u_rotationRad);", " float s = sin(u_rotationRad);", " normal = normalize(vec3(c * normal.x - s * normal.y, s * normal.x + c * normal.y, normal.z));", " vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;", " vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);", " vec3 lighting = vec3(u_ambient);", " for (int i = 0; i < 16; i++) {", " if (i >= u_lightCount) { break; }", " vec2 toLight = u_lightPos[i] - world;", " float dist = max(length(toLight), 0.001);", " vec3 lightDir = normalize(vec3(toLight / dist, 0.75));", " float diff = max(dot(normal, lightDir), 0.0);", " float atten = 1.0 / (1.0 + dist * 0.006 + dist * dist * 0.00004);", " float spec = pow(diff, 24.0) * u_specular;", " lighting += u_lightColor[i] * (diff + spec) * atten;", " }", " gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);", "}" ].join("\n"); // Replace pure R/G/B paint channel with u_replaceColor. Shader.Builtin.COLOUR_REPLACE_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform float u_alpha;", "uniform float u_channel;", "uniform vec3 u_replaceColor;", "varying vec2 v_texCoord;", "void main() {", " vec4 color = texture2D(u_texture, v_texCoord);", " float ch = color.r;", " float other = max(color.g, color.b);", " if (u_channel > 0.5 && u_channel < 1.5) { ch = color.g; other = max(color.r, color.b); }", " if (u_channel >= 1.5) { ch = color.b; other = max(color.r, color.g); }", " float mask = clamp((ch - other) * 1.75, 0.0, 1.0);", " vec3 rgb = mix(color.rgb, u_replaceColor, mask);", " gl_FragColor = vec4(rgb, color.a * u_alpha);", "}" ].join("\n"); // Clip sprite where secondary map luminance < u_amount (0 = full, 1 = gone). Shader.Builtin.DISINTEGRATE_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform sampler2D u_secondary;", "uniform float u_alpha;", "uniform float u_amount;", "varying vec2 v_texCoord;", "void main() {", " vec4 color = texture2D(u_texture, v_texCoord);", " float ref = texture2D(u_secondary, v_texCoord).r;", " if (ref < u_amount) { discard; }", " float edge = smoothstep(u_amount, u_amount + 0.08, ref);", " gl_FragColor = vec4(color.rgb, color.a * u_alpha * edge);", "}" ].join("\n"); Shader.getBumpmapShader = function () { if (!Shader._bumpmap || Shader._bumpmap.fragSource !== Shader.Builtin.BUMPMAP_FRAG) { Shader._bumpmap = Shader.fragment(Shader.Builtin.BUMPMAP_FRAG); } return Shader._bumpmap; }; Shader.getColourReplaceShader = function () { if (!Shader._colourReplace) { Shader._colourReplace = Shader.fragment(Shader.Builtin.COLOUR_REPLACE_FRAG); } return Shader._colourReplace; }; Shader.getDisintegrateShader = function () { if (!Shader._disintegrate) { Shader._disintegrate = Shader.fragment(Shader.Builtin.DISINTEGRATE_FRAG); } return Shader._disintegrate; }; // --- Screen-space effect builtins ------------------------------------------ Shader.Builtin.DIRECTIONAL_BLUR_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform vec2 u_resolution;", "uniform vec2 u_direction;", "uniform float u_length;", "varying vec2 v_texCoord;", "void main() {", " vec2 dir = u_direction;", " float len = length(dir);", " if (len > 0.0001) { dir /= len; } else { dir = vec2(1.0, 0.0); }", " vec2 step = dir * u_length / max(u_resolution, vec2(1.0));", " vec4 c = vec4(0.0);", " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.12;", " c += texture2D(u_texture, v_texCoord - step) * 0.23;", " c += texture2D(u_texture, v_texCoord) * 0.30;", " c += texture2D(u_texture, v_texCoord + step) * 0.23;", " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.12;", " gl_FragColor = c;", "}" ].join("\n"); Shader.Builtin.RADIAL_BLUR_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform vec2 u_center;", "uniform float u_amount;", "varying vec2 v_texCoord;", "void main() {", " vec2 toCenter = u_center - v_texCoord;", " vec4 c = vec4(0.0);", " for (int i = 0; i < 8; i++) {", " float t = float(i) / 7.0;", " vec2 uv = v_texCoord + toCenter * t * u_amount;", " c += texture2D(u_texture, uv);", " }", " gl_FragColor = c / 8.0;", "}" ].join("\n"); Shader.Builtin.COLOR_ADJUST_FRAG = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform float u_saturation;", "uniform float u_contrast;", "uniform float u_brightness;", "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));", " vec3 rgb = mix(vec3(lum), color.rgb, u_saturation);", " rgb = (rgb - 0.5) * u_contrast + 0.5 + u_brightness;", " gl_FragColor = vec4(rgb, color.a);", "}" ].join("\n"); Shader.getDirectionalBlurPostChain = function (options) { options = options || {}; const direction = options.direction || [1, 0]; const length = options.length != null ? options.length : 8; if (!Shader._directionalBlur) { Shader._directionalBlur = Shader.screenFragment(Shader.Builtin.DIRECTIONAL_BLUR_FRAG); } return [{ shader: Shader._directionalBlur, uniforms: { u_direction: [direction[0], direction[1]], u_length: length } }]; }; Shader.getRadialBlurPostChain = function (options) { options = options || {}; const amount = options.amount != null ? options.amount : 0.15; const center = options.center || [0.5, 0.5]; if (!Shader._radialBlur) { Shader._radialBlur = Shader.screenFragment(Shader.Builtin.RADIAL_BLUR_FRAG); } return [{ shader: Shader._radialBlur, uniforms: { u_amount: amount, u_center: [center[0], center[1]] } }]; }; Shader.getColorAdjustPostChain = function (options) { options = options || {}; const saturation = options.saturation != null ? options.saturation : 1.0; const contrast = options.contrast != null ? options.contrast : 1.0; const brightness = options.brightness != null ? options.brightness : 0.0; if (!Shader._colorAdjust) { Shader._colorAdjust = Shader.screenFragment(Shader.Builtin.COLOR_ADJUST_FRAG); } return [{ shader: Shader._colorAdjust, uniforms: { u_saturation: saturation, u_contrast: contrast, u_brightness: brightness } }]; };