From 921952cdb937fcc3d85e6370d51fb9396e88831b Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 28 Sep 2023 23:35:16 +0500 Subject: [PATCH] Allow to specify custom texture for Gravity example --- examples/common/params.js | 7 ++++++- examples/gravity/index.js | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/examples/common/params.js b/examples/common/params.js index 43f0a87..0fd6b50 100644 --- a/examples/common/params.js +++ b/examples/common/params.js @@ -49,6 +49,8 @@ export const Parser = { float: (v) => parseNumber(v, Number.parseFloat), /** @param {Object} type */ enum: type => parseEnum(type), + /** @param {*} v */ + string: v => v?.toString(), } export function parse(def = {}) { @@ -113,7 +115,10 @@ export function parseSettings(config) { const result = {} for (const [key, {parser, param, default: def}] of Object.entries(config)) { - const value = parser(params[param]) ?? def; + let value = parser(params[param]); + if (value === undefined || value === null || value === "") { + value = def; + } if (value !== null) { result[key] = value; diff --git a/examples/gravity/index.js b/examples/gravity/index.js index de0b3fc..ae6df6a 100644 --- a/examples/gravity/index.js +++ b/examples/gravity/index.js @@ -25,7 +25,8 @@ const BootstrapInstance = new Bootstrap( ); const { - count, minSize, maxSize, gravity, particleScale, particleOpacity, worldScale, minInteractionDistance + count, minSize, maxSize, gravity, particleScale, particleOpacity, worldScale, minInteractionDistance, + particleTextureUrl, particleBlending, particleColoring } = Params.parseSettings({ count: {parser: Params.Parser.int, param: "count", default: 200}, minSize: {parser: Params.Parser.int, param: "min_size", default: 10}, @@ -35,6 +36,14 @@ const { particleOpacity: {parser: Params.Parser.float, param: "opacity", default: 1}, worldScale: {parser: Params.Parser.float, param: "w_scale", default: 40}, minInteractionDistance: {parser: Params.Parser.float, param: "scale", default: 0.01 ** 2}, + + particleBlending: {parser: Params.Parser.bool, param: "blend", default: true}, + particleColoring: {parser: Params.Parser.bool, param: "color", default: true}, + particleTextureUrl: { + parser: Params.Parser.string, + param: "tex", + default: new URL("./sprites/particle.png", import.meta.url) + } }); @@ -53,9 +62,11 @@ BootstrapInstance.renderer.setProjectionMatrix(projMatrix) BootstrapInstance.addConstraint(new InsetConstraint(WorldRect)) BootstrapInstance.addForce(new ResistanceForce(options.resistance)); -BootstrapInstance.renderer.setBlending(WebGL2RenderingContext.SRC_COLOR, WebGL2RenderingContext.ONE); +if (particleBlending) { + BootstrapInstance.renderer.setBlending(WebGL2RenderingContext.SRC_COLOR, WebGL2RenderingContext.ONE); +} -const particleTexture = new ImageTexture(new URL("./sprites/particle.png", import.meta.url)); +const particleTexture = new ImageTexture(particleTextureUrl); particleTexture.glWrapS = WebGL2RenderingContext.CLAMP_TO_EDGE; particleTexture.glWrapT = WebGL2RenderingContext.CLAMP_TO_EDGE; particleTexture.glMin = WebGL2RenderingContext.LINEAR_MIPMAP_LINEAR; @@ -84,7 +95,9 @@ for (let i = 0; i < count; i++) { const renderer = new SpriteObject(body); renderer.texture = particleTexture; - renderer.color = Utils.randomColor(170, 255); + if (particleColoring) { + renderer.color = Utils.randomColor(170, 255); + } renderer.opacity = particleOpacity; renderer.scale = particleScale;