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