UNPKG

@rawify/vector3

Version:

The RAW JavaScript 3D Vector library

14 lines (12 loc) 2.95 kB
/* Vector3 v0.0.2 3/26/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,f){const c=Object.create(e.prototype);c.x=a;c.y=b;c.z=f;return c}function e(a,b,f){let c=this instanceof e?this:Object.create(e.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(f)||(c.x=a,c.y=b,c.z=f);return c}e.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)}, 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,f=this.y-a.y;a=this.z-a.z;return Math.sqrt(b*b+f*f+a*a)},set:function(a){this.x=a.x;this.y=a.y;this.z=a.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+")"}};e.random=function(){return d(Math.random(),Math.random(),Math.random())};e.fromPoints=function(a,b){return d(b.x-a.x,b.y-a.y,b.z-a.z)};e.fromBarycentric=function(a,b,f,c,g){const h=a.x,k=a.y;a=a.z;return d(h+(b.x-h)*c+(f.x-h)*g,k+(b.y-k)*c+(f.y-k)*g,a+(b.z-a)*c+(f.z-a)*g)};"function"=== typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?(Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=e,e.Vector3=e,module.exports=e):l.Vector3=e})(this);