Skip to content

Commit bca5733

Browse files
committed
docs
1 parent 81fa51b commit bca5733

File tree

2 files changed

+153
-21
lines changed

2 files changed

+153
-21
lines changed

README.md

+77-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Fast 3d math library for webgpu
66

77
* [Docs](https://wgpu-matrix.org/docs)
8-
* [Repo](https://github.com/greggman/wgpu-matrix)
8+
* [Github](https://github.com/greggman/wgpu-matrix)
99
* [Tests](https://wgpu-matrix.org/test/)
1010

1111
## Why another 3d math library?
@@ -146,6 +146,80 @@ import {vec3, mat3} from 'wgpu-matrix';
146146
* [tar](https://github.com/greggman/wgpu-matrix/tarball/main)
147147
* [github](https://github.com/greggman/wgpu-matrix)
148148

149+
## Types
150+
151+
### wgpu-matrix functions take any compatible type as input.
152+
153+
Examples:
154+
155+
```ts
156+
const view = mat4.lookAt( // view is Float32Array
157+
[10, 20, 30], // position
158+
[0, 5, 0], // target
159+
[0, 1, 0], // up
160+
);
161+
162+
const view2 = mat4.lookAt( // view2 is Float32Array
163+
new Float32Array([10, 20, 30]), // position
164+
new Float64Array([0, 5, 0], // target
165+
[0, 1, 0], // up
166+
);
167+
```
168+
169+
### wgpu-matrix functions return the type passed as the destination or their default
170+
171+
```ts
172+
const a = vec2.add([1, 2], [3, 4]); // a is Float32Array
173+
const b = vec2.add([1, 2], [3, 4], [0, 0]); // b is number[]
174+
175+
const j = vec2d.add([1, 2], [3, 4]); // j is Float64Array
176+
const k = vec2d.add([1, 2], [3, 4], [0, 0]); // b is number[]
177+
178+
const f32 = new Float32Array(2);
179+
const x = vec2d.add([1, 2], [3, 4]); // x is number[]
180+
const y = vec2d.add([1, 2], [3, 4], f32); // y is Float32Array
181+
```
182+
183+
etc...
184+
185+
Note: You're unlikely to need any thing except `mat3`, `mat4`, `quat`,
186+
`vec2`, `vec3`, and `vec4` but, there are 3 sets of functions,
187+
each one returning a different default
188+
189+
```ts
190+
mat4.identity() // returns Float32Array
191+
mat4d.identity() // returns Float64Array
192+
mat4n.identity() // returns number[]
193+
```
194+
195+
Similarly there's `mat3d`, `mat3n`, `quatd`, `quatn`,
196+
`vec2d`, `vec2n`, `vec3d`, `vec3n`, `vec4d`, `vec4n`.
197+
198+
Just to be clear, `identity`, like most functions, takes a destination so
199+
200+
```ts
201+
const f32 = new Float32Array(16);
202+
const f64 = new Float64Array(16);
203+
const arr = new Array<number>(16).fill(0);
204+
205+
mat4.identity() // returns Float32Array
206+
mat4.identity(f32) // returns Float32Array (f32)
207+
mat4.identity(f64) // returns Float64Array (f64)
208+
mat4.identity(arr) // returns number[] (arr)
209+
210+
mat4d.identity() // returns Float64Array
211+
mat4d.identity(f32) // returns Float32Array (f32)
212+
mat4d.identity(f64) // returns Float64Array (f64)
213+
mat4d.identity(arr) // returns number[] (arr)
214+
215+
mat4n.identity() // returns number[]
216+
mat4n.identity(f32) // returns Float32Array (f32)
217+
mat4n.identity(f64) // returns Float64Array (f64)
218+
mat4n.identity(arr) // returns number[] (arr)
219+
```
220+
221+
The only difference between the sets of functions is what type they default to returning.
222+
149223
## Notes
150224
151225
[`mat4.perspective`](https://wgpu-matrix.org/docs/functions/mat4.perspective.html),
@@ -280,7 +354,7 @@ mat4.identity(new Float64Array(16)); // returns Float64Array
280354
mat4.identity(new Array(16)); // returns number[]
281355
```
282356
283-
### Types are specific
357+
#### Types are specific
284358
285359
```ts
286360
const a: Mat4 = ...; // a = Float32Array
@@ -313,7 +387,7 @@ If you really want types for each concrete type there's
313387
* `Float64Array` types: `Mat3d`, `Mat4d`, `Quatd`, `Vec2d`, `Vec3d`, `Vec4d`,
314388
* `number[]` types: `Mat3n`, `Mat4n`, `Quatn`, `Vec2n`, `Vec3n`, `Vec4n`
315389
316-
### There are 3 sets of functions, each one returning a different default
390+
#### There are 3 sets of functions, each one returning a different default
317391
318392
```ts
319393
mat4.identity() // returns Float32Array

src/wgpu-matrix.ts

+76-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Some docs
3+
* @namespace wgpu-matrix
4+
*/
15
import {BaseArgType, ZeroArray} from './types';
26
import {Mat3Arg, Mat3Type, getAPI as getMat3API} from './mat3-impl';
37
import {Mat4Arg, Mat4Type, getAPI as getMat4API} from './mat4-impl';
@@ -86,51 +90,105 @@ function wgpuMatrixAPI<
8690
}
8791

8892
export const {
89-
/** @namespace */
93+
/**
94+
* 4x4 Matrix functions that default to returning `Float32Array`
95+
* @namespace
96+
*/
9097
mat4,
91-
/** @namespace */
98+
/**
99+
* 3x3 Matrix functions that default to returning `Float32Array`
100+
* @namespace
101+
*/
92102
mat3,
93-
/** @namespace */
103+
/**
104+
* Quaternion functions that default to returning `Float32Array`
105+
* @namespace
106+
*/
94107
quat,
95-
/** @namespace */
108+
/**
109+
* Vec2 functions that default to returning `Float32Array`
110+
* @namespace
111+
*/
96112
vec2,
97-
/** @namespace */
113+
/**
114+
* Vec3 functions that default to returning `Float32Array`
115+
* @namespace
116+
*/
98117
vec3,
99-
/** @namespace */
118+
/**
119+
* Vec3 functions that default to returning `Float32Array`
120+
* @namespace
121+
*/
100122
vec4,
101123
} = wgpuMatrixAPI<
102124
Mat3, Mat4, Quat, Vec2, Vec3, Vec4>(
103125
Float32Array, Float32Array, Float32Array, Float32Array, Float32Array, Float32Array);
104126

105127
export const {
106-
/** @namespace */
128+
/**
129+
* 4x4 Matrix functions that default to returning `Float64Array`
130+
* @namespace
131+
*/
107132
mat4: mat4d,
108-
/** @namespace */
133+
/**
134+
* 3x3 Matrix functions that default to returning `Float64Array`
135+
* @namespace
136+
*/
109137
mat3: mat3d,
110-
/** @namespace */
138+
/**
139+
* Quaternion functions that default to returning `Float64Array`
140+
* @namespace
141+
*/
111142
quat: quatd,
112-
/** @namespace */
143+
/**
144+
* Vec2 functions that default to returning `Float64Array`
145+
* @namespace
146+
*/
113147
vec2: vec2d,
114-
/** @namespace */
148+
/**
149+
* Vec3 functions that default to returning `Float64Array`
150+
* @namespace
151+
*/
115152
vec3: vec3d,
116-
/** @namespace */
153+
/**
154+
* Vec3 functions that default to returning `Float64Array`
155+
* @namespace
156+
*/
117157
vec4: vec4d,
118158
} = wgpuMatrixAPI<
119159
Mat3d, Mat4d, Quatd, Vec2d, Vec3d, Vec4d>(
120160
Float64Array, Float64Array, Float64Array, Float64Array, Float64Array, Float64Array);
121161

122162
export const {
123-
/** @namespace */
163+
/**
164+
* 4x4 Matrix functions that default to returning `number[]`
165+
* @namespace
166+
*/
124167
mat4: mat4n,
125-
/** @namespace */
168+
/**
169+
* 3x3 Matrix functions that default to returning `number[]`
170+
* @namespace
171+
*/
126172
mat3: mat3n,
127-
/** @namespace */
173+
/**
174+
* Quaternion functions that default to returning `number[]`
175+
* @namespace
176+
*/
128177
quat: quatn,
129-
/** @namespace */
178+
/**
179+
* Vec2 functions that default to returning `number[]`
180+
* @namespace
181+
*/
130182
vec2: vec2n,
131-
/** @namespace */
183+
/**
184+
* Vec3 functions that default to returning `number[]`
185+
* @namespace
186+
*/
132187
vec3: vec3n,
133-
/** @namespace */
188+
/**
189+
* Vec3 functions that default to returning `number[]`
190+
* @namespace
191+
*/
134192
vec4: vec4n,
135193
} = wgpuMatrixAPI<
136194
Mat3n, Mat4n, Quatn, Vec2n, Vec3n, Vec4n>(

0 commit comments

Comments
 (0)