Skip to content

Commit 5c31a02

Browse files
authored
Merge pull request #8800 from harshiltewari2004/strands-random-gaussian
Add p5.strands support for randomGaussian()
2 parents b1e7416 + c02213f commit 5c31a02

11 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/strands/strands_api.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
337337
const originalNoise = fn.noise;
338338
const originalNoiseDetail = fn.noiseDetail;
339339
const originalRandom = fn.random;
340+
const originalRandomGaussian=fn.randomGaussian;
340341
const originalRandomSeed = fn.randomSeed;
341342
const originalMillis = fn.millis;
342343

@@ -480,6 +481,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
480481
}
481482
});
482483

484+
augmentFn(fn, p5, 'randomGaussian', function(...args){
485+
if(!strandsContext.active){
486+
return originalRandomGaussian.apply(this, args);
487+
}
488+
const mean = args.length >= 1 ? args[0] : 0;
489+
const stdDev = args.length >= 2 ? args[1] : 1;
490+
491+
const u1 = this.max(this.random(), 1e-6);
492+
const u2 = this.random();
493+
const z = this.sqrt(this.log(u1).mult(-2)).mult(this.cos(u2.mult(2*Math.PI)));
494+
495+
return z.mult(stdDev).add(mean);
496+
});
497+
483498
augmentFn(fn, p5, 'millis', function (...args) {
484499
if (!strandsContext.active) {
485500
return originalMillis.apply(this, args);

test/unit/visual/cases/webgl.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,44 @@ visualSuite('WebGL', function() {
11331133
screenshot();
11341134
});
11351135

1136+
visualTest('randomGaussian() colors a basic shader', (p5, screenshot) => {
1137+
p5.createCanvas(50, 50, p5.WEBGL);
1138+
const shader = p5.baseColorShader().modify(() => {
1139+
p5.randomSeed(12);
1140+
p5.getFinalColor((color) => {
1141+
const value = p5.randomGaussian(0.5, 0.1);
1142+
color = [value, value, value, 1];
1143+
return color;
1144+
});
1145+
}, { p5 });
1146+
p5.background(0);
1147+
p5.noStroke();
1148+
p5.shader(shader);
1149+
p5.plane(50, 50);
1150+
screenshot();
1151+
});
1152+
1153+
visualTest('randomGaussian() in a fragment loop averages to the mean', (p5, screenshot) => {
1154+
p5.createCanvas(50, 50, p5.WEBGL);
1155+
const shader = p5.baseMaterialShader().modify(() => {
1156+
p5.randomSeed(7);
1157+
p5.getPixelInputs(inputs => {
1158+
let sum = p5.float(0.0);
1159+
for (let i = 0; i < 20; i++) {
1160+
sum = sum + p5.randomGaussian(0.5, 0.2);
1161+
}
1162+
const avg = sum / 20;
1163+
inputs.color = [avg, avg, avg, 1.0];
1164+
return inputs;
1165+
});
1166+
}, { p5 });
1167+
p5.background(0);
1168+
p5.noStroke();
1169+
p5.shader(shader);
1170+
p5.plane(50, 50);
1171+
screenshot();
1172+
});
1173+
11361174
visualTest('uses width/height in getFinalColor', (p5, screenshot) => {
11371175
let firstShader;
11381176
function firstShaderCallback() {

test/unit/visual/cases/webgpu.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,44 @@ visualSuite("WebGPU", function () {
366366
});
367367
});
368368

369+
visualTest('randomGaussian() colors a basic shader (WebGPU)', async function(p5, screenshot) {
370+
await p5.createCanvas(50, 50, p5.WEBGPU);
371+
const shader = p5.baseColorShader().modify(() => {
372+
p5.randomSeed(12);
373+
p5.getFinalColor((color) => {
374+
const value = p5.randomGaussian(0.5, 0.1);
375+
color = [value, value, value, 1];
376+
return color;
377+
});
378+
}, { p5 });
379+
p5.background(0);
380+
p5.noStroke();
381+
p5.shader(shader);
382+
p5.plane(50, 50);
383+
await screenshot();
384+
});
385+
386+
visualTest('randomGaussian() in a fragment loop averages to the mean (WebGPU)', async function(p5, screenshot) {
387+
await p5.createCanvas(50, 50, p5.WEBGPU);
388+
const shader = p5.baseMaterialShader().modify(() => {
389+
p5.randomSeed(7);
390+
p5.getPixelInputs(inputs => {
391+
let sum = p5.float(0.0);
392+
for (let i = 0; i < 20; i++) {
393+
sum = sum + p5.randomGaussian(0.5, 0.2);
394+
}
395+
const avg = sum / 20;
396+
inputs.color = [avg, avg, avg, 1.0];
397+
return inputs;
398+
});
399+
}, { p5 });
400+
p5.background(0);
401+
p5.noStroke();
402+
p5.shader(shader);
403+
p5.plane(50, 50);
404+
await screenshot();
405+
});
406+
369407

370408
visualSuite('filters', function() {
371409
const setupSketch = async (p5) => {
4.41 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3.82 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
4.39 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3.83 KB
Loading

0 commit comments

Comments
 (0)