Skip to content

Commit

Permalink
Allow to specify custom texture for Gravity example
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 28, 2023
1 parent 3b60f6e commit 921952c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/common/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 17 additions & 4 deletions examples/gravity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
}
});


Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 921952c

Please sign in to comment.