├── CNAME ├── examples ├── placeholder.js └── placeholder.html ├── .vscode └── settings.json ├── test ├── tests │ ├── wgpu-matrix-test.js │ └── utils-test.js ├── mocha-support.js ├── index.js ├── umd-test.html ├── index.html ├── puppeteer.js ├── assert.js └── mocha.css ├── tsconfig.json ├── dist ├── 1.x │ ├── array-like.d.ts │ ├── wgpu-matrix.d.ts │ ├── utils.d.ts │ ├── vec3.d.ts │ ├── vec2.d.ts │ ├── mat3-impl.d.ts │ ├── vec4-impl.d.ts │ ├── vec2-impl.d.ts │ └── vec3-impl.d.ts └── 2.x │ ├── mat3.d.ts │ ├── mat4.d.ts │ ├── wgpu-matrix.d.ts │ ├── types.d.ts │ ├── mat3.js │ ├── mat4.js │ ├── vec3.d.ts │ ├── utils.d.ts │ ├── vec4.d.ts │ ├── quat.d.ts │ ├── vec2.d.ts │ ├── vec3.js │ ├── vec4.js │ ├── quat.js │ ├── utils.js │ ├── vec2.js │ ├── mat3-impl.d.ts │ ├── quat-impl.d.ts │ ├── vec4-impl.d.ts │ └── vec2-impl.d.ts ├── .gitattributes ├── .gitignore ├── .github └── workflows │ ├── test.yml │ └── build-and-deploy.yml ├── resources ├── js │ └── index.js └── css │ └── style.css ├── src ├── types.ts ├── mat4.ts ├── vec2.ts ├── vec3.ts ├── vec4.ts ├── quat.ts ├── mat3.ts ├── utils.ts └── wgpu-matrix.ts ├── LICENSE.md ├── rollup.config.js ├── package.json ├── .eslintrc.cjs └── README.md /CNAME: -------------------------------------------------------------------------------- 1 | wgpu-matrix.org 2 | -------------------------------------------------------------------------------- /examples/placeholder.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /examples/placeholder.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cSpell.words": [ 3 | "slerp" 4 | ] 5 | } -------------------------------------------------------------------------------- /test/tests/wgpu-matrix-test.js: -------------------------------------------------------------------------------- 1 | import {describe} from '../mocha-support.js'; 2 | 3 | describe('wgpu-matrix', () => { 4 | 5 | }); 6 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/recommended/tsconfig.json", 3 | "compilerOptions": { 4 | "target": "ESNext", 5 | "module": "esnext", 6 | "outDir": "dist/3.x", 7 | }, 8 | "files": [ 9 | "src/wgpu-matrix.ts", 10 | ], 11 | } -------------------------------------------------------------------------------- /dist/1.x/array-like.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Array types for vectors and matrices 3 | */ 4 | export declare type ArrayLike = number[] | Float32Array | Float64Array; 5 | /** 6 | * Constructor function for array types for vectors and matrices 7 | */ 8 | export declare type ArrayLikeCtor = new (n: number) => ArrayLike; 9 | -------------------------------------------------------------------------------- /test/mocha-support.js: -------------------------------------------------------------------------------- 1 | /* global globalThis */ 2 | export const describe = globalThis.describe; 3 | export const it = globalThis.it; 4 | export const before = globalThis.before; 5 | export const after = globalThis.after; 6 | export const beforeEach = globalThis.beforeEach; 7 | export const afterEach = globalThis.afterEach; 8 | 9 | -------------------------------------------------------------------------------- /dist/2.x/mat3.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 12 values, a Float32Array with 12 values, or a Float64Array with 12 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link mat3.setDefaultType}. 5 | */ 6 | export type Mat3 = number[] | Float32Array | Float64Array; 7 | -------------------------------------------------------------------------------- /dist/2.x/mat4.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 16 values, a Float32Array with 16 values, or a Float64Array with 16 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link mat4.setDefaultType}. 5 | */ 6 | export type Mat4 = number[] | Float32Array | Float64Array; 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | 3 | *.md text 4 | *.css text 5 | *.js text 6 | *.html text 7 | 8 | *.jpeg binary 9 | *.dds binary 10 | *.wasm binary 11 | *.tga binary 12 | *.glb binary 13 | *.bin binary 14 | *.psd binary 15 | *.afdesign binary 16 | *.png binary 17 | *.jpg binary 18 | *.gif binary 19 | *.eot binary 20 | *.ttf binary 21 | *.woff binary 22 | *.zip binary 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # -- clip-for-deploy-start -- 3 | 4 | /docs 5 | /dist 6 | index.html 7 | 8 | # -- clip-for-deploy-end -- 9 | 10 | 11 | /.pnp 12 | .pnp.js 13 | 14 | *.pyc 15 | .DS_Store 16 | node_modules 17 | .env.local 18 | .env.development.local 19 | .env.test.local 20 | .env.production.local 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | 27 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 🍔🍟🥤 12 | uses: actions/checkout@v4 13 | with: 14 | persist-credentials: false 15 | 16 | - name: Use Node.js 😂 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: '18.x' 20 | 21 | - name: Test 🧪 22 | run: | 23 | npm ci 24 | npm run check-ci 25 | -------------------------------------------------------------------------------- /resources/js/index.js: -------------------------------------------------------------------------------- 1 | /* global prettyPrint */ 2 | 3 | document.addEventListener("DOMContentLoaded", function () { 4 | Array.prototype.forEach.call(document.querySelectorAll('pre>code'), function (section) { 5 | // Unwrap 6 | const parent = section.parentElement; 7 | while (section.firstChild) { 8 | const child = section.firstChild; 9 | section.removeChild(child); 10 | parent.appendChild(child); 11 | } 12 | parent.removeChild(section); 13 | // Add class 14 | parent.className = `prettyprint showlinemods ${section.className.replace(/language-(\w+)/g, 'lang-$1')}`; 15 | }); 16 | prettyPrint(); 17 | }); 18 | 19 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /* global mocha */ 2 | import './tests/mat3-test.js'; 3 | import './tests/mat4-test.js'; 4 | import './tests/quat-test.js'; 5 | import './tests/vec2-test.js'; 6 | import './tests/vec3-test.js'; 7 | import './tests/vec4-test.js'; 8 | import './tests/utils-test.js'; 9 | import './tests/wgpu-matrix-test.js'; 10 | 11 | const settings = Object.fromEntries(new URLSearchParams(window.location.search).entries()); 12 | if (settings.reporter) { 13 | mocha.reporter(settings.reporter); 14 | } 15 | if (settings.grep) { 16 | mocha.grep(new RegExp(settings.grep, 'i'), false); 17 | } 18 | 19 | mocha.run((failures) => { 20 | window.testsPromiseInfo.resolve(failures); 21 | }); 22 | -------------------------------------------------------------------------------- /dist/1.x/wgpu-matrix.d.ts: -------------------------------------------------------------------------------- 1 | import * as types from './array-like'; 2 | import * as mat3 from './mat3-impl'; 3 | import * as mat4 from './mat4-impl'; 4 | import * as vec2 from './vec2-impl'; 5 | import * as vec3 from './vec3-impl'; 6 | import * as vec4 from './vec4-impl'; 7 | import * as utils from './utils'; 8 | /** 9 | * Sets the type this library creates for all types 10 | * @remarks 11 | * 12 | * example: 13 | * 14 | * ``` 15 | * setDefaultType(Float64Array); 16 | * ``` 17 | * 18 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 19 | */ 20 | export declare function setDefaultType(ctor: new (n: number) => Float32Array | Float64Array | number[]): void; 21 | export { mat3, mat4, types, // for docs 22 | utils, vec2, vec3, vec4, }; 23 | -------------------------------------------------------------------------------- /dist/2.x/wgpu-matrix.d.ts: -------------------------------------------------------------------------------- 1 | import Mat3, * as mat3 from './mat3-impl'; 2 | import Mat4, * as mat4 from './mat4-impl'; 3 | import Quat, * as quat from './quat-impl'; 4 | import Vec2, * as vec2 from './vec2-impl'; 5 | import Vec3, * as vec3 from './vec3-impl'; 6 | import Vec4, * as vec4 from './vec4-impl'; 7 | import * as utils from './utils'; 8 | /** 9 | * Sets the type this library creates for all types 10 | * 11 | * example: 12 | * 13 | * ``` 14 | * setDefaultType(Float64Array); 15 | * ``` 16 | * 17 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 18 | */ 19 | export declare function setDefaultType(ctor: new (n: number) => Float32Array | Float64Array | number[]): void; 20 | export { Mat3, mat3, Mat4, mat4, Quat, quat, utils, Vec2, vec2, Vec3, vec3, Vec4, vec4, }; 21 | -------------------------------------------------------------------------------- /.github/workflows/build-and-deploy.yml: -------------------------------------------------------------------------------- 1 | name: Build and Deploy 2 | on: 3 | push: 4 | tags: 5 | - v* 6 | permissions: 7 | contents: write 8 | jobs: 9 | build-and-deploy: 10 | runs-on: ubuntu-latest 11 | environment: deploy 12 | steps: 13 | - name: Checkout 🛎️ 14 | uses: actions/checkout@v4 15 | 16 | - name: Use Node.js 😂 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: 18 20 | 21 | - name: Install and Build 🔧 22 | run: | 23 | npm ci 24 | npm run build-ci 25 | 26 | - name: Deploy 🚀 27 | uses: JamesIves/github-pages-deploy-action@v4 28 | with: 29 | folder: . 30 | 31 | - name: Publish to NPM 📖 32 | uses: JS-DevTools/npm-publish@v2 33 | with: 34 | token: ${{ secrets.NPM_TOKEN }} 35 | -------------------------------------------------------------------------------- /dist/2.x/types.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * The types you can pass to most functions that take an 3 | * array of numbers. 4 | */ 5 | export type BaseArgType = Float32Array | Float64Array | number[]; 6 | export declare const ZeroArray: { 7 | (arrayLength: number): number[]; 8 | (...items: number[]): number[]; 9 | new (arrayLength: number): number[]; 10 | new (...items: number[]): number[]; 11 | isArray(arg: any): arg is any[]; 12 | readonly prototype: any[]; 13 | from(arrayLike: ArrayLike): T[]; 14 | from(arrayLike: ArrayLike, mapfn: (v: T_1, k: number) => U, thisArg?: any): U[]; 15 | from(iterable: Iterable | ArrayLike): T_2[]; 16 | from(iterable: Iterable | ArrayLike, mapfn: (v: T_3, k: number) => U_1, thisArg?: any): U_1[]; 17 | of(...items: T_4[]): T_4[]; 18 | readonly [Symbol.species]: ArrayConstructor; 19 | }; 20 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A type wider than `number[]`, omitting any instance functions 3 | * unused by the API, e.g., map, sort. This allows the math 4 | * functions to operate on a wider range of array-like 5 | * values. 6 | */ 7 | export interface MutableNumberArray { 8 | readonly length: number; 9 | [n: number]: number; 10 | } 11 | 12 | /** 13 | * The types you can pass to most functions that take an 14 | * array of numbers. 15 | */ 16 | export type BaseArgType = Float32Array | Float64Array | MutableNumberArray; 17 | 18 | function wrapConstructor any>( 19 | OriginalConstructor: T, 20 | modifier: (instance: InstanceType) => void 21 | ): T { 22 | return class extends OriginalConstructor { 23 | constructor(...args: any[]) { 24 | super(...args); 25 | modifier(this as InstanceType); 26 | } 27 | } as T; // Type assertion is necessary here 28 | } 29 | 30 | export const ZeroArray = wrapConstructor(Array, a => a.fill(0)); 31 | 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Gregg Tavares 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /dist/2.x/mat3.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | export {}; 23 | -------------------------------------------------------------------------------- /dist/2.x/mat4.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | export {}; 23 | -------------------------------------------------------------------------------- /test/umd-test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | UMD Test 5 | 6 | 7 | 8 | 43 | 44 | -------------------------------------------------------------------------------- /src/mat4.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | import { BaseArgType } from "./types"; 23 | 24 | /** 25 | * A JavaScript array with 16 values, a Float32Array with 16 values, or a Float64Array with 16 values. 26 | */ 27 | export type Mat4Arg = BaseArgType; 28 | 29 | /** 30 | * A specific concrete 4x4 Matrix Type 31 | */ 32 | export type Mat4Type = T; 33 | 34 | -------------------------------------------------------------------------------- /src/vec2.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | import { BaseArgType } from "./types"; 24 | 25 | /** 26 | * A JavaScript array with 2 values, a Float32Array with 2 values, or a Float64Array with 2 values. 27 | */ 28 | export type Vec2Arg = BaseArgType; 29 | 30 | /** 31 | * A specific concrete 2 element vector. 32 | */ 33 | export type Vec2Type = T; 34 | -------------------------------------------------------------------------------- /src/vec3.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | import { BaseArgType } from "./types"; 24 | 25 | /** 26 | * A JavaScript array with 3 values, a Float32Array with 3 values, or a Float64Array with 3 values. 27 | */ 28 | export type Vec3Arg = BaseArgType; 29 | 30 | /** 31 | * A specific concrete 3 element vector. 32 | */ 33 | export type Vec3Type = T; 34 | -------------------------------------------------------------------------------- /src/vec4.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | import { BaseArgType } from "./types"; 24 | 25 | /** 26 | * A JavaScript array with 4 values, a Float32Array with 4 values, or a Float64Array with 4 values. 27 | */ 28 | export type Vec4Arg = BaseArgType; 29 | 30 | /** 31 | * A specific concrete 4 element vector. 32 | */ 33 | export type Vec4Type = T; 34 | -------------------------------------------------------------------------------- /src/quat.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | import { BaseArgType } from "./types"; 24 | 25 | /** 26 | * A JavaScript array with 4 values, a Float32Array with 4 values, or a Float64Array with 4 values. 27 | */ 28 | export type QuatArg = BaseArgType; 29 | 30 | /** 31 | * A specific concrete 4x4 Matrix Type 32 | */ 33 | export type QuatType = T; 34 | 35 | -------------------------------------------------------------------------------- /src/mat3.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | import { BaseArgType } from "./types"; 24 | 25 | /** 26 | * A JavaScript array with 12 values, a Float32Array with 12 values, or a Float64Array with 12 values. 27 | */ 28 | export type Mat3Arg = BaseArgType; 29 | 30 | /** 31 | * A specific concrete 3x3 element vector. 32 | */ 33 | export type Mat3Type = T; 34 | 35 | -------------------------------------------------------------------------------- /test/tests/utils-test.js: -------------------------------------------------------------------------------- 1 | import {utils} from '../../dist/3.x/wgpu-matrix.module.js'; 2 | 3 | import { 4 | assertEqual, 5 | } from '../assert.js'; 6 | import {describe, it} from '../mocha-support.js'; 7 | 8 | describe('utils', () => { 9 | 10 | it('converts degToRad', () => { 11 | assertEqual(utils.degToRad(0), 0); 12 | assertEqual(utils.degToRad(90), Math.PI * 0.5); 13 | assertEqual(utils.degToRad(180), Math.PI); 14 | assertEqual(utils.degToRad(360), Math.PI * 2); 15 | assertEqual(utils.degToRad(720), Math.PI * 4); 16 | assertEqual(utils.degToRad(-720), Math.PI * -4); 17 | }); 18 | 19 | it('converts radToDeg', () => { 20 | assertEqual(utils.radToDeg(0), 0); 21 | assertEqual(utils.radToDeg(Math.PI * 0.5), 90); 22 | assertEqual(utils.radToDeg(Math.PI), 180); 23 | assertEqual(utils.radToDeg(Math.PI * 2), 360); 24 | assertEqual(utils.radToDeg(Math.PI * 4), 720); 25 | assertEqual(utils.radToDeg(Math.PI * -4), -720); 26 | }); 27 | 28 | it('computes euclideanModulo', () => { 29 | let expected = 1; 30 | for (let i = -5; i <= 5; ++i) { 31 | assertEqual(utils.euclideanModulo(i, 3), expected); 32 | expected = (expected + 1) % 3; 33 | } 34 | }); 35 | 36 | it('lerps', () => { 37 | assertEqual(utils.lerp( 10, 20, 0.5), 15); 38 | assertEqual(utils.lerp(-10, -20, 0.5), -15); 39 | assertEqual(utils.lerp( 10, 20, 1.5), 25); 40 | assertEqual(utils.lerp( 10, 20, -.5), 5); 41 | }); 42 | 43 | it('inverse lerps', () => { 44 | assertEqual(utils.inverseLerp( 10, 20, 15), 0.5); 45 | assertEqual(utils.inverseLerp(-10, -20, -15), 0.5); 46 | assertEqual(utils.inverseLerp( 10, 20, 25), 1.5); 47 | assertEqual(utils.inverseLerp( 10, 20, 5), -.5); 48 | }); 49 | }); -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | wgpu-matrix tests 6 | 7 | 8 | 9 | 10 |
11 | 23 | 24 | 25 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /dist/2.x/vec3.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 3 values, Float32Array with 3 values, or a Float64Array with 3 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link vec3.setDefaultType}. 5 | */ 6 | export type Vec3 = number[] | Float32Array | Float64Array; 7 | /** 8 | * 9 | * Vec3 math functions. 10 | * 11 | * Almost all functions take an optional `dst` argument. If it is not passed in the 12 | * functions will create a new `Vec3`. In other words you can do this 13 | * 14 | * const v = vec3.cross(v1, v2); // Creates a new Vec3 with the cross product of v1 x v2. 15 | * 16 | * or 17 | * 18 | * const v = vec3.create(); 19 | * vec3.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 20 | * 21 | * The first style is often easier but depending on where it's used it generates garbage where 22 | * as there is almost never allocation with the second style. 23 | * 24 | * It is always safe to pass any vector as the destination. So for example 25 | * 26 | * vec3.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 27 | * 28 | */ 29 | export declare let VecType: new (n: number) => Vec3; 30 | /** 31 | * Sets the type this library creates for a Vec3 32 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 33 | * @returns previous constructor for Vec3 34 | */ 35 | export declare function setDefaultType(ctor: new (n: number) => Vec3): new (n: number) => Vec3; 36 | /** 37 | * Creates a vec3; may be called with x, y, z to set initial values. 38 | * @param x - Initial x value. 39 | * @param y - Initial y value. 40 | * @param z - Initial z value. 41 | * @returns the created vector 42 | */ 43 | export declare function create(x?: number, y?: number, z?: number): Vec3; 44 | -------------------------------------------------------------------------------- /test/puppeteer.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import puppeteer from 'puppeteer'; 4 | import path from 'path'; 5 | import express from 'express'; 6 | import url from 'url'; 7 | const app = express(); 8 | const port = 3000; 9 | const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); // eslint-disable-line 10 | 11 | app.use(express.static(path.dirname(__dirname))); 12 | const server = app.listen(port, () => { 13 | console.log(`Example app listening on port ${port}!`); 14 | test(port); 15 | }); 16 | 17 | function makePromiseInfo() { 18 | const info = {}; 19 | const promise = new Promise((resolve, reject) => { 20 | Object.assign(info, {resolve, reject}); 21 | }); 22 | info.promise = promise; 23 | return info; 24 | } 25 | 26 | 27 | async function test(port) { 28 | const browser = await puppeteer.launch(); 29 | const page = await browser.newPage(); 30 | 31 | page.on('console', async e => { 32 | const args = await Promise.all(e.args().map(a => a.jsonValue())); 33 | console.log(...args); 34 | }); 35 | 36 | let totalFailures = 0; 37 | let waitingPromiseInfo; 38 | 39 | // Get the "viewport" of the page, as reported by the page. 40 | page.on('domcontentloaded', async() => { 41 | const failures = await page.evaluate(() => { 42 | return window.testsPromiseInfo.promise; 43 | }); 44 | 45 | totalFailures += failures; 46 | 47 | waitingPromiseInfo.resolve(); 48 | }); 49 | 50 | const urls = [ 51 | `http://localhost:${port}/test/index.html?reporter=spec`, 52 | ]; 53 | 54 | for (const url of urls) { 55 | waitingPromiseInfo = makePromiseInfo(); 56 | await page.goto(url); 57 | await waitingPromiseInfo.promise; 58 | } 59 | 60 | await browser.close(); 61 | server.close(); 62 | 63 | process.exit(totalFailures ? 1 : 0); // eslint-disable-line 64 | } 65 | -------------------------------------------------------------------------------- /dist/1.x/utils.d.ts: -------------------------------------------------------------------------------- 1 | export declare let EPSILON: number; 2 | /** 3 | * Set the value for EPSILON for various checks 4 | * @param v - Value to use for EPSILON. 5 | * @returns previous value of EPSILON; 6 | */ 7 | export declare function setEpsilon(v: number): number; 8 | /** 9 | * Convert degrees to radians 10 | * @param degrees - Angle in degrees 11 | * @returns angle converted to radians 12 | */ 13 | export declare function degToRad(degrees: number): number; 14 | /** 15 | * Convert radians to degrees 16 | * @param radians - Angle in radians 17 | * @returns angle converted to degrees 18 | */ 19 | export declare function radToDeg(radians: number): number; 20 | /** 21 | * Lerps between a and b via t 22 | * @param a - starting value 23 | * @param b - ending value 24 | * @param t - value where 0 = a and 1 = b 25 | * @returns a + (b - a) * t 26 | */ 27 | export declare function lerp(a: number, b: number, t: number): number; 28 | /** 29 | * Compute the opposite of lerp. Given a and b and a value between 30 | * a and b returns a value between 0 and 1. 0 if a, 1 if b. 31 | * Note: no clamping is done. 32 | * @param a - start value 33 | * @param b - end value 34 | * @param v - value between a and b 35 | * @returns (v - a) / (b - a) 36 | */ 37 | export declare function inverseLerp(a: number, b: number, v: number): number; 38 | /** 39 | * Compute the euclidean modulo 40 | * 41 | * ``` 42 | * // table for n / 3 43 | * -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 <- n 44 | * ------------------------------------ 45 | * -2 -1 -0 -2 -1 0, 1, 2, 0, 1, 2 <- n % 3 46 | * 1 2 0 1 2 0, 1, 2, 0, 1, 2 <- euclideanModule(n, 3) 47 | * ``` 48 | * 49 | * @param n - dividend 50 | * @param m - divisor 51 | * @returns the euclidean modulo of n / m 52 | */ 53 | export declare function euclideanModulo(n: number, m: number): number; 54 | -------------------------------------------------------------------------------- /dist/2.x/utils.d.ts: -------------------------------------------------------------------------------- 1 | export declare let EPSILON: number; 2 | /** 3 | * Set the value for EPSILON for various checks 4 | * @param v - Value to use for EPSILON. 5 | * @returns previous value of EPSILON; 6 | */ 7 | export declare function setEpsilon(v: number): number; 8 | /** 9 | * Convert degrees to radians 10 | * @param degrees - Angle in degrees 11 | * @returns angle converted to radians 12 | */ 13 | export declare function degToRad(degrees: number): number; 14 | /** 15 | * Convert radians to degrees 16 | * @param radians - Angle in radians 17 | * @returns angle converted to degrees 18 | */ 19 | export declare function radToDeg(radians: number): number; 20 | /** 21 | * Lerps between a and b via t 22 | * @param a - starting value 23 | * @param b - ending value 24 | * @param t - value where 0 = a and 1 = b 25 | * @returns a + (b - a) * t 26 | */ 27 | export declare function lerp(a: number, b: number, t: number): number; 28 | /** 29 | * Compute the opposite of lerp. Given a and b and a value between 30 | * a and b returns a value between 0 and 1. 0 if a, 1 if b. 31 | * Note: no clamping is done. 32 | * @param a - start value 33 | * @param b - end value 34 | * @param v - value between a and b 35 | * @returns (v - a) / (b - a) 36 | */ 37 | export declare function inverseLerp(a: number, b: number, v: number): number; 38 | /** 39 | * Compute the euclidean modulo 40 | * 41 | * ``` 42 | * // table for n / 3 43 | * -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 <- n 44 | * ------------------------------------ 45 | * -2 -1 -0 -2 -1 0, 1, 2, 0, 1, 2 <- n % 3 46 | * 1 2 0 1 2 0, 1, 2, 0, 1, 2 <- euclideanModule(n, 3) 47 | * ``` 48 | * 49 | * @param n - dividend 50 | * @param m - divisor 51 | * @returns the euclidean modulo of n / m 52 | */ 53 | export declare function euclideanModulo(n: number, m: number): number; 54 | -------------------------------------------------------------------------------- /dist/1.x/vec3.d.ts: -------------------------------------------------------------------------------- 1 | import { ArrayLike } from './array-like'; 2 | /** 3 | * A JavaScript array with 3 values, Float32Array with 3 values, or a Float64Array with 3 values. 4 | * When created by the library will create the default type which is `Float32Array` 5 | * but can be set by calling {@link vec3.setDefaultType}. 6 | */ 7 | export declare type Vec3 = ArrayLike; 8 | /** 9 | * 10 | * Vec3 math functions. 11 | * 12 | * Almost all functions take an optional `dst` argument. If it is not passed in the 13 | * functions will create a new `Vec3`. In other words you can do this 14 | * 15 | * const v = vec3.cross(v1, v2); // Creates a new Vec3 with the cross product of v1 x v2. 16 | * 17 | * or 18 | * 19 | * const v = vec3.create(); 20 | * vec3.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 21 | * 22 | * The first style is often easier but depending on where it's used it generates garbage where 23 | * as there is almost never allocation with the second style. 24 | * 25 | * It is always safe to pass any vector as the destination. So for example 26 | * 27 | * vec3.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 28 | * 29 | */ 30 | export declare let VecType: new (n: number) => Vec3; 31 | /** 32 | * Sets the type this library creates for a Vec3 33 | * @param ctor - the constructor for the type. Either `Float32Array`, 'Float64Array', or `Array` 34 | * @returns previous constructor for Vec3 35 | */ 36 | export declare function setDefaultType(ctor: new (n: number) => Vec3): new (n: number) => ArrayLike; 37 | /** 38 | * Creates a vec3; may be called with x, y, z to set initial values. 39 | * @param x - Initial x value. 40 | * @param y - Initial y value. 41 | * @param z - Initial z value. 42 | * @returns the created vector 43 | */ 44 | export declare function create(x?: number, y?: number, z?: number): ArrayLike; 45 | -------------------------------------------------------------------------------- /dist/2.x/vec4.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 4 values, Float32Array with 4 values, or a Float64Array with 4 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link vec4.setDefaultType}. 5 | */ 6 | export type Vec4 = number[] | Float32Array | Float64Array; 7 | /** 8 | * 9 | * Vec4 math functions. 10 | * 11 | * Almost all functions take an optional `dst` argument. If it is not passed in the 12 | * functions will create a new `Vec4`. In other words you can do this 13 | * 14 | * const v = vec4.cross(v1, v2); // Creates a new Vec4 with the cross product of v1 x v2. 15 | * 16 | * or 17 | * 18 | * const v = vec4.create(); 19 | * vec4.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 20 | * 21 | * The first style is often easier but depending on where it's used it generates garbage where 22 | * as there is almost never allocation with the second style. 23 | * 24 | * It is always safe to pass any vector as the destination. So for example 25 | * 26 | * vec4.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 27 | * 28 | */ 29 | export declare let VecType: new (n: number) => Vec4; 30 | /** 31 | * Sets the type this library creates for a Vec4 32 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 33 | * @returns previous constructor for Vec4 34 | */ 35 | export declare function setDefaultType(ctor: new (n: number) => Vec4): new (n: number) => Vec4; 36 | /** 37 | * Creates a vec4; may be called with x, y, z to set initial values. 38 | * @param x - Initial x value. 39 | * @param y - Initial y value. 40 | * @param z - Initial z value. 41 | * @param w - Initial w value. 42 | * @returns the created vector 43 | */ 44 | export declare function create(x?: number, y?: number, z?: number, w?: number): Vec4; 45 | -------------------------------------------------------------------------------- /dist/2.x/quat.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 4 values, Float32Array with 4 values, or a Float64Array with 4 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link quat.setDefaultType}. 5 | */ 6 | export type Quat = number[] | Float32Array | Float64Array; 7 | /** 8 | * 9 | * Quat4 math functions. 10 | * 11 | * Almost all functions take an optional `dst` argument. If it is not passed in the 12 | * functions will create a new `Quat4`. In other words you can do this 13 | * 14 | * const v = quat4.cross(v1, v2); // Creates a new Quat4 with the cross product of v1 x v2. 15 | * 16 | * or 17 | * 18 | * const v = quat4.create(); 19 | * quat4.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 20 | * 21 | * The first style is often easier but depending on where it's used it generates garbage where 22 | * as there is almost never allocation with the second style. 23 | * 24 | * It is always safe to pass any vector as the destination. So for example 25 | * 26 | * quat4.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 27 | * 28 | */ 29 | export declare let QuatType: new (n: number) => Quat; 30 | /** 31 | * Sets the type this library creates for a Quat4 32 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 33 | * @returns previous constructor for Quat4 34 | */ 35 | export declare function setDefaultType(ctor: new (n: number) => Quat): new (n: number) => Quat; 36 | /** 37 | * Creates a quat4; may be called with x, y, z to set initial values. 38 | * @param x - Initial x value. 39 | * @param y - Initial y value. 40 | * @param z - Initial z value. 41 | * @param w - Initial w value. 42 | * @returns the created vector 43 | */ 44 | export declare function create(x?: number, y?: number, z?: number, w?: number): Quat; 45 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import typescript from '@rollup/plugin-typescript'; 2 | import terser from '@rollup/plugin-terser'; 3 | import fs from 'fs'; 4 | 5 | const pkg = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'})); 6 | const banner = `/* wgpu-matrix@${pkg.version}, license MIT */`; 7 | const ver = `${/^(\d+)\./.exec(pkg.version)[1]}.x`; 8 | 9 | const plugins = [ 10 | typescript({ tsconfig: './tsconfig.json' }), 11 | ]; 12 | 13 | const minPlugins = [ 14 | ...plugins, 15 | terser(), 16 | ]; 17 | 18 | export default [ 19 | { 20 | input: 'src/wgpu-matrix.ts', 21 | output: [ 22 | { 23 | file: `dist/${ver}/wgpu-matrix.module.js`, 24 | format: 'esm', 25 | sourcemap: true, 26 | banner, 27 | freeze: false, 28 | }, 29 | ], 30 | plugins, 31 | }, 32 | { 33 | input: 'src/wgpu-matrix.ts', 34 | output: [ 35 | { 36 | name: 'wgpuMatrix', 37 | file: `dist/${ver}/wgpu-matrix.js`, 38 | format: 'umd', 39 | sourcemap: true, 40 | banner, 41 | freeze: false, 42 | }, 43 | ], 44 | plugins, 45 | }, 46 | { 47 | input: 'src/wgpu-matrix.ts', 48 | output: [ 49 | { 50 | file: `dist/${ver}/wgpu-matrix.module.min.js`, 51 | format: 'esm', 52 | sourcemap: true, 53 | banner, 54 | freeze: false, 55 | }, 56 | ], 57 | plugins: minPlugins, 58 | }, 59 | { 60 | input: 'src/wgpu-matrix.ts', 61 | output: [ 62 | { 63 | name: 'wgpuMatrix', 64 | file: `dist/${ver}/wgpu-matrix.min.js`, 65 | format: 'umd', 66 | sourcemap: true, 67 | banner, 68 | freeze: false, 69 | }, 70 | ], 71 | plugins: minPlugins, 72 | }, 73 | ]; 74 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wgpu-matrix", 3 | "version": "3.4.0", 4 | "description": "fast matrix math library for WebGPU", 5 | "main": "dist/3.x/wgpu-matrix.module.js", 6 | "module": "dist/3.x/wgpu-matrix.module.js", 7 | "types": "dist/3.x/wgpu-matrix.d.ts", 8 | "type": "module", 9 | "sideEffects": false, 10 | "directories": { 11 | "test": "test" 12 | }, 13 | "scripts": { 14 | "build": "rollup -c && tsc --emitDeclarationOnly --declaration", 15 | "build-ci": "npm run pre-push && node build/tools/prep-for-deploy.js", 16 | "check": "npm run lint", 17 | "check-ci": "npm run pre-push", 18 | "docs": "typedoc --disableSources src/wgpu-matrix.ts", 19 | "lint": "eslint \"src/**/*.{js,ts,tsx}\"", 20 | "makeindex": "node build/tools/makeindex.js", 21 | "pre-push": "npm run lint && npm run build && npm run test && npm run docs", 22 | "rollup-watch": "rollup -c -w", 23 | "start": "rollup -c rollup.config.js -w", 24 | "test": "mocha test/tests" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "git+https://github.com/greggman/wgpu-matrix.git" 29 | }, 30 | "files": [ 31 | "dist/3.x/**" 32 | ], 33 | "keywords": [ 34 | "webgpu", 35 | "matrix", 36 | "math", 37 | "gpu", 38 | "3d", 39 | "2d", 40 | "graphics" 41 | ], 42 | "author": "Gregg Tavares", 43 | "license": "MIT", 44 | "bugs": { 45 | "url": "https://github.com/greggman/wgpu-matrix/issues" 46 | }, 47 | "homepage": "https://github.com/greggman/wgpu-matrix#readme", 48 | "devDependencies": { 49 | "@rollup/plugin-terser": "^0.4.4", 50 | "@rollup/plugin-typescript": "^11.1.6", 51 | "@tsconfig/recommended": "^1.0.8", 52 | "@typescript-eslint/eslint-plugin": "^5.62.0", 53 | "@typescript-eslint/parser": "^5.62.0", 54 | "eslint": "^8.57.1", 55 | "eslint-plugin-html": "^7.1.0", 56 | "eslint-plugin-one-variable-per-var": "^0.0.3", 57 | "eslint-plugin-optional-comma-spacing": "^0.0.4", 58 | "eslint-plugin-require-trailing-comma": "^0.0.1", 59 | "express": "^4.21.2", 60 | "mocha": "^10.8.2", 61 | "rollup": "^4.42.0", 62 | "showdown": "^2.1.0", 63 | "tslib": "^2.8.1", 64 | "typedoc": "^0.28.5", 65 | "typescript": "^5.8.3" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /dist/2.x/vec2.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A JavaScript array with 2 values, Float32Array with 2 values, or a Float64Array with 2 values. 3 | * When created by the library will create the default type which is `Float32Array` 4 | * but can be set by calling {@link vec2.setDefaultType}. 5 | */ 6 | export type Vec2 = number[] | Float32Array | Float64Array; 7 | /** 8 | * 9 | * Vec2 math functions. 10 | * 11 | * Almost all functions take an optional `dst` argument. If it is not passed in the 12 | * functions will create a new Vec2. In other words you can do this 13 | * 14 | * const v = vec2.cross(v1, v2); // Creates a new Vec2 with the cross product of v1 x v2. 15 | * 16 | * or 17 | * 18 | * const v = vec2.create(); 19 | * vec2.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 20 | * 21 | * The first style is often easier but depending on where it's used it generates garbage where 22 | * as there is almost never allocation with the second style. 23 | * 24 | * It is always safe to pass any vector as the destination. So for example 25 | * 26 | * vec2.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 27 | * 28 | */ 29 | export declare let VecType: new (n: number) => Vec2; 30 | /** 31 | * Sets the type this library creates for a Vec2 32 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 33 | * @returns previous constructor for Vec2 34 | */ 35 | export declare function setDefaultType(ctor: new (n: number) => Vec2): new (n: number) => Vec2; 36 | /** 37 | * Creates a Vec2; may be called with x, y, z to set initial values. 38 | * 39 | * Note: Since passing in a raw JavaScript array 40 | * is valid in all circumstances, if you want to 41 | * force a JavaScript array into a Vec2's specified type 42 | * it would be faster to use 43 | * 44 | * ``` 45 | * const v = vec2.clone(someJSArray); 46 | * ``` 47 | * 48 | * Note: a consequence of the implementation is if your Vec2Type = `Array` 49 | * instead of `Float32Array` or `Float64Array` then any values you 50 | * don't pass in will be undefined. Usually this is not an issue since 51 | * (a) using `Array` is rare and (b) using `vec2.create` is usually used 52 | * to create a Vec2 to be filled out as in 53 | * 54 | * ``` 55 | * const sum = vec2.create(); 56 | * vec2.add(v1, v2, sum); 57 | * ``` 58 | * 59 | * @param x - Initial x value. 60 | * @param y - Initial y value. 61 | * @returns the created vector 62 | */ 63 | export declare function create(x?: number, y?: number): Vec2; 64 | -------------------------------------------------------------------------------- /dist/1.x/vec2.d.ts: -------------------------------------------------------------------------------- 1 | import { ArrayLike } from './array-like'; 2 | /** 3 | * A JavaScript array with 2 values, Float32Array with 2 values, or a Float64Array with 2 values. 4 | * When created by the library will create the default type which is `Float32Array` 5 | * but can be set by calling {@link vec2.setDefaultType}. 6 | */ 7 | export declare type Vec2 = ArrayLike; 8 | /** 9 | * 10 | * Vec2 math functions. 11 | * 12 | * Almost all functions take an optional `dst` argument. If it is not passed in the 13 | * functions will create a new Vec2. In other words you can do this 14 | * 15 | * const v = vec2.cross(v1, v2); // Creates a new Vec2 with the cross product of v1 x v2. 16 | * 17 | * or 18 | * 19 | * const v = vec2.create(); 20 | * vec2.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 21 | * 22 | * The first style is often easier but depending on where it's used it generates garbage where 23 | * as there is almost never allocation with the second style. 24 | * 25 | * It is always safe to pass any vector as the destination. So for example 26 | * 27 | * vec2.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 28 | * 29 | */ 30 | export declare let VecType: new (n: number) => Vec2; 31 | /** 32 | * Sets the type this library creates for a Vec2 33 | * @param ctor - the constructor for the type. Either `Float32Array`, 'Float64Array', or `Array` 34 | * @returns previous constructor for Vec2 35 | */ 36 | export declare function setDefaultType(ctor: new (n: number) => Vec2): new (n: number) => ArrayLike; 37 | /** 38 | * Creates a Vec2; may be called with x, y, z to set initial values. 39 | * 40 | * Note: Since passing in a raw JavaScript array 41 | * is valid in all circumstances, if you want to 42 | * force a JavaScript array into a Vec2's specified type 43 | * it would be faster to use 44 | * 45 | * ``` 46 | * const v = vec2.clone(someJSArray); 47 | * ``` 48 | * 49 | * Note: a consequence of the implementation is if your Vec2Type = `Array` 50 | * instead of `Float32Array` or `Float64Array` then any values you 51 | * don't pass in will be undefined. Usually this is not an issue since 52 | * (a) using `Array` is rare and (b) using `vec2.create` is usually used 53 | * to create a Vec2 to be filled out as in 54 | * 55 | * ``` 56 | * const sum = vec2.create(); 57 | * vec2.add(v1, v2, sum); 58 | * ``` 59 | * 60 | * @param x - Initial x value. 61 | * @param y - Initial y value. 62 | * @returns the created vector 63 | */ 64 | export declare function create(x?: number, y?: number): Vec2; 65 | -------------------------------------------------------------------------------- /dist/2.x/vec3.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | /** 23 | * 24 | * Vec3 math functions. 25 | * 26 | * Almost all functions take an optional `dst` argument. If it is not passed in the 27 | * functions will create a new `Vec3`. In other words you can do this 28 | * 29 | * const v = vec3.cross(v1, v2); // Creates a new Vec3 with the cross product of v1 x v2. 30 | * 31 | * or 32 | * 33 | * const v = vec3.create(); 34 | * vec3.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 35 | * 36 | * The first style is often easier but depending on where it's used it generates garbage where 37 | * as there is almost never allocation with the second style. 38 | * 39 | * It is always safe to pass any vector as the destination. So for example 40 | * 41 | * vec3.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 42 | * 43 | */ 44 | export let VecType = Float32Array; 45 | /** 46 | * Sets the type this library creates for a Vec3 47 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 48 | * @returns previous constructor for Vec3 49 | */ 50 | export function setDefaultType(ctor) { 51 | const oldType = VecType; 52 | VecType = ctor; 53 | return oldType; 54 | } 55 | /** 56 | * Creates a vec3; may be called with x, y, z to set initial values. 57 | * @param x - Initial x value. 58 | * @param y - Initial y value. 59 | * @param z - Initial z value. 60 | * @returns the created vector 61 | */ 62 | export function create(x, y, z) { 63 | const dst = new VecType(3); 64 | if (x !== undefined) { 65 | dst[0] = x; 66 | if (y !== undefined) { 67 | dst[1] = y; 68 | if (z !== undefined) { 69 | dst[2] = z; 70 | } 71 | } 72 | } 73 | return dst; 74 | } 75 | -------------------------------------------------------------------------------- /dist/2.x/vec4.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | /** 23 | * 24 | * Vec4 math functions. 25 | * 26 | * Almost all functions take an optional `dst` argument. If it is not passed in the 27 | * functions will create a new `Vec4`. In other words you can do this 28 | * 29 | * const v = vec4.cross(v1, v2); // Creates a new Vec4 with the cross product of v1 x v2. 30 | * 31 | * or 32 | * 33 | * const v = vec4.create(); 34 | * vec4.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 35 | * 36 | * The first style is often easier but depending on where it's used it generates garbage where 37 | * as there is almost never allocation with the second style. 38 | * 39 | * It is always safe to pass any vector as the destination. So for example 40 | * 41 | * vec4.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 42 | * 43 | */ 44 | export let VecType = Float32Array; 45 | /** 46 | * Sets the type this library creates for a Vec4 47 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 48 | * @returns previous constructor for Vec4 49 | */ 50 | export function setDefaultType(ctor) { 51 | const oldType = VecType; 52 | VecType = ctor; 53 | return oldType; 54 | } 55 | /** 56 | * Creates a vec4; may be called with x, y, z to set initial values. 57 | * @param x - Initial x value. 58 | * @param y - Initial y value. 59 | * @param z - Initial z value. 60 | * @param w - Initial w value. 61 | * @returns the created vector 62 | */ 63 | export function create(x, y, z, w) { 64 | const dst = new VecType(4); 65 | if (x !== undefined) { 66 | dst[0] = x; 67 | if (y !== undefined) { 68 | dst[1] = y; 69 | if (z !== undefined) { 70 | dst[2] = z; 71 | if (w !== undefined) { 72 | dst[3] = w; 73 | } 74 | } 75 | } 76 | } 77 | return dst; 78 | } 79 | -------------------------------------------------------------------------------- /dist/2.x/quat.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | /** 23 | * 24 | * Quat4 math functions. 25 | * 26 | * Almost all functions take an optional `dst` argument. If it is not passed in the 27 | * functions will create a new `Quat4`. In other words you can do this 28 | * 29 | * const v = quat4.cross(v1, v2); // Creates a new Quat4 with the cross product of v1 x v2. 30 | * 31 | * or 32 | * 33 | * const v = quat4.create(); 34 | * quat4.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 35 | * 36 | * The first style is often easier but depending on where it's used it generates garbage where 37 | * as there is almost never allocation with the second style. 38 | * 39 | * It is always safe to pass any vector as the destination. So for example 40 | * 41 | * quat4.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 42 | * 43 | */ 44 | export let QuatType = Float32Array; 45 | /** 46 | * Sets the type this library creates for a Quat4 47 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 48 | * @returns previous constructor for Quat4 49 | */ 50 | export function setDefaultType(ctor) { 51 | const oldType = QuatType; 52 | QuatType = ctor; 53 | return oldType; 54 | } 55 | /** 56 | * Creates a quat4; may be called with x, y, z to set initial values. 57 | * @param x - Initial x value. 58 | * @param y - Initial y value. 59 | * @param z - Initial z value. 60 | * @param w - Initial w value. 61 | * @returns the created vector 62 | */ 63 | export function create(x, y, z, w) { 64 | const dst = new QuatType(4); 65 | if (x !== undefined) { 66 | dst[0] = x; 67 | if (y !== undefined) { 68 | dst[1] = y; 69 | if (z !== undefined) { 70 | dst[2] = z; 71 | if (w !== undefined) { 72 | dst[3] = w; 73 | } 74 | } 75 | } 76 | } 77 | return dst; 78 | } 79 | -------------------------------------------------------------------------------- /dist/2.x/utils.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | export let EPSILON = 0.000001; 23 | /** 24 | * Set the value for EPSILON for various checks 25 | * @param v - Value to use for EPSILON. 26 | * @returns previous value of EPSILON; 27 | */ 28 | export function setEpsilon(v) { 29 | const old = EPSILON; 30 | EPSILON = v; 31 | return old; 32 | } 33 | /** 34 | * Convert degrees to radians 35 | * @param degrees - Angle in degrees 36 | * @returns angle converted to radians 37 | */ 38 | export function degToRad(degrees) { 39 | return degrees * Math.PI / 180; 40 | } 41 | /** 42 | * Convert radians to degrees 43 | * @param radians - Angle in radians 44 | * @returns angle converted to degrees 45 | */ 46 | export function radToDeg(radians) { 47 | return radians * 180 / Math.PI; 48 | } 49 | /** 50 | * Lerps between a and b via t 51 | * @param a - starting value 52 | * @param b - ending value 53 | * @param t - value where 0 = a and 1 = b 54 | * @returns a + (b - a) * t 55 | */ 56 | export function lerp(a, b, t) { 57 | return a + (b - a) * t; 58 | } 59 | /** 60 | * Compute the opposite of lerp. Given a and b and a value between 61 | * a and b returns a value between 0 and 1. 0 if a, 1 if b. 62 | * Note: no clamping is done. 63 | * @param a - start value 64 | * @param b - end value 65 | * @param v - value between a and b 66 | * @returns (v - a) / (b - a) 67 | */ 68 | export function inverseLerp(a, b, v) { 69 | const d = b - a; 70 | return (Math.abs(b - a) < EPSILON) 71 | ? a 72 | : (v - a) / d; 73 | } 74 | /** 75 | * Compute the euclidean modulo 76 | * 77 | * ``` 78 | * // table for n / 3 79 | * -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 <- n 80 | * ------------------------------------ 81 | * -2 -1 -0 -2 -1 0, 1, 2, 0, 1, 2 <- n % 3 82 | * 1 2 0 1 2 0, 1, 2, 0, 1, 2 <- euclideanModule(n, 3) 83 | * ``` 84 | * 85 | * @param n - dividend 86 | * @param m - divisor 87 | * @returns the euclidean modulo of n / m 88 | */ 89 | export function euclideanModulo(n, m) { 90 | return ((n % m) + m) % m; 91 | } 92 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | export let EPSILON = 0.000001; 24 | 25 | /** 26 | * Set the value for EPSILON for various checks 27 | * @param v - Value to use for EPSILON. 28 | * @returns previous value of EPSILON; 29 | */ 30 | export function setEpsilon(v: number): number { 31 | const old = EPSILON; 32 | EPSILON = v; 33 | return old; 34 | } 35 | 36 | /** 37 | * Convert degrees to radians 38 | * @param degrees - Angle in degrees 39 | * @returns angle converted to radians 40 | */ 41 | export function degToRad(degrees: number): number { 42 | return degrees * Math.PI / 180; 43 | } 44 | 45 | /** 46 | * Convert radians to degrees 47 | * @param radians - Angle in radians 48 | * @returns angle converted to degrees 49 | */ 50 | export function radToDeg(radians: number): number { 51 | return radians * 180 / Math.PI; 52 | } 53 | 54 | /** 55 | * Lerps between a and b via t 56 | * @param a - starting value 57 | * @param b - ending value 58 | * @param t - value where 0 = a and 1 = b 59 | * @returns a + (b - a) * t 60 | */ 61 | export function lerp(a: number, b: number, t: number): number { 62 | return a + (b - a) * t; 63 | } 64 | 65 | /** 66 | * Compute the opposite of lerp. Given a and b and a value between 67 | * a and b returns a value between 0 and 1. 0 if a, 1 if b. 68 | * Note: no clamping is done. 69 | * @param a - start value 70 | * @param b - end value 71 | * @param v - value between a and b 72 | * @returns (v - a) / (b - a) 73 | */ 74 | export function inverseLerp(a: number, b: number, v: number): number { 75 | const d = b - a; 76 | return (Math.abs(b - a) < EPSILON) 77 | ? a 78 | : (v - a) / d; 79 | } 80 | 81 | /** 82 | * Compute the euclidean modulo 83 | * 84 | * ``` 85 | * // table for n / 3 86 | * -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 <- n 87 | * ------------------------------------ 88 | * -2 -1 -0 -2 -1 0, 1, 2, 0, 1, 2 <- n % 3 89 | * 1 2 0 1 2 0, 1, 2, 0, 1, 2 <- euclideanModule(n, 3) 90 | * ``` 91 | * 92 | * @param n - dividend 93 | * @param m - divisor 94 | * @returns the euclidean modulo of n / m 95 | */ 96 | export function euclideanModulo(n: number, m: number) { 97 | return ((n % m) + m) % m; 98 | } -------------------------------------------------------------------------------- /dist/2.x/vec2.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Gregg Tavares 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | /** 23 | * 24 | * Vec2 math functions. 25 | * 26 | * Almost all functions take an optional `dst` argument. If it is not passed in the 27 | * functions will create a new Vec2. In other words you can do this 28 | * 29 | * const v = vec2.cross(v1, v2); // Creates a new Vec2 with the cross product of v1 x v2. 30 | * 31 | * or 32 | * 33 | * const v = vec2.create(); 34 | * vec2.cross(v1, v2, v); // Puts the cross product of v1 x v2 in v 35 | * 36 | * The first style is often easier but depending on where it's used it generates garbage where 37 | * as there is almost never allocation with the second style. 38 | * 39 | * It is always safe to pass any vector as the destination. So for example 40 | * 41 | * vec2.cross(v1, v2, v1); // Puts the cross product of v1 x v2 in v1 42 | * 43 | */ 44 | export let VecType = Float32Array; 45 | /** 46 | * Sets the type this library creates for a Vec2 47 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 48 | * @returns previous constructor for Vec2 49 | */ 50 | export function setDefaultType(ctor) { 51 | const oldType = VecType; 52 | VecType = ctor; 53 | return oldType; 54 | } 55 | /** 56 | * Creates a Vec2; may be called with x, y, z to set initial values. 57 | * 58 | * Note: Since passing in a raw JavaScript array 59 | * is valid in all circumstances, if you want to 60 | * force a JavaScript array into a Vec2's specified type 61 | * it would be faster to use 62 | * 63 | * ``` 64 | * const v = vec2.clone(someJSArray); 65 | * ``` 66 | * 67 | * Note: a consequence of the implementation is if your Vec2Type = `Array` 68 | * instead of `Float32Array` or `Float64Array` then any values you 69 | * don't pass in will be undefined. Usually this is not an issue since 70 | * (a) using `Array` is rare and (b) using `vec2.create` is usually used 71 | * to create a Vec2 to be filled out as in 72 | * 73 | * ``` 74 | * const sum = vec2.create(); 75 | * vec2.add(v1, v2, sum); 76 | * ``` 77 | * 78 | * @param x - Initial x value. 79 | * @param y - Initial y value. 80 | * @returns the created vector 81 | */ 82 | export function create(x = 0, y = 0) { 83 | const dst = new VecType(2); 84 | if (x !== undefined) { 85 | dst[0] = x; 86 | if (y !== undefined) { 87 | dst[1] = y; 88 | } 89 | } 90 | return dst; 91 | } 92 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | /* global module */ 2 | module.exports = { 3 | parser: '@typescript-eslint/parser', 4 | env: { 5 | browser: true, 6 | es2022: true, 7 | }, 8 | parserOptions: { 9 | sourceType: 'module', 10 | ecmaVersion: 2022, 11 | }, 12 | plugins: [ 13 | 'eslint-plugin-html', 14 | 'eslint-plugin-optional-comma-spacing', 15 | 'eslint-plugin-one-variable-per-var', 16 | 'eslint-plugin-require-trailing-comma', 17 | ], 18 | extends: [ 19 | 'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin 20 | ], 21 | rules: { 22 | 'brace-style': [2, '1tbs', { allowSingleLine: false }], 23 | camelcase: [0], 24 | 'comma-dangle': 0, 25 | 'comma-spacing': 0, 26 | 'comma-style': [2, 'last'], 27 | 'consistent-return': 2, 28 | curly: [2, 'all'], 29 | 'dot-notation': 0, 30 | 'eol-last': [0], 31 | eqeqeq: 2, 32 | 'global-strict': [0], 33 | 'key-spacing': [0], 34 | 'keyword-spacing': [1, { before: true, after: true, overrides: {} }], 35 | 'new-cap': 2, 36 | 'new-parens': 2, 37 | 'no-alert': 2, 38 | 'no-array-constructor': 2, 39 | 'no-caller': 2, 40 | 'no-catch-shadow': 2, 41 | 'no-comma-dangle': [0], 42 | 'no-const-assign': 2, 43 | 'no-eval': 2, 44 | 'no-extend-native': 2, 45 | 'no-extra-bind': 2, 46 | 'no-extra-parens': [2, 'functions'], 47 | 'no-implied-eval': 2, 48 | 'no-irregular-whitespace': 2, 49 | 'no-iterator': 2, 50 | 'no-label-var': 2, 51 | 'no-labels': 2, 52 | 'no-lone-blocks': 2, 53 | 'no-loop-func': 2, 54 | 'no-multi-spaces': [0], 55 | 'no-multi-str': 2, 56 | 'no-native-reassign': 2, 57 | 'no-new-func': 2, 58 | 'no-new-object': 2, 59 | 'no-new-wrappers': 2, 60 | 'no-new': 2, 61 | 'no-obj-calls': 2, 62 | 'no-octal-escape': 2, 63 | 'no-process-exit': 2, 64 | 'no-proto': 2, 65 | 'no-return-assign': 2, 66 | 'no-script-url': 2, 67 | 'no-sequences': 2, 68 | 'no-shadow-restricted-names': 2, 69 | 'no-shadow': [0], 70 | 'no-spaced-func': 2, 71 | 'no-trailing-spaces': 2, 72 | 'no-undef-init': 2, 73 | 'no-undef': 2, 74 | 'no-underscore-dangle': 2, 75 | 'no-unreachable': 2, 76 | 'no-unused-expressions': 2, 77 | 'no-use-before-define': 0, 78 | 'no-var': 2, 79 | 'no-with': 2, 80 | 'one-variable-per-var/one-variable-per-var': [2], 81 | 'optional-comma-spacing/optional-comma-spacing': [2, { after: true }], 82 | 'prefer-const': 2, 83 | 'require-trailing-comma/require-trailing-comma': [2], 84 | 'semi-spacing': [2, { before: false, after: true }], 85 | semi: [2, 'always'], 86 | 'space-before-function-paren': [ 87 | 2, 88 | { 89 | anonymous: 'always', 90 | named: 'never', 91 | asyncArrow: 'always', 92 | }, 93 | ], 94 | 'space-infix-ops': 2, 95 | 'space-unary-ops': [2, { words: true, nonwords: false }], 96 | strict: [2, 'function'], 97 | yoda: [2, 'never'], 98 | '@typescript-eslint/no-empty-function': 'off', 99 | '@typescript-eslint/no-explicit-any': 'off', // TODO: Reenable this and figure out how to fix code. 100 | '@typescript-eslint/no-non-null-assertion': 'off', 101 | '@typescript-eslint/no-unused-vars': 2, 102 | }, 103 | overrides: [ 104 | { 105 | files: ['*.ts', '*.tsx', 'src/samples/**/*', 'example/**/*'], 106 | rules: { 107 | 'no-undef': 'off', 108 | }, 109 | }, 110 | ], 111 | }; 112 | -------------------------------------------------------------------------------- /src/wgpu-matrix.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Some docs 3 | * @namespace wgpu-matrix 4 | */ 5 | import {MutableNumberArray, BaseArgType, ZeroArray} from './types'; 6 | import {Mat3Arg, Mat3Type, getAPI as getMat3API} from './mat3-impl'; 7 | import {Mat4Arg, Mat4Type, getAPI as getMat4API} from './mat4-impl'; 8 | import {QuatArg, QuatType, getAPI as getQuatAPI, RotationOrder} from './quat-impl'; 9 | import {Vec2Arg, Vec2Type, getAPI as getVec2API} from './vec2-impl'; 10 | import {Vec3Arg, Vec3Type, getAPI as getVec3API} from './vec3-impl'; 11 | import {Vec4Arg, Vec4Type, getAPI as getVec4API} from './vec4-impl'; 12 | import * as utils from './utils'; 13 | 14 | export { 15 | RotationOrder, 16 | utils, 17 | 18 | MutableNumberArray, 19 | BaseArgType, 20 | 21 | Mat3Arg, 22 | Mat4Arg, 23 | QuatArg, 24 | Vec2Arg, 25 | Vec3Arg, 26 | Vec4Arg, 27 | 28 | Mat3Type, 29 | Mat4Type, 30 | QuatType, 31 | Vec2Type, 32 | Vec3Type, 33 | Vec4Type, 34 | }; 35 | 36 | export type BaseCtor = new (n: number) => T; 37 | 38 | export type Mat3 = Mat3Type; 39 | export type Mat4 = Mat4Type; 40 | export type Quat = QuatType; 41 | export type Vec2 = Vec2Type; 42 | export type Vec3 = Vec3Type; 43 | export type Vec4 = Vec4Type; 44 | 45 | export type Mat3d = Mat3Type; 46 | export type Mat4d = Mat4Type; 47 | export type Quatd = QuatType; 48 | export type Vec2d = Vec2Type; 49 | export type Vec3d = Vec3Type; 50 | export type Vec4d = Vec4Type; 51 | 52 | export type Mat3n = Mat3Type; 53 | export type Mat4n = Mat4Type; 54 | export type Quatn = QuatType; 55 | export type Vec2n = Vec2Type; 56 | export type Vec3n = Vec3Type; 57 | export type Vec4n = Vec4Type; 58 | 59 | /** 60 | * Generate wgpu-matrix API for type 61 | */ 62 | function wgpuMatrixAPI< 63 | Mat3 extends BaseArgType, 64 | Mat4 extends BaseArgType, 65 | Quat extends BaseArgType, 66 | Vec2 extends BaseArgType, 67 | Vec3 extends BaseArgType, 68 | Vec4 extends BaseArgType, 69 | >( 70 | Mat3Ctor: BaseCtor, 71 | Mat4Ctor: BaseCtor, 72 | QuatCtor: BaseCtor, 73 | Vec2Ctor: BaseCtor, 74 | Vec3Ctor: BaseCtor, 75 | Vec4Ctor: BaseCtor, 76 | ) { 77 | return { 78 | /** @namespace mat3 */ 79 | mat3: getMat3API(Mat3Ctor), 80 | /** @namespace mat4 */ 81 | mat4: getMat4API(Mat4Ctor), 82 | /** @namespace quat */ 83 | quat: getQuatAPI(QuatCtor), 84 | /** @namespace vec2 */ 85 | vec2: getVec2API(Vec2Ctor), 86 | /** @namespace vec3 */ 87 | vec3: getVec3API(Vec3Ctor), 88 | /** @namespace vec4 */ 89 | vec4: getVec4API(Vec4Ctor), 90 | }; 91 | } 92 | 93 | export const { 94 | /** 95 | * 3x3 Matrix functions that default to returning `Float32Array` 96 | * @namespace 97 | */ 98 | mat3, 99 | /** 100 | * 4x4 Matrix functions that default to returning `Float32Array` 101 | * @namespace 102 | */ 103 | mat4, 104 | /** 105 | * Quaternion functions that default to returning `Float32Array` 106 | * @namespace 107 | */ 108 | quat, 109 | /** 110 | * Vec2 functions that default to returning `Float32Array` 111 | * @namespace 112 | */ 113 | vec2, 114 | /** 115 | * Vec3 functions that default to returning `Float32Array` 116 | * @namespace 117 | */ 118 | vec3, 119 | /** 120 | * Vec3 functions that default to returning `Float32Array` 121 | * @namespace 122 | */ 123 | vec4, 124 | } = wgpuMatrixAPI< 125 | Mat3, Mat4, Quat, Vec2, Vec3, Vec4>( 126 | Float32Array, Float32Array, Float32Array, Float32Array, Float32Array, Float32Array); 127 | 128 | export const { 129 | /** 130 | * 3x3 Matrix functions that default to returning `Float64Array` 131 | * @namespace 132 | */ 133 | mat3: mat3d, 134 | /** 135 | * 4x4 Matrix functions that default to returning `Float64Array` 136 | * @namespace 137 | */ 138 | mat4: mat4d, 139 | /** 140 | * Quaternion functions that default to returning `Float64Array` 141 | * @namespace 142 | */ 143 | quat: quatd, 144 | /** 145 | * Vec2 functions that default to returning `Float64Array` 146 | * @namespace 147 | */ 148 | vec2: vec2d, 149 | /** 150 | * Vec3 functions that default to returning `Float64Array` 151 | * @namespace 152 | */ 153 | vec3: vec3d, 154 | /** 155 | * Vec3 functions that default to returning `Float64Array` 156 | * @namespace 157 | */ 158 | vec4: vec4d, 159 | } = wgpuMatrixAPI< 160 | Mat3d, Mat4d, Quatd, Vec2d, Vec3d, Vec4d>( 161 | Float64Array, Float64Array, Float64Array, Float64Array, Float64Array, Float64Array); 162 | 163 | export const { 164 | /** 165 | * 3x3 Matrix functions that default to returning `number[]` 166 | * @namespace 167 | */ 168 | mat3: mat3n, 169 | /** 170 | * 4x4 Matrix functions that default to returning `number[]` 171 | * @namespace 172 | */ 173 | mat4: mat4n, 174 | /** 175 | * Quaternion functions that default to returning `number[]` 176 | * @namespace 177 | */ 178 | quat: quatn, 179 | /** 180 | * Vec2 functions that default to returning `number[]` 181 | * @namespace 182 | */ 183 | vec2: vec2n, 184 | /** 185 | * Vec3 functions that default to returning `number[]` 186 | * @namespace 187 | */ 188 | vec3: vec3n, 189 | /** 190 | * Vec3 functions that default to returning `number[]` 191 | * @namespace 192 | */ 193 | vec4: vec4n, 194 | } = wgpuMatrixAPI< 195 | Mat3n, Mat4n, Quatn, Vec2n, Vec3n, Vec4n>( 196 | ZeroArray, Array, Array, Array, Array, Array); 197 | -------------------------------------------------------------------------------- /resources/css/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | } 4 | 5 | iframe { 6 | border: none; 7 | display: block; 8 | position: fixed; 9 | left: 0; 10 | top: 0; 11 | width: 100%; 12 | height: 100%; 13 | z-index: -1; 14 | } 15 | 16 | body { 17 | margin: 0; 18 | font-family: Helvetica, Arial, sanf-serif; 19 | line-height: 1.5em; 20 | } 21 | html { 22 | background-color: rgb(51, 102, 204); 23 | } 24 | 25 | .nav ul { 26 | list-style: none; 27 | background-color: #444; 28 | padding: 0; 29 | margin: 0; 30 | } 31 | .nav li { 32 | line-height: 2em; 33 | font-family: sans-serif; 34 | font-size: 1.2em; 35 | border-bottom: 1px solid #888; 36 | } 37 | 38 | .nav a { 39 | padding-left: 1em; 40 | padding-right: 1em; 41 | text-decoration: none; 42 | color: #fff; 43 | display: block; 44 | transition: .3s background-color; 45 | } 46 | 47 | .nav a:hover { 48 | background-color: #8AF; 49 | color: #000; 50 | } 51 | 52 | .nav a.active { 53 | background-color: #fff; 54 | color: #444; 55 | cursor: default; 56 | } 57 | 58 | @media screen and (min-width: 600px) { 59 | .nav li { 60 | border-bottom: none; 61 | font-size: 1.4em; 62 | } 63 | 64 | /* Option 1 - Display Inline */ 65 | .nav li { 66 | display: inline-block; 67 | margin-right: -4px; 68 | } 69 | } 70 | 71 | 72 | @media screen and (max-width: 400px) { 73 | ul { 74 | padding-left: 1em; 75 | } 76 | } 77 | 78 | h1 { 79 | line-height: 1.5em; 80 | position: relative; 81 | } 82 | h2,h3 { 83 | margin-top: 1.5em; 84 | } 85 | 86 | #pronounce { 87 | position: absolute; 88 | font-size: xx-small; 89 | bottom: 0em; 90 | left: 0px; 91 | line-height: 1em; 92 | height: 1em; 93 | display: block; 94 | } 95 | 96 | #frame { 97 | margin: 10px; 98 | max-width: 800px; 99 | margin: auto; 100 | background-color: rgba(255, 255, 255, 0.9); 101 | } 102 | #content { 103 | padding: 20px; 104 | } 105 | 106 | code { 107 | background-color: #DDD; 108 | color: black; 109 | font-family: monospace; 110 | font-size: larger; 111 | padding: 0.2em; 112 | } 113 | 114 | pre { 115 | background-color: #CCC; 116 | overflow-x: auto; 117 | font-size: small; 118 | } 119 | 120 | .lang-javascript { 121 | font-size: small; 122 | background-color: #CCC; 123 | display: block; 124 | padding: 1em; 125 | line-height: 1.2em; 126 | border: 1px solid #000; 127 | box-shadow: 3px 3px 10px #ccc; 128 | font-family: "Lucida Console", Monaco, monospace; 129 | } 130 | 131 | /* --- Prettify --- */ 132 | pre.prettyprint .nocode { background-color: none; color: #000 } 133 | pre.prettyprint .str { color: #080 } /* string */ 134 | pre.prettyprint .kwd { color: #008 } /* keyword */ 135 | pre.prettyprint .com { color: #800 } /* comment */ 136 | pre.prettyprint .typ { color: #606 } /* type */ 137 | pre.prettyprint .lit { color: #066 } /* literal */ 138 | pre.prettyprint .pun { color: #660 } /* punctuation */ 139 | pre.prettyprint .pln { color: #000 } /* plaintext */ 140 | pre.prettyprint .tag { color: #008 } /* html/xml tag */ 141 | pre.prettyprint .atn { color: #606 } /* attribute name */ 142 | pre.prettyprint .atv { color: #080 } /* attribute value */ 143 | pre.prettyprint .dec { color: #606 } /* decimal */ 144 | pre.prettyprint .var { color: #606 } /* variable name */ 145 | pre.prettyprint .fun { color: #F00 } /* function name */ 146 | 147 | pre.prettyprint ul.modifiedlines { 148 | list-style-type: none; 149 | padding-left: 0; 150 | } 151 | pre.prettyprint ul.modifiedlines li.linemodified { 152 | list-style-type: none; 153 | background-color: #A1EAF6; 154 | } 155 | pre.prettyprint ul.modifiedlines li.linedeleted { 156 | list-style-type: none; 157 | background-color: #F0B9B9; 158 | text-decoration: line-through; 159 | } 160 | 161 | pre.prettyprint ul.modifiedlines li.lineadded { 162 | list-style-type: none; 163 | background-color: #A2EDC9; 164 | } 165 | 166 | 167 | pre.prettyprint, code.prettyprint { 168 | color: #000; 169 | background: #ddd; 170 | border: 1px solid #000; 171 | box-shadow: 3px 3px 10px #ccc; 172 | font-family: "Lucida Console", Monaco, monospace; 173 | width: 95%; 174 | margin: auto; 175 | padding: 1em; 176 | text-align: left; /* override justify on body */ 177 | goverflow: auto; /* allow scroll bar in case of long lines - goes together with white-space: nowrap! */ 178 | white-space: pre; /* was nowrap, prevent line wrapping */ 179 | line-height: 1.2em; 180 | } 181 | @media print{ 182 | pre.prettyprint .str, code.prettyprint .str{color:#060} 183 | pre.prettyprint .kwd, code.prettyprint .kwd{color:#006;font-weight:bold} 184 | pre.prettyprint .com, code.prettyprint .com{color:#600;font-style:italic} 185 | pre.prettyprint .typ, code.prettyprint .typ{color:#404;font-weight:bold} 186 | pre.prettyprint .lit, code.prettyprint .lit{color:#044} 187 | pre.prettyprint .pun, code.prettyprint .pun{color:#440} 188 | pre.prettyprint .pln, code.prettyprint .pln{color:#000} 189 | pre.prettyprint .tag, code.prettyprint .tag{color:#006;font-weight:bold} 190 | pre.prettyprint .atn, code.prettyprint .atn{color:#404} 191 | pre.prettyprint .atv, code.prettyprint .atv{color:#060} 192 | pre.prettyprint, code.prettyprint { 193 | color: #000; 194 | background: #EEE; 195 | font-size: 8pt; 196 | font-family: "Lucida Console", Monaco, monospace; 197 | width: 95%; 198 | margin: auto; 199 | padding: 6px 3px 13px 3px; /* padding-bottom solves hor. scrollbar hiding single line of code in IE6 but causes vert. scrollbar... */ 200 | text-align: left; /* override justify on body */ 201 | goverflow: auto; /* allow scroll bar in case of long lines - goes together with white-space: nowrap! */ 202 | white-space: pre; /* was nowrap, prevent line wrapping */ 203 | line-height: 10pt; 204 | } 205 | } 206 | 207 | @media (prefers-color-scheme: dark) { 208 | #frame { 209 | background-color: rgba(0, 0, 0, 0.7); 210 | color: #ddd; 211 | } 212 | 213 | a { 214 | color: #8AF; 215 | } 216 | 217 | a:visited { 218 | color: #CAF; 219 | } 220 | 221 | code { 222 | background-color: #444; 223 | color: #ddd; 224 | } 225 | 226 | pre { 227 | background-color: #CCC; 228 | } 229 | 230 | .lang-javascript { 231 | background-color: #CCC; 232 | } 233 | 234 | pre.prettyprint, code.prettyprint { 235 | color: #CCC; 236 | background: #333; 237 | box-shadow: 3px 3px 10px #000; 238 | } 239 | 240 | pre.prettyprint .str { color: #b9ca4a } /* string */ 241 | pre.prettyprint .kwd { color: #c397d8 } /* keyword */ 242 | pre.prettyprint .com { color: #f3efb2 } /* comment */ 243 | pre.prettyprint .typ { color: #7aa6da } /* type */ 244 | pre.prettyprint .lit { color: #45e7a6 } /* literal */ 245 | pre.prettyprint .pun { color: #7ecce0 } /* punctuation */ 246 | pre.prettyprint .pln { color: #eaeaea } /* plaintext */ 247 | pre.prettyprint .tag { color: #d54e53 } /* html/xml tag */ 248 | pre.prettyprint .atn { color: #e78c45 } /* attribute name */ 249 | pre.prettyprint .atv { color: #70c0b1 } /* attribute value */ 250 | pre.prettyprint .dec { color: #e78c45 } /* decimal */ 251 | pre.prettyprint .var { color: #d54e53 } /* variable name */ 252 | pre.prettyprint .fun { color: #7aa6da } /* function name */ 253 | } -------------------------------------------------------------------------------- /test/assert.js: -------------------------------------------------------------------------------- 1 | export const config = {}; 2 | 3 | export function setConfig(options) { 4 | Object.assign(config, options); 5 | } 6 | 7 | function formatMsg(msg) { 8 | return `${msg}${msg ? ': ' : ''}`; 9 | } 10 | 11 | export function assertTruthy(actual, msg = '') { 12 | if (!actual) { 13 | throw new Error(`${formatMsg(msg)}expected: truthy, actual: ${actual}`); 14 | } 15 | } 16 | 17 | export function assertFalsy(actual, msg = '') { 18 | if (actual) { 19 | throw new Error(`${formatMsg(msg)}expected: falsy, actual: ${actual}`); 20 | } 21 | } 22 | 23 | export function assertStringMatchesRegEx(actual, regex, msg = '') { 24 | if (!regex.test(actual)) { 25 | throw new Error(`${formatMsg(msg)}expected: ${regex}, actual: ${actual}`); 26 | } 27 | } 28 | 29 | export function assertLessThan(actual, expected, msg = '') { 30 | if (actual >= expected) { 31 | throw new Error(`${formatMsg(msg)}expected: ${actual} to be less than: ${expected}`); 32 | } 33 | } 34 | 35 | export function assertEqualApproximately(actual, expected, range = 0.0000001, msg = '') { 36 | if (actual.length) { 37 | assertArrayEqualApproximately(actual, expected, range, msg); 38 | } 39 | const diff = Math.abs(actual - expected); 40 | if (diff > range) { 41 | throw new Error(`${formatMsg(msg)}expected: ${actual} to be less ${range} different than: ${expected}`); 42 | } 43 | } 44 | 45 | export function assertInstanceOf(actual, expectedType, msg = '') { 46 | if (!(actual instanceof expectedType)) { 47 | throw new Error(`${formatMsg(msg)}expected: ${actual} to be of type: ${expectedType.constructor.name}`); 48 | } 49 | } 50 | 51 | export function assertIsArray(actual, msg = '') { 52 | if (!Array.isArray(actual)) { 53 | throw new Error(`${formatMsg(msg)}expected: ${actual} to be an Array`); 54 | } 55 | } 56 | 57 | export function assertDeepEqual(actual, expected, msg = '') { 58 | if (typeof actual === 'number' || 59 | typeof actual === 'string' || 60 | typeof actual === 'boolean' || 61 | Array.isArray(actual)) { 62 | assertEqual(actual, expected, msg); 63 | } else { 64 | const actualKeys = [...Object.keys(actual)]; 65 | const expectedKeys = [...Object.keys(expected)]; 66 | const onActual = actualKeys.filter(k => !(k in expected)); 67 | const onExpected = expectedKeys.filter(k => !(k in actual)); 68 | if (actualKeys.length !== expectedKeys.length) { 69 | throw new Error(`missing keys:\on actual but not expected: ${onActual}\n on expected but not actual ${onExpected}`); 70 | } 71 | for (const key of actualKeys) { 72 | assertDeepEqual(actual[key], expected[key], `${msg}: for ${key}`); 73 | } 74 | } 75 | } 76 | 77 | export function assertDeepEqualApproximately(actual, expected, range = 1e7, msg = '') { 78 | if (typeof actual === 'number') { 79 | assertEqualApproximately(actual, expected, range, msg); 80 | } else if (Array.isArray(actual) || actual.length) { 81 | assertArrayEqualApproximately(actual, expected, range, msg); 82 | } else if (typeof actual === 'string' || 83 | typeof actual === 'boolean') { 84 | assertEqual(actual, expected, msg); 85 | } else { 86 | const actualKeys = [...Object.keys(actual)]; 87 | const expectedKeys = [...Object.keys(expected)]; 88 | const onActual = actualKeys.filter(k => !(k in expected)); 89 | const onExpected = expectedKeys.filter(k => !(k in actual)); 90 | if (actualKeys.length !== expectedKeys.length) { 91 | throw new Error(`missing keys:\on actual but not expected: ${onActual}\n on expected but not actual ${onExpected}`); 92 | } 93 | for (const key of actualKeys) { 94 | assertDeepEqualApproximately(actual[key], expected[key], range, `${msg}: for ${key}`); 95 | } 96 | } 97 | } 98 | export function assertEqual(actual, expected, msg = '') { 99 | // I'm sure this is not sufficient 100 | if (actual.length && expected.length) { 101 | assertArrayEqual(actual, expected); 102 | } else if (actual !== expected) { 103 | throw new Error(`${formatMsg(msg)}expected: ${expected} to equal actual: ${actual}`); 104 | } 105 | } 106 | 107 | export function assertStrictEqual(actual, expected, msg = '') { 108 | if (actual !== expected) { 109 | throw new Error(`${formatMsg(msg)}expected: ${expected} to equal actual: ${actual}`); 110 | } 111 | } 112 | 113 | export function assertNotEqual(actual, expected, msg = '') { 114 | if (actual === expected) { 115 | throw new Error(`${formatMsg(msg)}expected: ${expected} to not equal actual: ${actual}`); 116 | } 117 | } 118 | 119 | export function assertStrictNotEqual(actual, expected, msg = '') { 120 | if (actual === expected) { 121 | throw new Error(`${formatMsg(msg)}expected: ${expected} to not equal actual: ${actual}`); 122 | } 123 | } 124 | 125 | export function assertArrayEqual(actual, expected, msg = '') { 126 | if (actual.length !== expected.length) { 127 | throw new Error(`${formatMsg(msg)}expected: array.length ${expected.length} to equal actual.length: ${actual.length}`); 128 | } 129 | const errors = []; 130 | for (let i = 0; i < actual.length; ++i) { 131 | if (actual[i] !== expected[i]) { 132 | errors.push(`${formatMsg(msg)}expected: expected[${i}] ${expected[i]} to equal actual[${i}]: ${actual[i]}`); 133 | if (errors.length === 10) { 134 | break; 135 | } 136 | } 137 | } 138 | if (errors.length > 0) { 139 | throw new Error(errors.join('\n')); 140 | } 141 | } 142 | 143 | export function assertArrayEqualApproximately(actual, expected, range = 0.0000001, msg = '') { 144 | if (actual.length !== expected.length) { 145 | throw new Error(`${formatMsg(msg)}expected: array.length ${expected.length} to equal actual.length: ${actual.length}`); 146 | } 147 | const errors = []; 148 | for (let i = 0; i < actual.length; ++i) { 149 | const diff = Math.abs(actual[i] - expected[i]); 150 | if (diff > range) { 151 | errors.push(`${formatMsg(msg)}expected: expected[${i}] ${expected[i]} to equal actual[${i}]: ${actual[i]} within ${range} but was different by ${diff}`); 152 | if (errors.length === 10) { 153 | break; 154 | } 155 | } 156 | } 157 | if (errors.length > 0) { 158 | throw new Error(errors.join('\n')); 159 | } 160 | } 161 | 162 | export function assertThrowsWith(func, expectations, msg = '') { 163 | let error = ''; 164 | if (config.throwOnError === false) { 165 | const origFn = console.error; 166 | const errors = []; 167 | console.error = function (...args) { 168 | errors.push(args.join(' ')); 169 | }; 170 | func(); 171 | console.error = origFn; 172 | if (errors.length) { 173 | error = errors.join('\n'); 174 | console.error(error); 175 | } 176 | } else { 177 | try { 178 | func(); 179 | } catch (e) { 180 | console.error(e); // eslint-disable-line 181 | error = e; 182 | } 183 | 184 | } 185 | 186 | if (config.noLint) { 187 | return; 188 | } 189 | 190 | assertStringMatchesREs(error.toString().replace(/\n/g, ' '), expectations, msg); 191 | } 192 | 193 | // check if it throws it throws with x 194 | export function assertIfThrowsItThrowsWith(func, expectations, msg = '') { 195 | let error = ''; 196 | let threw = false; 197 | if (config.throwOnError === false) { 198 | const origFn = console.error; 199 | const errors = []; 200 | console.error = function (...args) { 201 | errors.push(args.join(' ')); 202 | }; 203 | func(); 204 | console.error = origFn; 205 | if (errors.length) { 206 | error = errors.join('\n'); 207 | console.error(error); 208 | } 209 | } else { 210 | try { 211 | func(); 212 | } catch (e) { 213 | console.error(e); // eslint-disable-line 214 | error = e; 215 | threw = true; 216 | } 217 | 218 | } 219 | 220 | if (config.noLint) { 221 | return; 222 | } 223 | 224 | if (!threw) { 225 | return; 226 | } 227 | 228 | assertStringMatchesREs(error.toString().replace(/\n/g, ' '), expectations, msg); 229 | } 230 | 231 | function assertStringMatchesREs(actual, expectations, msg) { 232 | for (const expectation of expectations) { 233 | if (expectation instanceof RegExp) { 234 | if (!expectation.test(actual)) { 235 | throw new Error(`${formatMsg(msg)}expected: ${expectation}, actual: ${actual}`); 236 | } 237 | } 238 | } 239 | } 240 | 241 | export function assertWarnsWith(func, expectations, msg = '') { 242 | const warnings = []; 243 | const origWarnFn = console.warn; 244 | console.warn = function (...args) { 245 | origWarnFn.call(this, ...args); 246 | warnings.push(args.join(' ')); 247 | }; 248 | 249 | let error; 250 | try { 251 | func(); 252 | } catch (e) { 253 | error = e; 254 | } 255 | 256 | console.warn = origWarnFn; 257 | 258 | if (error) { 259 | throw error; 260 | } 261 | 262 | if (config.noLint) { 263 | return; 264 | } 265 | 266 | assertStringMatchesREs(warnings.join(' '), expectations, msg); 267 | } 268 | 269 | export default { 270 | false: assertFalsy, 271 | equal: assertEqual, 272 | matchesRegEx: assertStringMatchesRegEx, 273 | notEqual: assertNotEqual, 274 | throwsWith: assertThrowsWith, 275 | true: assertTruthy, 276 | }; -------------------------------------------------------------------------------- /dist/1.x/mat3-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { ArrayLikeCtor } from './array-like'; 2 | import { Mat3 } from './mat3'; 3 | import { Mat4 } from './mat4'; 4 | import Vec2 from './vec2-impl'; 5 | export default Mat3; 6 | /** 7 | * Sets the type this library creates for a Mat3 8 | * @param ctor - the constructor for the type. Either `Float32Array`, 'Float64Array', or `Array` 9 | * @returns previous constructor for Mat3 10 | */ 11 | export declare function setDefaultType(ctor: new (n: number) => Mat3): ArrayLikeCtor; 12 | /** 13 | * Create a Mat3 from values 14 | * 15 | * Note: Since passing in a raw JavaScript array 16 | * is valid in all circumstances, if you want to 17 | * force a JavaScript array into a Mat3's specified type 18 | * it would be faster to use 19 | * 20 | * ``` 21 | * const m = mat3.clone(someJSArray); 22 | * ``` 23 | * 24 | * Note: a consequence of the implementation is if your Mat3Type = `Array` 25 | * instead of `Float32Array` or `Float64Array` then any values you 26 | * don't pass in will be undefined. Usually this is not an issue since 27 | * (a) using `Array` is rare and (b) using `mat3.create` is usually used 28 | * to create a Mat3 to be filled out as in 29 | * 30 | * ``` 31 | * const m = mat3.create(); 32 | * mat3.perspective(fov, aspect, near, far, m); 33 | * ``` 34 | * 35 | * @param v0 - value for element 0 36 | * @param v1 - value for element 1 37 | * @param v2 - value for element 2 38 | * @param v3 - value for element 3 39 | * @param v4 - value for element 4 40 | * @param v5 - value for element 5 41 | * @param v6 - value for element 6 42 | * @param v7 - value for element 7 43 | * @param v8 - value for element 8 44 | * @returns matrix created from values. 45 | */ 46 | export declare function create(v0?: number, v1?: number, v2?: number, v3?: number, v4?: number, v5?: number, v6?: number, v7?: number, v8?: number): Mat3; 47 | /** 48 | * Creates a Mat3 from the upper left 3x3 part of a Mat4 49 | * @param m4 - source matrix 50 | * @param dst - matrix to hold result. If not passed a new one is created. 51 | * @returns Mat3 made from m4 52 | */ 53 | export declare function fromMat4(m4: Mat4, dst?: Mat3): Mat3; 54 | /** 55 | * Negates a matrix. 56 | * @param m - The matrix. 57 | * @param dst - matrix to hold result. If not passed a new one is created. 58 | * @returns -m. 59 | */ 60 | export declare function negate(m: Mat3, dst?: Mat3): Mat3; 61 | /** 62 | * Copies a matrix. 63 | * @param m - The matrix. 64 | * @param dst - The matrix. If not passed a new one is created. 65 | * @returns A copy of m. 66 | */ 67 | export declare function copy(m: Mat3, dst?: Mat3): Mat3; 68 | /** 69 | * Copies a matrix (same as copy) 70 | * @param m - The matrix. 71 | * @param dst - The matrix. If not passed a new one is created. 72 | * @returns A copy of m. 73 | */ 74 | export declare const clone: typeof copy; 75 | /** 76 | * Check if 2 matrices are approximately equal 77 | * @param a Operand matrix. 78 | * @param b Operand matrix. 79 | * @returns true if matrices are approximately equal 80 | */ 81 | export declare function equalsApproximately(a: Mat3, b: Mat3): boolean; 82 | /** 83 | * Check if 2 matrices are exactly equal 84 | * @param a Operand matrix. 85 | * @param b Operand matrix. 86 | * @returns true if matrices are exactly equal 87 | */ 88 | export declare function equals(a: Mat3, b: Mat3): boolean; 89 | /** 90 | * Creates a 3-by-3 identity matrix. 91 | * 92 | * @param dst - matrix to hold result. If not passed a new one is created. 93 | * @returns A 3-by-3 identity matrix. 94 | */ 95 | export declare function identity(dst?: Mat3): Mat3; 96 | /** 97 | * Takes the transpose of a matrix. 98 | * @param m - The matrix. 99 | * @param dst - matrix to hold result. If not passed a new one is created. 100 | * @returns The transpose of m. 101 | */ 102 | export declare function transpose(m: Mat3, dst?: Mat3): Mat3; 103 | /** 104 | * Computes the inverse of a 3-by-3 matrix. 105 | * @param m - The matrix. 106 | * @param dst - matrix to hold result. If not passed a new one is created. 107 | * @returns The inverse of m. 108 | */ 109 | export declare function inverse(m: Mat3, dst?: Mat3): Mat3; 110 | /** 111 | * Compute the determinant of a matrix 112 | * @param m - the matrix 113 | * @returns the determinant 114 | */ 115 | export declare function determinant(m: Mat3): number; 116 | /** 117 | * Computes the inverse of a 3-by-3 matrix. (same as inverse) 118 | * @param m - The matrix. 119 | * @param dst - matrix to hold result. If not passed a new one is created. 120 | * @returns The inverse of m. 121 | */ 122 | export declare const invert: typeof inverse; 123 | /** 124 | * Multiplies two 3-by-3 matrices with a on the left and b on the right 125 | * @param a - The matrix on the left. 126 | * @param b - The matrix on the right. 127 | * @param dst - matrix to hold result. If not passed a new one is created. 128 | * @returns The matrix product of a and b. 129 | */ 130 | export declare function multiply(a: Mat3, b: Mat3, dst?: Mat3): Mat3; 131 | /** 132 | * Multiplies two 3-by-3 matrices with a on the left and b on the right (same as multiply) 133 | * @param a - The matrix on the left. 134 | * @param b - The matrix on the right. 135 | * @param dst - matrix to hold result. If not passed a new one is created. 136 | * @returns The matrix product of a and b. 137 | */ 138 | export declare const mul: typeof multiply; 139 | /** 140 | * Sets the translation component of a 3-by-3 matrix to the given 141 | * vector. 142 | * @param a - The matrix. 143 | * @param v - The vector. 144 | * @param dst - matrix to hold result. If not passed a new one is created. 145 | * @returns The matrix with translation set. 146 | */ 147 | export declare function setTranslation(a: Mat3, v: Vec2, dst?: Mat3): Mat3; 148 | /** 149 | * Returns the translation component of a 3-by-3 matrix as a vector with 3 150 | * entries. 151 | * @param m - The matrix. 152 | * @param dst - vector to hold result. If not passed a new one is created. 153 | * @returns The translation component of m. 154 | */ 155 | export declare function getTranslation(m: Mat3, dst?: Vec2): Vec2; 156 | /** 157 | * Returns an axis of a 3x3 matrix as a vector with 2 entries 158 | * @param m - The matrix. 159 | * @param axis - The axis 0 = x, 1 = y, 160 | * @returns The axis component of m. 161 | */ 162 | export declare function getAxis(m: Mat3, axis: number, dst?: Vec2): Vec2; 163 | /** 164 | * Sets an axis of a 3x3 matrix as a vector with 2 entries 165 | * @param m - The matrix. 166 | * @param v - the axis vector 167 | * @param axis - The axis 0 = x, 1 = y; 168 | * @param dst - The matrix to set. If not passed a new one is created. 169 | * @returns The matrix with axis set. 170 | */ 171 | export declare function setAxis(m: Mat3, v: Vec2, axis: number, dst?: Mat3): Mat3; 172 | /** 173 | * Returns the scaling component of the matrix 174 | * @param m - The Matrix 175 | * @param dst - The vector to set. If not passed a new one is created. 176 | */ 177 | export declare function getScaling(m: Mat3, dst?: Vec2): Vec2; 178 | /** 179 | * Creates a 3-by-3 matrix which translates by the given vector v. 180 | * @param v - The vector by which to translate. 181 | * @param dst - matrix to hold result. If not passed a new one is created. 182 | * @returns The translation matrix. 183 | */ 184 | export declare function translation(v: Vec2, dst?: Mat3): Mat3; 185 | /** 186 | * Translates the given 3-by-3 matrix by the given vector v. 187 | * @param m - The matrix. 188 | * @param v - The vector by which to translate. 189 | * @param dst - matrix to hold result. If not passed a new one is created. 190 | * @returns The translated matrix. 191 | */ 192 | export declare function translate(m: Mat3, v: Vec2, dst?: Mat3): Mat3; 193 | /** 194 | * Creates a 3-by-3 matrix which rotates by the given angle. 195 | * @param angleInRadians - The angle by which to rotate (in radians). 196 | * @param dst - matrix to hold result. If not passed a new one is created. 197 | * @returns The rotation matrix. 198 | */ 199 | export declare function rotation(angleInRadians: number, dst?: Mat3): Mat3; 200 | /** 201 | * Rotates the given 3-by-3 matrix by the given angle. 202 | * @param m - The matrix. 203 | * @param angleInRadians - The angle by which to rotate (in radians). 204 | * @param dst - matrix to hold result. If not passed a new one is created. 205 | * @returns The rotated matrix. 206 | */ 207 | export declare function rotate(m: Mat3, angleInRadians: number, dst?: Mat3): Mat3; 208 | /** 209 | * Creates a 3-by-3 matrix which scales in each dimension by an amount given by 210 | * the corresponding entry in the given vector; assumes the vector has three 211 | * entries. 212 | * @param v - A vector of 213 | * 2 entries specifying the factor by which to scale in each dimension. 214 | * @param dst - matrix to hold result. If not passed a new one is created. 215 | * @returns The scaling matrix. 216 | */ 217 | export declare function scaling(v: Vec2, dst?: Mat3): Mat3; 218 | /** 219 | * Scales the given 3-by-3 matrix in each dimension by an amount 220 | * given by the corresponding entry in the given vector; assumes the vector has 221 | * three entries. 222 | * @param m - The matrix to be modified. 223 | * @param v - A vector of 2 entries specifying the 224 | * factor by which to scale in each dimension. 225 | * @param dst - matrix to hold result. If not passed a new one is created. 226 | * @returns The scaled matrix. 227 | */ 228 | export declare function scale(m: Mat3, v: Vec2, dst?: Mat3): Mat3; 229 | -------------------------------------------------------------------------------- /test/mocha.css: -------------------------------------------------------------------------------- 1 | @charset "utf-8"; 2 | 3 | :root { 4 | --mocha-color: #000; 5 | --mocha-bg-color: #fff; 6 | --mocha-pass-icon-color: #00d6b2; 7 | --mocha-pass-color: #fff; 8 | --mocha-pass-shadow-color: rgba(0,0,0,.2); 9 | --mocha-pass-mediump-color: #c09853; 10 | --mocha-pass-slow-color: #b94a48; 11 | --mocha-test-pending-color: #0b97c4; 12 | --mocha-test-pending-icon-color: #0b97c4; 13 | --mocha-test-fail-color: #c00; 14 | --mocha-test-fail-icon-color: #c00; 15 | --mocha-test-fail-pre-color: #000; 16 | --mocha-test-fail-pre-error-color: #c00; 17 | --mocha-test-html-error-color: #000; 18 | --mocha-box-shadow-color: #eee; 19 | --mocha-box-bottom-color: #ddd; 20 | --mocha-test-replay-color: #888; 21 | --mocha-test-replay-bg-color: #eee; 22 | --mocha-stats-color: #888; 23 | --mocha-stats-em-color: #000; 24 | --mocha-stats-hover-color: #eee; 25 | --mocha-error-color: #c00; 26 | 27 | --mocha-code-comment: #ddd; 28 | --mocha-code-init: #2f6fad; 29 | --mocha-code-string: #5890ad; 30 | --mocha-code-keyword: #8a6343; 31 | --mocha-code-number: #2f6fad; 32 | } 33 | 34 | @media (prefers-color-scheme: dark) { 35 | :root { 36 | --mocha-color: #fff; 37 | --mocha-bg-color: #222; 38 | --mocha-pass-icon-color: #00d6b2; 39 | --mocha-pass-color: #222; 40 | --mocha-pass-shadow-color: rgba(255,255,255,.2); 41 | --mocha-pass-mediump-color: #f1be67; 42 | --mocha-pass-slow-color: #f49896; 43 | --mocha-test-pending-color: #0b97c4; 44 | --mocha-test-pending-icon-color: #0b97c4; 45 | --mocha-test-fail-color: #f44; 46 | --mocha-test-fail-icon-color: #f44; 47 | --mocha-test-fail-pre-color: #fff; 48 | --mocha-test-fail-pre-error-color: #f44; 49 | --mocha-test-html-error-color: #fff; 50 | --mocha-box-shadow-color: #444; 51 | --mocha-box-bottom-color: #555; 52 | --mocha-test-replay-color: #888; 53 | --mocha-test-replay-bg-color: #444; 54 | --mocha-stats-color: #aaa; 55 | --mocha-stats-em-color: #fff; 56 | --mocha-stats-hover-color: #444; 57 | --mocha-error-color: #f44; 58 | 59 | --mocha-code-comment: #ddd; 60 | --mocha-code-init: #9cc7f1; 61 | --mocha-code-string: #80d4ff; 62 | --mocha-code-keyword: #e3a470; 63 | --mocha-code-number: #4ca7ff; 64 | } 65 | } 66 | 67 | body { 68 | margin:0; 69 | background-color: var(--mocha-bg-color); 70 | color: var(--mocha-color); 71 | } 72 | 73 | #mocha { 74 | font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; 75 | margin: 60px 50px; 76 | } 77 | 78 | #mocha ul, 79 | #mocha li { 80 | margin: 0; 81 | padding: 0; 82 | } 83 | 84 | #mocha ul { 85 | list-style: none; 86 | } 87 | 88 | #mocha h1, 89 | #mocha h2 { 90 | margin: 0; 91 | } 92 | 93 | #mocha h1 { 94 | margin-top: 15px; 95 | font-size: 1em; 96 | font-weight: 200; 97 | } 98 | 99 | #mocha h1 a { 100 | text-decoration: none; 101 | color: inherit; 102 | } 103 | 104 | #mocha h1 a:hover { 105 | text-decoration: underline; 106 | } 107 | 108 | #mocha .suite .suite h1 { 109 | margin-top: 0; 110 | font-size: .8em; 111 | } 112 | 113 | #mocha .hidden { 114 | display: none; 115 | } 116 | 117 | #mocha h2 { 118 | font-size: 12px; 119 | font-weight: normal; 120 | cursor: pointer; 121 | } 122 | 123 | #mocha .suite { 124 | margin-left: 15px; 125 | } 126 | 127 | #mocha .test { 128 | margin-left: 15px; 129 | overflow: hidden; 130 | } 131 | 132 | #mocha .test.pending:hover h2::after { 133 | content: '(pending)'; 134 | font-family: arial, sans-serif; 135 | } 136 | 137 | #mocha .test.pass.medium .duration { 138 | background: var(--mocha-pass-mediump-color); 139 | } 140 | 141 | #mocha .test.pass.slow .duration { 142 | background: var(--mocha-pass-slow-color); 143 | } 144 | 145 | #mocha .test.pass::before { 146 | content: '✓'; 147 | font-size: 12px; 148 | display: block; 149 | float: left; 150 | margin-right: 5px; 151 | color: var(--mocha-pass-icon-color); 152 | } 153 | 154 | #mocha .test.pass .duration { 155 | font-size: 9px; 156 | margin-left: 5px; 157 | padding: 2px 5px; 158 | color: var(--mocha-pass-color); 159 | -webkit-box-shadow: inset 0 1px 1px var(--mocha-pass-shadow-color); 160 | -moz-box-shadow: inset 0 1px 1px var(--mocha-pass-shadow-color); 161 | box-shadow: inset 0 1px 1px var(--mocha-pass-shadow-color); 162 | -webkit-border-radius: 5px; 163 | -moz-border-radius: 5px; 164 | -ms-border-radius: 5px; 165 | -o-border-radius: 5px; 166 | border-radius: 5px; 167 | } 168 | 169 | #mocha .test.pass.fast .duration { 170 | display: none; 171 | } 172 | 173 | #mocha .test.pending { 174 | color: var(--mocha-test-pending-color); 175 | } 176 | 177 | #mocha .test.pending::before { 178 | content: '◦'; 179 | color: var(--mocha-test-pending-icon-color); 180 | } 181 | 182 | #mocha .test.fail { 183 | color: var(--mocha-test-fail-color); 184 | } 185 | 186 | #mocha .test.fail pre { 187 | color: var(--mocha-test-fail-pre-color); 188 | } 189 | 190 | #mocha .test.fail::before { 191 | content: '✖'; 192 | font-size: 12px; 193 | display: block; 194 | float: left; 195 | margin-right: 5px; 196 | color: var(--mocha-pass-icon-color); 197 | } 198 | 199 | #mocha .test pre.error { 200 | color: var(--mocha-test-fail-pre-error-color); 201 | max-height: 300px; 202 | overflow: auto; 203 | } 204 | 205 | #mocha .test .html-error { 206 | overflow: auto; 207 | color: var(--mocha-test-html-error-color); 208 | display: block; 209 | float: left; 210 | clear: left; 211 | font: 12px/1.5 monaco, monospace; 212 | margin: 5px; 213 | padding: 15px; 214 | border: 1px solid var(--mocha-box-shadow-color); 215 | max-width: 85%; /*(1)*/ 216 | max-width: -webkit-calc(100% - 42px); 217 | max-width: -moz-calc(100% - 42px); 218 | max-width: calc(100% - 42px); /*(2)*/ 219 | max-height: 300px; 220 | word-wrap: break-word; 221 | border-bottom-color: var(--mocha-box-bottom-color); 222 | -webkit-box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 223 | -moz-box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 224 | box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 225 | -webkit-border-radius: 3px; 226 | -moz-border-radius: 3px; 227 | border-radius: 3px; 228 | } 229 | 230 | #mocha .test .html-error pre.error { 231 | border: none; 232 | -webkit-border-radius: 0; 233 | -moz-border-radius: 0; 234 | border-radius: 0; 235 | -webkit-box-shadow: 0; 236 | -moz-box-shadow: 0; 237 | box-shadow: 0; 238 | padding: 0; 239 | margin: 0; 240 | margin-top: 18px; 241 | max-height: none; 242 | } 243 | 244 | /** 245 | * (1): approximate for browsers not supporting calc 246 | * (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border) 247 | * ^^ seriously 248 | */ 249 | #mocha .test pre { 250 | display: block; 251 | float: left; 252 | clear: left; 253 | font: 12px/1.5 monaco, monospace; 254 | margin: 5px; 255 | padding: 15px; 256 | border: 1px solid var(--mocha-box-shadow-color); 257 | max-width: 85%; /*(1)*/ 258 | max-width: -webkit-calc(100% - 42px); 259 | max-width: -moz-calc(100% - 42px); 260 | max-width: calc(100% - 42px); /*(2)*/ 261 | word-wrap: break-word; 262 | border-bottom-color: var(--mocha-box-bottom-color); 263 | -webkit-box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 264 | -moz-box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 265 | box-shadow: 0 1px 3px var(--mocha-box-shadow-color); 266 | -webkit-border-radius: 3px; 267 | -moz-border-radius: 3px; 268 | border-radius: 3px; 269 | } 270 | 271 | #mocha .test h2 { 272 | position: relative; 273 | } 274 | 275 | #mocha .test a.replay { 276 | position: absolute; 277 | top: 3px; 278 | right: 0; 279 | text-decoration: none; 280 | vertical-align: middle; 281 | display: block; 282 | width: 15px; 283 | height: 15px; 284 | line-height: 15px; 285 | text-align: center; 286 | background: var(--mocha-test-replay-bg-color); 287 | font-size: 15px; 288 | -webkit-border-radius: 15px; 289 | -moz-border-radius: 15px; 290 | border-radius: 15px; 291 | -webkit-transition:opacity 200ms; 292 | -moz-transition:opacity 200ms; 293 | -o-transition:opacity 200ms; 294 | transition: opacity 200ms; 295 | opacity: 0.3; 296 | color: var(--mocha-test-replay-color); 297 | } 298 | 299 | #mocha .test:hover a.replay { 300 | opacity: 1; 301 | } 302 | 303 | #mocha-report.pass .test.fail { 304 | display: none; 305 | } 306 | 307 | #mocha-report.fail .test.pass { 308 | display: none; 309 | } 310 | 311 | #mocha-report.pending .test.pass, 312 | #mocha-report.pending .test.fail { 313 | display: none; 314 | } 315 | #mocha-report.pending .test.pass.pending { 316 | display: block; 317 | } 318 | 319 | #mocha-error { 320 | color: var(--mocha-error-color); 321 | font-size: 1.5em; 322 | font-weight: 100; 323 | letter-spacing: 1px; 324 | } 325 | 326 | #mocha-stats { 327 | position: fixed; 328 | top: 15px; 329 | right: 10px; 330 | font-size: 12px; 331 | margin: 0; 332 | color: var(--mocha-stats-color); 333 | z-index: 1; 334 | } 335 | 336 | #mocha-stats .progress { 337 | float: right; 338 | padding-top: 0; 339 | 340 | /** 341 | * Set safe initial values, so mochas .progress does not inherit these 342 | * properties from Bootstrap .progress (which causes .progress height to 343 | * equal line height set in Bootstrap). 344 | */ 345 | height: auto; 346 | -webkit-box-shadow: none; 347 | -moz-box-shadow: none; 348 | box-shadow: none; 349 | background-color: initial; 350 | } 351 | 352 | #mocha-stats em { 353 | color: var(--mocha-stats-em-color); 354 | } 355 | 356 | #mocha-stats a { 357 | text-decoration: none; 358 | color: inherit; 359 | } 360 | 361 | #mocha-stats a:hover { 362 | border-bottom: 1px solid var(--mocha-stats-hover-color); 363 | } 364 | 365 | #mocha-stats li { 366 | display: inline-block; 367 | margin: 0 5px; 368 | list-style: none; 369 | padding-top: 11px; 370 | } 371 | 372 | #mocha-stats canvas { 373 | width: 40px; 374 | height: 40px; 375 | } 376 | 377 | #mocha code .comment { color: var(--mocha-code-comment); } 378 | #mocha code .init { color: var(--mocha-code-init); } 379 | #mocha code .string { color: var(--mocha-code-string); } 380 | #mocha code .keyword { color: var(--mocha-code-keyword); } 381 | #mocha code .number { color: var(--mocha-code-number); } 382 | 383 | @media screen and (max-device-width: 480px) { 384 | #mocha { 385 | margin: 60px 0px; 386 | } 387 | 388 | #mocha #stats { 389 | position: absolute; 390 | } 391 | } 392 | -------------------------------------------------------------------------------- /dist/2.x/mat3-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Quat } from './quat'; 2 | import { Mat3 } from './mat3'; 3 | import { Mat4 } from './mat4'; 4 | import Vec2 from './vec2-impl'; 5 | export default Mat3; 6 | export type Mat3LikeCtor = new (n: number) => Mat3; 7 | /** 8 | * Sets the type this library creates for a Mat3 9 | * @param ctor - the constructor for the type. Either `Float32Array`, `Float64Array`, or `Array` 10 | * @returns previous constructor for Mat3 11 | */ 12 | export declare function setDefaultType(ctor: new (n: number) => Mat3): Mat3LikeCtor; 13 | /** 14 | * Create a Mat3 from values 15 | * 16 | * Note: Since passing in a raw JavaScript array 17 | * is valid in all circumstances, if you want to 18 | * force a JavaScript array into a Mat3's specified type 19 | * it would be faster to use 20 | * 21 | * ``` 22 | * const m = mat3.clone(someJSArray); 23 | * ``` 24 | * 25 | * Note: a consequence of the implementation is if your Mat3Type = `Array` 26 | * instead of `Float32Array` or `Float64Array` then any values you 27 | * don't pass in will be undefined. Usually this is not an issue since 28 | * (a) using `Array` is rare and (b) using `mat3.create` is usually used 29 | * to create a Mat3 to be filled out as in 30 | * 31 | * ``` 32 | * const m = mat3.create(); 33 | * mat3.perspective(fov, aspect, near, far, m); 34 | * ``` 35 | * 36 | * @param v0 - value for element 0 37 | * @param v1 - value for element 1 38 | * @param v2 - value for element 2 39 | * @param v3 - value for element 3 40 | * @param v4 - value for element 4 41 | * @param v5 - value for element 5 42 | * @param v6 - value for element 6 43 | * @param v7 - value for element 7 44 | * @param v8 - value for element 8 45 | * @returns matrix created from values. 46 | */ 47 | export declare function create(v0?: number, v1?: number, v2?: number, v3?: number, v4?: number, v5?: number, v6?: number, v7?: number, v8?: number): Mat3; 48 | /** 49 | * Sets the values of a Mat3 50 | * Also see {@link mat3.create} and {@link mat3.copy} 51 | * 52 | * @param v0 - value for element 0 53 | * @param v1 - value for element 1 54 | * @param v2 - value for element 2 55 | * @param v3 - value for element 3 56 | * @param v4 - value for element 4 57 | * @param v5 - value for element 5 58 | * @param v6 - value for element 6 59 | * @param v7 - value for element 7 60 | * @param v8 - value for element 8 61 | * @param dst - matrix to hold result. If not passed a new one is created. 62 | * @returns Mat3 set from values. 63 | */ 64 | export declare function set(v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number, v8: number, dst?: Mat3): Mat3; 65 | /** 66 | * Creates a Mat3 from the upper left 3x3 part of a Mat4 67 | * @param m4 - source matrix 68 | * @param dst - matrix to hold result. If not passed a new one is created. 69 | * @returns Mat3 made from m4 70 | */ 71 | export declare function fromMat4(m4: Mat4, dst?: Mat3): Mat3; 72 | /** 73 | * Creates a Mat3 rotation matrix from a quaternion 74 | * @param q - quaternion to create matrix from 75 | * @param dst - matrix to hold result. If not passed a new one is created. 76 | * @returns Mat3 made from q 77 | */ 78 | export declare function fromQuat(q: Quat, dst?: Mat3): Mat3; 79 | /** 80 | * Negates a matrix. 81 | * @param m - The matrix. 82 | * @param dst - matrix to hold result. If not passed a new one is created. 83 | * @returns -m. 84 | */ 85 | export declare function negate(m: Mat3, dst?: Mat3): Mat3; 86 | /** 87 | * Copies a matrix. (same as {@link mat3.clone}) 88 | * Also see {@link mat3.create} and {@link mat3.set} 89 | * @param m - The matrix. 90 | * @param dst - The matrix. If not passed a new one is created. 91 | * @returns A copy of m. 92 | */ 93 | export declare function copy(m: Mat3, dst?: Mat3): Mat3; 94 | /** 95 | * Copies a matrix (same as {@link mat3.copy}) 96 | * Also see {@link mat3.create} and {@link mat3.set} 97 | * @param m - The matrix. 98 | * @param dst - The matrix. If not passed a new one is created. 99 | * @returns A copy of m. 100 | */ 101 | export declare const clone: typeof copy; 102 | /** 103 | * Check if 2 matrices are approximately equal 104 | * @param a Operand matrix. 105 | * @param b Operand matrix. 106 | * @returns true if matrices are approximately equal 107 | */ 108 | export declare function equalsApproximately(a: Mat3, b: Mat3): boolean; 109 | /** 110 | * Check if 2 matrices are exactly equal 111 | * @param a Operand matrix. 112 | * @param b Operand matrix. 113 | * @returns true if matrices are exactly equal 114 | */ 115 | export declare function equals(a: Mat3, b: Mat3): boolean; 116 | /** 117 | * Creates a 3-by-3 identity matrix. 118 | * 119 | * @param dst - matrix to hold result. If not passed a new one is created. 120 | * @returns A 3-by-3 identity matrix. 121 | */ 122 | export declare function identity(dst?: Mat3): Mat3; 123 | /** 124 | * Takes the transpose of a matrix. 125 | * @param m - The matrix. 126 | * @param dst - matrix to hold result. If not passed a new one is created. 127 | * @returns The transpose of m. 128 | */ 129 | export declare function transpose(m: Mat3, dst?: Mat3): Mat3; 130 | /** 131 | * Computes the inverse of a 3-by-3 matrix. 132 | * @param m - The matrix. 133 | * @param dst - matrix to hold result. If not passed a new one is created. 134 | * @returns The inverse of m. 135 | */ 136 | export declare function inverse(m: Mat3, dst?: Mat3): Mat3; 137 | /** 138 | * Compute the determinant of a matrix 139 | * @param m - the matrix 140 | * @returns the determinant 141 | */ 142 | export declare function determinant(m: Mat3): number; 143 | /** 144 | * Computes the inverse of a 3-by-3 matrix. (same as inverse) 145 | * @param m - The matrix. 146 | * @param dst - matrix to hold result. If not passed a new one is created. 147 | * @returns The inverse of m. 148 | */ 149 | export declare const invert: typeof inverse; 150 | /** 151 | * Multiplies two 3-by-3 matrices with a on the left and b on the right 152 | * @param a - The matrix on the left. 153 | * @param b - The matrix on the right. 154 | * @param dst - matrix to hold result. If not passed a new one is created. 155 | * @returns The matrix product of a and b. 156 | */ 157 | export declare function multiply(a: Mat3, b: Mat3, dst?: Mat3): Mat3; 158 | /** 159 | * Multiplies two 3-by-3 matrices with a on the left and b on the right (same as multiply) 160 | * @param a - The matrix on the left. 161 | * @param b - The matrix on the right. 162 | * @param dst - matrix to hold result. If not passed a new one is created. 163 | * @returns The matrix product of a and b. 164 | */ 165 | export declare const mul: typeof multiply; 166 | /** 167 | * Sets the translation component of a 3-by-3 matrix to the given 168 | * vector. 169 | * @param a - The matrix. 170 | * @param v - The vector. 171 | * @param dst - matrix to hold result. If not passed a new one is created. 172 | * @returns The matrix with translation set. 173 | */ 174 | export declare function setTranslation(a: Mat3, v: Vec2, dst?: Mat3): Mat3; 175 | /** 176 | * Returns the translation component of a 3-by-3 matrix as a vector with 3 177 | * entries. 178 | * @param m - The matrix. 179 | * @param dst - vector to hold result. If not passed a new one is created. 180 | * @returns The translation component of m. 181 | */ 182 | export declare function getTranslation(m: Mat3, dst?: Vec2): Vec2; 183 | /** 184 | * Returns an axis of a 3x3 matrix as a vector with 2 entries 185 | * @param m - The matrix. 186 | * @param axis - The axis 0 = x, 1 = y, 187 | * @returns The axis component of m. 188 | */ 189 | export declare function getAxis(m: Mat3, axis: number, dst?: Vec2): Vec2; 190 | /** 191 | * Sets an axis of a 3x3 matrix as a vector with 2 entries 192 | * @param m - The matrix. 193 | * @param v - the axis vector 194 | * @param axis - The axis 0 = x, 1 = y; 195 | * @param dst - The matrix to set. If not passed a new one is created. 196 | * @returns The matrix with axis set. 197 | */ 198 | export declare function setAxis(m: Mat3, v: Vec2, axis: number, dst?: Mat3): Mat3; 199 | /** 200 | * Returns the scaling component of the matrix 201 | * @param m - The Matrix 202 | * @param dst - The vector to set. If not passed a new one is created. 203 | */ 204 | export declare function getScaling(m: Mat3, dst?: Vec2): Vec2; 205 | /** 206 | * Creates a 3-by-3 matrix which translates by the given vector v. 207 | * @param v - The vector by which to translate. 208 | * @param dst - matrix to hold result. If not passed a new one is created. 209 | * @returns The translation matrix. 210 | */ 211 | export declare function translation(v: Vec2, dst?: Mat3): Mat3; 212 | /** 213 | * Translates the given 3-by-3 matrix by the given vector v. 214 | * @param m - The matrix. 215 | * @param v - The vector by which to translate. 216 | * @param dst - matrix to hold result. If not passed a new one is created. 217 | * @returns The translated matrix. 218 | */ 219 | export declare function translate(m: Mat3, v: Vec2, dst?: Mat3): Mat3; 220 | /** 221 | * Creates a 3-by-3 matrix which rotates by the given angle. 222 | * @param angleInRadians - The angle by which to rotate (in radians). 223 | * @param dst - matrix to hold result. If not passed a new one is created. 224 | * @returns The rotation matrix. 225 | */ 226 | export declare function rotation(angleInRadians: number, dst?: Mat3): Mat3; 227 | /** 228 | * Rotates the given 3-by-3 matrix by the given angle. 229 | * @param m - The matrix. 230 | * @param angleInRadians - The angle by which to rotate (in radians). 231 | * @param dst - matrix to hold result. If not passed a new one is created. 232 | * @returns The rotated matrix. 233 | */ 234 | export declare function rotate(m: Mat3, angleInRadians: number, dst?: Mat3): Mat3; 235 | /** 236 | * Creates a 3-by-3 matrix which scales in each dimension by an amount given by 237 | * the corresponding entry in the given vector; assumes the vector has three 238 | * entries. 239 | * @param v - A vector of 240 | * 2 entries specifying the factor by which to scale in each dimension. 241 | * @param dst - matrix to hold result. If not passed a new one is created. 242 | * @returns The scaling matrix. 243 | */ 244 | export declare function scaling(v: Vec2, dst?: Mat3): Mat3; 245 | /** 246 | * Scales the given 3-by-3 matrix in each dimension by an amount 247 | * given by the corresponding entry in the given vector; assumes the vector has 248 | * three entries. 249 | * @param m - The matrix to be modified. 250 | * @param v - A vector of 2 entries specifying the 251 | * factor by which to scale in each dimension. 252 | * @param dst - matrix to hold result. If not passed a new one is created. 253 | * @returns The scaled matrix. 254 | */ 255 | export declare function scale(m: Mat3, v: Vec2, dst?: Mat3): Mat3; 256 | /** 257 | * Creates a 3-by-3 matrix which scales uniformly in each dimension 258 | * @param s - Amount to scale 259 | * @param dst - matrix to hold result. If not passed a new one is created. 260 | * @returns The scaling matrix. 261 | */ 262 | export declare function uniformScaling(s: number, dst?: Mat3): Mat3; 263 | /** 264 | * Scales the given 3-by-3 matrix in each dimension by an amount 265 | * given. 266 | * @param m - The matrix to be modified. 267 | * @param s - Amount to scale. 268 | * @param dst - matrix to hold result. If not passed a new one is created. 269 | * @returns The scaled matrix. 270 | */ 271 | export declare function uniformScale(m: Mat3, s: number, dst?: Mat3): Mat3; 272 | -------------------------------------------------------------------------------- /dist/2.x/quat-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Quat, create, setDefaultType } from './quat'; 2 | import { Mat3 } from './mat3.js'; 3 | import { Mat4 } from './mat4.js'; 4 | import { Vec3 } from './vec3.js'; 5 | export type RotationOrder = 'xyz' | 'xzy' | 'yxz' | 'yzx' | 'zxy' | 'zyx'; 6 | export default Quat; 7 | export { create, setDefaultType }; 8 | /** 9 | * Creates a Quat; may be called with x, y, z to set initial values. (same as create) 10 | * @param x - Initial x value. 11 | * @param y - Initial y value. 12 | * @param z - Initial z value. 13 | * @param z - Initial w value. 14 | * @returns the created vector 15 | */ 16 | export declare const fromValues: typeof create; 17 | /** 18 | * Sets the values of a Quat 19 | * Also see {@link quat.create} and {@link quat.copy} 20 | * 21 | * @param x first value 22 | * @param y second value 23 | * @param z third value 24 | * @param w fourth value 25 | * @param dst - vector to hold result. If not passed in a new one is created. 26 | * @returns A vector with its elements set. 27 | */ 28 | export declare function set(x: number, y: number, z: number, w: number, dst?: Quat): Quat; 29 | /** 30 | * Sets a quaternion from the given angle and axis, 31 | * then returns it. 32 | * 33 | * @param axis - the axis to rotate around 34 | * @param angleInRadians - the angle 35 | * @param dst - quaternion to hold result. If not passed in a new one is created. 36 | * @returns The quaternion that represents the given axis and angle 37 | **/ 38 | export declare function fromAxisAngle(axis: Vec3, angleInRadians: number, dst?: Quat): Quat; 39 | /** 40 | * Gets the rotation axis and angle 41 | * @param q - quaternion to compute from 42 | * @param dst - Vec3 to hold result. If not passed in a new one is created. 43 | * @return angle and axis 44 | */ 45 | export declare function toAxisAngle(q: Quat, dst?: Vec3): { 46 | angle: number; 47 | axis: Vec3; 48 | }; 49 | /** 50 | * Returns the angle in degrees between two rotations a and b. 51 | * @param a - quaternion a 52 | * @param b - quaternion b 53 | * @return angle in radians between the two quaternions 54 | */ 55 | export declare function angle(a: Quat, b: Quat): number; 56 | /** 57 | * Multiplies two quaternions 58 | * 59 | * @param a - the first quaternion 60 | * @param b - the second quaternion 61 | * @param dst - quaternion to hold result. If not passed in a new one is created. 62 | * @returns A quaternion that is the result of a * b 63 | */ 64 | export declare function multiply(a: Quat, b: Quat, dst?: Quat): Quat; 65 | /** 66 | * Multiplies two quaternions 67 | * 68 | * @param a - the first quaternion 69 | * @param b - the second quaternion 70 | * @param dst - quaternion to hold result. If not passed in a new one is created. 71 | * @returns A quaternion that is the result of a * b 72 | */ 73 | export declare const mul: typeof multiply; 74 | /** 75 | * Rotates the given quaternion around the X axis by the given angle. 76 | * @param q - quaternion to rotate 77 | * @param angleInRadians - The angle by which to rotate 78 | * @param dst - quaternion to hold result. If not passed in a new one is created. 79 | * @returns A quaternion that is the result of a * b 80 | */ 81 | export declare function rotateX(q: Quat, angleInRadians: number, dst?: Quat): Quat; 82 | /** 83 | * Rotates the given quaternion around the Y axis by the given angle. 84 | * @param q - quaternion to rotate 85 | * @param angleInRadians - The angle by which to rotate 86 | * @param dst - quaternion to hold result. If not passed in a new one is created. 87 | * @returns A quaternion that is the result of a * b 88 | */ 89 | export declare function rotateY(q: Quat, angleInRadians: number, dst?: Quat): Quat; 90 | /** 91 | * Rotates the given quaternion around the Z axis by the given angle. 92 | * @param q - quaternion to rotate 93 | * @param angleInRadians - The angle by which to rotate 94 | * @param dst - quaternion to hold result. If not passed in a new one is created. 95 | * @returns A quaternion that is the result of a * b 96 | */ 97 | export declare function rotateZ(q: Quat, angleInRadians: number, dst?: Quat): Quat; 98 | /** 99 | * Spherically linear interpolate between two quaternions 100 | * 101 | * @param a - starting value 102 | * @param b - ending value 103 | * @param t - value where 0 = a and 1 = b 104 | * @param dst - quaternion to hold result. If not passed in a new one is created. 105 | * @returns A quaternion that is the result of a * b 106 | */ 107 | export declare function slerp(a: Quat, b: Quat, t: number, dst?: Quat): Quat; 108 | /** 109 | * Compute the inverse of a quaternion 110 | * 111 | * @param q - quaternion to compute the inverse of 112 | * @returns A quaternion that is the result of a * b 113 | */ 114 | export declare function inverse(q: Quat, dst?: Quat): Quat; 115 | /** 116 | * Compute the conjugate of a quaternion 117 | * For quaternions with a magnitude of 1 (a unit quaternion) 118 | * this returns the same as the inverse but is faster to calculate. 119 | * 120 | * @param q - quaternion to compute the conjugate of. 121 | * @param dst - quaternion to hold result. If not passed in a new one is created. 122 | * @returns The conjugate of q 123 | */ 124 | export declare function conjugate(q: Quat, dst?: Quat): Quat; 125 | /** 126 | * Creates a quaternion from the given rotation matrix. 127 | * 128 | * The created quaternion is not normalized. 129 | * 130 | * @param m - rotation matrix 131 | * @param dst - quaternion to hold result. If not passed in a new one is created. 132 | * @returns the result 133 | */ 134 | export declare function fromMat(m: Mat3 | Mat4, dst?: Quat): Quat; 135 | /** 136 | * Creates a quaternion from the given euler angle x, y, z using the provided intrinsic order for the conversion. 137 | * 138 | * @param xAngleInRadians - angle to rotate around X axis in radians. 139 | * @param yAngleInRadians - angle to rotate around Y axis in radians. 140 | * @param zAngleInRadians - angle to rotate around Z axis in radians. 141 | * @param order - order to apply euler angles 142 | * @param dst - quaternion to hold result. If not passed in a new one is created. 143 | * @returns A quaternion representing the same rotation as the euler angles applied in the given order 144 | */ 145 | export declare function fromEuler(xAngleInRadians: number, yAngleInRadians: number, zAngleInRadians: number, order: RotationOrder, dst?: Quat): Quat; 146 | /** 147 | * Copies a quaternion. (same as {@link quat.clone}) 148 | * Also see {@link quat.create} and {@link quat.set} 149 | * @param q - The quaternion. 150 | * @param dst - quaternion to hold result. If not passed in a new one is created. 151 | * @returns A quaternion that is a copy of q 152 | */ 153 | export declare function copy(q: Quat, dst?: Quat): Quat; 154 | /** 155 | * Clones a quaternion. (same as {@link quat.copy}) 156 | * Also see {@link quat.create} and {@link quat.set} 157 | * @param q - The quaternion. 158 | * @param dst - quaternion to hold result. If not passed in a new one is created. 159 | * @returns A copy of q. 160 | */ 161 | export declare const clone: typeof copy; 162 | /** 163 | * Adds two quaternions; assumes a and b have the same dimension. 164 | * @param a - Operand quaternion. 165 | * @param b - Operand quaternion. 166 | * @param dst - quaternion to hold result. If not passed in a new one is created. 167 | * @returns A quaternion that is the sum of a and b. 168 | */ 169 | export declare function add(a: Quat, b: Quat, dst?: Quat): Quat; 170 | /** 171 | * Subtracts two quaternions. 172 | * @param a - Operand quaternion. 173 | * @param b - Operand quaternion. 174 | * @param dst - quaternion to hold result. If not passed in a new one is created. 175 | * @returns A quaternion that is the difference of a and b. 176 | */ 177 | export declare function subtract(a: Quat, b: Quat, dst?: Quat): Quat; 178 | /** 179 | * Subtracts two quaternions. 180 | * @param a - Operand quaternion. 181 | * @param b - Operand quaternion. 182 | * @param dst - quaternion to hold result. If not passed in a new one is created. 183 | * @returns A quaternion that is the difference of a and b. 184 | */ 185 | export declare const sub: typeof subtract; 186 | /** 187 | * Multiplies a quaternion by a scalar. 188 | * @param v - The quaternion. 189 | * @param k - The scalar. 190 | * @param dst - quaternion to hold result. If not passed in a new one is created. 191 | * @returns The scaled quaternion. 192 | */ 193 | export declare function mulScalar(v: Quat, k: number, dst?: Quat): Quat; 194 | /** 195 | * Multiplies a quaternion by a scalar. (same as mulScalar) 196 | * @param v - The quaternion. 197 | * @param k - The scalar. 198 | * @param dst - quaternion to hold result. If not passed in a new one is created. 199 | * @returns The scaled quaternion. 200 | */ 201 | export declare const scale: typeof mulScalar; 202 | /** 203 | * Divides a vector by a scalar. 204 | * @param v - The vector. 205 | * @param k - The scalar. 206 | * @param dst - quaternion to hold result. If not passed in a new one is created. 207 | * @returns The scaled quaternion. 208 | */ 209 | export declare function divScalar(v: Quat, k: number, dst?: Quat): Quat; 210 | /** 211 | * Computes the dot product of two quaternions 212 | * @param a - Operand quaternion. 213 | * @param b - Operand quaternion. 214 | * @returns dot product 215 | */ 216 | export declare function dot(a: Quat, b: Quat): number; 217 | /** 218 | * Performs linear interpolation on two quaternions. 219 | * Given quaternions a and b and interpolation coefficient t, returns 220 | * a + t * (b - a). 221 | * @param a - Operand quaternion. 222 | * @param b - Operand quaternion. 223 | * @param t - Interpolation coefficient. 224 | * @param dst - quaternion to hold result. If not passed in a new one is created. 225 | * @returns The linear interpolated result. 226 | */ 227 | export declare function lerp(a: Quat, b: Quat, t: number, dst?: Quat): Quat; 228 | /** 229 | * Computes the length of quaternion 230 | * @param v - quaternion. 231 | * @returns length of quaternion. 232 | */ 233 | export declare function length(v: Quat): number; 234 | /** 235 | * Computes the length of quaternion (same as length) 236 | * @param v - quaternion. 237 | * @returns length of quaternion. 238 | */ 239 | export declare const len: typeof length; 240 | /** 241 | * Computes the square of the length of quaternion 242 | * @param v - quaternion. 243 | * @returns square of the length of quaternion. 244 | */ 245 | export declare function lengthSq(v: Quat): number; 246 | /** 247 | * Computes the square of the length of quaternion (same as lengthSq) 248 | * @param v - quaternion. 249 | * @returns square of the length of quaternion. 250 | */ 251 | export declare const lenSq: typeof lengthSq; 252 | /** 253 | * Divides a quaternion by its Euclidean length and returns the quotient. 254 | * @param v - The quaternion. 255 | * @param dst - quaternion to hold result. If not passed in a new one is created. 256 | * @returns The normalized quaternion. 257 | */ 258 | export declare function normalize(v: Quat, dst?: Quat): Quat; 259 | /** 260 | * Check if 2 quaternions are approximately equal 261 | * @param a - Operand quaternion. 262 | * @param b - Operand quaternion. 263 | * @returns true if quaternions are approximately equal 264 | */ 265 | export declare function equalsApproximately(a: Quat, b: Quat): boolean; 266 | /** 267 | * Check if 2 quaternions are exactly equal 268 | * @param a - Operand quaternion. 269 | * @param b - Operand quaternion. 270 | * @returns true if quaternions are exactly equal 271 | */ 272 | export declare function equals(a: Quat, b: Quat): boolean; 273 | /** 274 | * Creates an identity quaternion 275 | * @param dst - quaternion to hold result. If not passed in a new one is created. 276 | * @returns an identity quaternion 277 | */ 278 | export declare function identity(dst?: Quat): Quat; 279 | /** 280 | * Computes a quaternion to represent the shortest rotation from one vector to another. 281 | * 282 | * @param aUnit - the start vector 283 | * @param bUnit - the end vector 284 | * @param dst - quaternion to hold result. If not passed in a new one is created. 285 | * @returns the result 286 | */ 287 | export declare function rotationTo(aUnit: Vec3, bUnit: Vec3, dst?: Quat): Quat; 288 | /** 289 | * Performs a spherical linear interpolation with two control points 290 | * 291 | * @param a - the first quaternion 292 | * @param b - the second quaternion 293 | * @param c - the third quaternion 294 | * @param d - the fourth quaternion 295 | * @param t - Interpolation coefficient 0 to 1 296 | * @returns result 297 | */ 298 | export declare function sqlerp(a: Quat, b: Quat, c: Quat, d: Quat, t: number, dst?: Quat): Quat; 299 | -------------------------------------------------------------------------------- /dist/1.x/vec4-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { ArrayLikeCtor } from './array-like'; 2 | import { Mat4 } from './mat4'; 3 | import { Vec4 } from './vec4'; 4 | export default Vec4; 5 | /** 6 | * Sets the type this library creates for a Vec4 7 | * @param ctor - the constructor for the type. Either `Float32Array`, 'Float64Array', or `Array` 8 | * @returns previous constructor for Vec4 9 | */ 10 | export declare function setDefaultType(ctor: new (n: number) => Vec4): ArrayLikeCtor; 11 | /** 12 | * Creates a vec4; may be called with x, y, z to set initial values. 13 | * @param x - Initial x value. 14 | * @param y - Initial y value. 15 | * @param z - Initial z value. 16 | * @param w - Initial w value. 17 | * @returns the created vector 18 | */ 19 | export declare function create(x?: number, y?: number, z?: number, w?: number): Vec4; 20 | /** 21 | * Creates a vec4; may be called with x, y, z to set initial values. (same as create) 22 | * @param x - Initial x value. 23 | * @param y - Initial y value. 24 | * @param z - Initial z value. 25 | * @param z - Initial w value. 26 | * @returns the created vector 27 | */ 28 | export declare const fromValues: typeof create; 29 | /** 30 | * Applies Math.ceil to each element of vector 31 | * @param v - Operand vector. 32 | * @param dst - vector to hold result. If not new one is created. 33 | * @returns A vector that is the ceil of each element of v. 34 | */ 35 | export declare function ceil(v: Vec4, dst?: Vec4): Vec4; 36 | /** 37 | * Applies Math.floor to each element of vector 38 | * @param v - Operand vector. 39 | * @param dst - vector to hold result. If not new one is created. 40 | * @returns A vector that is the floor of each element of v. 41 | */ 42 | export declare function floor(v: Vec4, dst?: Vec4): Vec4; 43 | /** 44 | * Applies Math.round to each element of vector 45 | * @param v - Operand vector. 46 | * @param dst - vector to hold result. If not new one is created. 47 | * @returns A vector that is the round of each element of v. 48 | */ 49 | export declare function round(v: Vec4, dst?: Vec4): Vec4; 50 | /** 51 | * Clamp each element of vector between min and max 52 | * @param v - Operand vector. 53 | * @param max - Min value, default 0 54 | * @param min - Max value, default 1 55 | * @param dst - vector to hold result. If not new one is created. 56 | * @returns A vector that the clamped value of each element of v. 57 | */ 58 | export declare function clamp(v: Vec4, min?: number, max?: number, dst?: Vec4): Vec4; 59 | /** 60 | * Adds two vectors; assumes a and b have the same dimension. 61 | * @param a - Operand vector. 62 | * @param b - Operand vector. 63 | * @param dst - vector to hold result. If not new one is created. 64 | * @returns A vector that is the sum of a and b. 65 | */ 66 | export declare function add(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 67 | /** 68 | * Adds two vectors, scaling the 2nd; assumes a and b have the same dimension. 69 | * @param a - Operand vector. 70 | * @param b - Operand vector. 71 | * @param scale - Amount to scale b 72 | * @param dst - vector to hold result. If not new one is created. 73 | * @returns A vector that is the sum of a + b * scale. 74 | */ 75 | export declare function addScaled(a: Vec4, b: Vec4, scale: number, dst?: Vec4): Vec4; 76 | /** 77 | * Subtracts two vectors. 78 | * @param a - Operand vector. 79 | * @param b - Operand vector. 80 | * @param dst - vector to hold result. If not new one is created. 81 | * @returns A vector that is the difference of a and b. 82 | */ 83 | export declare function subtract(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 84 | /** 85 | * Subtracts two vectors. 86 | * @param a - Operand vector. 87 | * @param b - Operand vector. 88 | * @param dst - vector to hold result. If not new one is created. 89 | * @returns A vector that is the difference of a and b. 90 | */ 91 | export declare const sub: typeof subtract; 92 | /** 93 | * Check if 2 vectors are approximately equal 94 | * @param a - Operand vector. 95 | * @param b - Operand vector. 96 | * @returns true if vectors are approximately equal 97 | */ 98 | export declare function equalsApproximately(a: Vec4, b: Vec4): boolean; 99 | /** 100 | * Check if 2 vectors are exactly equal 101 | * @param a - Operand vector. 102 | * @param b - Operand vector. 103 | * @returns true if vectors are exactly equal 104 | */ 105 | export declare function equals(a: Vec4, b: Vec4): boolean; 106 | /** 107 | * Performs linear interpolation on two vectors. 108 | * Given vectors a and b and interpolation coefficient t, returns 109 | * a + t * (b - a). 110 | * @param a - Operand vector. 111 | * @param b - Operand vector. 112 | * @param t - Interpolation coefficient. 113 | * @param dst - vector to hold result. If not new one is created. 114 | * @returns The linear interpolated result. 115 | */ 116 | export declare function lerp(a: Vec4, b: Vec4, t: number, dst?: Vec4): Vec4; 117 | /** 118 | * Performs linear interpolation on two vectors. 119 | * Given vectors a and b and interpolation coefficient vector t, returns 120 | * a + t * (b - a). 121 | * @param a - Operand vector. 122 | * @param b - Operand vector. 123 | * @param t - Interpolation coefficients vector. 124 | * @param dst - vector to hold result. If not new one is created. 125 | * @returns the linear interpolated result. 126 | */ 127 | export declare function lerpV(a: Vec4, b: Vec4, t: Vec4, dst?: Vec4): Vec4; 128 | /** 129 | * Return max values of two vectors. 130 | * Given vectors a and b returns 131 | * [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])]. 132 | * @param a - Operand vector. 133 | * @param b - Operand vector. 134 | * @param dst - vector to hold result. If not new one is created. 135 | * @returns The max components vector. 136 | */ 137 | export declare function max(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 138 | /** 139 | * Return min values of two vectors. 140 | * Given vectors a and b returns 141 | * [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])]. 142 | * @param a - Operand vector. 143 | * @param b - Operand vector. 144 | * @param dst - vector to hold result. If not new one is created. 145 | * @returns The min components vector. 146 | */ 147 | export declare function min(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 148 | /** 149 | * Multiplies a vector by a scalar. 150 | * @param v - The vector. 151 | * @param k - The scalar. 152 | * @param dst - vector to hold result. If not new one is created. 153 | * @returns The scaled vector. 154 | */ 155 | export declare function mulScalar(v: Vec4, k: number, dst?: Vec4): Vec4; 156 | /** 157 | * Multiplies a vector by a scalar. (same as mulScalar) 158 | * @param v - The vector. 159 | * @param k - The scalar. 160 | * @param dst - vector to hold result. If not new one is created. 161 | * @returns The scaled vector. 162 | */ 163 | export declare const scale: typeof mulScalar; 164 | /** 165 | * Divides a vector by a scalar. 166 | * @param v - The vector. 167 | * @param k - The scalar. 168 | * @param dst - vector to hold result. If not new one is created. 169 | * @returns The scaled vector. 170 | */ 171 | export declare function divScalar(v: Vec4, k: number, dst?: Vec4): Vec4; 172 | /** 173 | * Inverse a vector. 174 | * @param v - The vector. 175 | * @param dst - vector to hold result. If not new one is created. 176 | * @returns The inverted vector. 177 | */ 178 | export declare function inverse(v: Vec4, dst?: Vec4): Vec4; 179 | /** 180 | * Invert a vector. (same as inverse) 181 | * @param v - The vector. 182 | * @param dst - vector to hold result. If not new one is created. 183 | * @returns The inverted vector. 184 | */ 185 | export declare const invert: typeof inverse; 186 | /** 187 | * Computes the dot product of two vectors; assumes both vectors have 188 | * three entries. 189 | * @param a - Operand vector. 190 | * @param b - Operand vector. 191 | * @returns dot product 192 | */ 193 | export declare function dot(a: Vec4, b: Vec4): number; 194 | /** 195 | * Computes the length of vector 196 | * @param v - vector. 197 | * @returns length of vector. 198 | */ 199 | export declare function length(v: Vec4): number; 200 | /** 201 | * Computes the length of vector (same as length) 202 | * @param v - vector. 203 | * @returns length of vector. 204 | */ 205 | export declare const len: typeof length; 206 | /** 207 | * Computes the square of the length of vector 208 | * @param v - vector. 209 | * @returns square of the length of vector. 210 | */ 211 | export declare function lengthSq(v: Vec4): number; 212 | /** 213 | * Computes the square of the length of vector (same as lengthSq) 214 | * @param v - vector. 215 | * @returns square of the length of vector. 216 | */ 217 | export declare const lenSq: typeof lengthSq; 218 | /** 219 | * Computes the distance between 2 points 220 | * @param a - vector. 221 | * @param b - vector. 222 | * @returns distance between a and b 223 | */ 224 | export declare function distance(a: Vec4, b: Vec4): number; 225 | /** 226 | * Computes the distance between 2 points (same as distance) 227 | * @param a - vector. 228 | * @param b - vector. 229 | * @returns distance between a and b 230 | */ 231 | export declare const dist: typeof distance; 232 | /** 233 | * Computes the square of the distance between 2 points 234 | * @param a - vector. 235 | * @param b - vector. 236 | * @returns square of the distance between a and b 237 | */ 238 | export declare function distanceSq(a: Vec4, b: Vec4): number; 239 | /** 240 | * Computes the square of the distance between 2 points (same as distanceSq) 241 | * @param a - vector. 242 | * @param b - vector. 243 | * @returns square of the distance between a and b 244 | */ 245 | export declare const distSq: typeof distanceSq; 246 | /** 247 | * Divides a vector by its Euclidean length and returns the quotient. 248 | * @param v - The vector. 249 | * @param dst - vector to hold result. If not new one is created. 250 | * @returns The normalized vector. 251 | */ 252 | export declare function normalize(v: Vec4, dst?: Vec4): Vec4; 253 | /** 254 | * Negates a vector. 255 | * @param v - The vector. 256 | * @param dst - vector to hold result. If not new one is created. 257 | * @returns -v. 258 | */ 259 | export declare function negate(v: Vec4, dst?: Vec4): Vec4; 260 | /** 261 | * Copies a vector. (same as clone) 262 | * @param v - The vector. 263 | * @param dst - vector to hold result. If not new one is created. 264 | * @returns A copy of v. 265 | */ 266 | export declare function copy(v: Vec4, dst?: Vec4): Vec4; 267 | /** 268 | * Clones a vector. (same as copy) 269 | * @param v - The vector. 270 | * @param dst - vector to hold result. If not new one is created. 271 | * @returns A copy of v. 272 | */ 273 | export declare const clone: typeof copy; 274 | /** 275 | * Multiplies a vector by another vector (component-wise); assumes a and 276 | * b have the same length. 277 | * @param a - Operand vector. 278 | * @param b - Operand vector. 279 | * @param dst - vector to hold result. If not new one is created. 280 | * @returns The vector of products of entries of a and b. 281 | */ 282 | export declare function multiply(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 283 | /** 284 | * Multiplies a vector by another vector (component-wise); assumes a and 285 | * b have the same length. (same as mul) 286 | * @param a - Operand vector. 287 | * @param b - Operand vector. 288 | * @param dst - vector to hold result. If not new one is created. 289 | * @returns The vector of products of entries of a and b. 290 | */ 291 | export declare const mul: typeof multiply; 292 | /** 293 | * Divides a vector by another vector (component-wise); assumes a and 294 | * b have the same length. 295 | * @param a - Operand vector. 296 | * @param b - Operand vector. 297 | * @param dst - vector to hold result. If not new one is created. 298 | * @returns The vector of quotients of entries of a and b. 299 | */ 300 | export declare function divide(a: Vec4, b: Vec4, dst?: Vec4): import("./array-like").ArrayLike; 301 | /** 302 | * Divides a vector by another vector (component-wise); assumes a and 303 | * b have the same length. (same as divide) 304 | * @param a - Operand vector. 305 | * @param b - Operand vector. 306 | * @param dst - vector to hold result. If not new one is created. 307 | * @returns The vector of quotients of entries of a and b. 308 | */ 309 | export declare const div: typeof divide; 310 | /** 311 | * Zero's a vector 312 | * @param dst - vector to hold result. If not new one is created. 313 | * @returns The zeroed vector. 314 | */ 315 | export declare function zero(dst?: Vec4): Vec4; 316 | /** 317 | * transform vec4 by 4x4 matrix 318 | * @param v - the vector 319 | * @param m - The matrix. 320 | * @param dst - optional vec4 to store result. If not passed a new one is created. 321 | * @returns the transformed vector 322 | */ 323 | export declare function transformMat4(v: Vec4, m: Mat4, dst?: Vec4): Vec4; 324 | -------------------------------------------------------------------------------- /dist/1.x/vec2-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Mat3 } from './mat3'; 2 | import { Mat4 } from './mat4'; 3 | import { Vec2, create, setDefaultType } from './vec2'; 4 | export default Vec2; 5 | export { create, setDefaultType }; 6 | /** 7 | * Creates a Vec2; may be called with x, y, z to set initial values. (same as create) 8 | * @param x - Initial x value. 9 | * @param y - Initial y value. 10 | * @returns the created vector 11 | */ 12 | export declare const fromValues: typeof create; 13 | /** 14 | * Applies Math.ceil to each element of vector 15 | * @param v - Operand vector. 16 | * @param dst - vector to hold result. If not new one is created. 17 | * @returns A vector that is the ceil of each element of v. 18 | */ 19 | export declare function ceil(v: Vec2, dst?: Vec2): Vec2; 20 | /** 21 | * Applies Math.floor to each element of vector 22 | * @param v - Operand vector. 23 | * @param dst - vector to hold result. If not new one is created. 24 | * @returns A vector that is the floor of each element of v. 25 | */ 26 | export declare function floor(v: Vec2, dst?: Vec2): Vec2; 27 | /** 28 | * Applies Math.round to each element of vector 29 | * @param v - Operand vector. 30 | * @param dst - vector to hold result. If not new one is created. 31 | * @returns A vector that is the round of each element of v. 32 | */ 33 | export declare function round(v: Vec2, dst?: Vec2): Vec2; 34 | /** 35 | * Clamp each element of vector between min and max 36 | * @param v - Operand vector. 37 | * @param max - Min value, default 0 38 | * @param min - Max value, default 1 39 | * @param dst - vector to hold result. If not new one is created. 40 | * @returns A vector that the clamped value of each element of v. 41 | */ 42 | export declare function clamp(v: Vec2, min?: number, max?: number, dst?: Vec2): Vec2; 43 | /** 44 | * Adds two vectors; assumes a and b have the same dimension. 45 | * @param a - Operand vector. 46 | * @param b - Operand vector. 47 | * @param dst - vector to hold result. If not new one is created. 48 | * @returns A vector that is the sum of a and b. 49 | */ 50 | export declare function add(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 51 | /** 52 | * Adds two vectors, scaling the 2nd; assumes a and b have the same dimension. 53 | * @param a - Operand vector. 54 | * @param b - Operand vector. 55 | * @param scale - Amount to scale b 56 | * @param dst - vector to hold result. If not new one is created. 57 | * @returns A vector that is the sum of a + b * scale. 58 | */ 59 | export declare function addScaled(a: Vec2, b: Vec2, scale: number, dst?: Vec2): import("./array-like.js").ArrayLike; 60 | /** 61 | * Returns the angle in radians between two vectors. 62 | * @param a - Operand vector. 63 | * @param b - Operand vector. 64 | * @returns The angle in radians between the 2 vectors. 65 | */ 66 | export declare function angle(a: Vec2, b: Vec2): number; 67 | /** 68 | * Subtracts two vectors. 69 | * @param a - Operand vector. 70 | * @param b - Operand vector. 71 | * @param dst - vector to hold result. If not new one is created. 72 | * @returns A vector that is the difference of a and b. 73 | */ 74 | export declare function subtract(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 75 | /** 76 | * Subtracts two vectors. 77 | * @param a - Operand vector. 78 | * @param b - Operand vector. 79 | * @param dst - vector to hold result. If not new one is created. 80 | * @returns A vector that is the difference of a and b. 81 | */ 82 | export declare const sub: typeof subtract; 83 | /** 84 | * Check if 2 vectors are approximately equal 85 | * @param a - Operand vector. 86 | * @param b - Operand vector. 87 | * @returns true if vectors are approximately equal 88 | */ 89 | export declare function equalsApproximately(a: Vec2, b: Vec2): boolean; 90 | /** 91 | * Check if 2 vectors are exactly equal 92 | * @param a - Operand vector. 93 | * @param b - Operand vector. 94 | * @returns true if vectors are exactly equal 95 | */ 96 | export declare function equals(a: Vec2, b: Vec2): boolean; 97 | /** 98 | * Performs linear interpolation on two vectors. 99 | * Given vectors a and b and interpolation coefficient t, returns 100 | * a + t * (b - a). 101 | * @param a - Operand vector. 102 | * @param b - Operand vector. 103 | * @param t - Interpolation coefficient. 104 | * @param dst - vector to hold result. If not new one is created. 105 | * @returns The linear interpolated result. 106 | */ 107 | export declare function lerp(a: Vec2, b: Vec2, t: number, dst?: Vec2): Vec2; 108 | /** 109 | * Performs linear interpolation on two vectors. 110 | * Given vectors a and b and interpolation coefficient vector t, returns 111 | * a + t * (b - a). 112 | * @param a - Operand vector. 113 | * @param b - Operand vector. 114 | * @param t - Interpolation coefficients vector. 115 | * @param dst - vector to hold result. If not new one is created. 116 | * @returns the linear interpolated result. 117 | */ 118 | export declare function lerpV(a: Vec2, b: Vec2, t: Vec2, dst?: Vec2): Vec2; 119 | /** 120 | * Return max values of two vectors. 121 | * Given vectors a and b returns 122 | * [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])]. 123 | * @param a - Operand vector. 124 | * @param b - Operand vector. 125 | * @param dst - vector to hold result. If not new one is created. 126 | * @returns The max components vector. 127 | */ 128 | export declare function max(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 129 | /** 130 | * Return min values of two vectors. 131 | * Given vectors a and b returns 132 | * [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])]. 133 | * @param a - Operand vector. 134 | * @param b - Operand vector. 135 | * @param dst - vector to hold result. If not new one is created. 136 | * @returns The min components vector. 137 | */ 138 | export declare function min(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 139 | /** 140 | * Multiplies a vector by a scalar. 141 | * @param v - The vector. 142 | * @param k - The scalar. 143 | * @param dst - vector to hold result. If not new one is created. 144 | * @returns The scaled vector. 145 | */ 146 | export declare function mulScalar(v: Vec2, k: number, dst?: Vec2): Vec2; 147 | /** 148 | * Multiplies a vector by a scalar. (same as mulScalar) 149 | * @param v - The vector. 150 | * @param k - The scalar. 151 | * @param dst - vector to hold result. If not new one is created. 152 | * @returns The scaled vector. 153 | */ 154 | export declare const scale: typeof mulScalar; 155 | /** 156 | * Divides a vector by a scalar. 157 | * @param v - The vector. 158 | * @param k - The scalar. 159 | * @param dst - vector to hold result. If not new one is created. 160 | * @returns The scaled vector. 161 | */ 162 | export declare function divScalar(v: Vec2, k: number, dst?: Vec2): Vec2; 163 | /** 164 | * Inverse a vector. 165 | * @param v - The vector. 166 | * @param dst - vector to hold result. If not new one is created. 167 | * @returns The inverted vector. 168 | */ 169 | export declare function inverse(v: Vec2, dst?: Vec2): Vec2; 170 | /** 171 | * Invert a vector. (same as inverse) 172 | * @param v - The vector. 173 | * @param dst - vector to hold result. If not new one is created. 174 | * @returns The inverted vector. 175 | */ 176 | export declare const invert: typeof inverse; 177 | /** 178 | * Computes the cross product of two vectors; assumes both vectors have 179 | * three entries. 180 | * @param a - Operand vector. 181 | * @param b - Operand vector. 182 | * @param dst - vector to hold result. If not new one is created. 183 | * @returns The vector of a cross b. 184 | */ 185 | export declare function cross(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 186 | /** 187 | * Computes the dot product of two vectors; assumes both vectors have 188 | * three entries. 189 | * @param a - Operand vector. 190 | * @param b - Operand vector. 191 | * @returns dot product 192 | */ 193 | export declare function dot(a: Vec2, b: Vec2): number; 194 | /** 195 | * Computes the length of vector 196 | * @param v - vector. 197 | * @returns length of vector. 198 | */ 199 | export declare function length(v: Vec2): number; 200 | /** 201 | * Computes the length of vector (same as length) 202 | * @param v - vector. 203 | * @returns length of vector. 204 | */ 205 | export declare const len: typeof length; 206 | /** 207 | * Computes the square of the length of vector 208 | * @param v - vector. 209 | * @returns square of the length of vector. 210 | */ 211 | export declare function lengthSq(v: Vec2): number; 212 | /** 213 | * Computes the square of the length of vector (same as lengthSq) 214 | * @param v - vector. 215 | * @returns square of the length of vector. 216 | */ 217 | export declare const lenSq: typeof lengthSq; 218 | /** 219 | * Computes the distance between 2 points 220 | * @param a - vector. 221 | * @param b - vector. 222 | * @returns distance between a and b 223 | */ 224 | export declare function distance(a: Vec2, b: Vec2): number; 225 | /** 226 | * Computes the distance between 2 points (same as distance) 227 | * @param a - vector. 228 | * @param b - vector. 229 | * @returns distance between a and b 230 | */ 231 | export declare const dist: typeof distance; 232 | /** 233 | * Computes the square of the distance between 2 points 234 | * @param a - vector. 235 | * @param b - vector. 236 | * @returns square of the distance between a and b 237 | */ 238 | export declare function distanceSq(a: Vec2, b: Vec2): number; 239 | /** 240 | * Computes the square of the distance between 2 points (same as distanceSq) 241 | * @param a - vector. 242 | * @param b - vector. 243 | * @returns square of the distance between a and b 244 | */ 245 | export declare const distSq: typeof distanceSq; 246 | /** 247 | * Divides a vector by its Euclidean length and returns the quotient. 248 | * @param v - The vector. 249 | * @param dst - vector to hold result. If not new one is created. 250 | * @returns The normalized vector. 251 | */ 252 | export declare function normalize(v: Vec2, dst?: Vec2): Vec2; 253 | /** 254 | * Negates a vector. 255 | * @param v - The vector. 256 | * @param dst - vector to hold result. If not new one is created. 257 | * @returns -v. 258 | */ 259 | export declare function negate(v: Vec2, dst?: Vec2): Vec2; 260 | /** 261 | * Copies a vector. (same as clone) 262 | * @param v - The vector. 263 | * @param dst - vector to hold result. If not new one is created. 264 | * @returns A copy of v. 265 | */ 266 | export declare function copy(v: Vec2, dst?: Vec2): Vec2; 267 | /** 268 | * Clones a vector. (same as copy) 269 | * @param v - The vector. 270 | * @param dst - vector to hold result. If not new one is created. 271 | * @returns A copy of v. 272 | */ 273 | export declare const clone: typeof copy; 274 | /** 275 | * Multiplies a vector by another vector (component-wise); assumes a and 276 | * b have the same length. 277 | * @param a - Operand vector. 278 | * @param b - Operand vector. 279 | * @param dst - vector to hold result. If not new one is created. 280 | * @returns The vector of products of entries of a and b. 281 | */ 282 | export declare function multiply(a: Vec2, b: Vec2, dst?: Vec2): import("./array-like.js").ArrayLike; 283 | /** 284 | * Multiplies a vector by another vector (component-wise); assumes a and 285 | * b have the same length. (same as mul) 286 | * @param a - Operand vector. 287 | * @param b - Operand vector. 288 | * @param dst - vector to hold result. If not new one is created. 289 | * @returns The vector of products of entries of a and b. 290 | */ 291 | export declare const mul: typeof multiply; 292 | /** 293 | * Divides a vector by another vector (component-wise); assumes a and 294 | * b have the same length. 295 | * @param a - Operand vector. 296 | * @param b - Operand vector. 297 | * @param dst - vector to hold result. If not new one is created. 298 | * @returns The vector of quotients of entries of a and b. 299 | */ 300 | export declare function divide(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 301 | /** 302 | * Divides a vector by another vector (component-wise); assumes a and 303 | * b have the same length. (same as divide) 304 | * @param a - Operand vector. 305 | * @param b - Operand vector. 306 | * @param dst - vector to hold result. If not new one is created. 307 | * @returns The vector of quotients of entries of a and b. 308 | */ 309 | export declare const div: typeof divide; 310 | /** 311 | * Creates a random unit vector * scale 312 | * @param scale - Default 1 313 | * @param dst - vector to hold result. If not new one is created. 314 | * @returns The random vector. 315 | */ 316 | export declare function random(scale?: number, dst?: Vec2): Vec2; 317 | /** 318 | * Zero's a vector 319 | * @param dst - vector to hold result. If not new one is created. 320 | * @returns The zeroed vector. 321 | */ 322 | export declare function zero(dst?: Vec2): Vec2; 323 | /** 324 | * transform Vec2 by 4x4 matrix 325 | * @param v - the vector 326 | * @param m - The matrix. 327 | * @param dst - optional Vec2 to store result. If not passed a new one is created. 328 | * @returns the transformed vector 329 | */ 330 | export declare function transformMat4(v: Vec2, m: Mat4, dst?: Vec2): Vec2; 331 | /** 332 | * Transforms vec4 by 3x3 matrix 333 | * 334 | * @param v - the vector 335 | * @param m - The matrix. 336 | * @param dst - optional Vec2 to store result. If not passed a new one is created. 337 | * @returns the transformed vector 338 | */ 339 | export declare function transformMat3(v: Vec2, m: Mat3, dst?: Vec2): Vec2; 340 | -------------------------------------------------------------------------------- /dist/1.x/vec3-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Vec3, create, setDefaultType } from './vec3'; 2 | import { Mat3 } from './mat3'; 3 | import { Mat4 } from './mat4'; 4 | export default Vec3; 5 | export { create, setDefaultType }; 6 | /** 7 | * Creates a vec3; may be called with x, y, z to set initial values. (same as create) 8 | * @param x - Initial x value. 9 | * @param y - Initial y value. 10 | * @param z - Initial z value. 11 | * @returns the created vector 12 | */ 13 | export declare const fromValues: typeof create; 14 | /** 15 | * Applies Math.ceil to each element of vector 16 | * @param v - Operand vector. 17 | * @param dst - vector to hold result. If not new one is created. 18 | * @returns A vector that is the ceil of each element of v. 19 | */ 20 | export declare function ceil(v: Vec3, dst?: Vec3): Vec3; 21 | /** 22 | * Applies Math.floor to each element of vector 23 | * @param v - Operand vector. 24 | * @param dst - vector to hold result. If not new one is created. 25 | * @returns A vector that is the floor of each element of v. 26 | */ 27 | export declare function floor(v: Vec3, dst?: Vec3): Vec3; 28 | /** 29 | * Applies Math.round to each element of vector 30 | * @param v - Operand vector. 31 | * @param dst - vector to hold result. If not new one is created. 32 | * @returns A vector that is the round of each element of v. 33 | */ 34 | export declare function round(v: Vec3, dst?: Vec3): Vec3; 35 | /** 36 | * Clamp each element of vector between min and max 37 | * @param v - Operand vector. 38 | * @param max - Min value, default 0 39 | * @param min - Max value, default 1 40 | * @param dst - vector to hold result. If not new one is created. 41 | * @returns A vector that the clamped value of each element of v. 42 | */ 43 | export declare function clamp(v: Vec3, min?: number, max?: number, dst?: Vec3): Vec3; 44 | /** 45 | * Adds two vectors; assumes a and b have the same dimension. 46 | * @param a - Operand vector. 47 | * @param b - Operand vector. 48 | * @param dst - vector to hold result. If not new one is created. 49 | * @returns A vector that is the sum of a and b. 50 | */ 51 | export declare function add(a: Vec3, b: Vec3, dst?: Vec3): import("./array-like.js").ArrayLike; 52 | /** 53 | * Adds two vectors, scaling the 2nd; assumes a and b have the same dimension. 54 | * @param a - Operand vector. 55 | * @param b - Operand vector. 56 | * @param scale - Amount to scale b 57 | * @param dst - vector to hold result. If not new one is created. 58 | * @returns A vector that is the sum of a + b * scale. 59 | */ 60 | export declare function addScaled(a: Vec3, b: Vec3, scale: number, dst?: Vec3): Vec3; 61 | /** 62 | * Returns the angle in radians between two vectors. 63 | * @param a - Operand vector. 64 | * @param b - Operand vector. 65 | * @returns The angle in radians between the 2 vectors. 66 | */ 67 | export declare function angle(a: Vec3, b: Vec3): number; 68 | /** 69 | * Subtracts two vectors. 70 | * @param a - Operand vector. 71 | * @param b - Operand vector. 72 | * @param dst - vector to hold result. If not new one is created. 73 | * @returns A vector that is the difference of a and b. 74 | */ 75 | export declare function subtract(a: Vec3, b: Vec3, dst?: Vec3): Vec3; 76 | /** 77 | * Subtracts two vectors. 78 | * @param a - Operand vector. 79 | * @param b - Operand vector. 80 | * @param dst - vector to hold result. If not new one is created. 81 | * @returns A vector that is the difference of a and b. 82 | */ 83 | export declare const sub: typeof subtract; 84 | /** 85 | * Check if 2 vectors are approximately equal 86 | * @param a - Operand vector. 87 | * @param b - Operand vector. 88 | * @returns true if vectors are approximately equal 89 | */ 90 | export declare function equalsApproximately(a: Vec3, b: Vec3): boolean; 91 | /** 92 | * Check if 2 vectors are exactly equal 93 | * @param a - Operand vector. 94 | * @param b - Operand vector. 95 | * @returns true if vectors are exactly equal 96 | */ 97 | export declare function equals(a: Vec3, b: Vec3): boolean; 98 | /** 99 | * Performs linear interpolation on two vectors. 100 | * Given vectors a and b and interpolation coefficient t, returns 101 | * a + t * (b - a). 102 | * @param a - Operand vector. 103 | * @param b - Operand vector. 104 | * @param t - Interpolation coefficient. 105 | * @param dst - vector to hold result. If not new one is created. 106 | * @returns The linear interpolated result. 107 | */ 108 | export declare function lerp(a: Vec3, b: Vec3, t: number, dst?: Vec3): Vec3; 109 | /** 110 | * Performs linear interpolation on two vectors. 111 | * Given vectors a and b and interpolation coefficient vector t, returns 112 | * a + t * (b - a). 113 | * @param a - Operand vector. 114 | * @param b - Operand vector. 115 | * @param t - Interpolation coefficients vector. 116 | * @param dst - vector to hold result. If not new one is created. 117 | * @returns the linear interpolated result. 118 | */ 119 | export declare function lerpV(a: Vec3, b: Vec3, t: Vec3, dst?: Vec3): Vec3; 120 | /** 121 | * Return max values of two vectors. 122 | * Given vectors a and b returns 123 | * [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])]. 124 | * @param a - Operand vector. 125 | * @param b - Operand vector. 126 | * @param dst - vector to hold result. If not new one is created. 127 | * @returns The max components vector. 128 | */ 129 | export declare function max(a: Vec3, b: Vec3, dst?: Vec3): Vec3; 130 | /** 131 | * Return min values of two vectors. 132 | * Given vectors a and b returns 133 | * [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])]. 134 | * @param a - Operand vector. 135 | * @param b - Operand vector. 136 | * @param dst - vector to hold result. If not new one is created. 137 | * @returns The min components vector. 138 | */ 139 | export declare function min(a: Vec3, b: Vec3, dst?: Vec3): Vec3; 140 | /** 141 | * Multiplies a vector by a scalar. 142 | * @param v - The vector. 143 | * @param k - The scalar. 144 | * @param dst - vector to hold result. If not new one is created. 145 | * @returns The scaled vector. 146 | */ 147 | export declare function mulScalar(v: Vec3, k: number, dst?: Vec3): Vec3; 148 | /** 149 | * Multiplies a vector by a scalar. (same as mulScalar) 150 | * @param v - The vector. 151 | * @param k - The scalar. 152 | * @param dst - vector to hold result. If not new one is created. 153 | * @returns The scaled vector. 154 | */ 155 | export declare const scale: typeof mulScalar; 156 | /** 157 | * Divides a vector by a scalar. 158 | * @param v - The vector. 159 | * @param k - The scalar. 160 | * @param dst - vector to hold result. If not new one is created. 161 | * @returns The scaled vector. 162 | */ 163 | export declare function divScalar(v: Vec3, k: number, dst?: Vec3): Vec3; 164 | /** 165 | * Inverse a vector. 166 | * @param v - The vector. 167 | * @param dst - vector to hold result. If not new one is created. 168 | * @returns The inverted vector. 169 | */ 170 | export declare function inverse(v: Vec3, dst?: Vec3): Vec3; 171 | /** 172 | * Invert a vector. (same as inverse) 173 | * @param v - The vector. 174 | * @param dst - vector to hold result. If not new one is created. 175 | * @returns The inverted vector. 176 | */ 177 | export declare const invert: typeof inverse; 178 | /** 179 | * Computes the cross product of two vectors; assumes both vectors have 180 | * three entries. 181 | * @param a - Operand vector. 182 | * @param b - Operand vector. 183 | * @param dst - vector to hold result. If not new one is created. 184 | * @returns The vector of a cross b. 185 | */ 186 | export declare function cross(a: Vec3, b: Vec3, dst?: Vec3): Vec3; 187 | /** 188 | * Computes the dot product of two vectors; assumes both vectors have 189 | * three entries. 190 | * @param a - Operand vector. 191 | * @param b - Operand vector. 192 | * @returns dot product 193 | */ 194 | export declare function dot(a: Vec3, b: Vec3): number; 195 | /** 196 | * Computes the length of vector 197 | * @param v - vector. 198 | * @returns length of vector. 199 | */ 200 | export declare function length(v: Vec3): number; 201 | /** 202 | * Computes the length of vector (same as length) 203 | * @param v - vector. 204 | * @returns length of vector. 205 | */ 206 | export declare const len: typeof length; 207 | /** 208 | * Computes the square of the length of vector 209 | * @param v - vector. 210 | * @returns square of the length of vector. 211 | */ 212 | export declare function lengthSq(v: Vec3): number; 213 | /** 214 | * Computes the square of the length of vector (same as lengthSq) 215 | * @param v - vector. 216 | * @returns square of the length of vector. 217 | */ 218 | export declare const lenSq: typeof lengthSq; 219 | /** 220 | * Computes the distance between 2 points 221 | * @param a - vector. 222 | * @param b - vector. 223 | * @returns distance between a and b 224 | */ 225 | export declare function distance(a: Vec3, b: Vec3): number; 226 | /** 227 | * Computes the distance between 2 points (same as distance) 228 | * @param a - vector. 229 | * @param b - vector. 230 | * @returns distance between a and b 231 | */ 232 | export declare const dist: typeof distance; 233 | /** 234 | * Computes the square of the distance between 2 points 235 | * @param a - vector. 236 | * @param b - vector. 237 | * @returns square of the distance between a and b 238 | */ 239 | export declare function distanceSq(a: Vec3, b: Vec3): number; 240 | /** 241 | * Computes the square of the distance between 2 points (same as distanceSq) 242 | * @param a - vector. 243 | * @param b - vector. 244 | * @returns square of the distance between a and b 245 | */ 246 | export declare const distSq: typeof distanceSq; 247 | /** 248 | * Divides a vector by its Euclidean length and returns the quotient. 249 | * @param v - The vector. 250 | * @param dst - vector to hold result. If not new one is created. 251 | * @returns The normalized vector. 252 | */ 253 | export declare function normalize(v: Vec3, dst?: Vec3): Vec3; 254 | /** 255 | * Negates a vector. 256 | * @param v - The vector. 257 | * @param dst - vector to hold result. If not new one is created. 258 | * @returns -v. 259 | */ 260 | export declare function negate(v: Vec3, dst?: Vec3): Vec3; 261 | /** 262 | * Copies a vector. (same as clone) 263 | * @param v - The vector. 264 | * @param dst - vector to hold result. If not new one is created. 265 | * @returns A copy of v. 266 | */ 267 | export declare function copy(v: Vec3, dst?: Vec3): Vec3; 268 | /** 269 | * Clones a vector. (same as copy) 270 | * @param v - The vector. 271 | * @param dst - vector to hold result. If not new one is created. 272 | * @returns A copy of v. 273 | */ 274 | export declare const clone: typeof copy; 275 | /** 276 | * Multiplies a vector by another vector (component-wise); assumes a and 277 | * b have the same length. 278 | * @param a - Operand vector. 279 | * @param b - Operand vector. 280 | * @param dst - vector to hold result. If not new one is created. 281 | * @returns The vector of products of entries of a and b. 282 | */ 283 | export declare function multiply(a: Vec3, b: Vec3, dst?: Vec3): Vec3; 284 | /** 285 | * Multiplies a vector by another vector (component-wise); assumes a and 286 | * b have the same length. (same as mul) 287 | * @param a - Operand vector. 288 | * @param b - Operand vector. 289 | * @param dst - vector to hold result. If not new one is created. 290 | * @returns The vector of products of entries of a and b. 291 | */ 292 | export declare const mul: typeof multiply; 293 | /** 294 | * Divides a vector by another vector (component-wise); assumes a and 295 | * b have the same length. 296 | * @param a - Operand vector. 297 | * @param b - Operand vector. 298 | * @param dst - vector to hold result. If not new one is created. 299 | * @returns The vector of quotients of entries of a and b. 300 | */ 301 | export declare function divide(a: Vec3, b: Vec3, dst?: Vec3): import("./array-like.js").ArrayLike; 302 | /** 303 | * Divides a vector by another vector (component-wise); assumes a and 304 | * b have the same length. (same as divide) 305 | * @param a - Operand vector. 306 | * @param b - Operand vector. 307 | * @param dst - vector to hold result. If not new one is created. 308 | * @returns The vector of quotients of entries of a and b. 309 | */ 310 | export declare const div: typeof divide; 311 | /** 312 | * Creates a random vector 313 | * @param scale - Default 1 314 | * @param dst - vector to hold result. If not new one is created. 315 | * @returns The random vector. 316 | */ 317 | export declare function random(scale?: number, dst?: Vec3): Vec3; 318 | /** 319 | * Zero's a vector 320 | * @param dst - vector to hold result. If not new one is created. 321 | * @returns The zeroed vector. 322 | */ 323 | export declare function zero(dst?: Vec3): Vec3; 324 | /** 325 | * transform vec3 by 4x4 matrix 326 | * @param v - the vector 327 | * @param m - The matrix. 328 | * @param dst - optional vec3 to store result. If not passed a new one is created. 329 | * @returns the transformed vector 330 | */ 331 | export declare function transformMat4(v: Vec3, m: Mat4, dst?: Vec3): Vec3; 332 | /** 333 | * Transform vec4 by upper 3x3 matrix inside 4x4 matrix. 334 | * @param v - The direction. 335 | * @param m - The matrix. 336 | * @param dst - optional Vec3 to store result. If not passed a new one is created. 337 | * @returns The transformed vector. 338 | */ 339 | export declare function transformMat4Upper3x3(v: Vec3, m: Mat4, dst?: Vec3): Vec3; 340 | /** 341 | * Transforms vec4 by 3x3 matrix 342 | * 343 | * @param v - the vector 344 | * @param m - The matrix. 345 | * @param dst - optional vec3 to store result. If not passed a new one is created. 346 | * @returns the transformed vector 347 | */ 348 | export declare function transformMat3(v: Vec3, m: Mat3, dst?: Vec3): Vec3; 349 | -------------------------------------------------------------------------------- /dist/2.x/vec4-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Vec4, create, setDefaultType } from './vec4'; 2 | import { Mat4 } from './mat4'; 3 | export default Vec4; 4 | export { create, setDefaultType }; 5 | /** 6 | * Creates a vec4; may be called with x, y, z to set initial values. (same as create) 7 | * @param x - Initial x value. 8 | * @param y - Initial y value. 9 | * @param z - Initial z value. 10 | * @param z - Initial w value. 11 | * @returns the created vector 12 | */ 13 | export declare const fromValues: typeof create; 14 | /** 15 | * Sets the values of a Vec4 16 | * Also see {@link vec4.create} and {@link vec4.copy} 17 | * 18 | * @param x first value 19 | * @param y second value 20 | * @param z third value 21 | * @param w fourth value 22 | * @param dst - vector to hold result. If not passed in a new one is created. 23 | * @returns A vector with its elements set. 24 | */ 25 | export declare function set(x: number, y: number, z: number, w: number, dst?: Vec4): Vec4; 26 | /** 27 | * Applies Math.ceil to each element of vector 28 | * @param v - Operand vector. 29 | * @param dst - vector to hold result. If not passed in a new one is created. 30 | * @returns A vector that is the ceil of each element of v. 31 | */ 32 | export declare function ceil(v: Vec4, dst?: Vec4): Vec4; 33 | /** 34 | * Applies Math.floor to each element of vector 35 | * @param v - Operand vector. 36 | * @param dst - vector to hold result. If not passed in a new one is created. 37 | * @returns A vector that is the floor of each element of v. 38 | */ 39 | export declare function floor(v: Vec4, dst?: Vec4): Vec4; 40 | /** 41 | * Applies Math.round to each element of vector 42 | * @param v - Operand vector. 43 | * @param dst - vector to hold result. If not passed in a new one is created. 44 | * @returns A vector that is the round of each element of v. 45 | */ 46 | export declare function round(v: Vec4, dst?: Vec4): Vec4; 47 | /** 48 | * Clamp each element of vector between min and max 49 | * @param v - Operand vector. 50 | * @param max - Min value, default 0 51 | * @param min - Max value, default 1 52 | * @param dst - vector to hold result. If not passed in a new one is created. 53 | * @returns A vector that the clamped value of each element of v. 54 | */ 55 | export declare function clamp(v: Vec4, min?: number, max?: number, dst?: Vec4): Vec4; 56 | /** 57 | * Adds two vectors; assumes a and b have the same dimension. 58 | * @param a - Operand vector. 59 | * @param b - Operand vector. 60 | * @param dst - vector to hold result. If not passed in a new one is created. 61 | * @returns A vector that is the sum of a and b. 62 | */ 63 | export declare function add(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 64 | /** 65 | * Adds two vectors, scaling the 2nd; assumes a and b have the same dimension. 66 | * @param a - Operand vector. 67 | * @param b - Operand vector. 68 | * @param scale - Amount to scale b 69 | * @param dst - vector to hold result. If not passed in a new one is created. 70 | * @returns A vector that is the sum of a + b * scale. 71 | */ 72 | export declare function addScaled(a: Vec4, b: Vec4, scale: number, dst?: Vec4): Vec4; 73 | /** 74 | * Subtracts two vectors. 75 | * @param a - Operand vector. 76 | * @param b - Operand vector. 77 | * @param dst - vector to hold result. If not passed in a new one is created. 78 | * @returns A vector that is the difference of a and b. 79 | */ 80 | export declare function subtract(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 81 | /** 82 | * Subtracts two vectors. 83 | * @param a - Operand vector. 84 | * @param b - Operand vector. 85 | * @param dst - vector to hold result. If not passed in a new one is created. 86 | * @returns A vector that is the difference of a and b. 87 | */ 88 | export declare const sub: typeof subtract; 89 | /** 90 | * Check if 2 vectors are approximately equal 91 | * @param a - Operand vector. 92 | * @param b - Operand vector. 93 | * @returns true if vectors are approximately equal 94 | */ 95 | export declare function equalsApproximately(a: Vec4, b: Vec4): boolean; 96 | /** 97 | * Check if 2 vectors are exactly equal 98 | * @param a - Operand vector. 99 | * @param b - Operand vector. 100 | * @returns true if vectors are exactly equal 101 | */ 102 | export declare function equals(a: Vec4, b: Vec4): boolean; 103 | /** 104 | * Performs linear interpolation on two vectors. 105 | * Given vectors a and b and interpolation coefficient t, returns 106 | * a + t * (b - a). 107 | * @param a - Operand vector. 108 | * @param b - Operand vector. 109 | * @param t - Interpolation coefficient. 110 | * @param dst - vector to hold result. If not passed in a new one is created. 111 | * @returns The linear interpolated result. 112 | */ 113 | export declare function lerp(a: Vec4, b: Vec4, t: number, dst?: Vec4): Vec4; 114 | /** 115 | * Performs linear interpolation on two vectors. 116 | * Given vectors a and b and interpolation coefficient vector t, returns 117 | * a + t * (b - a). 118 | * @param a - Operand vector. 119 | * @param b - Operand vector. 120 | * @param t - Interpolation coefficients vector. 121 | * @param dst - vector to hold result. If not passed in a new one is created. 122 | * @returns the linear interpolated result. 123 | */ 124 | export declare function lerpV(a: Vec4, b: Vec4, t: Vec4, dst?: Vec4): Vec4; 125 | /** 126 | * Return max values of two vectors. 127 | * Given vectors a and b returns 128 | * [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])]. 129 | * @param a - Operand vector. 130 | * @param b - Operand vector. 131 | * @param dst - vector to hold result. If not passed in a new one is created. 132 | * @returns The max components vector. 133 | */ 134 | export declare function max(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 135 | /** 136 | * Return min values of two vectors. 137 | * Given vectors a and b returns 138 | * [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])]. 139 | * @param a - Operand vector. 140 | * @param b - Operand vector. 141 | * @param dst - vector to hold result. If not passed in a new one is created. 142 | * @returns The min components vector. 143 | */ 144 | export declare function min(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 145 | /** 146 | * Multiplies a vector by a scalar. 147 | * @param v - The vector. 148 | * @param k - The scalar. 149 | * @param dst - vector to hold result. If not passed in a new one is created. 150 | * @returns The scaled vector. 151 | */ 152 | export declare function mulScalar(v: Vec4, k: number, dst?: Vec4): Vec4; 153 | /** 154 | * Multiplies a vector by a scalar. (same as mulScalar) 155 | * @param v - The vector. 156 | * @param k - The scalar. 157 | * @param dst - vector to hold result. If not passed in a new one is created. 158 | * @returns The scaled vector. 159 | */ 160 | export declare const scale: typeof mulScalar; 161 | /** 162 | * Divides a vector by a scalar. 163 | * @param v - The vector. 164 | * @param k - The scalar. 165 | * @param dst - vector to hold result. If not passed in a new one is created. 166 | * @returns The scaled vector. 167 | */ 168 | export declare function divScalar(v: Vec4, k: number, dst?: Vec4): Vec4; 169 | /** 170 | * Inverse a vector. 171 | * @param v - The vector. 172 | * @param dst - vector to hold result. If not passed in a new one is created. 173 | * @returns The inverted vector. 174 | */ 175 | export declare function inverse(v: Vec4, dst?: Vec4): Vec4; 176 | /** 177 | * Invert a vector. (same as inverse) 178 | * @param v - The vector. 179 | * @param dst - vector to hold result. If not passed in a new one is created. 180 | * @returns The inverted vector. 181 | */ 182 | export declare const invert: typeof inverse; 183 | /** 184 | * Computes the dot product of two vectors 185 | * @param a - Operand vector. 186 | * @param b - Operand vector. 187 | * @returns dot product 188 | */ 189 | export declare function dot(a: Vec4, b: Vec4): number; 190 | /** 191 | * Computes the length of vector 192 | * @param v - vector. 193 | * @returns length of vector. 194 | */ 195 | export declare function length(v: Vec4): number; 196 | /** 197 | * Computes the length of vector (same as length) 198 | * @param v - vector. 199 | * @returns length of vector. 200 | */ 201 | export declare const len: typeof length; 202 | /** 203 | * Computes the square of the length of vector 204 | * @param v - vector. 205 | * @returns square of the length of vector. 206 | */ 207 | export declare function lengthSq(v: Vec4): number; 208 | /** 209 | * Computes the square of the length of vector (same as lengthSq) 210 | * @param v - vector. 211 | * @returns square of the length of vector. 212 | */ 213 | export declare const lenSq: typeof lengthSq; 214 | /** 215 | * Computes the distance between 2 points 216 | * @param a - vector. 217 | * @param b - vector. 218 | * @returns distance between a and b 219 | */ 220 | export declare function distance(a: Vec4, b: Vec4): number; 221 | /** 222 | * Computes the distance between 2 points (same as distance) 223 | * @param a - vector. 224 | * @param b - vector. 225 | * @returns distance between a and b 226 | */ 227 | export declare const dist: typeof distance; 228 | /** 229 | * Computes the square of the distance between 2 points 230 | * @param a - vector. 231 | * @param b - vector. 232 | * @returns square of the distance between a and b 233 | */ 234 | export declare function distanceSq(a: Vec4, b: Vec4): number; 235 | /** 236 | * Computes the square of the distance between 2 points (same as distanceSq) 237 | * @param a - vector. 238 | * @param b - vector. 239 | * @returns square of the distance between a and b 240 | */ 241 | export declare const distSq: typeof distanceSq; 242 | /** 243 | * Divides a vector by its Euclidean length and returns the quotient. 244 | * @param v - The vector. 245 | * @param dst - vector to hold result. If not passed in a new one is created. 246 | * @returns The normalized vector. 247 | */ 248 | export declare function normalize(v: Vec4, dst?: Vec4): Vec4; 249 | /** 250 | * Negates a vector. 251 | * @param v - The vector. 252 | * @param dst - vector to hold result. If not passed in a new one is created. 253 | * @returns -v. 254 | */ 255 | export declare function negate(v: Vec4, dst?: Vec4): Vec4; 256 | /** 257 | * Copies a vector. (same as {@link vec4.clone}) 258 | * Also see {@link vec4.create} and {@link vec4.set} 259 | * @param v - The vector. 260 | * @param dst - vector to hold result. If not passed in a new one is created. 261 | * @returns A copy of v. 262 | */ 263 | export declare function copy(v: Vec4, dst?: Vec4): Vec4; 264 | /** 265 | * Clones a vector. (same as {@link vec4.copy}) 266 | * Also see {@link vec4.create} and {@link vec4.set} 267 | * @param v - The vector. 268 | * @param dst - vector to hold result. If not passed in a new one is created. 269 | * @returns A copy of v. 270 | */ 271 | export declare const clone: typeof copy; 272 | /** 273 | * Multiplies a vector by another vector (component-wise); assumes a and 274 | * b have the same length. 275 | * @param a - Operand vector. 276 | * @param b - Operand vector. 277 | * @param dst - vector to hold result. If not passed in a new one is created. 278 | * @returns The vector of products of entries of a and b. 279 | */ 280 | export declare function multiply(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 281 | /** 282 | * Multiplies a vector by another vector (component-wise); assumes a and 283 | * b have the same length. (same as mul) 284 | * @param a - Operand vector. 285 | * @param b - Operand vector. 286 | * @param dst - vector to hold result. If not passed in a new one is created. 287 | * @returns The vector of products of entries of a and b. 288 | */ 289 | export declare const mul: typeof multiply; 290 | /** 291 | * Divides a vector by another vector (component-wise); assumes a and 292 | * b have the same length. 293 | * @param a - Operand vector. 294 | * @param b - Operand vector. 295 | * @param dst - vector to hold result. If not passed in a new one is created. 296 | * @returns The vector of quotients of entries of a and b. 297 | */ 298 | export declare function divide(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 299 | /** 300 | * Divides a vector by another vector (component-wise); assumes a and 301 | * b have the same length. (same as divide) 302 | * @param a - Operand vector. 303 | * @param b - Operand vector. 304 | * @param dst - vector to hold result. If not passed in a new one is created. 305 | * @returns The vector of quotients of entries of a and b. 306 | */ 307 | export declare const div: typeof divide; 308 | /** 309 | * Zero's a vector 310 | * @param dst - vector to hold result. If not passed in a new one is created. 311 | * @returns The zeroed vector. 312 | */ 313 | export declare function zero(dst?: Vec4): Vec4; 314 | /** 315 | * transform vec4 by 4x4 matrix 316 | * @param v - the vector 317 | * @param m - The matrix. 318 | * @param dst - optional vec4 to store result. If not passed a new one is created. 319 | * @returns the transformed vector 320 | */ 321 | export declare function transformMat4(v: Vec4, m: Mat4, dst?: Vec4): Vec4; 322 | /** 323 | * Treat a 4D vector as a direction and set it's length 324 | * 325 | * @param a The vec4 to lengthen 326 | * @param len The length of the resulting vector 327 | * @returns The lengthened vector 328 | */ 329 | export declare function setLength(a: Vec4, len: number, dst?: Vec4): Vec4; 330 | /** 331 | * Ensure a vector is not longer than a max length 332 | * 333 | * @param a The vec4 to limit 334 | * @param maxLen The longest length of the resulting vector 335 | * @returns The vector, shortened to maxLen if it's too long 336 | */ 337 | export declare function truncate(a: Vec4, maxLen: number, dst?: Vec4): Vec4; 338 | /** 339 | * Return the vector exactly between 2 endpoint vectors 340 | * 341 | * @param a Endpoint 1 342 | * @param b Endpoint 2 343 | * @returns The vector exactly residing between endpoints 1 and 2 344 | */ 345 | export declare function midpoint(a: Vec4, b: Vec4, dst?: Vec4): Vec4; 346 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wgpu-matrix 2 | 3 | [![NPM Package][npm]][npm-url] 4 | 5 | Fast 3d math library for webgpu 6 | 7 | * [Docs](https://wgpu-matrix.org/docs) 8 | * [Github](https://github.com/greggman/wgpu-matrix) 9 | * [Tests](https://wgpu-matrix.org/test/) 10 | 11 | ## Why another 3d math library? 12 | 13 | * Most other 3D math libraries are designed for WebGL, not WebGPU 14 | * WebGPU uses clip space Z 0 to 1, vs WebGL -1 to 1. So `ortho`, `perspective`, `frustum` are different 15 | * WebGPU mat3s are 12 floats (padded), WebGL they're 9. 16 | * Many other 3D math libraries are overly verbose 17 | * compare 18 | 19 | ```js 20 | // wgpu-matrix 21 | const t = mat4.translation([x, y, z]); 22 | const p = mat4.perspective(fov, aspect, near, far); 23 | const r = mat4.rotationX(rad); 24 | ``` 25 | 26 | ```js 27 | // gl-matrix 28 | const t = mat4.create(); 29 | mat4.fromTranslation(t, [x, y, z]); 30 | 31 | const p = mat4.create(); 32 | mat4.perspective(p, fov, aspect, near, far); 33 | 34 | const r = mat4.create(); 35 | mat4.fromXRotation(r, rad); 36 | ``` 37 | 38 | note that if you want to pre-create matrices you can still do this in wgpu-matrix 39 | 40 | ```js 41 | const t = mat4.create(); 42 | mat4.translation([x, y, z], t); 43 | 44 | const p = mat4.create(); 45 | mat4.perspective(fov, aspect, near, far, p); 46 | 47 | const r = mat4.create(); 48 | mat4.rotationX(rad, r); 49 | ``` 50 | 51 | ## Usage 52 | 53 | ```js 54 | import { 55 | vec3, 56 | mat4, 57 | } from 'https://wgpu-matrix.org/dist/3.x/wgpu-matrix.module.js'; 58 | 59 | const fov = 60 * Math.PI / 180 60 | const aspect = width / height; 61 | const near = 0.1; 62 | const far = 1000; 63 | const perspective = mat4.perspective(fov, aspect, near, far); 64 | 65 | const eye = [3, 5, 10]; 66 | const target = [0, 4, 0]; 67 | const up = [0, 1, 0]; 68 | const view = mat4.lookAt(eye, target, up); 69 | ``` 70 | 71 | Note: for translation, rotation, and scaling there are 2 versions 72 | of each function. One generates a translation, rotation, or scaling matrix. 73 | The other translates, rotates, or scales a matrix. 74 | 75 | ```js 76 | const t = mat4.translation([1, 2, 3]); // a translation matrix 77 | const r = mat4.rotationX(Math.PI * 0.5); // a rotation matrix 78 | const s = mat4.scaling([1, 2, 3]); // a scaling matrix 79 | ``` 80 | 81 | ```js 82 | const m = mat4.identity(); 83 | const t = mat4.translate(m, [1, 2, 3]); // m * translation([1, 2, 3]) 84 | const r = mat4.rotateX(m, Math.PI * 0.5); // m * rotationX(Math.PI * 0.5) 85 | const s = mat4.scale(m, [1, 2, 3]); // m * scaling([1, 2, 3]) 86 | ``` 87 | 88 | Functions take an optional destination to hold the result. 89 | 90 | ```js 91 | const m = mat4.create(); // m = new mat4 92 | mat4.identity(m); // m = identity 93 | mat4.translate(m, [1, 2, 3], m); // m *= translation([1, 2, 3]) 94 | mat4.rotateX(m, Math.PI * 0.5, m); // m *= rotationX(Math.PI * 0.5) 95 | mat4.scale(m, [1, 2, 3], m); // m *= scaling([1, 2, 3]) 96 | ``` 97 | 98 | There is also the minified version 99 | 100 | ```js 101 | import { 102 | vec3, 103 | mat4, 104 | } from 'https://wgpu-matrix.org/dist/3.x/wgpu-matrix.module.min.js'; 105 | 106 | // ... etc ... 107 | ``` 108 | 109 | and a UMD version 110 | 111 | ```html 112 | 113 | 118 | ``` 119 | 120 | or UDM min version 121 | 122 | ```html 123 | 124 | ... 125 | ``` 126 | 127 | or via npm 128 | 129 | ```sh 130 | npm install --save wgpu-matrix 131 | ``` 132 | 133 | then using a build process 134 | 135 | ```js 136 | import {vec3, mat3} from 'wgpu-matrix'; 137 | 138 | // ... etc ... 139 | ``` 140 | 141 | [Example](https://codesandbox.io/s/cocky-bogdan-bwq5x?file=/src/index.js) 142 | 143 | ## Download 144 | 145 | * [zip](https://github.com/greggman/wgpu-matrix/zipball/main) 146 | * [tar](https://github.com/greggman/wgpu-matrix/tarball/main) 147 | * [github](https://github.com/greggman/wgpu-matrix) 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(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 | 223 | ## Notes 224 | 225 | [`mat4.perspective`](https://wgpu-matrix.org/docs/functions/mat4.perspective.html), 226 | [`mat4.ortho`](https://wgpu-matrix.org/docs/functions/mat4.ortho.html), and 227 | [`mat4.frustum`](https://wgpu-matrix.org/docs/functions/mat4.frustum.html) 228 | all return matrices with Z clip space from 0 to 1 (unlike most WebGL matrix libraries which return -1 to 1) 229 | 230 | [`mat4.create`](https://wgpu-matrix.org/docs/functions/mat4.create.html) makes an all zero matrix if passed no parameters. 231 | If you want an identity matrix call `mat4.identity` 232 | 233 | ## Important! 234 | 235 | `mat3` uses the space of 12 elements 236 | 237 | ```js 238 | // a mat3 239 | [ 240 | xx, xy, xz, ? 241 | yx, yy, yz, ? 242 | zx, zy, zz, ? 243 | ] 244 | ``` 245 | 246 | This is because WebGPU requires mat3s to be in this format and since 247 | this library is for WebGPU it makes sense to match so you can manipulate 248 | mat3s in TypeArrays directly. 249 | 250 | `vec3` in this library uses 3 floats per but be aware that an array of 251 | `vec3` in a Uniform Block or other structure in WGSL, each vec3 is 252 | padded to 4 floats! In other words, if you declare 253 | 254 | ``` 255 | struct Foo { 256 | bar: vec3[3]; 257 | }; 258 | ``` 259 | 260 | then bar[0] is at byte offset 0, bar[1] at byte offset 16, bar[2] at byte offset 32. 261 | 262 | See [the WGSL spec on alignment and size](https://www.w3.org/TR/WGSL/#alignment-and-size). 263 | 264 | ## Columns vs Rows 265 | 266 | WebGPU follows the same conventions as OpenGL, Vulkan, Metal for matrices. Some people call this "column major". The issue is the columns of 267 | a traditional "math" matrix are stored as rows when declaring a matrix in code. 268 | 269 | ```js 270 | [ 271 | x1, x2, x3, x4, // <- column 0 272 | y1, y2, y3, y4, // <- column 1 273 | z1, z2, z3, z4, // <- column 2 274 | w1, w2, w3, w4, // <- column 3 275 | ] 276 | ``` 277 | 278 | To put it another way, the translation vector is in elements 12, 13, 14 279 | 280 | ```js 281 | [ 282 | xx, xy, xz, 0, // <- x-axis 283 | yx, yy, yz, 0, // <- y-axis 284 | zx, zy, zz, 0, // <- z-axis 285 | tx, ty, tz, 1, // <- translation 286 | ] 287 | ``` 288 | 289 | This issue has confused programmers since at least the early 90s 😌 290 | 291 | ## Performance vs Convenience 292 | 293 | Most functions take an optional destination as the last argument. 294 | If you don't supply it, a new one (vector, matrix) will be created 295 | for you. 296 | 297 | ```js 298 | // convenient usage 299 | 300 | const persp = mat4.perspective(fov, aspect, near, far); 301 | const camera = mat4.lookAt(eye, target, up); 302 | const view = mat4.inverse(camera); 303 | ``` 304 | 305 | ```js 306 | // performant usage 307 | 308 | // at init time 309 | const persp = mat4.create(); 310 | const camera = mat4.create(); 311 | const view = mat4.create(); 312 | 313 | // at usage time 314 | mat4.perspective(fov, aspect, near, far, persp); 315 | mat4.lookAt(eye, target, up, camera); 316 | mat4.inverse(camera, view); 317 | ``` 318 | 319 | For me, most of the stuff I do in WebGPU, the supposed performance I might lose 320 | from using the convenient style is so small as to be unmeasurable. I'd prefer to 321 | stay convenient and then, if and only if I find a performance issue, then I 322 | might bother to switch to the performant style. 323 | 324 | As the saying goes [*premature optimization is the root of all evil.*](https://softwareengineering.stackexchange.com/questions/80084/is-premature-optimization-really-the-root-of-all-evil) 😉 325 | 326 | ## Migration 327 | 328 | ### 2.x -> 3.x 329 | 330 | In JavaScript there should be no difference in the API except for the removable of `setDefaultType`. 331 | 332 | In TypeScript, 3.x should mostly be type compatible with 2.x. 333 | 3.x is an attempt to fix the casting that was necessary in 2.x. 334 | 335 | ```js 336 | // 2.x 337 | device.queue.writeData(buffer, 0, mat4.identity() as Float32Array); // sadness! 😭 338 | 339 | // 3.x 340 | device.queue.writeData(buffer, 0, mat4.identity()); // Yay! 🎉 341 | ``` 342 | 343 | In TypeScript the differences are as follows 344 | 345 | #### Functions have a default type but return what is passed to them as the dst. 346 | 347 | In 3.x each function has a default type but if you pass it 348 | a destination it returns the type of the destination 349 | 350 | ```ts 351 | mat4.identity() // returns Float32Array 352 | mat4.identity(new Float32Array(16)); // returns Float32Array 353 | mat4.identity(new Float64Array(16)); // returns Float64Array 354 | mat4.identity(new Array(16)); // returns number[] 355 | ``` 356 | 357 | #### Types are specific 358 | 359 | ```ts 360 | const a: Mat4 = ...; // a = Float32Array 361 | const b: Mat4d = ...; // b = Float64Array 362 | const c: Mat4n = ...; // c = number[] 363 | ``` 364 | 365 | This is means code like this 366 | 367 | ```ts 368 | const position: Mat4 = [10, 20, 30]; 369 | ``` 370 | 371 | No longer works because `Mat4` is a `Float32Array`. 372 | 373 | **BUT, functions take any of the normal types as an argument just like they used to** 374 | 375 | ```js 376 | const position = [10, 20, 30]; // number[] 377 | const target = vec3.create(1, 2, 3); // Float32Array 378 | const up = new Float64Array([0, 1, 0]); // Float64Array 379 | 380 | // Works fine, even those types are different, just like 2.x did 381 | const view = mat4.lookAt(position, target, up); // Float32Array 382 | ``` 383 | 384 | If you really want types for each concrete type there's 385 | 386 | * `Float32Array` types: `Mat3`, `Mat4`, `Quat`, `Vec2`, `Vec3`, `Vec4` 387 | * `Float64Array` types: `Mat3d`, `Mat4d`, `Quatd`, `Vec2d`, `Vec3d`, `Vec4d`, 388 | * `number[]` types: `Mat3n`, `Mat4n`, `Quatn`, `Vec2n`, `Vec3n`, `Vec4n` 389 | 390 | #### There are 3 sets of functions, each one returning a different default 391 | 392 | ```ts 393 | mat4.identity() // returns Float32Array 394 | mat4d.identity() // returns Float64Array 395 | mat4n.identity() // returns number[] 396 | ``` 397 | 398 | Similarly there's `mat3d`, `mat3n`, `quatd`, `quatn`, 399 | `vec2d`, `vec2n`, `vec3d`, `vec3n`, `vec4d`, `vec4n`. 400 | 401 | **Note: that in general you're unlikely to need any of these. Just use the 402 | same ones you were using in 2.x** 403 | 404 | ### 1.x -> 2.x 405 | 406 | * [`mat4.lookAt`](https://wgpu-matrix.org/docs/functions/mat4.lookAt.html) 407 | changed from a "camera matrix" to a "view matrix" (same as gluLookAt). 408 | If you want a matrix that orients an something in world space see 409 | [`mat4.aim`](https://wgpu-matrix.org/docs/functions/mat4.frustum.html). 410 | Sorry about this change but people are used to lookAt making a a view matrix 411 | and it seemed prudent to make this change now and save more people from 412 | frustration going forward. 413 | 414 | ## Development 415 | 416 | ```sh 417 | git clone https://github.com/greggman/wgpu-matrix.git 418 | cd wgpu-matrix 419 | npm i 420 | npm run build 421 | npm test 422 | ``` 423 | 424 | You can run tests in the browser by starting a local server 425 | 426 | ```sh 427 | npx servez 428 | ``` 429 | 430 | Now go to wherever your server serves pages. In the case of `servez` that's 431 | probably [http://localhost:8080/test/](http://localhost:8080/test/). 432 | 433 | By default the tests test the minified version. To test the source use `src=true` 434 | as in [http://localhost:8080/test/?src=true](http://localhost:8080/test/src=true). 435 | 436 | To limit which tests are run use `grep=`. For example 437 | [http://localhost:8080/test/?src=true&grep=mat3.*?translate](http://localhost:8080/test/src=true&grep=mat3.*?translate) 438 | runs only tests with `mat3` followed by `translate` in the name of test. 439 | 440 | ## License 441 | 442 | [MIT](LICENSE.md) 443 | 444 | 445 | [npm]: https://img.shields.io/npm/v/wgpu-matrix 446 | [npm-url]: https://www.npmjs.com/package/wgpu-matrix 447 | -------------------------------------------------------------------------------- /dist/2.x/vec2-impl.d.ts: -------------------------------------------------------------------------------- 1 | import { Mat3 } from './mat3'; 2 | import { Mat4 } from './mat4'; 3 | import { Vec2, create, setDefaultType } from './vec2'; 4 | import { Vec3 } from './vec3'; 5 | export default Vec2; 6 | export { create, setDefaultType }; 7 | /** 8 | * Creates a Vec2; may be called with x, y, z to set initial values. (same as create) 9 | * @param x - Initial x value. 10 | * @param y - Initial y value. 11 | * @returns the created vector 12 | */ 13 | export declare const fromValues: typeof create; 14 | /** 15 | * Sets the values of a Vec2 16 | * Also see {@link vec2.create} and {@link vec2.copy} 17 | * 18 | * @param x first value 19 | * @param y second value 20 | * @param dst - vector to hold result. If not passed in a new one is created. 21 | * @returns A vector with its elements set. 22 | */ 23 | export declare function set(x: number, y: number, dst?: Vec2): Vec2; 24 | /** 25 | * Applies Math.ceil to each element of vector 26 | * @param v - Operand vector. 27 | * @param dst - vector to hold result. If not passed in a new one is created. 28 | * @returns A vector that is the ceil of each element of v. 29 | */ 30 | export declare function ceil(v: Vec2, dst?: Vec2): Vec2; 31 | /** 32 | * Applies Math.floor to each element of vector 33 | * @param v - Operand vector. 34 | * @param dst - vector to hold result. If not passed in a new one is created. 35 | * @returns A vector that is the floor of each element of v. 36 | */ 37 | export declare function floor(v: Vec2, dst?: Vec2): Vec2; 38 | /** 39 | * Applies Math.round to each element of vector 40 | * @param v - Operand vector. 41 | * @param dst - vector to hold result. If not passed in a new one is created. 42 | * @returns A vector that is the round of each element of v. 43 | */ 44 | export declare function round(v: Vec2, dst?: Vec2): Vec2; 45 | /** 46 | * Clamp each element of vector between min and max 47 | * @param v - Operand vector. 48 | * @param max - Min value, default 0 49 | * @param min - Max value, default 1 50 | * @param dst - vector to hold result. If not passed in a new one is created. 51 | * @returns A vector that the clamped value of each element of v. 52 | */ 53 | export declare function clamp(v: Vec2, min?: number, max?: number, dst?: Vec2): Vec2; 54 | /** 55 | * Adds two vectors; assumes a and b have the same dimension. 56 | * @param a - Operand vector. 57 | * @param b - Operand vector. 58 | * @param dst - vector to hold result. If not passed in a new one is created. 59 | * @returns A vector that is the sum of a and b. 60 | */ 61 | export declare function add(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 62 | /** 63 | * Adds two vectors, scaling the 2nd; assumes a and b have the same dimension. 64 | * @param a - Operand vector. 65 | * @param b - Operand vector. 66 | * @param scale - Amount to scale b 67 | * @param dst - vector to hold result. If not passed in a new one is created. 68 | * @returns A vector that is the sum of a + b * scale. 69 | */ 70 | export declare function addScaled(a: Vec2, b: Vec2, scale: number, dst?: Vec2): Vec2; 71 | /** 72 | * Returns the angle in radians between two vectors. 73 | * @param a - Operand vector. 74 | * @param b - Operand vector. 75 | * @returns The angle in radians between the 2 vectors. 76 | */ 77 | export declare function angle(a: Vec2, b: Vec2): number; 78 | /** 79 | * Subtracts two vectors. 80 | * @param a - Operand vector. 81 | * @param b - Operand vector. 82 | * @param dst - vector to hold result. If not passed in a new one is created. 83 | * @returns A vector that is the difference of a and b. 84 | */ 85 | export declare function subtract(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 86 | /** 87 | * Subtracts two vectors. 88 | * @param a - Operand vector. 89 | * @param b - Operand vector. 90 | * @param dst - vector to hold result. If not passed in a new one is created. 91 | * @returns A vector that is the difference of a and b. 92 | */ 93 | export declare const sub: typeof subtract; 94 | /** 95 | * Check if 2 vectors are approximately equal 96 | * @param a - Operand vector. 97 | * @param b - Operand vector. 98 | * @returns true if vectors are approximately equal 99 | */ 100 | export declare function equalsApproximately(a: Vec2, b: Vec2): boolean; 101 | /** 102 | * Check if 2 vectors are exactly equal 103 | * @param a - Operand vector. 104 | * @param b - Operand vector. 105 | * @returns true if vectors are exactly equal 106 | */ 107 | export declare function equals(a: Vec2, b: Vec2): boolean; 108 | /** 109 | * Performs linear interpolation on two vectors. 110 | * Given vectors a and b and interpolation coefficient t, returns 111 | * a + t * (b - a). 112 | * @param a - Operand vector. 113 | * @param b - Operand vector. 114 | * @param t - Interpolation coefficient. 115 | * @param dst - vector to hold result. If not passed in a new one is created. 116 | * @returns The linear interpolated result. 117 | */ 118 | export declare function lerp(a: Vec2, b: Vec2, t: number, dst?: Vec2): Vec2; 119 | /** 120 | * Performs linear interpolation on two vectors. 121 | * Given vectors a and b and interpolation coefficient vector t, returns 122 | * a + t * (b - a). 123 | * @param a - Operand vector. 124 | * @param b - Operand vector. 125 | * @param t - Interpolation coefficients vector. 126 | * @param dst - vector to hold result. If not passed in a new one is created. 127 | * @returns the linear interpolated result. 128 | */ 129 | export declare function lerpV(a: Vec2, b: Vec2, t: Vec2, dst?: Vec2): Vec2; 130 | /** 131 | * Return max values of two vectors. 132 | * Given vectors a and b returns 133 | * [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])]. 134 | * @param a - Operand vector. 135 | * @param b - Operand vector. 136 | * @param dst - vector to hold result. If not passed in a new one is created. 137 | * @returns The max components vector. 138 | */ 139 | export declare function max(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 140 | /** 141 | * Return min values of two vectors. 142 | * Given vectors a and b returns 143 | * [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2])]. 144 | * @param a - Operand vector. 145 | * @param b - Operand vector. 146 | * @param dst - vector to hold result. If not passed in a new one is created. 147 | * @returns The min components vector. 148 | */ 149 | export declare function min(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 150 | /** 151 | * Multiplies a vector by a scalar. 152 | * @param v - The vector. 153 | * @param k - The scalar. 154 | * @param dst - vector to hold result. If not passed in a new one is created. 155 | * @returns The scaled vector. 156 | */ 157 | export declare function mulScalar(v: Vec2, k: number, dst?: Vec2): Vec2; 158 | /** 159 | * Multiplies a vector by a scalar. (same as mulScalar) 160 | * @param v - The vector. 161 | * @param k - The scalar. 162 | * @param dst - vector to hold result. If not passed in a new one is created. 163 | * @returns The scaled vector. 164 | */ 165 | export declare const scale: typeof mulScalar; 166 | /** 167 | * Divides a vector by a scalar. 168 | * @param v - The vector. 169 | * @param k - The scalar. 170 | * @param dst - vector to hold result. If not passed in a new one is created. 171 | * @returns The scaled vector. 172 | */ 173 | export declare function divScalar(v: Vec2, k: number, dst?: Vec2): Vec2; 174 | /** 175 | * Inverse a vector. 176 | * @param v - The vector. 177 | * @param dst - vector to hold result. If not passed in a new one is created. 178 | * @returns The inverted vector. 179 | */ 180 | export declare function inverse(v: Vec2, dst?: Vec2): Vec2; 181 | /** 182 | * Invert a vector. (same as inverse) 183 | * @param v - The vector. 184 | * @param dst - vector to hold result. If not passed in a new one is created. 185 | * @returns The inverted vector. 186 | */ 187 | export declare const invert: typeof inverse; 188 | /** 189 | * Computes the cross product of two vectors; assumes both vectors have 190 | * three entries. 191 | * @param a - Operand vector. 192 | * @param b - Operand vector. 193 | * @param dst - vector to hold result. If not passed in a new one is created. 194 | * @returns The vector of a cross b. 195 | */ 196 | export declare function cross(a: Vec2, b: Vec2, dst?: Vec3): Vec3; 197 | /** 198 | * Computes the dot product of two vectors; assumes both vectors have 199 | * three entries. 200 | * @param a - Operand vector. 201 | * @param b - Operand vector. 202 | * @returns dot product 203 | */ 204 | export declare function dot(a: Vec2, b: Vec2): number; 205 | /** 206 | * Computes the length of vector 207 | * @param v - vector. 208 | * @returns length of vector. 209 | */ 210 | export declare function length(v: Vec2): number; 211 | /** 212 | * Computes the length of vector (same as length) 213 | * @param v - vector. 214 | * @returns length of vector. 215 | */ 216 | export declare const len: typeof length; 217 | /** 218 | * Computes the square of the length of vector 219 | * @param v - vector. 220 | * @returns square of the length of vector. 221 | */ 222 | export declare function lengthSq(v: Vec2): number; 223 | /** 224 | * Computes the square of the length of vector (same as lengthSq) 225 | * @param v - vector. 226 | * @returns square of the length of vector. 227 | */ 228 | export declare const lenSq: typeof lengthSq; 229 | /** 230 | * Computes the distance between 2 points 231 | * @param a - vector. 232 | * @param b - vector. 233 | * @returns distance between a and b 234 | */ 235 | export declare function distance(a: Vec2, b: Vec2): number; 236 | /** 237 | * Computes the distance between 2 points (same as distance) 238 | * @param a - vector. 239 | * @param b - vector. 240 | * @returns distance between a and b 241 | */ 242 | export declare const dist: typeof distance; 243 | /** 244 | * Computes the square of the distance between 2 points 245 | * @param a - vector. 246 | * @param b - vector. 247 | * @returns square of the distance between a and b 248 | */ 249 | export declare function distanceSq(a: Vec2, b: Vec2): number; 250 | /** 251 | * Computes the square of the distance between 2 points (same as distanceSq) 252 | * @param a - vector. 253 | * @param b - vector. 254 | * @returns square of the distance between a and b 255 | */ 256 | export declare const distSq: typeof distanceSq; 257 | /** 258 | * Divides a vector by its Euclidean length and returns the quotient. 259 | * @param v - The vector. 260 | * @param dst - vector to hold result. If not passed in a new one is created. 261 | * @returns The normalized vector. 262 | */ 263 | export declare function normalize(v: Vec2, dst?: Vec2): Vec2; 264 | /** 265 | * Negates a vector. 266 | * @param v - The vector. 267 | * @param dst - vector to hold result. If not passed in a new one is created. 268 | * @returns -v. 269 | */ 270 | export declare function negate(v: Vec2, dst?: Vec2): Vec2; 271 | /** 272 | * Copies a vector. (same as {@link vec2.clone}) 273 | * Also see {@link vec2.create} and {@link vec2.set} 274 | * @param v - The vector. 275 | * @param dst - vector to hold result. If not passed in a new one is created. 276 | * @returns A copy of v. 277 | */ 278 | export declare function copy(v: Vec2, dst?: Vec2): Vec2; 279 | /** 280 | * Clones a vector. (same as {@link vec2.copy}) 281 | * Also see {@link vec2.create} and {@link vec2.set} 282 | * @param v - The vector. 283 | * @param dst - vector to hold result. If not passed in a new one is created. 284 | * @returns A copy of v. 285 | */ 286 | export declare const clone: typeof copy; 287 | /** 288 | * Multiplies a vector by another vector (component-wise); assumes a and 289 | * b have the same length. 290 | * @param a - Operand vector. 291 | * @param b - Operand vector. 292 | * @param dst - vector to hold result. If not passed in a new one is created. 293 | * @returns The vector of products of entries of a and b. 294 | */ 295 | export declare function multiply(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 296 | /** 297 | * Multiplies a vector by another vector (component-wise); assumes a and 298 | * b have the same length. (same as mul) 299 | * @param a - Operand vector. 300 | * @param b - Operand vector. 301 | * @param dst - vector to hold result. If not passed in a new one is created. 302 | * @returns The vector of products of entries of a and b. 303 | */ 304 | export declare const mul: typeof multiply; 305 | /** 306 | * Divides a vector by another vector (component-wise); assumes a and 307 | * b have the same length. 308 | * @param a - Operand vector. 309 | * @param b - Operand vector. 310 | * @param dst - vector to hold result. If not passed in a new one is created. 311 | * @returns The vector of quotients of entries of a and b. 312 | */ 313 | export declare function divide(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 314 | /** 315 | * Divides a vector by another vector (component-wise); assumes a and 316 | * b have the same length. (same as divide) 317 | * @param a - Operand vector. 318 | * @param b - Operand vector. 319 | * @param dst - vector to hold result. If not passed in a new one is created. 320 | * @returns The vector of quotients of entries of a and b. 321 | */ 322 | export declare const div: typeof divide; 323 | /** 324 | * Creates a random unit vector * scale 325 | * @param scale - Default 1 326 | * @param dst - vector to hold result. If not passed in a new one is created. 327 | * @returns The random vector. 328 | */ 329 | export declare function random(scale?: number, dst?: Vec2): Vec2; 330 | /** 331 | * Zero's a vector 332 | * @param dst - vector to hold result. If not passed in a new one is created. 333 | * @returns The zeroed vector. 334 | */ 335 | export declare function zero(dst?: Vec2): Vec2; 336 | /** 337 | * transform Vec2 by 4x4 matrix 338 | * @param v - the vector 339 | * @param m - The matrix. 340 | * @param dst - optional Vec2 to store result. If not passed a new one is created. 341 | * @returns the transformed vector 342 | */ 343 | export declare function transformMat4(v: Vec2, m: Mat4, dst?: Vec2): Vec2; 344 | /** 345 | * Transforms vec4 by 3x3 matrix 346 | * 347 | * @param v - the vector 348 | * @param m - The matrix. 349 | * @param dst - optional Vec2 to store result. If not passed a new one is created. 350 | * @returns the transformed vector 351 | */ 352 | export declare function transformMat3(v: Vec2, m: Mat3, dst?: Vec2): Vec2; 353 | /** 354 | * Rotate a 2D vector 355 | * 356 | * @param a The vec2 point to rotate 357 | * @param b The origin of the rotation 358 | * @param rad The angle of rotation in radians 359 | * @returns the rotated vector 360 | */ 361 | export declare function rotate(a: Vec2, b: Vec2, rad: number, dst?: Vec2): Vec2; 362 | /** 363 | * Treat a 2D vector as a direction and set it's length 364 | * 365 | * @param a The vec2 to lengthen 366 | * @param len The length of the resulting vector 367 | * @returns The lengthened vector 368 | */ 369 | export declare function setLength(a: Vec2, len: number, dst?: Vec2): Vec2; 370 | /** 371 | * Ensure a vector is not longer than a max length 372 | * 373 | * @param a The vec2 to limit 374 | * @param maxLen The longest length of the resulting vector 375 | * @returns The vector, shortened to maxLen if it's too long 376 | */ 377 | export declare function truncate(a: Vec2, maxLen: number, dst?: Vec2): Vec2; 378 | /** 379 | * Return the vector exactly between 2 endpoint vectors 380 | * 381 | * @param a Endpoint 1 382 | * @param b Endpoint 2 383 | * @returns The vector exactly residing between endpoints 1 and 2 384 | */ 385 | export declare function midpoint(a: Vec2, b: Vec2, dst?: Vec2): Vec2; 386 | --------------------------------------------------------------------------------