@rawify/vector3
Version:
The RAW JavaScript 3D Vector library
17 lines (15 loc) • 4.22 kB
JavaScript
/*
Vector3 v0.0.6 8/16/2025
https://github.com/rawify/Vector3.js
Copyright (c) 2025, Robert Eisele (https://raw.org/)
Licensed under the MIT license.
*/
'use strict';(function(l){function e(a,b,c){const d=Object.create(f.prototype);d.x=a;d.y=b;d.z=c;return d}function f(a,b,c){let d=this instanceof f?this:Object.create(f.prototype);"object"===typeof a?a instanceof Array?(d.x=a[0],d.y=a[1],d.z=a[2]):(d.x=a.x,d.y=a.y,d.z=a.z):isNaN(a)||isNaN(b)||isNaN(c)||(d.x=a,d.y=b,d.z=c);return d}f.prototype={x:0,y:0,z:0,add:function(a){return e(this.x+a.x,this.y+a.y,this.z+a.z)},sub:function(a){return e(this.x-a.x,this.y-a.y,this.z-a.z)},neg:function(){return e(-this.x,
-this.y,-this.z)},scale:function(a){return e(this.x*a,this.y*a,this.z*a)},prod:function(a){return e(this.x*a.x,this.y*a.y,this.z*a.z)},dot:function(a){return this.x*a.x+this.y*a.y+this.z*a.z},cross:function(a){const b=this.x,c=this.y,d=this.z,g=a.x,h=a.y;a=a.z;return e(c*a-d*h,d*g-b*a,b*h-c*g)},projectTo:function(a){const b=a.x,c=a.y;a=a.z;const d=(this.x*b+this.y*c+this.z*a)/(b*b+c*c+a*a);return e(b*d,c*d,a*d)},rejectFrom:function(a){const b=a.x,c=a.y;a=a.z;const d=(this.x*b+this.y*c+this.z*a)/(b*
b+c*c+a*a);return e(this.x-b*d,this.y-c*d,this.z-a*d)},reflect:function(a){const b=a.x,c=a.y;a=a.z;const d=(this.x*b+this.y*c+this.z*a)/(b*b+c*c+a*a);return e(2*b*d-this.x,2*c*d-this.y,2*a*d-this.z)},refract:function(a,b){var c=this.x*a.x+this.y*a.y+this.z*a.z;const d=1-b*b*(1-c*c);if(0>d)return null;c=b*c+Math.sqrt(d);return e(b*this.x-c*a.x,b*this.y-c*a.y,b*this.z-c*a.z)},scaleAlongAxis:function(a,b){var c=a.x,d=a.y;a=a.z;const g=(this.x*c+this.y*d+this.z*a)/(c*c+d*d+a*a);c*=g;d*=g;a*=g;return e(this.x-
c+b*c,this.y-d+b*d,this.z-a+b*a)},norm:function(){const a=this.x,b=this.y,c=this.z;return Math.sqrt(a*a+b*b+c*c)},norm2:function(){const a=this.x,b=this.y,c=this.z;return a*a+b*b+c*c},normalize:function(){const a=this.x,b=this.y,c=this.z;var d=a*a+b*b+c*c;if(0===d||1===d)return this;d=1/Math.sqrt(d);return e(a*d,b*d,c*d)},distance:function(a){const b=this.x-a.x,c=this.y-a.y;a=this.z-a.z;return Math.sqrt(b*b+c*c+a*a)},set:function(a){this.x=a.x;this.y=a.y;this.z=a.z},rotateX:function(a){const b=Math.cos(a);
a=Math.sin(a);return e(this.x,this.y*b-this.z*a,this.z*b+this.y*a)},rotateY:function(a){const b=Math.cos(a);a=Math.sin(a);return e(this.x*b+this.z*a,this.y,this.z*b-this.x*a)},rotateZ:function(a){const b=Math.cos(a);a=Math.sin(a);return e(this.x*b-this.y*a,this.y*b+this.x*a,this.z)},applyMatrix:function(a){a=a.M||a;const b=this.x,c=this.y,d=this.z;return e(b*a[0][0]+c*a[0][1]+d*a[0][2]+(a[0][3]||0),b*a[1][0]+c*a[1][1]+d*a[1][2]+(a[1][3]||0),b*a[2][0]+c*a[2][1]+d*a[2][2]+(a[2][3]||0))},apply:function(a,
b={x:0,y:0,z:0}){return e(a(this.x,b.x),a(this.y,b.y),a(this.z,b.z))},toArray:function(){return[this.x,this.y,this.z]},clone:function(){return e(this.x,this.y,this.z)},equals:function(a){return this===a||1E-13>Math.abs(this.x-a.x)&&1E-13>Math.abs(this.y-a.y)&&1E-13>Math.abs(this.z-a.z)},isUnit:function(){return 1E-13>Math.abs(this.x*this.x+this.y*this.y+this.z*this.z-1)},lerp:function(a,b){const c=this.x,d=this.y,g=this.z;return e(c+b*(a.x-c),d+b*(a.y-d),g+b*(a.z-g))},toString:function(){return"("+
this.x+", "+this.y+", "+this.z+")"},add$:function(a){this.x+=a.x;this.y+=a.y;this.z+=a.z;return this},sub$:function(a){this.x-=a.x;this.y-=a.y;this.z-=a.z;return this},neg$:function(){this.x=-this.x;this.y=-this.y;this.z=-this.z;return this},scale$:function(a){this.x*=a;this.y*=a;this.z*=a;return this},prod$:function(a){this.x*=a.x;this.y*=a.y;this.z*=a.z;return this},normalize$:function(){var a=this.x*this.x+this.y*this.y+this.z*this.z;if(0===a||1===a)return this;a=1/Math.sqrt(a);this.x*=a;this.y*=
a;this.z*=a;return this}};f.random=function(){return e(Math.random(),Math.random(),Math.random())};f.fromPoints=function(a,b){return e(b.x-a.x,b.y-a.y,b.z-a.z)};f.fromBarycentric=function(a,b,c,d,g){const h=a.x,k=a.y;a=a.z;return e(h+(b.x-h)*d+(c.x-h)*g,k+(b.y-k)*d+(c.y-k)*g,a+(b.z-a)*d+(c.z-a)*g)};"function"===typeof define&&define.amd?define([],function(){return f}):"object"===typeof exports?(Object.defineProperty(f,"__esModule",{value:!0}),f["default"]=f,f.Vector3=f,module.exports=f):l.Vector3=
f})(this);