@predy-js/math 0.3.0-beta.8 → 0.3.0-beta.80

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -170,6 +170,13 @@ const temps = [0, 0, 0];
170
170
  function mat4create() {
171
171
  return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
172
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
+ }
173
180
  function mat4fromRotationTranslationScale(out, q, v, s) {
174
181
  const x = q[0];
175
182
  const y = q[1];
@@ -541,6 +548,29 @@ function mat4scale(out, a, v) {
541
548
  out[15] = a[15];
542
549
  return out;
543
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
+ }
544
574
  function mat4rotate(out, a, rad, axis) {
545
575
  let x = axis[0];
546
576
  let y = axis[1];
@@ -752,9 +782,9 @@ function getMat4Transform(te) {
752
782
  // create vec2
753
783
  function vec2Create(v2) {
754
784
  if (v2) {
755
- return [v2[0], v2[1]];
785
+ return new Float32Array([v2[0], v2[1]]);
756
786
  }
757
- return [0, 0];
787
+ return new Float32Array([0, 0]);
758
788
  }
759
789
  // vec2 add vec2
760
790
  function vec2AddVec2(out, a, b) {
@@ -917,9 +947,9 @@ function clamp(v, min, max) {
917
947
  const r2d = 180 / Math.PI;
918
948
  function vec3Create(v) {
919
949
  if (v) {
920
- return [v[0], v[1], v[2]];
950
+ return new Float32Array([v[0], v[1], v[2]]);
921
951
  }
922
- return [0, 0, 0];
952
+ return new Float32Array([0, 0, 0]);
923
953
  }
924
954
  function vec3MulVec3(out, a, b) {
925
955
  out[0] = a[0] * b[0];
@@ -1023,9 +1053,9 @@ function vec3RotateByQuat(out, a, quat) {
1023
1053
 
1024
1054
  function vec4Create(v) {
1025
1055
  if (v) {
1026
- return [v[0], v[1], v[2], v[3]];
1056
+ return new Float32Array([v[0], v[1], v[2], v[3]]);
1027
1057
  }
1028
- return [0, 0, 0, 0];
1058
+ return new Float32Array([0, 0, 0, 0]);
1029
1059
  }
1030
1060
 
1031
1061
  const d2r = Math.PI / 180;
@@ -1086,6 +1116,19 @@ function quatFromRotation(out, x, y, z) {
1086
1116
  out[3] = c1 * c2 * c3 + s1 * s2 * s3;
1087
1117
  return out;
1088
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
+ }
1089
1132
  function quatStar(out, quat) {
1090
1133
  const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
1091
1134
  out[0] = -x;
@@ -1094,6 +1137,33 @@ function quatStar(out, quat) {
1094
1137
  out[3] = w;
1095
1138
  return out;
1096
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
+ }
1097
1167
 
1098
1168
  exports.NumberEpsilon = NumberEpsilon;
1099
1169
  exports.clamp = clamp;
@@ -1114,6 +1184,7 @@ exports.mat3Translate = mat3Translate;
1114
1184
  exports.mat3create = mat3create;
1115
1185
  exports.mat4Clone = mat4Clone;
1116
1186
  exports.mat4Determinate = mat4Determinate;
1187
+ exports.mat4FromArray = mat4FromArray;
1117
1188
  exports.mat4create = mat4create;
1118
1189
  exports.mat4fromRotationTranslationScale = mat4fromRotationTranslationScale;
1119
1190
  exports.mat4identity = mat4identity;
@@ -1123,9 +1194,12 @@ exports.mat4perspective = mat4perspective;
1123
1194
  exports.mat4rotate = mat4rotate;
1124
1195
  exports.mat4scale = mat4scale;
1125
1196
  exports.mat4translate = mat4translate;
1197
+ exports.mat4transpose = mat4transpose;
1126
1198
  exports.quatCreate = quatCreate;
1127
1199
  exports.quatEquals = quatEquals;
1128
1200
  exports.quatFromRotation = quatFromRotation;
1201
+ exports.quatFromRotationXYZ = quatFromRotationXYZ;
1202
+ exports.quatFromVec3s = quatFromVec3s;
1129
1203
  exports.quatMultiply = quatMultiply;
1130
1204
  exports.quatStar = quatStar;
1131
1205
  exports.rotateVec2 = rotateVec2;
package/dist/index.mjs CHANGED
@@ -166,6 +166,13 @@ const temps = [0, 0, 0];
166
166
  function mat4create() {
167
167
  return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
168
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
+ }
169
176
  function mat4fromRotationTranslationScale(out, q, v, s) {
170
177
  const x = q[0];
171
178
  const y = q[1];
@@ -537,6 +544,29 @@ function mat4scale(out, a, v) {
537
544
  out[15] = a[15];
538
545
  return out;
539
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
+ }
540
570
  function mat4rotate(out, a, rad, axis) {
541
571
  let x = axis[0];
542
572
  let y = axis[1];
@@ -748,9 +778,9 @@ function getMat4Transform(te) {
748
778
  // create vec2
749
779
  function vec2Create(v2) {
750
780
  if (v2) {
751
- return [v2[0], v2[1]];
781
+ return new Float32Array([v2[0], v2[1]]);
752
782
  }
753
- return [0, 0];
783
+ return new Float32Array([0, 0]);
754
784
  }
755
785
  // vec2 add vec2
756
786
  function vec2AddVec2(out, a, b) {
@@ -913,9 +943,9 @@ function clamp(v, min, max) {
913
943
  const r2d = 180 / Math.PI;
914
944
  function vec3Create(v) {
915
945
  if (v) {
916
- return [v[0], v[1], v[2]];
946
+ return new Float32Array([v[0], v[1], v[2]]);
917
947
  }
918
- return [0, 0, 0];
948
+ return new Float32Array([0, 0, 0]);
919
949
  }
920
950
  function vec3MulVec3(out, a, b) {
921
951
  out[0] = a[0] * b[0];
@@ -1019,9 +1049,9 @@ function vec3RotateByQuat(out, a, quat) {
1019
1049
 
1020
1050
  function vec4Create(v) {
1021
1051
  if (v) {
1022
- return [v[0], v[1], v[2], v[3]];
1052
+ return new Float32Array([v[0], v[1], v[2], v[3]]);
1023
1053
  }
1024
- return [0, 0, 0, 0];
1054
+ return new Float32Array([0, 0, 0, 0]);
1025
1055
  }
1026
1056
 
1027
1057
  const d2r = Math.PI / 180;
@@ -1082,6 +1112,19 @@ function quatFromRotation(out, x, y, z) {
1082
1112
  out[3] = c1 * c2 * c3 + s1 * s2 * s3;
1083
1113
  return out;
1084
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
+ }
1085
1128
  function quatStar(out, quat) {
1086
1129
  const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
1087
1130
  out[0] = -x;
@@ -1090,5 +1133,32 @@ function quatStar(out, quat) {
1090
1133
  out[3] = w;
1091
1134
  return out;
1092
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
+ }
1093
1163
 
1094
- export { NumberEpsilon, clamp, ensureVec3, getMat4TR, getMat4TRS, getMat4Transform, 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, 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 };
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/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,7 @@ 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;
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/vec2.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { vec2, mat3 } from '@predy-js/render-interface';
2
- export declare function vec2Create(v2?: vec2): number[];
2
+ export declare function vec2Create(v2?: vec2): any;
3
3
  export declare function vec2AddVec2(out: vec2, a: vec2, b: vec2): vec2;
4
4
  export declare function vec2SubVec2(out: vec2, a: vec2, b: vec2): vec2;
5
5
  export declare function vec2MulVec2(out: vec2, a: vec2, b: vec2): vec2;
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: vec4): vec4;
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.8",
3
+ "version": "0.3.0-beta.80",
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.8",
31
+ "@predy-js/render-interface": "0.3.0-beta.80",
32
32
  "@commitlint/cli": "^13.2.1",
33
33
  "@commitlint/config-conventional": "^13.2.0",
34
34
  "@rollup/plugin-commonjs": "^21.0.3",