BlueGit

timfa frameworkv3

Add builtin sprite and screen-space effect shaders.

Support bump lights, secondary maps, colour replace, disintegrate, and screen blur/color-adjust chains on top of the existing bloom API.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-10 · 3 months ago

Hash
d5e45af0f2b03778ecfd36bd073c55bb0e84f798
Parent
2487eecd
4 files changed +341 −3
framework/engine/gl/builtinShaders.js +214 −0
@@ -170,3 +170,217 @@ Shader.getBloomPostChain = function (options)
170 170 });
171 171 return chain;
172 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 "varying vec2 v_texCoord;",
190 "void main() {",
191 " vec4 albedo = texture2D(u_texture, v_texCoord);",
192 " float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
193 " float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
194 " float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
195 " float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
196 " vec3 normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
197 " vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
198 " float c = cos(u_rotationRad);",
199 " float s = sin(u_rotationRad);",
200 " vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",
201 " vec3 lighting = vec3(u_ambient);",
202 " for (int i = 0; i < 16; i++) {",
203 " if (i >= u_lightCount) { break; }",
204 " vec2 toLight = u_lightPos[i] - world;",
205 " float dist = max(length(toLight), 0.001);",
206 " vec3 lightDir = normalize(vec3(toLight / dist, 0.65));",
207 " float diff = max(dot(normal, lightDir), 0.0);",
208 " float atten = 1.0 / (1.0 + dist * 0.008 + dist * dist * 0.00005);",
209 " float spec = pow(diff, 16.0) * u_specular;",
210 " lighting += u_lightColor[i] * (diff + spec) * atten;",
211 " }",
212 " gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",
213 "}"
214 ].join("\n");
215
216 // Replace pure R/G/B paint channel with u_replaceColor.
217 Shader.Builtin.COLOUR_REPLACE_FRAG = [
218 "precision mediump float;",
219 "uniform sampler2D u_texture;",
220 "uniform float u_alpha;",
221 "uniform float u_channel;",
222 "uniform vec3 u_replaceColor;",
223 "varying vec2 v_texCoord;",
224 "void main() {",
225 " vec4 color = texture2D(u_texture, v_texCoord);",
226 " float ch = color.r;",
227 " float other = max(color.g, color.b);",
228 " if (u_channel > 0.5 && u_channel < 1.5) { ch = color.g; other = max(color.r, color.b); }",
229 " if (u_channel >= 1.5) { ch = color.b; other = max(color.r, color.g); }",
230 " float mask = clamp((ch - other) * 1.75, 0.0, 1.0);",
231 " vec3 rgb = mix(color.rgb, u_replaceColor, mask);",
232 " gl_FragColor = vec4(rgb, color.a * u_alpha);",
233 "}"
234 ].join("\n");
235
236 // Clip sprite where secondary map luminance < u_amount (0 = full, 1 = gone).
237 Shader.Builtin.DISINTEGRATE_FRAG = [
238 "precision mediump float;",
239 "uniform sampler2D u_texture;",
240 "uniform sampler2D u_secondary;",
241 "uniform float u_alpha;",
242 "uniform float u_amount;",
243 "varying vec2 v_texCoord;",
244 "void main() {",
245 " vec4 color = texture2D(u_texture, v_texCoord);",
246 " float ref = texture2D(u_secondary, v_texCoord).r;",
247 " if (ref < u_amount) { discard; }",
248 " float edge = smoothstep(u_amount, u_amount + 0.08, ref);",
249 " gl_FragColor = vec4(color.rgb, color.a * u_alpha * edge);",
250 "}"
251 ].join("\n");
252
253 Shader.getBumpmapShader = function ()
254 {
255 if (!Shader._bumpmap)
256 { Shader._bumpmap = Shader.fragment(Shader.Builtin.BUMPMAP_FRAG); }
257 return Shader._bumpmap;
258 };
259
260 Shader.getColourReplaceShader = function ()
261 {
262 if (!Shader._colourReplace)
263 { Shader._colourReplace = Shader.fragment(Shader.Builtin.COLOUR_REPLACE_FRAG); }
264 return Shader._colourReplace;
265 };
266
267 Shader.getDisintegrateShader = function ()
268 {
269 if (!Shader._disintegrate)
270 { Shader._disintegrate = Shader.fragment(Shader.Builtin.DISINTEGRATE_FRAG); }
271 return Shader._disintegrate;
272 };
273
274 // --- Screen-space effect builtins ------------------------------------------
275
276 Shader.Builtin.DIRECTIONAL_BLUR_FRAG = [
277 "precision mediump float;",
278 "uniform sampler2D u_texture;",
279 "uniform vec2 u_resolution;",
280 "uniform vec2 u_direction;",
281 "uniform float u_length;",
282 "varying vec2 v_texCoord;",
283 "void main() {",
284 " vec2 dir = u_direction;",
285 " float len = length(dir);",
286 " if (len > 0.0001) { dir /= len; } else { dir = vec2(1.0, 0.0); }",
287 " vec2 step = dir * u_length / max(u_resolution, vec2(1.0));",
288 " vec4 c = vec4(0.0);",
289 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.12;",
290 " c += texture2D(u_texture, v_texCoord - step) * 0.23;",
291 " c += texture2D(u_texture, v_texCoord) * 0.30;",
292 " c += texture2D(u_texture, v_texCoord + step) * 0.23;",
293 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.12;",
294 " gl_FragColor = c;",
295 "}"
296 ].join("\n");
297
298 Shader.Builtin.RADIAL_BLUR_FRAG = [
299 "precision mediump float;",
300 "uniform sampler2D u_texture;",
301 "uniform vec2 u_center;",
302 "uniform float u_amount;",
303 "varying vec2 v_texCoord;",
304 "void main() {",
305 " vec2 toCenter = u_center - v_texCoord;",
306 " vec4 c = vec4(0.0);",
307 " for (int i = 0; i < 8; i++) {",
308 " float t = float(i) / 7.0;",
309 " vec2 uv = v_texCoord + toCenter * t * u_amount;",
310 " c += texture2D(u_texture, uv);",
311 " }",
312 " gl_FragColor = c / 8.0;",
313 "}"
314 ].join("\n");
315
316 Shader.Builtin.COLOR_ADJUST_FRAG = [
317 "precision mediump float;",
318 "uniform sampler2D u_texture;",
319 "uniform float u_saturation;",
320 "uniform float u_contrast;",
321 "uniform float u_brightness;",
322 "varying vec2 v_texCoord;",
323 "void main() {",
324 " vec4 color = texture2D(u_texture, v_texCoord);",
325 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
326 " vec3 rgb = mix(vec3(lum), color.rgb, u_saturation);",
327 " rgb = (rgb - 0.5) * u_contrast + 0.5 + u_brightness;",
328 " gl_FragColor = vec4(rgb, color.a);",
329 "}"
330 ].join("\n");
331
332 Shader.getDirectionalBlurPostChain = function (options)
333 {
334 options = options || {};
335 const direction = options.direction || [1, 0];
336 const length = options.length != null ? options.length : 8;
337
338 if (!Shader._directionalBlur)
339 { Shader._directionalBlur = Shader.screenFragment(Shader.Builtin.DIRECTIONAL_BLUR_FRAG); }
340
341 return [{
342 shader: Shader._directionalBlur,
343 uniforms: {
344 u_direction: [direction[0], direction[1]],
345 u_length: length
346 }
347 }];
348 };
349
350 Shader.getRadialBlurPostChain = function (options)
351 {
352 options = options || {};
353 const amount = options.amount != null ? options.amount : 0.15;
354 const center = options.center || [0.5, 0.5];
355
356 if (!Shader._radialBlur)
357 { Shader._radialBlur = Shader.screenFragment(Shader.Builtin.RADIAL_BLUR_FRAG); }
358
359 return [{
360 shader: Shader._radialBlur,
361 uniforms: {
362 u_amount: amount,
363 u_center: [center[0], center[1]]
364 }
365 }];
366 };
367
368 Shader.getColorAdjustPostChain = function (options)
369 {
370 options = options || {};
371 const saturation = options.saturation != null ? options.saturation : 1.0;
372 const contrast = options.contrast != null ? options.contrast : 1.0;
373 const brightness = options.brightness != null ? options.brightness : 0.0;
374
375 if (!Shader._colorAdjust)
376 { Shader._colorAdjust = Shader.screenFragment(Shader.Builtin.COLOR_ADJUST_FRAG); }
377
378 return [{
379 shader: Shader._colorAdjust,
380 uniforms: {
381 u_saturation: saturation,
382 u_contrast: contrast,
383 u_brightness: brightness
384 }
385 }];
386 };
@@ -170,3 +170,217 @@ Shader.getBloomPostChain = function (options)
170 });
170 });
171 return chain;
171 return chain;
172};
172};
173
174// --- Sprite effect builtins -------------------------------------------------
175
176Shader.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 "varying vec2 v_texCoord;",
190 "void main() {",
191 " vec4 albedo = texture2D(u_texture, v_texCoord);",
192 " float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
193 " float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
194 " float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
195 " float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
196 " vec3 normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
197 " vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
198 " float c = cos(u_rotationRad);",
199 " float s = sin(u_rotationRad);",
200 " vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",
201 " vec3 lighting = vec3(u_ambient);",
202 " for (int i = 0; i < 16; i++) {",
203 " if (i >= u_lightCount) { break; }",
204 " vec2 toLight = u_lightPos[i] - world;",
205 " float dist = max(length(toLight), 0.001);",
206 " vec3 lightDir = normalize(vec3(toLight / dist, 0.65));",
207 " float diff = max(dot(normal, lightDir), 0.0);",
208 " float atten = 1.0 / (1.0 + dist * 0.008 + dist * dist * 0.00005);",
209 " float spec = pow(diff, 16.0) * u_specular;",
210 " lighting += u_lightColor[i] * (diff + spec) * atten;",
211 " }",
212 " gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",
213 "}"
214].join("\n");
215
216// Replace pure R/G/B paint channel with u_replaceColor.
217Shader.Builtin.COLOUR_REPLACE_FRAG = [
218 "precision mediump float;",
219 "uniform sampler2D u_texture;",
220 "uniform float u_alpha;",
221 "uniform float u_channel;",
222 "uniform vec3 u_replaceColor;",
223 "varying vec2 v_texCoord;",
224 "void main() {",
225 " vec4 color = texture2D(u_texture, v_texCoord);",
226 " float ch = color.r;",
227 " float other = max(color.g, color.b);",
228 " if (u_channel > 0.5 && u_channel < 1.5) { ch = color.g; other = max(color.r, color.b); }",
229 " if (u_channel >= 1.5) { ch = color.b; other = max(color.r, color.g); }",
230 " float mask = clamp((ch - other) * 1.75, 0.0, 1.0);",
231 " vec3 rgb = mix(color.rgb, u_replaceColor, mask);",
232 " gl_FragColor = vec4(rgb, color.a * u_alpha);",
233 "}"
234].join("\n");
235
236// Clip sprite where secondary map luminance < u_amount (0 = full, 1 = gone).
237Shader.Builtin.DISINTEGRATE_FRAG = [
238 "precision mediump float;",
239 "uniform sampler2D u_texture;",
240 "uniform sampler2D u_secondary;",
241 "uniform float u_alpha;",
242 "uniform float u_amount;",
243 "varying vec2 v_texCoord;",
244 "void main() {",
245 " vec4 color = texture2D(u_texture, v_texCoord);",
246 " float ref = texture2D(u_secondary, v_texCoord).r;",
247 " if (ref < u_amount) { discard; }",
248 " float edge = smoothstep(u_amount, u_amount + 0.08, ref);",
249 " gl_FragColor = vec4(color.rgb, color.a * u_alpha * edge);",
250 "}"
251].join("\n");
252
253Shader.getBumpmapShader = function ()
254{
255 if (!Shader._bumpmap)
256 { Shader._bumpmap = Shader.fragment(Shader.Builtin.BUMPMAP_FRAG); }
257 return Shader._bumpmap;
258};
259
260Shader.getColourReplaceShader = function ()
261{
262 if (!Shader._colourReplace)
263 { Shader._colourReplace = Shader.fragment(Shader.Builtin.COLOUR_REPLACE_FRAG); }
264 return Shader._colourReplace;
265};
266
267Shader.getDisintegrateShader = function ()
268{
269 if (!Shader._disintegrate)
270 { Shader._disintegrate = Shader.fragment(Shader.Builtin.DISINTEGRATE_FRAG); }
271 return Shader._disintegrate;
272};
273
274// --- Screen-space effect builtins ------------------------------------------
275
276Shader.Builtin.DIRECTIONAL_BLUR_FRAG = [
277 "precision mediump float;",
278 "uniform sampler2D u_texture;",
279 "uniform vec2 u_resolution;",
280 "uniform vec2 u_direction;",
281 "uniform float u_length;",
282 "varying vec2 v_texCoord;",
283 "void main() {",
284 " vec2 dir = u_direction;",
285 " float len = length(dir);",
286 " if (len > 0.0001) { dir /= len; } else { dir = vec2(1.0, 0.0); }",
287 " vec2 step = dir * u_length / max(u_resolution, vec2(1.0));",
288 " vec4 c = vec4(0.0);",
289 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.12;",
290 " c += texture2D(u_texture, v_texCoord - step) * 0.23;",
291 " c += texture2D(u_texture, v_texCoord) * 0.30;",
292 " c += texture2D(u_texture, v_texCoord + step) * 0.23;",
293 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.12;",
294 " gl_FragColor = c;",
295 "}"
296].join("\n");
297
298Shader.Builtin.RADIAL_BLUR_FRAG = [
299 "precision mediump float;",
300 "uniform sampler2D u_texture;",
301 "uniform vec2 u_center;",
302 "uniform float u_amount;",
303 "varying vec2 v_texCoord;",
304 "void main() {",
305 " vec2 toCenter = u_center - v_texCoord;",
306 " vec4 c = vec4(0.0);",
307 " for (int i = 0; i < 8; i++) {",
308 " float t = float(i) / 7.0;",
309 " vec2 uv = v_texCoord + toCenter * t * u_amount;",
310 " c += texture2D(u_texture, uv);",
311 " }",
312 " gl_FragColor = c / 8.0;",
313 "}"
314].join("\n");
315
316Shader.Builtin.COLOR_ADJUST_FRAG = [
317 "precision mediump float;",
318 "uniform sampler2D u_texture;",
319 "uniform float u_saturation;",
320 "uniform float u_contrast;",
321 "uniform float u_brightness;",
322 "varying vec2 v_texCoord;",
323 "void main() {",
324 " vec4 color = texture2D(u_texture, v_texCoord);",
325 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
326 " vec3 rgb = mix(vec3(lum), color.rgb, u_saturation);",
327 " rgb = (rgb - 0.5) * u_contrast + 0.5 + u_brightness;",
328 " gl_FragColor = vec4(rgb, color.a);",
329 "}"
330].join("\n");
331
332Shader.getDirectionalBlurPostChain = function (options)
333{
334 options = options || {};
335 const direction = options.direction || [1, 0];
336 const length = options.length != null ? options.length : 8;
337
338 if (!Shader._directionalBlur)
339 { Shader._directionalBlur = Shader.screenFragment(Shader.Builtin.DIRECTIONAL_BLUR_FRAG); }
340
341 return [{
342 shader: Shader._directionalBlur,
343 uniforms: {
344 u_direction: [direction[0], direction[1]],
345 u_length: length
346 }
347 }];
348};
349
350Shader.getRadialBlurPostChain = function (options)
351{
352 options = options || {};
353 const amount = options.amount != null ? options.amount : 0.15;
354 const center = options.center || [0.5, 0.5];
355
356 if (!Shader._radialBlur)
357 { Shader._radialBlur = Shader.screenFragment(Shader.Builtin.RADIAL_BLUR_FRAG); }
358
359 return [{
360 shader: Shader._radialBlur,
361 uniforms: {
362 u_amount: amount,
363 u_center: [center[0], center[1]]
364 }
365 }];
366};
367
368Shader.getColorAdjustPostChain = function (options)
369{
370 options = options || {};
371 const saturation = options.saturation != null ? options.saturation : 1.0;
372 const contrast = options.contrast != null ? options.contrast : 1.0;
373 const brightness = options.brightness != null ? options.brightness : 0.0;
374
375 if (!Shader._colorAdjust)
376 { Shader._colorAdjust = Shader.screenFragment(Shader.Builtin.COLOR_ADJUST_FRAG); }
377
378 return [{
379 shader: Shader._colorAdjust,
380 uniforms: {
381 u_saturation: saturation,
382 u_contrast: contrast,
383 u_brightness: brightness
384 }
385 }];
386};
framework/engine/gl/glContext.js +87 −0
@@ -1,5 +1,7 @@
1 1 class GLContext
2 2 {
3 static MAX_LIGHTS = 16;
4
3 5 constructor (canvas)
4 6 {
5 7 this.canvas = canvas;
@@ -356,11 +358,44 @@ class GLContext
356 358
357 359 this._setUniform(info, "u_matrix", matrix);
358 360 this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha);
361 this._setUniform(info, "u_worldCenter", [options.x, options.y]);
362 this._setUniform(info, "u_worldSize", [options.w, options.h]);
363 this._setUniform(info, "u_rotationRad", (options.rotationDeg || 0) * Math.PI / 180);
359 364
360 365 gl.activeTexture(gl.TEXTURE0);
361 366 gl.bindTexture(gl.TEXTURE_2D, texture);
362 367 this._setUniform(info, "u_texture", 0);
363 368
369 let nextUnit = 1;
370 if (options.textures)
371 {
372 for (const name in options.textures)
373 {
374 if (!Object.prototype.hasOwnProperty.call(options.textures, name))
375 { continue; }
376
377 const extra = options.textures[name];
378 if (!extra)
379 { continue; }
380
381 gl.activeTexture(gl.TEXTURE0 + nextUnit);
382 gl.bindTexture(gl.TEXTURE_2D, extra);
383 this._setUniform(info, name, nextUnit);
384 nextUnit++;
385 }
386 }
387
388 if (options.lights)
389 { this._setLights(info, options.lights); }
390 else if (info.uniforms.u_lightCount)
391 { this._setUniform(info, "u_lightCount", 0); }
392
393 // Sensible bump defaults when the shader asks for them.
394 if (info.uniforms.u_ambient && (!options.uniforms || options.uniforms.u_ambient == null))
395 { this._setUniform(info, "u_ambient", 0.35); }
396 if (info.uniforms.u_specular && (!options.uniforms || options.uniforms.u_specular == null))
397 { this._setUniform(info, "u_specular", 0.25); }
398
364 399 if (options.uniforms)
365 400 {
366 401 for (const name in options.uniforms)
@@ -375,6 +410,58 @@ class GLContext
375 410 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
376 411 }
377 412
413 _setLights (info, lights)
414 {
415 const gl = this.gl;
416 const max = GLContext.MAX_LIGHTS;
417 const count = Math.max(0, Math.min(lights.length, max));
418 this._setUniform(info, "u_lightCount", count);
419
420 const posEntry = info.uniforms["u_lightPos[0]"] || info.uniforms.u_lightPos;
421 const colEntry = info.uniforms["u_lightColor[0]"] || info.uniforms.u_lightColor;
422 if (!posEntry || !colEntry || count === 0)
423 {
424 if (count === 0)
425 { this._setUniform(info, "u_lightCount", 0); }
426 return;
427 }
428
429 const pos = new Float32Array(max * 2);
430 const col = new Float32Array(max * 3);
431
432 for (let i = 0; i < count; i++)
433 {
434 const light = lights[i] || {};
435 pos[i * 2] = light.x != null ? light.x : (light.position ? light.position.x : 0);
436 pos[i * 2 + 1] = light.y != null ? light.y : (light.position ? light.position.y : 0);
437
438 const c = light.color != null ? light.color : [1, 1, 1];
439 if (Array.isArray(c))
440 {
441 col[i * 3] = c[0] != null ? c[0] : 0;
442 col[i * 3 + 1] = c[1] != null ? c[1] : 0;
443 col[i * 3 + 2] = c[2] != null ? c[2] : 0;
444 }
445 else
446 {
447 col[i * 3] = (c.r != null ? c.r : 1);
448 col[i * 3 + 1] = (c.g != null ? c.g : 1);
449 col[i * 3 + 2] = (c.b != null ? c.b : 1);
450 }
451
452 // Allow 0–255 style colors.
453 if (col[i * 3] > 1 || col[i * 3 + 1] > 1 || col[i * 3 + 2] > 1)
454 {
455 col[i * 3] /= 255;
456 col[i * 3 + 1] /= 255;
457 col[i * 3 + 2] /= 255;
458 }
459 }
460
461 gl.uniform2fv(posEntry.location, pos);
462 gl.uniform3fv(colEntry.location, col);
463 }
464
378 465 _setUniform (info, name, value)
379 466 {
380 467 const entry = info.uniforms[name];
@@ -1,5 +1,7 @@
1class GLContext
1class GLContext
2{
2{
3 static MAX_LIGHTS = 16;
4
3 constructor (canvas)
5 constructor (canvas)
4 {
6 {
5 this.canvas = canvas;
7 this.canvas = canvas;
@@ -356,11 +358,44 @@ class GLContext
356
358
357 this._setUniform(info, "u_matrix", matrix);
359 this._setUniform(info, "u_matrix", matrix);
358 this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha);
360 this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha);
361 this._setUniform(info, "u_worldCenter", [options.x, options.y]);
362 this._setUniform(info, "u_worldSize", [options.w, options.h]);
363 this._setUniform(info, "u_rotationRad", (options.rotationDeg || 0) * Math.PI / 180);
359
364
360 gl.activeTexture(gl.TEXTURE0);
365 gl.activeTexture(gl.TEXTURE0);
361 gl.bindTexture(gl.TEXTURE_2D, texture);
366 gl.bindTexture(gl.TEXTURE_2D, texture);
362 this._setUniform(info, "u_texture", 0);
367 this._setUniform(info, "u_texture", 0);
363
368
369 let nextUnit = 1;
370 if (options.textures)
371 {
372 for (const name in options.textures)
373 {
374 if (!Object.prototype.hasOwnProperty.call(options.textures, name))
375 { continue; }
376
377 const extra = options.textures[name];
378 if (!extra)
379 { continue; }
380
381 gl.activeTexture(gl.TEXTURE0 + nextUnit);
382 gl.bindTexture(gl.TEXTURE_2D, extra);
383 this._setUniform(info, name, nextUnit);
384 nextUnit++;
385 }
386 }
387
388 if (options.lights)
389 { this._setLights(info, options.lights); }
390 else if (info.uniforms.u_lightCount)
391 { this._setUniform(info, "u_lightCount", 0); }
392
393 // Sensible bump defaults when the shader asks for them.
394 if (info.uniforms.u_ambient && (!options.uniforms || options.uniforms.u_ambient == null))
395 { this._setUniform(info, "u_ambient", 0.35); }
396 if (info.uniforms.u_specular && (!options.uniforms || options.uniforms.u_specular == null))
397 { this._setUniform(info, "u_specular", 0.25); }
398
364 if (options.uniforms)
399 if (options.uniforms)
365 {
400 {
366 for (const name in options.uniforms)
401 for (const name in options.uniforms)
@@ -375,6 +410,58 @@ class GLContext
375 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
410 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
376 }
411 }
377
412
413 _setLights (info, lights)
414 {
415 const gl = this.gl;
416 const max = GLContext.MAX_LIGHTS;
417 const count = Math.max(0, Math.min(lights.length, max));
418 this._setUniform(info, "u_lightCount", count);
419
420 const posEntry = info.uniforms["u_lightPos[0]"] || info.uniforms.u_lightPos;
421 const colEntry = info.uniforms["u_lightColor[0]"] || info.uniforms.u_lightColor;
422 if (!posEntry || !colEntry || count === 0)
423 {
424 if (count === 0)
425 { this._setUniform(info, "u_lightCount", 0); }
426 return;
427 }
428
429 const pos = new Float32Array(max * 2);
430 const col = new Float32Array(max * 3);
431
432 for (let i = 0; i < count; i++)
433 {
434 const light = lights[i] || {};
435 pos[i * 2] = light.x != null ? light.x : (light.position ? light.position.x : 0);
436 pos[i * 2 + 1] = light.y != null ? light.y : (light.position ? light.position.y : 0);
437
438 const c = light.color != null ? light.color : [1, 1, 1];
439 if (Array.isArray(c))
440 {
441 col[i * 3] = c[0] != null ? c[0] : 0;
442 col[i * 3 + 1] = c[1] != null ? c[1] : 0;
443 col[i * 3 + 2] = c[2] != null ? c[2] : 0;
444 }
445 else
446 {
447 col[i * 3] = (c.r != null ? c.r : 1);
448 col[i * 3 + 1] = (c.g != null ? c.g : 1);
449 col[i * 3 + 2] = (c.b != null ? c.b : 1);
450 }
451
452 // Allow 0–255 style colors.
453 if (col[i * 3] > 1 || col[i * 3 + 1] > 1 || col[i * 3 + 2] > 1)
454 {
455 col[i * 3] /= 255;
456 col[i * 3 + 1] /= 255;
457 col[i * 3 + 2] /= 255;
458 }
459 }
460
461 gl.uniform2fv(posEntry.location, pos);
462 gl.uniform3fv(colEntry.location, col);
463 }
464
378 _setUniform (info, name, value)
465 _setUniform (info, name, value)
379 {
466 {
380 const entry = info.uniforms[name];
467 const entry = info.uniforms[name];
framework/engine/image.js +21 −2
@@ -30,7 +30,7 @@ class image
30 30 console.log("Image loaded: " + (image && image.src ? image.src : ""));
31 31 }
32 32
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms)
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms, textures, lights)
34 34 {
35 35 var shouldDraw = true;
36 36
@@ -52,6 +52,23 @@ class image
52 52 const subImageData = this.getSubImage(subImage);
53 53 const imgW = this.g_image.width;
54 54 const imgH = this.g_image.height;
55 const glTextures = {};
56
57 if (textures)
58 {
59 for (const name in textures)
60 {
61 if (!Object.prototype.hasOwnProperty.call(textures, name))
62 { continue; }
63
64 const src = textures[name];
65 if (!src)
66 { continue; }
67
68 const htmlImage = src.g_image || src;
69 glTextures[name] = scene.glContext.getTexture(htmlImage);
70 }
71 }
55 72
56 73 scene.glContext.drawSprite({
57 74 texture: scene.glContext.getTexture(this.g_image),
@@ -70,7 +87,9 @@ class image
70 87 v1: (subImageData.sy + subImageData.sh) / imgH
71 88 },
72 89 shader: shader || scene.glContext.defaultShader,
73 uniforms: uniforms
90 uniforms: uniforms,
91 textures: glTextures,
92 lights: lights
74 93 });
75 94 }
76 95
@@ -30,7 +30,7 @@ class image
30 console.log("Image loaded: " + (image && image.src ? image.src : ""));
30 console.log("Image loaded: " + (image && image.src ? image.src : ""));
31 }
31 }
32
32
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms)
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms, textures, lights)
34 {
34 {
35 var shouldDraw = true;
35 var shouldDraw = true;
36
36
@@ -52,6 +52,23 @@ class image
52 const subImageData = this.getSubImage(subImage);
52 const subImageData = this.getSubImage(subImage);
53 const imgW = this.g_image.width;
53 const imgW = this.g_image.width;
54 const imgH = this.g_image.height;
54 const imgH = this.g_image.height;
55 const glTextures = {};
56
57 if (textures)
58 {
59 for (const name in textures)
60 {
61 if (!Object.prototype.hasOwnProperty.call(textures, name))
62 { continue; }
63
64 const src = textures[name];
65 if (!src)
66 { continue; }
67
68 const htmlImage = src.g_image || src;
69 glTextures[name] = scene.glContext.getTexture(htmlImage);
70 }
71 }
55
72
56 scene.glContext.drawSprite({
73 scene.glContext.drawSprite({
57 texture: scene.glContext.getTexture(this.g_image),
74 texture: scene.glContext.getTexture(this.g_image),
@@ -70,7 +87,9 @@ class image
70 v1: (subImageData.sy + subImageData.sh) / imgH
87 v1: (subImageData.sy + subImageData.sh) / imgH
71 },
88 },
72 shader: shader || scene.glContext.defaultShader,
89 shader: shader || scene.glContext.defaultShader,
73 uniforms: uniforms
90 uniforms: uniforms,
91 textures: glTextures,
92 lights: lights
74 });
93 });
75 }
94 }
76
95
framework/engine/objectComponents/renderer.js +19 −1
@@ -16,6 +16,13 @@ class Renderer
16 16 shader = null;
17 17 shaderUniforms = null;
18 18
19 // Optional maps for builtin sprite effects (framework `image` instances).
20 bumpMap = null;
21 secondaryMap = null;
22
23 // Dynamic lights for bumpmap: [{ x, y, color: [r,g,b]|{r,g,b} }, ...]
24 lights = null;
25
19 26 // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default").
20 27 layer = null;
21 28
@@ -25,6 +32,9 @@ class Renderer
25 32 this.sprite = sprite;
26 33 this.shader = null;
27 34 this.shaderUniforms = null;
35 this.bumpMap = null;
36 this.secondaryMap = null;
37 this.lights = null;
28 38 this.layer = null;
29 39 }
30 40
@@ -110,6 +120,12 @@ class Renderer
110 120 this.g_subImgOld = this.subImage;
111 121 }
112 122
123 const textures = {};
124 if (this.bumpMap)
125 { textures.u_bump = this.bumpMap; }
126 if (this.secondaryMap)
127 { textures.u_secondary = this.secondaryMap; }
128
113 129 this.sprite.draw(
114 130 this.gameObject.scene,
115 131 Math.round(dx),
@@ -122,7 +138,9 @@ class Renderer
122 138 this.alpha,
123 139 this.subImage,
124 140 this.shader,
125 this.shaderUniforms
141 this.shaderUniforms,
142 textures,
143 this.lights
126 144 );
127 145 }
128 146
@@ -16,6 +16,13 @@ class Renderer
16 shader = null;
16 shader = null;
17 shaderUniforms = null;
17 shaderUniforms = null;
18
18
19 // Optional maps for builtin sprite effects (framework `image` instances).
20 bumpMap = null;
21 secondaryMap = null;
22
23 // Dynamic lights for bumpmap: [{ x, y, color: [r,g,b]|{r,g,b} }, ...]
24 lights = null;
25
19 // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default").
26 // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default").
20 layer = null;
27 layer = null;
21
28
@@ -25,6 +32,9 @@ class Renderer
25 this.sprite = sprite;
32 this.sprite = sprite;
26 this.shader = null;
33 this.shader = null;
27 this.shaderUniforms = null;
34 this.shaderUniforms = null;
35 this.bumpMap = null;
36 this.secondaryMap = null;
37 this.lights = null;
28 this.layer = null;
38 this.layer = null;
29 }
39 }
30
40
@@ -110,6 +120,12 @@ class Renderer
110 this.g_subImgOld = this.subImage;
120 this.g_subImgOld = this.subImage;
111 }
121 }
112
122
123 const textures = {};
124 if (this.bumpMap)
125 { textures.u_bump = this.bumpMap; }
126 if (this.secondaryMap)
127 { textures.u_secondary = this.secondaryMap; }
128
113 this.sprite.draw(
129 this.sprite.draw(
114 this.gameObject.scene,
130 this.gameObject.scene,
115 Math.round(dx),
131 Math.round(dx),
@@ -122,7 +138,9 @@ class Renderer
122 this.alpha,
138 this.alpha,
123 this.subImage,
139 this.subImage,
124 this.shader,
140 this.shader,
125 this.shaderUniforms
141 this.shaderUniforms,
142 textures,
143 this.lights
126 );
144 );
127 }
145 }
128
146