|
5 | 5 | Fast 3d math library for webgpu
|
6 | 6 |
|
7 | 7 | * [Docs](https://wgpu-matrix.org/docs)
|
8 |
| -* [Repo](https://github.com/greggman/wgpu-matrix) |
| 8 | +* [Github](https://github.com/greggman/wgpu-matrix) |
9 | 9 | * [Tests](https://wgpu-matrix.org/test/)
|
10 | 10 |
|
11 | 11 | ## Why another 3d math library?
|
@@ -146,6 +146,80 @@ import {vec3, mat3} from 'wgpu-matrix';
|
146 | 146 | * [tar](https://github.com/greggman/wgpu-matrix/tarball/main)
|
147 | 147 | * [github](https://github.com/greggman/wgpu-matrix)
|
148 | 148 |
|
| 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 | +
|
149 | 223 | ## Notes
|
150 | 224 |
|
151 | 225 | [`mat4.perspective`](https://wgpu-matrix.org/docs/functions/mat4.perspective.html),
|
@@ -280,7 +354,7 @@ mat4.identity(new Float64Array(16)); // returns Float64Array
|
280 | 354 | mat4.identity(new Array(16)); // returns number[]
|
281 | 355 | ```
|
282 | 356 |
|
283 |
| -### Types are specific |
| 357 | +#### Types are specific |
284 | 358 |
|
285 | 359 | ```ts
|
286 | 360 | const a: Mat4 = ...; // a = Float32Array
|
@@ -313,7 +387,7 @@ If you really want types for each concrete type there's
|
313 | 387 | * `Float64Array` types: `Mat3d`, `Mat4d`, `Quatd`, `Vec2d`, `Vec3d`, `Vec4d`,
|
314 | 388 | * `number[]` types: `Mat3n`, `Mat4n`, `Quatn`, `Vec2n`, `Vec3n`, `Vec4n`
|
315 | 389 |
|
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 |
317 | 391 |
|
318 | 392 | ```ts
|
319 | 393 | mat4.identity() // returns Float32Array
|
|
0 commit comments