glmaths 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +561 -543
- package/dist/cjs/glmaths.d.ts +240 -198
- package/dist/cjs/glmaths.js +544 -462
- package/dist/cjs/glmaths.js.map +1 -1
- package/dist/cjs/glmaths.min.js +1 -1
- package/dist/cjs/glmaths.min.js.map +1 -1
- package/dist/esm/glmaths.d.ts +240 -198
- package/dist/esm/glmaths.js +544 -462
- package/dist/esm/glmaths.js.map +1 -1
- package/dist/esm/glmaths.min.js +1 -1
- package/dist/esm/glmaths.min.js.map +1 -1
- package/dist/glmaths.d.ts +240 -198
- package/dist/glmaths.js +544 -462
- package/dist/glmaths.js.map +1 -1
- package/dist/glmaths.min.js +1 -1
- package/dist/glmaths.min.js.map +1 -1
- package/examples/index.html +23 -0
- package/examples/main.js +134 -0
- package/examples/main.ts +159 -0
- package/package.json +1 -1
- package/src/mat2.ts +27 -27
- package/src/mat2x3.ts +37 -31
- package/src/mat3.ts +53 -54
- package/src/mat4.ts +81 -82
- package/src/quat.ts +36 -23
- package/src/quat2.ts +45 -31
- package/src/vec2.ts +90 -70
- package/src/vec3.ts +143 -121
- package/src/vec4.ts +79 -57
- package/tests/mat2.test.ts +53 -53
- package/tests/mat2x3.test.ts +46 -46
- package/tests/mat3.test.ts +59 -59
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>glmaths - Rotating Cube Demo</title>
|
|
7
|
+
<style>
|
|
8
|
+
* { margin: 0; padding: 0; }
|
|
9
|
+
body { background: #111; overflow: hidden; }
|
|
10
|
+
canvas { display: block; }
|
|
11
|
+
#info {
|
|
12
|
+
position: absolute; top: 12px; left: 12px;
|
|
13
|
+
color: #aaa; font: 13px/1.5 monospace;
|
|
14
|
+
pointer-events: none; white-space: pre;
|
|
15
|
+
}
|
|
16
|
+
</style>
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<canvas id="c"></canvas>
|
|
20
|
+
<div id="info">move mouse to cast ray into 3d</div>
|
|
21
|
+
<script type="module" src="main.js"></script>
|
|
22
|
+
</body>
|
|
23
|
+
</html>
|
package/examples/main.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { vec3, vec4, mat4 } from '../dist/esm/glmaths.js';
|
|
2
|
+
// --- WebGL setup ---
|
|
3
|
+
const canvas = document.getElementById('c');
|
|
4
|
+
const gl = canvas.getContext('webgl');
|
|
5
|
+
function resize() {
|
|
6
|
+
canvas.width = innerWidth * devicePixelRatio;
|
|
7
|
+
canvas.height = innerHeight * devicePixelRatio;
|
|
8
|
+
canvas.style.width = innerWidth + 'px';
|
|
9
|
+
canvas.style.height = innerHeight + 'px';
|
|
10
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
11
|
+
}
|
|
12
|
+
resize();
|
|
13
|
+
addEventListener('resize', resize);
|
|
14
|
+
// --- Shaders ---
|
|
15
|
+
const vsSource = `
|
|
16
|
+
attribute vec3 aPos;
|
|
17
|
+
attribute vec3 aColor;
|
|
18
|
+
uniform mat4 uProj;
|
|
19
|
+
uniform mat4 uView;
|
|
20
|
+
uniform mat4 uModel;
|
|
21
|
+
varying vec3 vColor;
|
|
22
|
+
void main() {
|
|
23
|
+
vColor = aColor;
|
|
24
|
+
gl_Position = uProj * uView * uModel * vec4(aPos, 1.0);
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
const fsSource = `
|
|
28
|
+
precision mediump float;
|
|
29
|
+
varying vec3 vColor;
|
|
30
|
+
void main() {
|
|
31
|
+
gl_FragColor = vec4(vColor, 1.0);
|
|
32
|
+
}
|
|
33
|
+
`;
|
|
34
|
+
function compileShader(src, type) {
|
|
35
|
+
const s = gl.createShader(type);
|
|
36
|
+
gl.shaderSource(s, src);
|
|
37
|
+
gl.compileShader(s);
|
|
38
|
+
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
|
|
39
|
+
throw new Error(gl.getShaderInfoLog(s));
|
|
40
|
+
return s;
|
|
41
|
+
}
|
|
42
|
+
const prog = gl.createProgram();
|
|
43
|
+
gl.attachShader(prog, compileShader(vsSource, gl.VERTEX_SHADER));
|
|
44
|
+
gl.attachShader(prog, compileShader(fsSource, gl.FRAGMENT_SHADER));
|
|
45
|
+
gl.linkProgram(prog);
|
|
46
|
+
gl.useProgram(prog);
|
|
47
|
+
const aPos = gl.getAttribLocation(prog, 'aPos');
|
|
48
|
+
const aColor = gl.getAttribLocation(prog, 'aColor');
|
|
49
|
+
const uProj = gl.getUniformLocation(prog, 'uProj');
|
|
50
|
+
const uView = gl.getUniformLocation(prog, 'uView');
|
|
51
|
+
const uModel = gl.getUniformLocation(prog, 'uModel');
|
|
52
|
+
// --- Cube geometry ---
|
|
53
|
+
const S = 0.5;
|
|
54
|
+
const positions = [
|
|
55
|
+
-S, -S, -S, S, -S, -S, S, S, -S, -S, S, -S,
|
|
56
|
+
-S, -S, S, S, -S, S, S, S, S, -S, S, S,
|
|
57
|
+
];
|
|
58
|
+
const colors = [
|
|
59
|
+
1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0,
|
|
60
|
+
1, 0, 1, 0, 1, 1, 1, 1, 1, 0.5, 0.5, 0,
|
|
61
|
+
];
|
|
62
|
+
const indices = [
|
|
63
|
+
0, 1, 2, 0, 2, 3,
|
|
64
|
+
4, 5, 6, 4, 6, 7,
|
|
65
|
+
0, 4, 7, 0, 7, 3,
|
|
66
|
+
1, 5, 6, 1, 6, 2,
|
|
67
|
+
3, 2, 6, 3, 6, 7,
|
|
68
|
+
0, 1, 5, 0, 5, 4,
|
|
69
|
+
];
|
|
70
|
+
const posBuf = gl.createBuffer();
|
|
71
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
|
|
72
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
|
|
73
|
+
gl.enableVertexAttribArray(aPos);
|
|
74
|
+
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 0, 0);
|
|
75
|
+
const colBuf = gl.createBuffer();
|
|
76
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf);
|
|
77
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
|
|
78
|
+
gl.enableVertexAttribArray(aColor);
|
|
79
|
+
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0);
|
|
80
|
+
const idxBuf = gl.createBuffer();
|
|
81
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, idxBuf);
|
|
82
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
|
|
83
|
+
gl.enable(gl.DEPTH_TEST);
|
|
84
|
+
gl.clearColor(0.07, 0.07, 0.1, 1);
|
|
85
|
+
// --- Matrices ---
|
|
86
|
+
const proj = mat4.perspective(Math.PI / 4, canvas.width / canvas.height, 0.1, 100);
|
|
87
|
+
const view = mat4.lookAt(vec3(0, 2, 5), vec3(0), vec3(0, 1, 0));
|
|
88
|
+
let model = mat4.identity;
|
|
89
|
+
const invProjView = mat4();
|
|
90
|
+
// --- Mouse tracking ---
|
|
91
|
+
let mouseX = 0, mouseY = 0;
|
|
92
|
+
addEventListener('mousemove', e => {
|
|
93
|
+
mouseX = e.clientX;
|
|
94
|
+
mouseY = e.clientY;
|
|
95
|
+
});
|
|
96
|
+
// Unproject screen coords to world ray using (proj * view)^-1
|
|
97
|
+
function screenToRay(sx, sy) {
|
|
98
|
+
const ndcX = (sx / innerWidth) * 2 - 1;
|
|
99
|
+
const ndcY = 1 - (sy / innerHeight) * 2;
|
|
100
|
+
(proj.times(view)).invert(invProjView);
|
|
101
|
+
const nearW = invProjView.times(vec4(ndcX, ndcY, -1, 1));
|
|
102
|
+
const farW = invProjView.times(vec4(ndcX, ndcY, 1, 1));
|
|
103
|
+
const nearPt = nearW.xyz.div(nearW.w);
|
|
104
|
+
const farPt = farW.xyz.div(farW.w);
|
|
105
|
+
const dir = (farPt.minus(nearPt)).normalized();
|
|
106
|
+
return { origin: nearPt, dir };
|
|
107
|
+
}
|
|
108
|
+
// --- Render loop ---
|
|
109
|
+
const info = document.getElementById('info');
|
|
110
|
+
let lastT = performance.now();
|
|
111
|
+
function frame(now) {
|
|
112
|
+
requestAnimationFrame(frame);
|
|
113
|
+
const dt = (now - lastT) / 1000;
|
|
114
|
+
lastT = now;
|
|
115
|
+
mat4.perspective(Math.PI / 4, canvas.width / canvas.height, 0.1, 100, proj);
|
|
116
|
+
model = model.times(mat4.fromYRotation(dt * 0.7));
|
|
117
|
+
model = model.times(mat4.fromXRotation(dt * 0.42));
|
|
118
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
119
|
+
gl.useProgram(prog);
|
|
120
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
|
|
121
|
+
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 0, 0);
|
|
122
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf);
|
|
123
|
+
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0);
|
|
124
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, idxBuf);
|
|
125
|
+
gl.uniformMatrix4fv(uProj, false, proj);
|
|
126
|
+
gl.uniformMatrix4fv(uView, false, view);
|
|
127
|
+
gl.uniformMatrix4fv(uModel, false, model);
|
|
128
|
+
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
|
|
129
|
+
const ray = screenToRay(mouseX, mouseY);
|
|
130
|
+
info.textContent =
|
|
131
|
+
`ray origin: ${ray.origin.x.toFixed(2)}, ${ray.origin.y.toFixed(2)}, ${ray.origin.z.toFixed(2)}\n` +
|
|
132
|
+
`ray dir: ${ray.dir.x.toFixed(2)}, ${ray.dir.y.toFixed(2)}, ${ray.dir.z.toFixed(2)}`;
|
|
133
|
+
}
|
|
134
|
+
requestAnimationFrame(frame);
|
package/examples/main.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { vec3, vec4, mat4, Mat4 } from '../dist/esm/glmaths.js'
|
|
2
|
+
|
|
3
|
+
// --- WebGL setup ---
|
|
4
|
+
const canvas = document.getElementById('c') as HTMLCanvasElement
|
|
5
|
+
const gl = canvas.getContext('webgl')!
|
|
6
|
+
|
|
7
|
+
function resize() {
|
|
8
|
+
canvas.width = innerWidth * devicePixelRatio
|
|
9
|
+
canvas.height = innerHeight * devicePixelRatio
|
|
10
|
+
canvas.style.width = innerWidth + 'px'
|
|
11
|
+
canvas.style.height = innerHeight + 'px'
|
|
12
|
+
gl.viewport(0, 0, canvas.width, canvas.height)
|
|
13
|
+
}
|
|
14
|
+
resize()
|
|
15
|
+
addEventListener('resize', resize)
|
|
16
|
+
|
|
17
|
+
// --- Shaders ---
|
|
18
|
+
const vsSource = `
|
|
19
|
+
attribute vec3 aPos;
|
|
20
|
+
attribute vec3 aColor;
|
|
21
|
+
uniform mat4 uProj;
|
|
22
|
+
uniform mat4 uView;
|
|
23
|
+
uniform mat4 uModel;
|
|
24
|
+
varying vec3 vColor;
|
|
25
|
+
void main() {
|
|
26
|
+
vColor = aColor;
|
|
27
|
+
gl_Position = uProj * uView * uModel * vec4(aPos, 1.0);
|
|
28
|
+
}
|
|
29
|
+
`
|
|
30
|
+
const fsSource = `
|
|
31
|
+
precision mediump float;
|
|
32
|
+
varying vec3 vColor;
|
|
33
|
+
void main() {
|
|
34
|
+
gl_FragColor = vec4(vColor, 1.0);
|
|
35
|
+
}
|
|
36
|
+
`
|
|
37
|
+
|
|
38
|
+
function compileShader(src: string, type: number) {
|
|
39
|
+
const s = gl.createShader(type)!
|
|
40
|
+
gl.shaderSource(s, src)
|
|
41
|
+
gl.compileShader(s)
|
|
42
|
+
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
|
|
43
|
+
throw new Error(gl.getShaderInfoLog(s)!)
|
|
44
|
+
return s
|
|
45
|
+
}
|
|
46
|
+
const prog = gl.createProgram()!
|
|
47
|
+
gl.attachShader(prog, compileShader(vsSource, gl.VERTEX_SHADER))
|
|
48
|
+
gl.attachShader(prog, compileShader(fsSource, gl.FRAGMENT_SHADER))
|
|
49
|
+
gl.linkProgram(prog)
|
|
50
|
+
gl.useProgram(prog)
|
|
51
|
+
|
|
52
|
+
const aPos = gl.getAttribLocation(prog, 'aPos')
|
|
53
|
+
const aColor = gl.getAttribLocation(prog, 'aColor')
|
|
54
|
+
const uProj = gl.getUniformLocation(prog, 'uProj')
|
|
55
|
+
const uView = gl.getUniformLocation(prog, 'uView')
|
|
56
|
+
const uModel = gl.getUniformLocation(prog, 'uModel')
|
|
57
|
+
|
|
58
|
+
// --- Cube geometry ---
|
|
59
|
+
const S = 0.5
|
|
60
|
+
const positions = [
|
|
61
|
+
-S,-S,-S, S,-S,-S, S, S,-S, -S, S,-S,
|
|
62
|
+
-S,-S, S, S,-S, S, S, S, S, -S, S, S,
|
|
63
|
+
]
|
|
64
|
+
const colors = [
|
|
65
|
+
1,0,0, 0,1,0, 0,0,1, 1,1,0,
|
|
66
|
+
1,0,1, 0,1,1, 1,1,1, 0.5,0.5,0,
|
|
67
|
+
]
|
|
68
|
+
const indices = [
|
|
69
|
+
0,1,2, 0,2,3,
|
|
70
|
+
4,5,6, 4,6,7,
|
|
71
|
+
0,4,7, 0,7,3,
|
|
72
|
+
1,5,6, 1,6,2,
|
|
73
|
+
3,2,6, 3,6,7,
|
|
74
|
+
0,1,5, 0,5,4,
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
const posBuf = gl.createBuffer()
|
|
78
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf)
|
|
79
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW)
|
|
80
|
+
gl.enableVertexAttribArray(aPos)
|
|
81
|
+
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 0, 0)
|
|
82
|
+
|
|
83
|
+
const colBuf = gl.createBuffer()
|
|
84
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf)
|
|
85
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW)
|
|
86
|
+
gl.enableVertexAttribArray(aColor)
|
|
87
|
+
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0)
|
|
88
|
+
|
|
89
|
+
const idxBuf = gl.createBuffer()
|
|
90
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, idxBuf)
|
|
91
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW)
|
|
92
|
+
|
|
93
|
+
gl.enable(gl.DEPTH_TEST)
|
|
94
|
+
gl.clearColor(0.07, 0.07, 0.1, 1)
|
|
95
|
+
|
|
96
|
+
// --- Matrices ---
|
|
97
|
+
const proj = mat4.perspective(Math.PI / 4, canvas.width / canvas.height, 0.1, 100)
|
|
98
|
+
const view = mat4.lookAt(vec3(0, 2, 5), vec3(0), vec3(0, 1, 0))
|
|
99
|
+
let model = mat4.identity
|
|
100
|
+
const invProjView = mat4()
|
|
101
|
+
|
|
102
|
+
// --- Mouse tracking ---
|
|
103
|
+
let mouseX = 0, mouseY = 0
|
|
104
|
+
addEventListener('mousemove', e => {
|
|
105
|
+
mouseX = e.clientX
|
|
106
|
+
mouseY = e.clientY
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
// Unproject screen coords to world ray using (proj * view)^-1
|
|
110
|
+
function screenToRay(sx: number, sy: number) {
|
|
111
|
+
const ndcX = (sx / innerWidth) * 2 - 1
|
|
112
|
+
const ndcY = 1 - (sy / innerHeight) * 2
|
|
113
|
+
|
|
114
|
+
;(proj * view).invert(invProjView)
|
|
115
|
+
|
|
116
|
+
const nearW = invProjView * vec4(ndcX, ndcY, -1, 1)
|
|
117
|
+
const farW = invProjView * vec4(ndcX, ndcY, 1, 1)
|
|
118
|
+
|
|
119
|
+
const nearPt = nearW.xyz / nearW.w
|
|
120
|
+
const farPt = farW.xyz / farW.w
|
|
121
|
+
|
|
122
|
+
const dir = (farPt - nearPt).normalized()
|
|
123
|
+
return { origin: nearPt, dir }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// --- Render loop ---
|
|
127
|
+
const info = document.getElementById('info')!
|
|
128
|
+
let lastT = performance.now()
|
|
129
|
+
function frame(now: number) {
|
|
130
|
+
requestAnimationFrame(frame)
|
|
131
|
+
const dt = (now - lastT) / 1000
|
|
132
|
+
lastT = now
|
|
133
|
+
|
|
134
|
+
mat4.perspective(Math.PI / 4, canvas.width / canvas.height, 0.1, 100, proj)
|
|
135
|
+
|
|
136
|
+
model *= mat4.fromYRotation(dt * 0.7)
|
|
137
|
+
model *= mat4.fromXRotation(dt * 0.42)
|
|
138
|
+
|
|
139
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
|
140
|
+
|
|
141
|
+
gl.useProgram(prog)
|
|
142
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf)
|
|
143
|
+
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 0, 0)
|
|
144
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf)
|
|
145
|
+
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0)
|
|
146
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, idxBuf)
|
|
147
|
+
|
|
148
|
+
gl.uniformMatrix4fv(uProj, false, proj)
|
|
149
|
+
gl.uniformMatrix4fv(uView, false, view)
|
|
150
|
+
gl.uniformMatrix4fv(uModel, false, model)
|
|
151
|
+
|
|
152
|
+
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0)
|
|
153
|
+
|
|
154
|
+
const ray = screenToRay(mouseX, mouseY)
|
|
155
|
+
info.textContent =
|
|
156
|
+
`ray origin: ${ray.origin.x.toFixed(2)}, ${ray.origin.y.toFixed(2)}, ${ray.origin.z.toFixed(2)}\n` +
|
|
157
|
+
`ray dir: ${ray.dir.x.toFixed(2)}, ${ray.dir.y.toFixed(2)}, ${ray.dir.z.toFixed(2)}`
|
|
158
|
+
}
|
|
159
|
+
requestAnimationFrame(frame)
|
package/package.json
CHANGED
package/src/mat2.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { Vec2 } from './vec2'
|
|
|
7
7
|
*/
|
|
8
8
|
export class Mat2 extends Float32Array {
|
|
9
9
|
|
|
10
|
-
static get identity() { return
|
|
11
|
-
static get Identity() { return
|
|
12
|
-
static get IDENTITY() { return
|
|
10
|
+
static get identity() { return mat2(1, 0, 0, 1) }
|
|
11
|
+
static get Identity() { return mat2(1, 0, 0, 1) }
|
|
12
|
+
static get IDENTITY() { return mat2(1, 0, 0, 1) }
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Creates a new Mat2
|
|
@@ -33,16 +33,16 @@ export class Mat2 extends Float32Array {
|
|
|
33
33
|
* @returns {Mat2} a new Mat2
|
|
34
34
|
*/
|
|
35
35
|
clone() {
|
|
36
|
-
return
|
|
36
|
+
return mat2(this[0], this[1], this[2], this[3])
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Transposes a mat2
|
|
41
41
|
*
|
|
42
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
42
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
43
43
|
* @returns {Mat2} out
|
|
44
44
|
*/
|
|
45
|
-
transpose(out = glmaths.ALWAYS_COPY ?
|
|
45
|
+
transpose(out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
46
46
|
if (out === this) {
|
|
47
47
|
const tmp = out[1]
|
|
48
48
|
out[1] = out[2]
|
|
@@ -59,10 +59,10 @@ export class Mat2 extends Float32Array {
|
|
|
59
59
|
/**
|
|
60
60
|
* Inverts a mat2
|
|
61
61
|
*
|
|
62
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
62
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
63
63
|
* @returns {Mat2} out or null if the matrix is not invertible
|
|
64
64
|
*/
|
|
65
|
-
invert(out = glmaths.ALWAYS_COPY ?
|
|
65
|
+
invert(out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
66
66
|
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
67
67
|
|
|
68
68
|
// Calculate the determinant
|
|
@@ -80,10 +80,10 @@ export class Mat2 extends Float32Array {
|
|
|
80
80
|
/**
|
|
81
81
|
* Calculates the adjugate of a mat2
|
|
82
82
|
*
|
|
83
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
83
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
84
84
|
* @returns {Mat2} out
|
|
85
85
|
*/
|
|
86
|
-
adjoint(out = glmaths.ALWAYS_COPY ?
|
|
86
|
+
adjoint(out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
87
87
|
let a0 = this[0]
|
|
88
88
|
out[0] = this[3]
|
|
89
89
|
out[1] = -this[1]
|
|
@@ -105,10 +105,10 @@ export class Mat2 extends Float32Array {
|
|
|
105
105
|
* Rotates a mat2 by the given angle
|
|
106
106
|
*
|
|
107
107
|
* @param {Number} rad the angle to rotate the matrix by
|
|
108
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
108
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
109
109
|
* @returns {Mat2} out
|
|
110
110
|
*/
|
|
111
|
-
rotate(rad: number, out = glmaths.ALWAYS_COPY ?
|
|
111
|
+
rotate(rad: number, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
112
112
|
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
113
113
|
const s = Math.sin(rad), c = Math.cos(rad)
|
|
114
114
|
out[0] = a0 * c + a2 * s
|
|
@@ -122,10 +122,10 @@ export class Mat2 extends Float32Array {
|
|
|
122
122
|
* Scales a mat2 by the dimensions in the given Vec2
|
|
123
123
|
*
|
|
124
124
|
* @param {Vec2} v the Vec2 to scale the matrix by
|
|
125
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
125
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
126
126
|
* @returns {Mat2} out
|
|
127
127
|
*/
|
|
128
|
-
scale(v: Vec2, out = glmaths.ALWAYS_COPY ?
|
|
128
|
+
scale(v: Vec2, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
129
129
|
const v0 = v[0], v1 = v[1]
|
|
130
130
|
out[0] = this[0] * v0
|
|
131
131
|
out[1] = this[1] * v0
|
|
@@ -138,10 +138,10 @@ export class Mat2 extends Float32Array {
|
|
|
138
138
|
* Creates a Mat2 from a given angle
|
|
139
139
|
*
|
|
140
140
|
* @param {Number} rad the angle to rotate the matrix by
|
|
141
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
141
|
+
* @param {Mat2} out the receiving matrix, defaults to mat2()
|
|
142
142
|
* @returns {Mat2} out
|
|
143
143
|
*/
|
|
144
|
-
static fromRotation(rad: number, out =
|
|
144
|
+
static fromRotation(rad: number, out = mat2()) {
|
|
145
145
|
const s = Math.sin(rad), c = Math.cos(rad)
|
|
146
146
|
out[0] = c
|
|
147
147
|
out[1] = s
|
|
@@ -154,10 +154,10 @@ export class Mat2 extends Float32Array {
|
|
|
154
154
|
* Creates a Mat2 from a scaling vector
|
|
155
155
|
*
|
|
156
156
|
* @param {Vec2} v scaling vector
|
|
157
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
157
|
+
* @param {Mat2} out the receiving matrix, defaults to mat2()
|
|
158
158
|
* @returns {Mat2} out
|
|
159
159
|
*/
|
|
160
|
-
static fromScaling(v: Vec2, out =
|
|
160
|
+
static fromScaling(v: Vec2, out = mat2()) {
|
|
161
161
|
out[0] = v[0]
|
|
162
162
|
out[1] = out[2] = 0
|
|
163
163
|
out[3] = v[1]
|
|
@@ -188,7 +188,7 @@ export class Mat2 extends Float32Array {
|
|
|
188
188
|
* @param {Mat2} D the diagonal matrix
|
|
189
189
|
* @param {Mat2} U the upper triangular matrix
|
|
190
190
|
*/
|
|
191
|
-
LDU(L =
|
|
191
|
+
LDU(L = mat2(), D = mat2(), U = mat2()) {
|
|
192
192
|
L[2] = this[2] / this[0]
|
|
193
193
|
U[0] = this[0]
|
|
194
194
|
U[1] = this[1]
|
|
@@ -200,10 +200,10 @@ export class Mat2 extends Float32Array {
|
|
|
200
200
|
* Adds two Mat2's
|
|
201
201
|
*
|
|
202
202
|
* @param {Mat2} b the second operand
|
|
203
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
203
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
204
204
|
* @returns {Mat2} out
|
|
205
205
|
*/
|
|
206
|
-
plus(b: Mat2, out = glmaths.ALWAYS_COPY ?
|
|
206
|
+
plus(b: Mat2, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
207
207
|
out[0] = this[0] + b[0]
|
|
208
208
|
out[1] = this[1] + b[1]
|
|
209
209
|
out[2] = this[2] + b[2]
|
|
@@ -215,10 +215,10 @@ export class Mat2 extends Float32Array {
|
|
|
215
215
|
* Subtracts matrix b from a mat2
|
|
216
216
|
*
|
|
217
217
|
* @param {Mat2} b the second operand
|
|
218
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
218
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
219
219
|
* @returns {Mat2} out
|
|
220
220
|
*/
|
|
221
|
-
minus(b: Mat2, out = glmaths.ALWAYS_COPY ?
|
|
221
|
+
minus(b: Mat2, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
222
222
|
out[0] = this[0] - b[0]
|
|
223
223
|
out[1] = this[1] - b[1]
|
|
224
224
|
out[2] = this[2] - b[2]
|
|
@@ -230,12 +230,12 @@ export class Mat2 extends Float32Array {
|
|
|
230
230
|
* Multiplies a mat2 by another Mat2
|
|
231
231
|
*
|
|
232
232
|
* @param {Mat2} b the second operand
|
|
233
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
233
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
234
234
|
* @returns {Mat2} out
|
|
235
235
|
*/
|
|
236
236
|
multiply(b: Vec2): Vec2
|
|
237
237
|
multiply(b: Mat2, out?: Mat2): Mat2
|
|
238
|
-
multiply(b: Mat2 | Vec2, out = glmaths.ALWAYS_COPY ?
|
|
238
|
+
multiply(b: Mat2 | Vec2, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
239
239
|
if (b instanceof Vec2)
|
|
240
240
|
return b.transformMat2(this)
|
|
241
241
|
|
|
@@ -274,10 +274,10 @@ export class Mat2 extends Float32Array {
|
|
|
274
274
|
* Multiplies each element of a mat2 by a scalar value
|
|
275
275
|
*
|
|
276
276
|
* @param {Number} b amount to scale the matrix's elements by
|
|
277
|
-
* @param {Mat2} out the receiving matrix, defaults to
|
|
277
|
+
* @param {Mat2} out the receiving matrix, defaults to new mat2()
|
|
278
278
|
* @returns {Mat2} out
|
|
279
279
|
*/
|
|
280
|
-
scaleScalar(b: number, out = glmaths.ALWAYS_COPY ?
|
|
280
|
+
scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? mat2() : this) {
|
|
281
281
|
out[0] = this[0] * b
|
|
282
282
|
out[1] = this[1] * b
|
|
283
283
|
out[2] = this[2] * b
|
package/src/mat2x3.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { Vec2 } from './vec2'
|
|
|
7
7
|
*/
|
|
8
8
|
export class Mat2x3 extends Float32Array {
|
|
9
9
|
|
|
10
|
-
static get identity() { return
|
|
11
|
-
static get Identity() { return
|
|
12
|
-
static get IDENTITY() { return
|
|
10
|
+
static get identity() { return mat2x3(1, 0, 0, 1, 0, 0) }
|
|
11
|
+
static get Identity() { return mat2x3(1, 0, 0, 1, 0, 0) }
|
|
12
|
+
static get IDENTITY() { return mat2x3(1, 0, 0, 1, 0, 0) }
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Creates a new Mat2x3
|
|
@@ -34,10 +34,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
34
34
|
/**
|
|
35
35
|
* Inverts a mat2x3
|
|
36
36
|
*
|
|
37
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
37
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
38
38
|
* @returns {Mat2x3} out or null if the matrix is not invertible
|
|
39
39
|
*/
|
|
40
|
-
invert(out = glmaths.ALWAYS_COPY ?
|
|
40
|
+
invert(out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
41
41
|
const aa = this[0], ab = this[1], ac = this[2], ad = this[3]
|
|
42
42
|
const atx = this[4], aty = this[5]
|
|
43
43
|
let det = aa * ad - ab * ac
|
|
@@ -65,10 +65,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
65
65
|
* Rotates a mat2x3 by the given angle
|
|
66
66
|
*
|
|
67
67
|
* @param {Number} rad the angle to rotate the matrix by
|
|
68
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
68
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
69
69
|
* @returns {Mat2x3} out
|
|
70
70
|
*/
|
|
71
|
-
rotate(rad: number, out = glmaths.ALWAYS_COPY ?
|
|
71
|
+
rotate(rad: number, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
72
72
|
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3], a4 = this[4], a5 = this[5];
|
|
73
73
|
const s = Math.sin(rad)
|
|
74
74
|
const c = Math.cos(rad)
|
|
@@ -85,10 +85,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
85
85
|
* Scales a mat2x3 by the dimensions in the given Vec2
|
|
86
86
|
*
|
|
87
87
|
* @param {Vec2} v the Vec2 to scale the matrix by
|
|
88
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
88
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
89
89
|
* @returns {Mat2x3} out
|
|
90
90
|
*/
|
|
91
|
-
scale(v: Vec2, out = glmaths.ALWAYS_COPY ?
|
|
91
|
+
scale(v: Vec2, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
92
92
|
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3], a4 = this[4], a5 = this[5]
|
|
93
93
|
const v0 = v[0], v1 = v[1]
|
|
94
94
|
out[0] = a0 * v0
|
|
@@ -104,10 +104,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
104
104
|
* Translates a mat2x3 by the dimensions in the given Vec2
|
|
105
105
|
*
|
|
106
106
|
* @param {Vec2} v the Vec2 to translate the matrix by
|
|
107
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
107
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
108
108
|
* @returns {Mat2x3} out
|
|
109
109
|
*/
|
|
110
|
-
translate(v: Vec2, out = glmaths.ALWAYS_COPY ?
|
|
110
|
+
translate(v: Vec2, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
111
111
|
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3], a4 = this[4], a5 = this[5]
|
|
112
112
|
const v0 = v[0], v1 = v[1]
|
|
113
113
|
out[0] = a0
|
|
@@ -123,10 +123,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
123
123
|
* Creates a Mat2x3 from a given angle
|
|
124
124
|
*
|
|
125
125
|
* @param {Number} rad the angle to rotate the matrix by
|
|
126
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
126
|
+
* @param {Mat2x3} out the receiving matrix, defaults to mat2x3()
|
|
127
127
|
* @returns {Mat2x3} out
|
|
128
128
|
*/
|
|
129
|
-
static fromRotation(rad: number, out =
|
|
129
|
+
static fromRotation(rad: number, out = mat2x3()) {
|
|
130
130
|
const s = Math.sin(rad), c = Math.cos(rad)
|
|
131
131
|
out[0] = c
|
|
132
132
|
out[1] = s
|
|
@@ -140,10 +140,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
140
140
|
* Creates a Mat2x3 from a scaling vector
|
|
141
141
|
*
|
|
142
142
|
* @param {Vec2} v scaling vector
|
|
143
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
143
|
+
* @param {Mat2x3} out the receiving matrix, defaults to mat2x3()
|
|
144
144
|
* @returns {Mat2x3} out
|
|
145
145
|
*/
|
|
146
|
-
static fromScaling(v: Vec2, out =
|
|
146
|
+
static fromScaling(v: Vec2, out = mat2x3()) {
|
|
147
147
|
out[0] = v[0]
|
|
148
148
|
out[1] = out[2] = 0
|
|
149
149
|
out[3] = v[1]
|
|
@@ -155,10 +155,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
155
155
|
* Creates a Mat2x3 from a translation vector
|
|
156
156
|
*
|
|
157
157
|
* @param {Vec2} v translation vector
|
|
158
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
158
|
+
* @param {Mat2x3} out the receiving matrix, defaults to mat2x3()
|
|
159
159
|
* @returns {Mat2x3} out
|
|
160
160
|
*/
|
|
161
|
-
static fromTranslation(v: Vec2, out =
|
|
161
|
+
static fromTranslation(v: Vec2, out = mat2x3()) {
|
|
162
162
|
out[0] = out[3] = 1
|
|
163
163
|
out[1] = out[2] = 0
|
|
164
164
|
out[4] = v[0]
|
|
@@ -188,10 +188,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
188
188
|
* Adds two Mat2x3's
|
|
189
189
|
*
|
|
190
190
|
* @param {Mat2x3} b the second operand
|
|
191
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
191
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
192
192
|
* @returns {Mat2x3} out
|
|
193
193
|
*/
|
|
194
|
-
plus(b: Mat2x3, out = glmaths.ALWAYS_COPY ?
|
|
194
|
+
plus(b: Mat2x3, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
195
195
|
out[0] = this[0] + b[0]
|
|
196
196
|
out[1] = this[1] + b[1]
|
|
197
197
|
out[2] = this[2] + b[2]
|
|
@@ -205,10 +205,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
205
205
|
* Subtracts matrix b from a mat2x3
|
|
206
206
|
*
|
|
207
207
|
* @param {Mat2x3} b the second operand
|
|
208
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
208
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
209
209
|
* @returns {Mat2x3} out
|
|
210
210
|
*/
|
|
211
|
-
minus(b: Mat2x3, out = glmaths.ALWAYS_COPY ?
|
|
211
|
+
minus(b: Mat2x3, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
212
212
|
out[0] = this[0] - b[0]
|
|
213
213
|
out[1] = this[1] - b[1]
|
|
214
214
|
out[2] = this[2] - b[2]
|
|
@@ -222,12 +222,12 @@ export class Mat2x3 extends Float32Array {
|
|
|
222
222
|
* Multiplies a mat2x3 by another Mat2x3
|
|
223
223
|
*
|
|
224
224
|
* @param {Mat2x3} b the second operand
|
|
225
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
225
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
226
226
|
* @returns {Mat2x3} out
|
|
227
227
|
*/
|
|
228
228
|
multiply(b: Vec2): Vec2
|
|
229
229
|
multiply(b: Mat2x3, out?: Mat2x3): Mat2x3
|
|
230
|
-
multiply(b: Mat2x3 | Vec2, out = glmaths.ALWAYS_COPY ?
|
|
230
|
+
multiply(b: Mat2x3 | Vec2, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
231
231
|
if (b instanceof Vec2)
|
|
232
232
|
return b.transformMat2x3(this)
|
|
233
233
|
|
|
@@ -271,10 +271,10 @@ export class Mat2x3 extends Float32Array {
|
|
|
271
271
|
* Multiplies each element of a mat2x3 by a scalar value
|
|
272
272
|
*
|
|
273
273
|
* @param {Number} b amount to scale the matrix's elements by
|
|
274
|
-
* @param {Mat2x3} out the receiving matrix, defaults to
|
|
274
|
+
* @param {Mat2x3} out the receiving matrix, defaults to new mat2x3()
|
|
275
275
|
* @returns {Mat2x3} out
|
|
276
276
|
*/
|
|
277
|
-
scaleScalar(b: number, out = glmaths.ALWAYS_COPY ?
|
|
277
|
+
scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? mat2x3() : this) {
|
|
278
278
|
out[0] = this[0] * b
|
|
279
279
|
out[1] = this[1] * b
|
|
280
280
|
out[2] = this[2] * b
|
|
@@ -290,7 +290,7 @@ export class Mat2x3 extends Float32Array {
|
|
|
290
290
|
* @returns {Mat2x3} a new Mat2x3
|
|
291
291
|
*/
|
|
292
292
|
clone(): Mat2x3 {
|
|
293
|
-
return
|
|
293
|
+
return mat2x3(
|
|
294
294
|
this[0], this[1], this[2],
|
|
295
295
|
this[3], this[4], this[5]
|
|
296
296
|
)
|
|
@@ -320,9 +320,15 @@ Mat2x3.prototype.times = Mat2x3.prototype.multiply
|
|
|
320
320
|
Mat2x3.prototype.str = Mat2x3.prototype.toString
|
|
321
321
|
Mat2x3.prototype.multiplyScalar = Mat2x3.prototype.scaleScalar
|
|
322
322
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
)
|
|
323
|
+
const createMat2x3 = (...args: (number | Float32Array)[]): Mat2x3 => {
|
|
324
|
+
const out = new Mat2x3()
|
|
325
|
+
let i = 0
|
|
326
|
+
for (const a of args) {
|
|
327
|
+
if (typeof a === 'number') out[i++] = a
|
|
328
|
+
else for (const v of a) out[i++] = v
|
|
329
|
+
}
|
|
330
|
+
return out
|
|
331
|
+
}
|
|
332
|
+
Object.setPrototypeOf(createMat2x3, Mat2x3)
|
|
333
|
+
export const mat2x3 = createMat2x3 as typeof createMat2x3 & typeof Mat2x3
|
|
328
334
|
export const mat2d = mat2x3
|