Skip to content

Commit a452336

Browse files
committed
v3.0.0
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.
1 parent 92195f1 commit a452336

24 files changed

+3079
-3016
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,82 @@ As the saying goes [*premature optimization is the root of all evil.*](https://s
251251
252252
## Migration
253253
254+
### 2.x -> 3.x
255+
256+
In JavaScript there should be no difference in the API except for the removable of `setDefaultType`.
257+
258+
In TypeScript, 3.x should mostly be type compatible with 2.x.
259+
3.x is an attempt to fix the casting that was necessary in 2.x.
260+
261+
```js
262+
// 2.x
263+
device.queue.writeData(buffer, 0, mat4.identity() as Float32Array); // sadness! 😭
264+
265+
// 3.x
266+
device.queue.writeData(buffer, 0, mat4.identity()); // Yay! 🎉
267+
```
268+
269+
In TypeScript the differences are as follows
270+
271+
#### Functions have a default type but return what is passed to them as the dst.
272+
273+
In 3.x each function has a default type but if you pass it
274+
a destination it returns the type of the destination
275+
276+
```ts
277+
mat4.identity() // returns Float32Array
278+
mat4.identity(new Float32Array(16)); // returns Float32Array
279+
mat4.identity(new Float64Array(16)); // returns Float32Array
280+
mat4.identity(new Array(16)); // returns number[]
281+
```
282+
283+
### Types are specific
284+
285+
```ts
286+
const a: Mat4 = ...; // a = Float32Array
287+
const b: Mat4d = ...; // b = Float64Array
288+
const c: Mat4n = ...; // c = number[]
289+
```
290+
291+
This is means code like this
292+
293+
```ts
294+
const position: Mat4 = [10, 20, 30];
295+
```
296+
297+
No longer works because `Mat4` is a `Float32Array`.
298+
299+
**BUT, functions take any of the normal types as an argument just like they used to**
300+
301+
```js
302+
const position = [10, 20, 30]; // number[]
303+
const target = vec3.create(1, 2, 3); // Float32Array
304+
const up = new Float64Array([0, 1, 0]); // Float64Array
305+
306+
// Works fine, even those types are different, just like 2.x did
307+
const view = mat4.lookAt(position, target, up); // Float32Array
308+
```
309+
310+
If you really want types for each concrete type there's
311+
312+
* `Float32Array` types: `Mat3`, `Mat4`, `Quat`, `Vec2`, `Vec3`, `Vec4`
313+
* `Float64Array` types: `Mat3d`, `Mat4d`, `Quatd`, `Vec2d`, `Vec3d`, `Vec4d`,
314+
* `number[]` types: `Mat3n`, `Mat4n`, `Quatn`, `Vec2n`, `Vec3n`, `Vec4n`
315+
316+
### There are 3 sets of functions, each one returning a different default
317+
318+
```ts
319+
mat4.identity() // returns Float32Array
320+
mat4d.identity() // returns Float64Array
321+
mat4n.identity() // returns number[]
322+
```
323+
324+
Similarly there's `mat3d`, `mat3n`, `quatd`, `quatn`,
325+
`vec2d`, `vec2n`, `vec3d`, `vec3n`, `vec4d`, `vec4n`.
326+
327+
**Note: that in general you're unlikely to need any of these. Just use the
328+
same ones you were using in 2.x**
329+
254330
### 1.x -> 2.x
255331
256332
* [`mat4.lookAt`](https://wgpu-matrix.org/docs/functions/mat4.lookAt.html)

0 commit comments

Comments
 (0)