Skip to content

Commit c9c8903

Browse files
committed
feat: add quat/mat4. fromPointToPoint
1 parent 14d8ace commit c9c8903

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

mat4.js

+72
Original file line numberDiff line numberDiff line change
@@ -885,3 +885,75 @@ export function targetTo(
885885
a[15] = 1;
886886
return a;
887887
}
888+
889+
/**
890+
* Sets a matrix from a point to another.
891+
* @param {import("./types.js").mat4} a
892+
* @param {import("./types.js").vec3} from
893+
* @param {import("./types.js").vec3} to
894+
* @param {import("./types.js").vec3} [up=Y_UP]
895+
* @returns {import("./types.js").mat4}
896+
*/
897+
export function fromPointToPoint(
898+
a,
899+
[eyex, eyey, eyez],
900+
[targetx, targety, targetz],
901+
[upx, upy, upz] = Y_UP
902+
) {
903+
let z0 = targetx - eyex;
904+
let z1 = targety - eyey;
905+
let z2 = targetz - eyez;
906+
907+
let len = z0 * z0 + z1 * z1 + z2 * z2;
908+
909+
if (len > 0) {
910+
len = 1 / Math.sqrt(len);
911+
z0 *= len;
912+
z1 *= len;
913+
z2 *= len;
914+
}
915+
916+
let x0 = upy * z2 - upz * z1;
917+
let x1 = upz * z0 - upx * z2;
918+
let x2 = upx * z1 - upy * z0;
919+
920+
len = x0 * x0 + x1 * x1 + x2 * x2;
921+
922+
if (len > 0) {
923+
len = 1 / Math.sqrt(len);
924+
x0 *= len;
925+
x1 *= len;
926+
x2 *= len;
927+
}
928+
929+
upx = z1 * x2 - z2 * x1;
930+
upy = z2 * x0 - z0 * x2;
931+
upz = z0 * x1 - z1 * x0;
932+
933+
len = upx * upx + upy * upy + upz * upz;
934+
935+
if (len > 0) {
936+
len = 1 / Math.sqrt(len);
937+
upx *= len;
938+
upy *= len;
939+
upz *= len;
940+
}
941+
942+
a[0] = x0;
943+
a[1] = x1;
944+
a[2] = x2;
945+
a[3] = 0;
946+
a[4] = upx;
947+
a[5] = upy;
948+
a[6] = upz;
949+
a[7] = 0;
950+
a[8] = z0;
951+
a[9] = z1;
952+
a[10] = z2;
953+
a[11] = 0;
954+
a[12] = 0;
955+
a[13] = 0;
956+
a[14] = 0;
957+
a[15] = 1;
958+
return a;
959+
}

quat.js

+12
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ export function targetTo(a, eye, target, up) {
288288
return fromMat4(a, mat4.targetTo(TEMP_MAT4, eye, target, up));
289289
}
290290

291+
/**
292+
* Sets a quaternion from a point to another.
293+
* @param {import("./types.js").quat} a
294+
* @param {import("./types.js").vec3} eye
295+
* @param {import("./types.js").vec3} target
296+
* @param {import("./types.js").vec3} [up=Y_UP]
297+
* @returns {import("./types.js").quat}
298+
*/
299+
export function fromPointToPoint(a, eye, target, up) {
300+
return fromMat4(a, mat4.fromPointToPoint(TEMP_MAT4, eye, target, up));
301+
}
302+
291303
/**
292304
* Spherical linear interpolates between two quaternions.
293305
* @param {import("./types.js").quat} a

0 commit comments

Comments
 (0)