Skip to content
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

Using typed arrays instead of JavaScript objects #623

Open
greggman opened this issue Sep 17, 2024 · 0 comments
Open

Using typed arrays instead of JavaScript objects #623

greggman opened this issue Sep 17, 2024 · 0 comments

Comments

@greggman
Copy link

greggman commented Sep 17, 2024

I'm doing lots of WebGPU and I'm finding it difficult to use tweakpane because tweakpane seems to want things in JavaScript objects but I need them in typedarrays.

One example: Color, In WebGPU I have color = new Float32Array(4)

AFAICT, tweakpane wants {r: number, b: number, g: number, a: number}

I can work around this issue by making proxy objects that convert to/from one to the other but it would be really great if I could use the typed arrays directly with tweakpane.

Have you given any thought to supporting this use case or accepting a PR for this use case?

What I'm hoping for is something

const wgslCode = `
  struct Uniforms {
     color: vec3f,
     lightDirection: vec3f,
     color: f32,
  } 
  ...
`;

// Make an ArrayBuffer to contain the uniform values
const uniformValues = new ArrayBuffer(8 * 4);

// Make PARAMS that are views into the ArrayBuffer
const PARAMS = {
  color: new Float32Array(uniformValues, 0, 3),   
  lightDirection: new Float32Array(uniformValues, 16, 3),
  mix: new Float32Array(uniformValues, 28, 1),
};

const pane = new Pane();
pane.addBinding(PARAMS, 'color', { color: { type: 'float' } });
pane.addBinding(PARAMS, 'lightDirection');
pane.addBinding(PARAMS, 'mix', { min: 0, max: 1 });

Right now, those first 2 are problematic. Since color and lightDirection are typed arrays, tweakpane will just error internally. Fixing them outside tweakpane with proxies isn't a huge ton of code but it sure would be nice if tweakpane could somehow handle this because it seems like it would get more and more common

The last one can be changed to

pane.addBinding(PARAMS.mix, '0', { min: 0, max: 1, label: 'mix' });

which is probably fine but if we're dreaming then the first way would be supported. Tweakpane could see the min/max or see it's an array with 1 value and make a Number controller.

wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant