Skip to content

implementing random function for strands#8730

Open
perminder-17 wants to merge 4 commits intoprocessing:dev-2.0from
perminder-17:random-strands
Open

implementing random function for strands#8730
perminder-17 wants to merge 4 commits intoprocessing:dev-2.0from
perminder-17:random-strands

Conversation

@perminder-17
Copy link
Copy Markdown
Collaborator

@perminder-17 perminder-17 commented Apr 18, 2026

No description provided.

@p5-bot
Copy link
Copy Markdown

p5-bot Bot commented Apr 18, 2026

@ksen0 ksen0 added this to the 2.3 milestone Apr 19, 2026
@ksen0 ksen0 moved this to In Progress in p5.js 2.x 🌱🌳 Apr 19, 2026
@perminder-17 perminder-17 marked this pull request as ready for review April 25, 2026 22:10
@perminder-17
Copy link
Copy Markdown
Collaborator Author

Hi @davepagurek and @ksen0 , sorry for the delay, its open for the review, Can you please check once?

Copy link
Copy Markdown
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on, the structure is looking good! I left one thing about possibly making the call index a runtime thing instead of compile time to better support for loops, and maybe we could add a test case for vertex + fragment shaders too?


const callIndex = strandsContext._randomCallCount++;

const nodeArgs = [seedNode, callIndex];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that means that if you were to call random() in a for loop, e.g.:

let data = uniformStorage(someStorage)

for (let i = 0; i < 5; i++) {
  data[i] += random()
}

...then they'd all get the same random value added to them because random here is just one AST node and would have one fixed call index, translating into something like:

for (var i = 0; i < 5; i++) {
  data[i] += random(seed, 0);
}

Do we have the option of making the call index be a global variable in the shader code so that it's not fixed? e.g something like:

var<private> invocation: i32 = 0;
fn random(seed: f32) -> f32 {
  let val = ...; // Do random gen here, using `invocation`
  invocation++;
  return val;
}

// So then the call site is just:
for (var i = 0; i < 5; i++) {
  data[i] += random(seed);
}

if (shaderType === 'vertex') {
if (!main.match(/\@builtin\s*\(\s*instance_index\s*\)/)) {
main = main.replace(/\)\s*(->|\{)/, ', @builtin(instance_index) instanceID: u32) $1');
const ensuredInstance = ensureBuiltinParam(main, 'instance_index', 'instanceID', 'u32');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work generalizing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants