Skip to content

Add bloom filter example using p5.strands under filter() reference #7983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev-2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/webgl/ShaderGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ function shadergenerator(p5, fn) {
}
}

/**
* @example
* This example demonstrates how to apply a simple bloom effect
* using `p5.strands` inside the `filter()` function.
* The strands DSL lets you manipulate fragment colors declaratively.
*
* Uses a spinning box with a blur effect created using a 3x3 kernel.
*/
{
let pg;
let shader;

function setup() {
createCanvas(400, 400, WEBGL);
pg = createGraphics(width, height, WEBGL);
pg.noStroke();
}
function draw() {
// Render spinning cube to offscreen buffer
pg.push();
pg.background(0);
pg.rotateY(frameCount * 0.01);
pg.box(100);
pg.pop();

// Apply bloom-like filter using p5.strands
filter(
modify(() => {
let colorSum = vec3(0.0);
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
colorSum += texture2D(tex, texcoord + vec2(dx, dy) * 0.002).rgb;
}
}
colorSum /= 9.0;
return vec4(colorSum, 1.0);
})
);
}
}


// AST Transpiler Callbacks and helper functions
function replaceBinaryOperator(codeSource) {
switch (codeSource) {
Expand Down
Loading