diff --git a/src/components/EditableSketch/index.astro b/src/components/EditableSketch/index.astro index 45520d6d0d..51a939cdfa 100644 --- a/src/components/EditableSketch/index.astro +++ b/src/components/EditableSketch/index.astro @@ -10,6 +10,7 @@ interface Props { height?: number; base?: string; scripts?: string[]; + includeWebGPU?: boolean; } const props = Astro.props as Props; @@ -38,6 +39,7 @@ const scrollable = props.scrollable ?? false; allowSideBySide={!vertical} fullWidth={!vertical} scripts={props.scripts} + includeWebGPU={props.includeWebGPU} cssCode={props.cssCode} htmlBodyCode={props.bodyCode} scrollable={scrollable} diff --git a/src/content/examples/config.ts b/src/content/examples/config.ts index 24056e49aa..711d6e7fd9 100644 --- a/src/content/examples/config.ts +++ b/src/content/examples/config.ts @@ -25,8 +25,12 @@ export const examplesCollection = defineCollection({ featuredImage: image(), featuredImageAlt: z.string().optional().default(""), - // Optional list of scripts - scripts: z.array(z.enum(["p5.sound.js", "Tone.js"])).optional(), + // Named libraries, or a full script URL such as ml5.js. + scripts: z + .array(z.union([z.enum(["p5.sound.js", "Tone.js"]), z.string().url()])) + .optional(), + // Load the p5 WebGPU addon for sketches that call createCanvas(..., WEBGPU). + webgpu: z.boolean().optional(), // Optional list of remixes to add to license remix: z diff --git a/src/content/examples/en/16_Shaders/10_Body_Field/code.js b/src/content/examples/en/16_Shaders/10_Body_Field/code.js new file mode 100644 index 0000000000..c35d8a76c1 --- /dev/null +++ b/src/content/examples/en/16_Shaders/10_Body_Field/code.js @@ -0,0 +1,156 @@ +/* + * Inspired by + * Golan's https://openprocessing.org/@golan/2405195 + * and Dave's https://editor.p5js.org/davepagurek/sketches/dTizHPqbM + */ + +let video; +let bodyPose; +let poses = []; +let connections; +let dataStorage; +let bodyFieldShader; +let webgpuUnavailable = false; + +const loopN = 100; +const pointN = 100; + +// Adapted from https://openprocessing.org/@golan/2405195 +function bodyField() { + const points = uniformStorage(dataStorage); + const progress = uniformFloat(); + const green = uniformFloat(); + filterColor.begin(); + const coord = filterColor.texCoord; + + // Find the distance from this pixel to the nearest body point. + let minDist = 1; + for (let i = 0; i < 100; i++) { + const point = [points[i].x, points[i].y]; + minDist = min(minDist, length(coord - point)); + } + const r = 6 * minDist; + const g = green * coord.y; + const b = 0.5 + 0.5 * cos(TWO_PI * progress); + filterColor.set([r, g, b, 1]); + filterColor.end(); +} + +async function setup() { + if (!(await hasWebGPU())) { + createCanvas(640, 640); + webgpuUnavailable = true; + showWebGPUMessage(); + noLoop(); + return; + } + + await createCanvas(640, 640, WEBGPU); + + // Share a fixed-size array of body points with the GPU. + dataStorage = createStorage(emptyPoints()); + bodyFieldShader = buildFilterShader(bodyField); + bodyPose = await ml5.bodyPose(); + video = createCapture(VIDEO); + video.size(width, height); + video.hide(); + connections = bodyPose.getSkeleton(); + bodyPose.detectStart(video, gotPoses); +} + +function draw() { + if (webgpuUnavailable) return; + if (!connections || !dataStorage) return; + const points = emptyPoints(); + let count = 0; + for (const pose of poses) { + for (const [a, b] of connections) { + if (count >= pointN) break; + const pointA = pose.keypoints[a]; + const pointB = pose.keypoints[b]; + if (pointA.confidence <= 0.1 || pointB.confidence <= 0.1) continue; + + // Use each skeleton segment's midpoint as a point in the field. + const x = (pointA.x + pointB.x) / 2; + const y = (pointA.y + pointB.y) / 2; + + // Normalize to shader coordinates and mirror the webcam horizontally. + points[count++] = { x: 1 - x / width, y: y / height }; + } + } + dataStorage.update(points); + const t = millis() / 1000; + const progress = (frameCount % loopN) / loopN; + bodyFieldShader.setUniform("progress", progress); + bodyFieldShader.setUniform("green", noise(t + 1)); + filter(bodyFieldShader); +} + +// navigator.gpu is missing in browsers without WebGPU. An adapter can also +// be missing when the browser has the API but no usable GPU. +async function hasWebGPU() { + if (!navigator.gpu) return false; + try { + return (await navigator.gpu.requestAdapter()) !== null; + } catch { + return false; + } +} + +function showWebGPUMessage() { + background(255); + fill(30); + noStroke(); + textFont('sans-serif'); + textAlign(CENTER, CENTER); + textSize(18); + textLeading(28); + text( + 'This browser does not support WebGPU,\nso this example cannot run.\nThat is expected, not a bug.', + width / 2, + height / 2 + ); +} + +function emptyPoints() { + // Keep unused points far outside the canvas. + return Array.from({ length: pointN }, () => ({ x: -10, y: -10 })); +} + +function gotPoses(results) { + // Track one person and ease toward each new pose to reduce jitter. + const smoothing = 0.2; + poses = results.slice(0, 1).map((pose, i) => { + const previousPose = poses[i]; + if (!previousPose) return pose; + return { + ...pose, + keypoints: pose.keypoints.map((point, j) => { + const previousPoint = previousPose.keypoints[j]; + if (!previousPoint) return point; + return { + ...point, + x: lerp(previousPoint.x, point.x, smoothing), + y: lerp(previousPoint.y, point.y, smoothing), + confidence: lerp(previousPoint.confidence, point.confidence, smoothing), + }; + }), + }; + }); + + // Merge facial landmarks so the head contributes a single location. + for (const pose of poses) { + const leftEye = pose.keypoints.find((p) => p.name === "left_eye"); + const rightEye = pose.keypoints.find((p) => p.name === "right_eye"); + if (!(leftEye?.confidence > 0.1 && rightEye?.confidence > 0.1)) continue; + const x = (leftEye.x + rightEye.x) / 2; + const y = (leftEye.y + rightEye.y) / 2; + const merged = ["nose", "left_eye", "right_eye", "left_ear", "right_ear"]; + for (const point of pose.keypoints) { + if (merged.includes(point.name)) { + point.x = x; + point.y = y; + } + } + } +} diff --git a/src/content/examples/en/16_Shaders/10_Body_Field/description.mdx b/src/content/examples/en/16_Shaders/10_Body_Field/description.mdx new file mode 100644 index 0000000000..d7eec7f691 --- /dev/null +++ b/src/content/examples/en/16_Shaders/10_Body_Field/description.mdx @@ -0,0 +1,41 @@ +--- +featuredImage: "../../../images/featured/16_Shaders_10_body_field.png" +featuredImageAlt: Soft blue and purple circles trace a person's body against a pink background. +title: Body Field (p5.strands) +oneLineDescription: Turn webcam body tracking into a colorful distance field with p5.strands and ml5.js. +featured: false +webgpu: true +scripts: + - https://unpkg.com/ml5@1/dist/ml5.js + +remix: + - description: Created by + attribution: + - name: Bairui Su + URL: https://bairui.dev/ + code: + - label: Body Field source + URL: https://github.com/processing/p5.js-website/tree/main/src/content/examples/en/16_Shaders/10_Body_Field + - description: Inspired by + attribution: + - name: Golan Levin + URL: https://openprocessing.org/@golan + - name: Dave Pagurek + URL: https://www.davepagurek.com/ + - collectivelyAttributedSince: 2026 +--- + +Move in front of your webcam to shape a colorful field. The ml5.js BodyPose model +tracks your body, and the midpoints of its skeleton connections are passed to a +p5.strands filter shader using a storage buffer. The shader colors each pixel +based on its distance to the nearest point. Pose smoothing keeps the movement fluid. + +This example requires a browser with WebGPU support and access to your webcam. +If WebGPU is unavailable, the canvas shows a message and does not start the camera. +That is expected, not a bug. Allow camera access when prompted, and wait for the body tracking model to load. + +If you copy this sketch into your own HTML file, also include the p5 WebGPU addon and ml5.js. A normal p5 page already has `p5.js`; these two are the extra scripts: +```html + + +``` diff --git a/src/content/examples/images/featured/16_Shaders_10_body_field.png b/src/content/examples/images/featured/16_Shaders_10_body_field.png new file mode 100644 index 0000000000..ceda4c925f Binary files /dev/null and b/src/content/examples/images/featured/16_Shaders_10_body_field.png differ diff --git a/src/layouts/ExampleLayout.astro b/src/layouts/ExampleLayout.astro index 18fb6c8474..cfe49ca141 100644 --- a/src/layouts/ExampleLayout.astro +++ b/src/layouts/ExampleLayout.astro @@ -58,7 +58,7 @@ const editableSketchScripts = Array.isArray(example.data.scripts) ? [`https://cdn.jsdelivr.net/npm/p5.sound@${p5SoundVersion}/dist/p5.sound.js`] : script === "Tone.js" ? ["https://unpkg.com/tone@15.1.22/build/Tone.js"] - : [] + : [script] ) : []; @@ -178,7 +178,11 @@ const remixHistoryHasCodeLinks = remixData?.some( - +

{example.data.arialabel}