UNPKG

@rawify/vector3

Version:

The RAW JavaScript 3D Vector library

15 lines (13 loc) 3.44 kB
/* Vector3 v0.0.4 8/3/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 d(a,b,e){const c=Object.create(f.prototype);c.x=a;c.y=b;c.z=e;return c}function f(a,b,e){let c=this instanceof f?this:Object.create(f.prototype);"object"===typeof a?a instanceof Array?(c.x=a[0],c.y=a[1],c.z=a[2]):(c.x=a.x,c.y=a.y,c.z=a.z):isNaN(a)||isNaN(b)||isNaN(e)||(c.x=a,c.y=b,c.z=e);return c}f.prototype={x:0,y:0,z:0,add:function(a){return d(this.x+a.x,this.y+a.y,this.z+a.z)},sub:function(a){return d(this.x-a.x,this.y-a.y,this.z-a.z)},neg:function(){return d(-this.x, -this.y,-this.z)},scale:function(a){return d(this.x*a,this.y*a,this.z*a)},prod:function(a){return d(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){return d(this.y*a.z-this.z*a.y,this.z*a.x-this.x*a.z,this.x*a.y-this.y*a.x)},projectTo:function(a){const b=(this.x*a.x+this.y*a.y+this.z*a.z)/(a.x*a.x+a.y*a.y+a.z*a.z);return d(a.x*b,a.y*b,a.z*b)},rejectFrom:function(a){a=this.projectTo(a);return this.sub(a)},reflect:function(a){return this.projectTo(a).scale(2).sub(this)}, refract:function(a,b){const e=this.dot(a);var c=1-b*b*(1-e*e);if(0>c)return null;c=Math.sqrt(c);return d(b*this.x-(b*e+c)*a.x,b*this.y-(b*e+c)*a.y,b*this.z-(b*e+c)*a.z)},scaleAlongAxis:function(a,b){a=a.scale(this.dot(a)/a.dot(a));return this.subtract(a).add(a.scale(b))},norm:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},norm2:function(){return this.x*this.x+this.y*this.y+this.z*this.z},normalize:function(){const a=this.g();return 0===a||1===a?this:d(this.x/a,this.y/a,this.z/ a)},distance:function(a){const b=this.x-a.x,e=this.y-a.y;a=this.z-a.z;return Math.sqrt(b*b+e*e+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 d(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 d(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 d(this.x*b-this.y*a,this.y*b+this.x*a,this.z)},applyMatrix:function(a){a= a.M||a;return d(this.x*a[0][0]+this.y*a[0][1]+this.z*a[0][2]+(a[0][3]||0),this.x*a[1][0]+this.y*a[1][1]+this.z*a[1][2]+(a[1][3]||0),this.x*a[2][0]+this.y*a[2][1]+this.z*a[2][2]+(a[2][3]||0))},apply:function(a,b={x:0,y:0}){return d(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 d(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){return d(this.x+b*(a.x-this.x),this.y+b*(a.y-this.y),this.z+b*(a.z-this.z))},toString:function(){return"("+this.x+", "+this.y+", "+this.z+")"}};f.random=function(){return d(Math.random(),Math.random(),Math.random())};f.fromPoints=function(a,b){return d(b.x-a.x,b.y-a.y,b.z-a.z)};f.fromBarycentric=function(a,b,e,c,g){const h=a.x,k=a.y;a=a.z;return d(h+(b.x-h)*c+(e.x-h)*g,k+(b.y-k)*c+(e.y-k)*g,a+(b.z-a)*c+(e.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);