@predy-js/math 0.3.0-beta.1 → 0.3.0-beta.100
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +255 -4
- package/dist/index.mjs +243 -5
- package/dist/mat3.d.ts +1 -0
- package/dist/mat4.d.ts +8 -0
- package/dist/quat.d.ts +8 -1
- package/dist/vec.d.ts +1 -0
- package/dist/vec2.d.ts +6 -0
- package/dist/vec3.d.ts +1 -1
- package/dist/vec4.d.ts +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -138,6 +138,30 @@ function mat3Scale(out, a, v) {
|
|
138
138
|
out[8] = a[8];
|
139
139
|
return out;
|
140
140
|
}
|
141
|
+
function transposeMat3(out, e) {
|
142
|
+
if (!e || out === e) {
|
143
|
+
let t;
|
144
|
+
t = out[1];
|
145
|
+
out[1] = out[3];
|
146
|
+
out[3] = t;
|
147
|
+
t = out[2];
|
148
|
+
out[2] = out[6];
|
149
|
+
out[6] = t;
|
150
|
+
t = out[5];
|
151
|
+
out[5] = out[7];
|
152
|
+
out[7] = t;
|
153
|
+
}
|
154
|
+
else {
|
155
|
+
const a01 = e[1], a02 = e[2], a12 = e[5];
|
156
|
+
out[1] = e[3];
|
157
|
+
out[2] = e[6];
|
158
|
+
out[3] = a01;
|
159
|
+
out[5] = e[7];
|
160
|
+
out[6] = a02;
|
161
|
+
out[7] = a12;
|
162
|
+
}
|
163
|
+
return out;
|
164
|
+
}
|
141
165
|
|
142
166
|
const d2r$1 = Math.PI / 180;
|
143
167
|
const matrixRotation = new Float32Array(9);
|
@@ -146,6 +170,13 @@ const temps = [0, 0, 0];
|
|
146
170
|
function mat4create() {
|
147
171
|
return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
|
148
172
|
}
|
173
|
+
function mat4FromArray(arrLike, index = 0) {
|
174
|
+
const ret = new Float32Array(16);
|
175
|
+
for (let i = 0; i < 16; i++) {
|
176
|
+
ret[i] = (arrLike[index + i]) || 0;
|
177
|
+
}
|
178
|
+
return ret;
|
179
|
+
}
|
149
180
|
function mat4fromRotationTranslationScale(out, q, v, s) {
|
150
181
|
const x = q[0];
|
151
182
|
const y = q[1];
|
@@ -517,6 +548,29 @@ function mat4scale(out, a, v) {
|
|
517
548
|
out[15] = a[15];
|
518
549
|
return out;
|
519
550
|
}
|
551
|
+
function mat4transpose(out, a) {
|
552
|
+
//transpose mat4
|
553
|
+
const a01 = a[1], a02 = a[2], a03 = a[3];
|
554
|
+
const a12 = a[6], a13 = a[7];
|
555
|
+
const a23 = a[11];
|
556
|
+
out[0] = a[0];
|
557
|
+
out[1] = a[4];
|
558
|
+
out[2] = a[8];
|
559
|
+
out[3] = a[12];
|
560
|
+
out[4] = a01;
|
561
|
+
out[5] = a[5];
|
562
|
+
out[6] = a[9];
|
563
|
+
out[7] = a[13];
|
564
|
+
out[8] = a02;
|
565
|
+
out[9] = a12;
|
566
|
+
out[10] = a[10];
|
567
|
+
out[11] = a[14];
|
568
|
+
out[12] = a03;
|
569
|
+
out[13] = a13;
|
570
|
+
out[14] = a23;
|
571
|
+
out[15] = a[15];
|
572
|
+
return out;
|
573
|
+
}
|
520
574
|
function mat4rotate(out, a, rad, axis) {
|
521
575
|
let x = axis[0];
|
522
576
|
let y = axis[1];
|
@@ -619,6 +673,144 @@ function mat4identity(out) {
|
|
619
673
|
out[14] = 0;
|
620
674
|
out[15] = 1;
|
621
675
|
}
|
676
|
+
function transposeMat4(out, e) {
|
677
|
+
if (!e || out === e) {
|
678
|
+
let t;
|
679
|
+
t = out[1];
|
680
|
+
out[1] = out[4];
|
681
|
+
out[4] = t;
|
682
|
+
t = out[2];
|
683
|
+
out[2] = out[8];
|
684
|
+
out[8] = t;
|
685
|
+
t = out[3];
|
686
|
+
out[3] = out[12];
|
687
|
+
out[12] = t;
|
688
|
+
//
|
689
|
+
t = out[6];
|
690
|
+
out[6] = out[9];
|
691
|
+
out[9] = t;
|
692
|
+
t = out[7];
|
693
|
+
out[7] = out[13];
|
694
|
+
out[13] = t;
|
695
|
+
t = out[11];
|
696
|
+
out[11] = out[14];
|
697
|
+
out[14] = t;
|
698
|
+
}
|
699
|
+
else {
|
700
|
+
out[0] = e[0];
|
701
|
+
out[1] = e[4];
|
702
|
+
out[2] = e[8];
|
703
|
+
out[3] = e[12];
|
704
|
+
out[4] = e[1];
|
705
|
+
out[5] = e[5];
|
706
|
+
out[6] = e[9];
|
707
|
+
out[7] = e[13];
|
708
|
+
out[8] = e[2];
|
709
|
+
out[9] = e[6];
|
710
|
+
out[10] = e[10];
|
711
|
+
out[11] = e[14];
|
712
|
+
out[12] = e[3];
|
713
|
+
out[13] = e[7];
|
714
|
+
out[14] = e[11];
|
715
|
+
out[15] = e[15];
|
716
|
+
}
|
717
|
+
return out;
|
718
|
+
}
|
719
|
+
function getMat4Transform(te) {
|
720
|
+
// Extract the translation
|
721
|
+
const translation = [te[12], te[13], te[14]];
|
722
|
+
// Extract the scale
|
723
|
+
const sx = Math.hypot(te[0], te[1], te[2]);
|
724
|
+
const sy = Math.hypot(te[4], te[5], te[6]);
|
725
|
+
const sz = Math.hypot(te[8], te[9], te[10]);
|
726
|
+
const scale = [sx, sy, sz];
|
727
|
+
// Remove the scale from the matrix
|
728
|
+
const matrix = [
|
729
|
+
te[0] / sx, te[1] / sx, te[2] / sx, 0,
|
730
|
+
te[4] / sy, te[5] / sy, te[6] / sy, 0,
|
731
|
+
te[8] / sz, te[9] / sz, te[10] / sz, 0,
|
732
|
+
0, 0, 0, 1,
|
733
|
+
];
|
734
|
+
// Extract the rotation as a quaternion
|
735
|
+
const m11 = matrix[0];
|
736
|
+
const m12 = matrix[4];
|
737
|
+
const m13 = matrix[8];
|
738
|
+
const m21 = matrix[1];
|
739
|
+
const m22 = matrix[5];
|
740
|
+
const m23 = matrix[9];
|
741
|
+
const m31 = matrix[2];
|
742
|
+
const m32 = matrix[6];
|
743
|
+
const m33 = matrix[10];
|
744
|
+
const trace = m11 + m22 + m33;
|
745
|
+
let s;
|
746
|
+
let x;
|
747
|
+
let y;
|
748
|
+
let z;
|
749
|
+
let w;
|
750
|
+
if (trace > 0) {
|
751
|
+
s = 0.5 / Math.sqrt(trace + 1.0);
|
752
|
+
w = 0.25 / s;
|
753
|
+
x = (m32 - m23) * s;
|
754
|
+
y = (m13 - m31) * s;
|
755
|
+
z = (m21 - m12) * s;
|
756
|
+
}
|
757
|
+
else if (m11 > m22 && m11 > m33) {
|
758
|
+
s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
|
759
|
+
w = (m32 - m23) / s;
|
760
|
+
x = 0.25 * s;
|
761
|
+
y = (m12 + m21) / s;
|
762
|
+
z = (m13 + m31) / s;
|
763
|
+
}
|
764
|
+
else if (m22 > m33) {
|
765
|
+
s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
|
766
|
+
w = (m13 - m31) / s;
|
767
|
+
x = (m12 + m21) / s;
|
768
|
+
y = 0.25 * s;
|
769
|
+
z = (m23 + m32) / s;
|
770
|
+
}
|
771
|
+
else {
|
772
|
+
s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
|
773
|
+
w = (m21 - m12) / s;
|
774
|
+
x = (m13 + m31) / s;
|
775
|
+
y = (m23 + m32) / s;
|
776
|
+
z = 0.25 * s;
|
777
|
+
}
|
778
|
+
const rotation = [x, y, z, w];
|
779
|
+
return { translation, rotation, scale };
|
780
|
+
}
|
781
|
+
|
782
|
+
// create vec2
|
783
|
+
function vec2Create(v2) {
|
784
|
+
if (v2) {
|
785
|
+
return new Float32Array([v2[0], v2[1]]);
|
786
|
+
}
|
787
|
+
return new Float32Array([0, 0]);
|
788
|
+
}
|
789
|
+
// vec2 add vec2
|
790
|
+
function vec2AddVec2(out, a, b) {
|
791
|
+
out[0] = a[0] + b[0];
|
792
|
+
out[1] = a[1] + b[1];
|
793
|
+
return out;
|
794
|
+
}
|
795
|
+
// vec2 sub vec2
|
796
|
+
function vec2SubVec2(out, a, b) {
|
797
|
+
out[0] = a[0] - b[0];
|
798
|
+
out[1] = a[1] - b[1];
|
799
|
+
return out;
|
800
|
+
}
|
801
|
+
// vec2 mul vec2
|
802
|
+
function vec2MulVec2(out, a, b) {
|
803
|
+
out[0] = a[0] * b[0];
|
804
|
+
out[1] = a[1] * b[1];
|
805
|
+
return out;
|
806
|
+
}
|
807
|
+
// vec2 transform by mat3
|
808
|
+
function vec2MulMat3(out, a, m) {
|
809
|
+
const x = a[0], y = a[1];
|
810
|
+
out[0] = x * m[0] + y * m[3] + m[6];
|
811
|
+
out[1] = x * m[1] + y * m[4] + m[7];
|
812
|
+
return out;
|
813
|
+
}
|
622
814
|
|
623
815
|
const cos$1 = Math.cos;
|
624
816
|
const sin$1 = Math.sin;
|
@@ -628,6 +820,12 @@ function vecAdd(out, a, b) {
|
|
628
820
|
}
|
629
821
|
return out;
|
630
822
|
}
|
823
|
+
function vecSub(out, a, b) {
|
824
|
+
for (let i = 0, len = a.length; i < len; i++) {
|
825
|
+
out[i] = a[i] - b[i];
|
826
|
+
}
|
827
|
+
return out;
|
828
|
+
}
|
631
829
|
function vecFill(out, number) {
|
632
830
|
for (let i = 0, len = out.length; i < len; i++) {
|
633
831
|
out[i] = number;
|
@@ -749,9 +947,9 @@ function clamp(v, min, max) {
|
|
749
947
|
const r2d = 180 / Math.PI;
|
750
948
|
function vec3Create(v) {
|
751
949
|
if (v) {
|
752
|
-
return [v[0], v[1], v[2]];
|
950
|
+
return new Float32Array([v[0], v[1], v[2]]);
|
753
951
|
}
|
754
|
-
return [0, 0, 0];
|
952
|
+
return new Float32Array([0, 0, 0]);
|
755
953
|
}
|
756
954
|
function vec3MulVec3(out, a, b) {
|
757
955
|
out[0] = a[0] * b[0];
|
@@ -855,9 +1053,9 @@ function vec3RotateByQuat(out, a, quat) {
|
|
855
1053
|
|
856
1054
|
function vec4Create(v) {
|
857
1055
|
if (v) {
|
858
|
-
return [v[0], v[1], v[2], v[3]];
|
1056
|
+
return new Float32Array([v[0], v[1], v[2], v[3]]);
|
859
1057
|
}
|
860
|
-
return [0, 0, 0, 0];
|
1058
|
+
return new Float32Array([0, 0, 0, 0]);
|
861
1059
|
}
|
862
1060
|
|
863
1061
|
const d2r = Math.PI / 180;
|
@@ -918,6 +1116,19 @@ function quatFromRotation(out, x, y, z) {
|
|
918
1116
|
out[3] = c1 * c2 * c3 + s1 * s2 * s3;
|
919
1117
|
return out;
|
920
1118
|
}
|
1119
|
+
function quatFromRotationXYZ(out, x, y, z) {
|
1120
|
+
const c1 = Math.cos((x * d2r) / 2);
|
1121
|
+
const c2 = Math.cos((y * d2r) / 2);
|
1122
|
+
const c3 = Math.cos((z * d2r) / 2);
|
1123
|
+
const s1 = Math.sin((x * d2r) / 2);
|
1124
|
+
const s2 = Math.sin((y * d2r) / 2);
|
1125
|
+
const s3 = Math.sin((z * d2r) / 2);
|
1126
|
+
out[0] = s1 * c2 * c3 + c1 * s2 * s3;
|
1127
|
+
out[1] = c1 * s2 * c3 - s1 * c2 * s3;
|
1128
|
+
out[2] = c1 * c2 * s3 + s1 * s2 * c3;
|
1129
|
+
out[3] = c1 * c2 * c3 - s1 * s2 * s3;
|
1130
|
+
return out;
|
1131
|
+
}
|
921
1132
|
function quatStar(out, quat) {
|
922
1133
|
const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
|
923
1134
|
out[0] = -x;
|
@@ -926,12 +1137,40 @@ function quatStar(out, quat) {
|
|
926
1137
|
out[3] = w;
|
927
1138
|
return out;
|
928
1139
|
}
|
1140
|
+
/**
|
1141
|
+
* @param {vec3} vstart
|
1142
|
+
* @param {vec3} vend
|
1143
|
+
* @returns {vec4}
|
1144
|
+
*/
|
1145
|
+
function quatFromVec3s(v1, v2) {
|
1146
|
+
const cross = [
|
1147
|
+
v1[1] * v2[2] - v1[2] * v2[1],
|
1148
|
+
v1[2] * v2[0] - v1[0] * v2[2],
|
1149
|
+
v1[0] * v2[1] - v1[1] * v2[0],
|
1150
|
+
];
|
1151
|
+
const dot = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
1152
|
+
if (dot < -0.999999) {
|
1153
|
+
let orthogonalAxis = [1, 0, 0];
|
1154
|
+
if (Math.abs(v1[0]) > Math.abs(v1[2])) {
|
1155
|
+
orthogonalAxis = [0, 0, 1];
|
1156
|
+
}
|
1157
|
+
const orthogonalCross = [
|
1158
|
+
v1[1] * orthogonalAxis[2] - v1[2] * orthogonalAxis[1],
|
1159
|
+
v1[2] * orthogonalAxis[0] - v1[0] * orthogonalAxis[2],
|
1160
|
+
v1[0] * orthogonalAxis[1] - v1[1] * orthogonalAxis[0],
|
1161
|
+
];
|
1162
|
+
return vecNormalize([orthogonalCross[0], orthogonalCross[1], orthogonalCross[2], 0]);
|
1163
|
+
}
|
1164
|
+
const w = Math.sqrt((Math.pow(v1[0], 2) + Math.pow(v1[1], 2) + Math.pow(v1[2], 2)) * (Math.pow(v2[0], 2) + Math.pow(v2[1], 2) + Math.pow(v2[2], 2))) + dot;
|
1165
|
+
return vecNormalize([cross[0], cross[1], cross[2], w]);
|
1166
|
+
}
|
929
1167
|
|
930
1168
|
exports.NumberEpsilon = NumberEpsilon;
|
931
1169
|
exports.clamp = clamp;
|
932
1170
|
exports.ensureVec3 = ensureVec3;
|
933
1171
|
exports.getMat4TR = getMat4TR;
|
934
1172
|
exports.getMat4TRS = getMat4TRS;
|
1173
|
+
exports.getMat4Transform = getMat4Transform;
|
935
1174
|
exports.invertMat4 = invertMat4;
|
936
1175
|
exports.isZeroVec = isZeroVec;
|
937
1176
|
exports.mat3FromQuat = mat3FromQuat;
|
@@ -945,6 +1184,7 @@ exports.mat3Translate = mat3Translate;
|
|
945
1184
|
exports.mat3create = mat3create;
|
946
1185
|
exports.mat4Clone = mat4Clone;
|
947
1186
|
exports.mat4Determinate = mat4Determinate;
|
1187
|
+
exports.mat4FromArray = mat4FromArray;
|
948
1188
|
exports.mat4create = mat4create;
|
949
1189
|
exports.mat4fromRotationTranslationScale = mat4fromRotationTranslationScale;
|
950
1190
|
exports.mat4identity = mat4identity;
|
@@ -954,13 +1194,23 @@ exports.mat4perspective = mat4perspective;
|
|
954
1194
|
exports.mat4rotate = mat4rotate;
|
955
1195
|
exports.mat4scale = mat4scale;
|
956
1196
|
exports.mat4translate = mat4translate;
|
1197
|
+
exports.mat4transpose = mat4transpose;
|
957
1198
|
exports.quatCreate = quatCreate;
|
958
1199
|
exports.quatEquals = quatEquals;
|
959
1200
|
exports.quatFromRotation = quatFromRotation;
|
1201
|
+
exports.quatFromRotationXYZ = quatFromRotationXYZ;
|
1202
|
+
exports.quatFromVec3s = quatFromVec3s;
|
960
1203
|
exports.quatMultiply = quatMultiply;
|
961
1204
|
exports.quatStar = quatStar;
|
962
1205
|
exports.rotateVec2 = rotateVec2;
|
963
1206
|
exports.rotationFromMat3 = rotationFromMat3;
|
1207
|
+
exports.transposeMat3 = transposeMat3;
|
1208
|
+
exports.transposeMat4 = transposeMat4;
|
1209
|
+
exports.vec2AddVec2 = vec2AddVec2;
|
1210
|
+
exports.vec2Create = vec2Create;
|
1211
|
+
exports.vec2MulMat3 = vec2MulMat3;
|
1212
|
+
exports.vec2MulVec2 = vec2MulVec2;
|
1213
|
+
exports.vec2SubVec2 = vec2SubVec2;
|
964
1214
|
exports.vec2TransformByMat3 = vec2TransformByMat3;
|
965
1215
|
exports.vec3AddVec3 = vec3AddVec3;
|
966
1216
|
exports.vec3Create = vec3Create;
|
@@ -983,3 +1233,4 @@ exports.vecMulCombine = vecMulCombine;
|
|
983
1233
|
exports.vecMulScalar = vecMulScalar;
|
984
1234
|
exports.vecNormalize = vecNormalize;
|
985
1235
|
exports.vecSquareDistance = vecSquareDistance;
|
1236
|
+
exports.vecSub = vecSub;
|
package/dist/index.mjs
CHANGED
@@ -134,6 +134,30 @@ function mat3Scale(out, a, v) {
|
|
134
134
|
out[8] = a[8];
|
135
135
|
return out;
|
136
136
|
}
|
137
|
+
function transposeMat3(out, e) {
|
138
|
+
if (!e || out === e) {
|
139
|
+
let t;
|
140
|
+
t = out[1];
|
141
|
+
out[1] = out[3];
|
142
|
+
out[3] = t;
|
143
|
+
t = out[2];
|
144
|
+
out[2] = out[6];
|
145
|
+
out[6] = t;
|
146
|
+
t = out[5];
|
147
|
+
out[5] = out[7];
|
148
|
+
out[7] = t;
|
149
|
+
}
|
150
|
+
else {
|
151
|
+
const a01 = e[1], a02 = e[2], a12 = e[5];
|
152
|
+
out[1] = e[3];
|
153
|
+
out[2] = e[6];
|
154
|
+
out[3] = a01;
|
155
|
+
out[5] = e[7];
|
156
|
+
out[6] = a02;
|
157
|
+
out[7] = a12;
|
158
|
+
}
|
159
|
+
return out;
|
160
|
+
}
|
137
161
|
|
138
162
|
const d2r$1 = Math.PI / 180;
|
139
163
|
const matrixRotation = new Float32Array(9);
|
@@ -142,6 +166,13 @@ const temps = [0, 0, 0];
|
|
142
166
|
function mat4create() {
|
143
167
|
return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
|
144
168
|
}
|
169
|
+
function mat4FromArray(arrLike, index = 0) {
|
170
|
+
const ret = new Float32Array(16);
|
171
|
+
for (let i = 0; i < 16; i++) {
|
172
|
+
ret[i] = (arrLike[index + i]) || 0;
|
173
|
+
}
|
174
|
+
return ret;
|
175
|
+
}
|
145
176
|
function mat4fromRotationTranslationScale(out, q, v, s) {
|
146
177
|
const x = q[0];
|
147
178
|
const y = q[1];
|
@@ -513,6 +544,29 @@ function mat4scale(out, a, v) {
|
|
513
544
|
out[15] = a[15];
|
514
545
|
return out;
|
515
546
|
}
|
547
|
+
function mat4transpose(out, a) {
|
548
|
+
//transpose mat4
|
549
|
+
const a01 = a[1], a02 = a[2], a03 = a[3];
|
550
|
+
const a12 = a[6], a13 = a[7];
|
551
|
+
const a23 = a[11];
|
552
|
+
out[0] = a[0];
|
553
|
+
out[1] = a[4];
|
554
|
+
out[2] = a[8];
|
555
|
+
out[3] = a[12];
|
556
|
+
out[4] = a01;
|
557
|
+
out[5] = a[5];
|
558
|
+
out[6] = a[9];
|
559
|
+
out[7] = a[13];
|
560
|
+
out[8] = a02;
|
561
|
+
out[9] = a12;
|
562
|
+
out[10] = a[10];
|
563
|
+
out[11] = a[14];
|
564
|
+
out[12] = a03;
|
565
|
+
out[13] = a13;
|
566
|
+
out[14] = a23;
|
567
|
+
out[15] = a[15];
|
568
|
+
return out;
|
569
|
+
}
|
516
570
|
function mat4rotate(out, a, rad, axis) {
|
517
571
|
let x = axis[0];
|
518
572
|
let y = axis[1];
|
@@ -615,6 +669,144 @@ function mat4identity(out) {
|
|
615
669
|
out[14] = 0;
|
616
670
|
out[15] = 1;
|
617
671
|
}
|
672
|
+
function transposeMat4(out, e) {
|
673
|
+
if (!e || out === e) {
|
674
|
+
let t;
|
675
|
+
t = out[1];
|
676
|
+
out[1] = out[4];
|
677
|
+
out[4] = t;
|
678
|
+
t = out[2];
|
679
|
+
out[2] = out[8];
|
680
|
+
out[8] = t;
|
681
|
+
t = out[3];
|
682
|
+
out[3] = out[12];
|
683
|
+
out[12] = t;
|
684
|
+
//
|
685
|
+
t = out[6];
|
686
|
+
out[6] = out[9];
|
687
|
+
out[9] = t;
|
688
|
+
t = out[7];
|
689
|
+
out[7] = out[13];
|
690
|
+
out[13] = t;
|
691
|
+
t = out[11];
|
692
|
+
out[11] = out[14];
|
693
|
+
out[14] = t;
|
694
|
+
}
|
695
|
+
else {
|
696
|
+
out[0] = e[0];
|
697
|
+
out[1] = e[4];
|
698
|
+
out[2] = e[8];
|
699
|
+
out[3] = e[12];
|
700
|
+
out[4] = e[1];
|
701
|
+
out[5] = e[5];
|
702
|
+
out[6] = e[9];
|
703
|
+
out[7] = e[13];
|
704
|
+
out[8] = e[2];
|
705
|
+
out[9] = e[6];
|
706
|
+
out[10] = e[10];
|
707
|
+
out[11] = e[14];
|
708
|
+
out[12] = e[3];
|
709
|
+
out[13] = e[7];
|
710
|
+
out[14] = e[11];
|
711
|
+
out[15] = e[15];
|
712
|
+
}
|
713
|
+
return out;
|
714
|
+
}
|
715
|
+
function getMat4Transform(te) {
|
716
|
+
// Extract the translation
|
717
|
+
const translation = [te[12], te[13], te[14]];
|
718
|
+
// Extract the scale
|
719
|
+
const sx = Math.hypot(te[0], te[1], te[2]);
|
720
|
+
const sy = Math.hypot(te[4], te[5], te[6]);
|
721
|
+
const sz = Math.hypot(te[8], te[9], te[10]);
|
722
|
+
const scale = [sx, sy, sz];
|
723
|
+
// Remove the scale from the matrix
|
724
|
+
const matrix = [
|
725
|
+
te[0] / sx, te[1] / sx, te[2] / sx, 0,
|
726
|
+
te[4] / sy, te[5] / sy, te[6] / sy, 0,
|
727
|
+
te[8] / sz, te[9] / sz, te[10] / sz, 0,
|
728
|
+
0, 0, 0, 1,
|
729
|
+
];
|
730
|
+
// Extract the rotation as a quaternion
|
731
|
+
const m11 = matrix[0];
|
732
|
+
const m12 = matrix[4];
|
733
|
+
const m13 = matrix[8];
|
734
|
+
const m21 = matrix[1];
|
735
|
+
const m22 = matrix[5];
|
736
|
+
const m23 = matrix[9];
|
737
|
+
const m31 = matrix[2];
|
738
|
+
const m32 = matrix[6];
|
739
|
+
const m33 = matrix[10];
|
740
|
+
const trace = m11 + m22 + m33;
|
741
|
+
let s;
|
742
|
+
let x;
|
743
|
+
let y;
|
744
|
+
let z;
|
745
|
+
let w;
|
746
|
+
if (trace > 0) {
|
747
|
+
s = 0.5 / Math.sqrt(trace + 1.0);
|
748
|
+
w = 0.25 / s;
|
749
|
+
x = (m32 - m23) * s;
|
750
|
+
y = (m13 - m31) * s;
|
751
|
+
z = (m21 - m12) * s;
|
752
|
+
}
|
753
|
+
else if (m11 > m22 && m11 > m33) {
|
754
|
+
s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
|
755
|
+
w = (m32 - m23) / s;
|
756
|
+
x = 0.25 * s;
|
757
|
+
y = (m12 + m21) / s;
|
758
|
+
z = (m13 + m31) / s;
|
759
|
+
}
|
760
|
+
else if (m22 > m33) {
|
761
|
+
s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
|
762
|
+
w = (m13 - m31) / s;
|
763
|
+
x = (m12 + m21) / s;
|
764
|
+
y = 0.25 * s;
|
765
|
+
z = (m23 + m32) / s;
|
766
|
+
}
|
767
|
+
else {
|
768
|
+
s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
|
769
|
+
w = (m21 - m12) / s;
|
770
|
+
x = (m13 + m31) / s;
|
771
|
+
y = (m23 + m32) / s;
|
772
|
+
z = 0.25 * s;
|
773
|
+
}
|
774
|
+
const rotation = [x, y, z, w];
|
775
|
+
return { translation, rotation, scale };
|
776
|
+
}
|
777
|
+
|
778
|
+
// create vec2
|
779
|
+
function vec2Create(v2) {
|
780
|
+
if (v2) {
|
781
|
+
return new Float32Array([v2[0], v2[1]]);
|
782
|
+
}
|
783
|
+
return new Float32Array([0, 0]);
|
784
|
+
}
|
785
|
+
// vec2 add vec2
|
786
|
+
function vec2AddVec2(out, a, b) {
|
787
|
+
out[0] = a[0] + b[0];
|
788
|
+
out[1] = a[1] + b[1];
|
789
|
+
return out;
|
790
|
+
}
|
791
|
+
// vec2 sub vec2
|
792
|
+
function vec2SubVec2(out, a, b) {
|
793
|
+
out[0] = a[0] - b[0];
|
794
|
+
out[1] = a[1] - b[1];
|
795
|
+
return out;
|
796
|
+
}
|
797
|
+
// vec2 mul vec2
|
798
|
+
function vec2MulVec2(out, a, b) {
|
799
|
+
out[0] = a[0] * b[0];
|
800
|
+
out[1] = a[1] * b[1];
|
801
|
+
return out;
|
802
|
+
}
|
803
|
+
// vec2 transform by mat3
|
804
|
+
function vec2MulMat3(out, a, m) {
|
805
|
+
const x = a[0], y = a[1];
|
806
|
+
out[0] = x * m[0] + y * m[3] + m[6];
|
807
|
+
out[1] = x * m[1] + y * m[4] + m[7];
|
808
|
+
return out;
|
809
|
+
}
|
618
810
|
|
619
811
|
const cos$1 = Math.cos;
|
620
812
|
const sin$1 = Math.sin;
|
@@ -624,6 +816,12 @@ function vecAdd(out, a, b) {
|
|
624
816
|
}
|
625
817
|
return out;
|
626
818
|
}
|
819
|
+
function vecSub(out, a, b) {
|
820
|
+
for (let i = 0, len = a.length; i < len; i++) {
|
821
|
+
out[i] = a[i] - b[i];
|
822
|
+
}
|
823
|
+
return out;
|
824
|
+
}
|
627
825
|
function vecFill(out, number) {
|
628
826
|
for (let i = 0, len = out.length; i < len; i++) {
|
629
827
|
out[i] = number;
|
@@ -745,9 +943,9 @@ function clamp(v, min, max) {
|
|
745
943
|
const r2d = 180 / Math.PI;
|
746
944
|
function vec3Create(v) {
|
747
945
|
if (v) {
|
748
|
-
return [v[0], v[1], v[2]];
|
946
|
+
return new Float32Array([v[0], v[1], v[2]]);
|
749
947
|
}
|
750
|
-
return [0, 0, 0];
|
948
|
+
return new Float32Array([0, 0, 0]);
|
751
949
|
}
|
752
950
|
function vec3MulVec3(out, a, b) {
|
753
951
|
out[0] = a[0] * b[0];
|
@@ -851,9 +1049,9 @@ function vec3RotateByQuat(out, a, quat) {
|
|
851
1049
|
|
852
1050
|
function vec4Create(v) {
|
853
1051
|
if (v) {
|
854
|
-
return [v[0], v[1], v[2], v[3]];
|
1052
|
+
return new Float32Array([v[0], v[1], v[2], v[3]]);
|
855
1053
|
}
|
856
|
-
return [0, 0, 0, 0];
|
1054
|
+
return new Float32Array([0, 0, 0, 0]);
|
857
1055
|
}
|
858
1056
|
|
859
1057
|
const d2r = Math.PI / 180;
|
@@ -914,6 +1112,19 @@ function quatFromRotation(out, x, y, z) {
|
|
914
1112
|
out[3] = c1 * c2 * c3 + s1 * s2 * s3;
|
915
1113
|
return out;
|
916
1114
|
}
|
1115
|
+
function quatFromRotationXYZ(out, x, y, z) {
|
1116
|
+
const c1 = Math.cos((x * d2r) / 2);
|
1117
|
+
const c2 = Math.cos((y * d2r) / 2);
|
1118
|
+
const c3 = Math.cos((z * d2r) / 2);
|
1119
|
+
const s1 = Math.sin((x * d2r) / 2);
|
1120
|
+
const s2 = Math.sin((y * d2r) / 2);
|
1121
|
+
const s3 = Math.sin((z * d2r) / 2);
|
1122
|
+
out[0] = s1 * c2 * c3 + c1 * s2 * s3;
|
1123
|
+
out[1] = c1 * s2 * c3 - s1 * c2 * s3;
|
1124
|
+
out[2] = c1 * c2 * s3 + s1 * s2 * c3;
|
1125
|
+
out[3] = c1 * c2 * c3 - s1 * s2 * s3;
|
1126
|
+
return out;
|
1127
|
+
}
|
917
1128
|
function quatStar(out, quat) {
|
918
1129
|
const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
|
919
1130
|
out[0] = -x;
|
@@ -922,5 +1133,32 @@ function quatStar(out, quat) {
|
|
922
1133
|
out[3] = w;
|
923
1134
|
return out;
|
924
1135
|
}
|
1136
|
+
/**
|
1137
|
+
* @param {vec3} vstart
|
1138
|
+
* @param {vec3} vend
|
1139
|
+
* @returns {vec4}
|
1140
|
+
*/
|
1141
|
+
function quatFromVec3s(v1, v2) {
|
1142
|
+
const cross = [
|
1143
|
+
v1[1] * v2[2] - v1[2] * v2[1],
|
1144
|
+
v1[2] * v2[0] - v1[0] * v2[2],
|
1145
|
+
v1[0] * v2[1] - v1[1] * v2[0],
|
1146
|
+
];
|
1147
|
+
const dot = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
1148
|
+
if (dot < -0.999999) {
|
1149
|
+
let orthogonalAxis = [1, 0, 0];
|
1150
|
+
if (Math.abs(v1[0]) > Math.abs(v1[2])) {
|
1151
|
+
orthogonalAxis = [0, 0, 1];
|
1152
|
+
}
|
1153
|
+
const orthogonalCross = [
|
1154
|
+
v1[1] * orthogonalAxis[2] - v1[2] * orthogonalAxis[1],
|
1155
|
+
v1[2] * orthogonalAxis[0] - v1[0] * orthogonalAxis[2],
|
1156
|
+
v1[0] * orthogonalAxis[1] - v1[1] * orthogonalAxis[0],
|
1157
|
+
];
|
1158
|
+
return vecNormalize([orthogonalCross[0], orthogonalCross[1], orthogonalCross[2], 0]);
|
1159
|
+
}
|
1160
|
+
const w = Math.sqrt((Math.pow(v1[0], 2) + Math.pow(v1[1], 2) + Math.pow(v1[2], 2)) * (Math.pow(v2[0], 2) + Math.pow(v2[1], 2) + Math.pow(v2[2], 2))) + dot;
|
1161
|
+
return vecNormalize([cross[0], cross[1], cross[2], w]);
|
1162
|
+
}
|
925
1163
|
|
926
|
-
export { NumberEpsilon, clamp, ensureVec3, getMat4TR, getMat4TRS, invertMat4, isZeroVec, mat3FromQuat, mat3FromRotation, mat3FromRotationZ, mat3MulMat3, mat3NormalFromMat4, mat3Rotate, mat3Scale, mat3Translate, mat3create, mat4Clone, mat4Determinate, mat4create, mat4fromRotationTranslationScale, mat4identity, mat4invert, mat4multiply, mat4perspective, mat4rotate, mat4scale, mat4translate, quatCreate, quatEquals, quatFromRotation, quatMultiply, quatStar, rotateVec2, rotationFromMat3, vec2TransformByMat3, vec3AddVec3, vec3Create, vec3Cross, vec3MulMat3, vec3MulMat4, vec3MulNum, vec3MulVec3, vec3RotateByMat4, vec3RotateByQuat, vec3TranslateByMat4, vec4Create, vecAdd, vecAddCombine, vecAssign, vecDot, vecFill, vecMinus, vecMulCombine, vecMulScalar, vecNormalize, vecSquareDistance };
|
1164
|
+
export { NumberEpsilon, clamp, ensureVec3, getMat4TR, getMat4TRS, getMat4Transform, invertMat4, isZeroVec, mat3FromQuat, mat3FromRotation, mat3FromRotationZ, mat3MulMat3, mat3NormalFromMat4, mat3Rotate, mat3Scale, mat3Translate, mat3create, mat4Clone, mat4Determinate, mat4FromArray, mat4create, mat4fromRotationTranslationScale, mat4identity, mat4invert, mat4multiply, mat4perspective, mat4rotate, mat4scale, mat4translate, mat4transpose, quatCreate, quatEquals, quatFromRotation, quatFromRotationXYZ, quatFromVec3s, quatMultiply, quatStar, rotateVec2, rotationFromMat3, transposeMat3, transposeMat4, vec2AddVec2, vec2Create, vec2MulMat3, vec2MulVec2, vec2SubVec2, vec2TransformByMat3, vec3AddVec3, vec3Create, vec3Cross, vec3MulMat3, vec3MulMat4, vec3MulNum, vec3MulVec3, vec3RotateByMat4, vec3RotateByQuat, vec3TranslateByMat4, vec4Create, vecAdd, vecAddCombine, vecAssign, vecDot, vecFill, vecMinus, vecMulCombine, vecMulScalar, vecNormalize, vecSquareDistance, vecSub };
|
package/dist/mat3.d.ts
CHANGED
@@ -7,3 +7,4 @@ export declare function mat3NormalFromMat4(out: mat3 | number[], a: mat4): mat3;
|
|
7
7
|
export declare function mat3Translate(out: mat3, a: mat3, v: vec2): mat3;
|
8
8
|
export declare function mat3Rotate(out: mat3, a: mat3, rad: number): mat3;
|
9
9
|
export declare function mat3Scale(out: mat3, a: mat3, v: vec2): mat3;
|
10
|
+
export declare function transposeMat3(out: mat3 | number[], e?: mat3): mat3;
|
package/dist/mat4.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import type { mat4 } from '@predy-js/render-interface';
|
2
2
|
import type { vec3, vec4 } from '@predy-js/render-interface';
|
3
3
|
export declare function mat4create(): mat4;
|
4
|
+
export declare function mat4FromArray(arrLike: ArrayLike<number>, index?: number): mat4;
|
4
5
|
export declare function mat4fromRotationTranslationScale(out: mat4 | number[], q: vec4, v: vec3, s: vec3): mat4;
|
5
6
|
export declare function invertMat4(out: mat4 | number[], a: mat4): mat4;
|
6
7
|
export declare function mat4Determinate(a: mat4): number;
|
@@ -11,6 +12,13 @@ export declare function mat4Clone(out: mat4 | number[], from: mat4): mat4;
|
|
11
12
|
export declare function mat4invert(out: mat4 | number[], a: mat4): mat4;
|
12
13
|
export declare function mat4translate(out: mat4, a: mat4, v: vec3): mat4;
|
13
14
|
export declare function mat4scale(out: mat4, a: mat4, v: vec3): mat4;
|
15
|
+
export declare function mat4transpose(out: mat4, a: mat4): mat4;
|
14
16
|
export declare function mat4rotate(out: mat4, a: mat4, rad: number, axis: vec3): mat4;
|
15
17
|
export declare function mat4perspective(out: number[] | mat4, fovy: number, aspect: number, near: number, far: number, reverse?: boolean): mat4;
|
16
18
|
export declare function mat4identity(out: mat4): void;
|
19
|
+
export declare function transposeMat4(out: mat4 | number[], e?: mat4): mat4;
|
20
|
+
export declare function getMat4Transform(te: mat4): {
|
21
|
+
translation: vec3;
|
22
|
+
rotation: vec4;
|
23
|
+
scale: vec3;
|
24
|
+
};
|
package/dist/quat.d.ts
CHANGED
@@ -1,7 +1,14 @@
|
|
1
|
-
import type { mat3, vec4 } from '@predy-js/render-interface';
|
1
|
+
import type { mat3, vec3, vec4 } from '@predy-js/render-interface';
|
2
2
|
export declare function quatCreate(): vec4;
|
3
3
|
export declare function quatMultiply(out: vec4, a: vec4, b: vec4): vec4;
|
4
4
|
export declare function mat3FromQuat(out: mat3 | number[], quat: vec4): mat3;
|
5
5
|
export declare function quatEquals(q1: vec4, q2: vec4): boolean;
|
6
6
|
export declare function quatFromRotation(out: vec4, x: number, y: number, z: number): vec4;
|
7
|
+
export declare function quatFromRotationXYZ(out: vec4, x: number, y: number, z: number): vec4;
|
7
8
|
export declare function quatStar(out: vec4, quat: vec4): vec4;
|
9
|
+
/**
|
10
|
+
* @param {vec3} vstart
|
11
|
+
* @param {vec3} vend
|
12
|
+
* @returns {vec4}
|
13
|
+
*/
|
14
|
+
export declare function quatFromVec3s(v1: vec3, v2: vec3): vec4;
|
package/dist/vec.d.ts
CHANGED
@@ -2,6 +2,7 @@ import type { vec3, vec4, vec2, mat4, mat2, mat3 } from '@predy-js/render-interf
|
|
2
2
|
export type vec = number[];
|
3
3
|
export { vec2, vec4, vec3, mat2, mat3, mat4 };
|
4
4
|
export declare function vecAdd<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
|
5
|
+
export declare function vecSub<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
|
5
6
|
export declare function vecFill<T extends vec | vec3 | vec4 | vec2>(out: T | number[], number: number): T;
|
6
7
|
export declare function vecAddCombine<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, b: T): T;
|
7
8
|
export declare function vecAssign<T extends vec | vec3 | vec4 | vec2>(out: T | number[], a: T, count: number, start?: number): T;
|
package/dist/vec2.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { vec2, mat3 } from '@predy-js/render-interface';
|
2
|
+
export declare function vec2Create(v2?: vec2): any;
|
3
|
+
export declare function vec2AddVec2(out: vec2, a: vec2, b: vec2): vec2;
|
4
|
+
export declare function vec2SubVec2(out: vec2, a: vec2, b: vec2): vec2;
|
5
|
+
export declare function vec2MulVec2(out: vec2, a: vec2, b: vec2): vec2;
|
6
|
+
export declare function vec2MulMat3(out: vec2, a: vec2, m: mat3): vec2;
|
package/dist/vec3.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { vec3, mat4, mat3 } from '@predy-js/render-interface';
|
2
2
|
import type { vec4 } from '@predy-js/render-interface';
|
3
|
-
export declare function vec3Create(v?: vec3): vec3;
|
3
|
+
export declare function vec3Create(v?: vec3 | number[] | Float32Array): vec3;
|
4
4
|
export declare function vec3MulVec3(out: vec3, a: vec3, b: vec3): vec3;
|
5
5
|
export declare function vec3AddVec3(out: vec3, a: vec3, b: vec3): vec3;
|
6
6
|
export declare function vec3Cross(out: vec3, a: vec3, b: vec3): vec3;
|
package/dist/vec4.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
import type { vec4 } from '@predy-js/render-interface';
|
2
|
-
export declare function vec4Create(v
|
2
|
+
export declare function vec4Create(v?: vec4): vec4;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@predy-js/math",
|
3
|
-
"version": "0.3.0-beta.
|
3
|
+
"version": "0.3.0-beta.100",
|
4
4
|
"description": "Mars JSON Specification",
|
5
5
|
"module": "./dist/index.mjs",
|
6
6
|
"main": "./dist/index.js",
|
@@ -28,7 +28,7 @@
|
|
28
28
|
"prepublishOnly": "npm run build"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
|
-
"@predy-js/render-interface": "0.3.0-beta.
|
31
|
+
"@predy-js/render-interface": "0.3.0-beta.100",
|
32
32
|
"@commitlint/cli": "^13.2.1",
|
33
33
|
"@commitlint/config-conventional": "^13.2.0",
|
34
34
|
"@rollup/plugin-commonjs": "^21.0.3",
|