Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
This version changes the types of functions so
they return a specific type where as the old functions
returned a union type

old

     mat4.identity() -> Float32Array | Float64Array | number[]

new

     mat4.identity() -> Float32Array

But, these new funcitons will also return what you pass them

     mat4.identity([]) -> number[];
     mat4.identity(new Float64Array(16)) -> Float64Array

Further, they still take varied input

     mat4.lookAt(
       [4, 2, 9], // position
       [0, 0, 0], // target
       [0, 1, 0], // up
    ) -> Float32Array

     mat4.lookAt(
       someFloat64Array,, // position
       someFloat32Array, // target
       [0, 1, 0], // up
    ) -> Float32Array

`setDefaultType` is removed. Instead there are 3 versions
of every type and every API

   mat4  (generates Float32Array by default)
   mat4d (generates Float64Array by default)
   mat4n (generates number[] by default)

   similarly

   mat3, mat3d, mat3n
   vec4, vec4d, vec4n
   vec3, vec3d, vec3n
   vec2, vec2d, vec2n
   quad, quadd, quadn

There are also types

   Mat4  = Float32Array
   Mat4d = Float64Array
   Mat4n = number[]

   And all the corresponding other types

by default, if you use mat4, mat3, vec3, then they
generate Float32Array which you means you can pass them
directly to any function that takes a Float32Array
like `device.queue.writeBuffer` etc...

1. ## Mat4, Vec3, etc are specific types

You can't do this

     const position: Vec3 = [10, 20, 30];  // error, Vec3 is Float32Array

You can do this

     const position: Vec3n = [10, 20, 30];

Or just don't type it

    const positon = [10, 20, 30];

2. ## Bloat

   There's a ton of types and a ton of functions.
   The code isn't bigger. Only the names of the types
   and typed APIs and all their funcitons.
  • Loading branch information
greggman committed Jun 1, 2024
1 parent 92195f1 commit a452336
Show file tree
Hide file tree
Showing 24 changed files with 3,079 additions and 3,016 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,82 @@ As the saying goes [*premature optimization is the root of all evil.*](https://s
## Migration
### 2.x -> 3.x
In JavaScript there should be no difference in the API except for the removable of `setDefaultType`.
In TypeScript, 3.x should mostly be type compatible with 2.x.
3.x is an attempt to fix the casting that was necessary in 2.x.
```js
// 2.x
device.queue.writeData(buffer, 0, mat4.identity() as Float32Array); // sadness! 😭

// 3.x
device.queue.writeData(buffer, 0, mat4.identity()); // Yay! 🎉
```
In TypeScript the differences are as follows
#### Functions have a default type but return what is passed to them as the dst.
In 3.x each function has a default type but if you pass it
a destination it returns the type of the destination
```ts
mat4.identity() // returns Float32Array
mat4.identity(new Float32Array(16)); // returns Float32Array
mat4.identity(new Float64Array(16)); // returns Float32Array
mat4.identity(new Array(16)); // returns number[]
```
### Types are specific
```ts
const a: Mat4 = ...; // a = Float32Array
const b: Mat4d = ...; // b = Float64Array
const c: Mat4n = ...; // c = number[]
```
This is means code like this
```ts
const position: Mat4 = [10, 20, 30];
```
No longer works because `Mat4` is a `Float32Array`.
**BUT, functions take any of the normal types as an argument just like they used to**
```js
const position = [10, 20, 30]; // number[]
const target = vec3.create(1, 2, 3); // Float32Array
const up = new Float64Array([0, 1, 0]); // Float64Array

// Works fine, even those types are different, just like 2.x did
const view = mat4.lookAt(position, target, up); // Float32Array
```
If you really want types for each concrete type there's
* `Float32Array` types: `Mat3`, `Mat4`, `Quat`, `Vec2`, `Vec3`, `Vec4`
* `Float64Array` types: `Mat3d`, `Mat4d`, `Quatd`, `Vec2d`, `Vec3d`, `Vec4d`,
* `number[]` types: `Mat3n`, `Mat4n`, `Quatn`, `Vec2n`, `Vec3n`, `Vec4n`
### There are 3 sets of functions, each one returning a different default
```ts
mat4.identity() // returns Float32Array
mat4d.identity() // returns Float64Array
mat4n.identity() // returns number[]
```
Similarly there's `mat3d`, `mat3n`, `quatd`, `quatn`,
`vec2d`, `vec2n`, `vec3d`, `vec3n`, `vec4d`, `vec4n`.
**Note: that in general you're unlikely to need any of these. Just use the
same ones you were using in 2.x**
### 1.x -> 2.x
* [`mat4.lookAt`](https://wgpu-matrix.org/docs/functions/mat4.lookAt.html)
Expand Down
Loading

0 comments on commit a452336

Please sign in to comment.