greggman / wgpu-matrix

Fast WebGPU 3d math library
https://wgpu-matrix.org
MIT License
298 stars 13 forks source link

mat4 functions that reset a specific component (position, rotation, scale) #17

Closed sguimmara closed 1 year ago

sguimmara commented 1 year ago

Maybe I'm missing something, but I don't think it exists yet. It would be great to have a set of functions to set a specific component of a matrix.

i.e mat4.scaling() creates a new scale matrix, mat4.scale()multiplies the scale component by the values. Maybe something like mat4.setScale(src, x, y, z, dst?) ?

greggman commented 1 year ago

What other math library has such a function?

I doesn't seem to make sense to be able to just arbitrarily set 3 elements of a matrix. They aren't the scale for an arbitrary matrix. They're only the scale in specific and very rare situations.

sguimmara commented 1 year ago

I guess most of the time it is implemented in a transform abstraction, rather than a matrix, then compute the matrix from the P, R, S components.

const transform = new Transform();
transform.scale = [1, 2, 3];
transform.position = [4, 5, 6];

const matrix = transform.computeMatrix();

I wanted to avoid the intermediate transform step and manipulate matrices directly, but I guess this would no be convenient.

greggman commented 1 year ago

Sure, I can see the usefulness of a function that takes translation, rotation, and scale.

Unfortunately there's 48 versions of that function. There's 6 orders of operation

t * r * s
t * s * r
s * t * r
s * r * t
r * t * s
r * s * s

Then there's the definition of r which could be one of these 8

xr * yr * zr
yr * xr * zr
xr * zr * yr
yr * zr * xr
zr * xr * yr
zr * yr * yr
axis + rotation
quat

So, do you provide

Either way, I'm not sure I'm personally in the mood to write and test all 48 variations ATM ...

PRs welcome

sguimmara commented 1 year ago

I ended up writing a Transform class and specify an order of operations in this class. This is for the best actually, as wgpu-matrixcan remain lightweight.