Browse Source

Math classes from threejs

tentone 6 years ago
parent
commit
712d222c59
11 changed files with 1347 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 23 0
      index.html
  3. 29 0
      package.json
  4. 3 0
      source/Diagram.js
  5. 69 0
      source/EventManager.js
  6. 18 0
      source/Object2D.js
  7. 174 0
      source/math/Box2.js
  8. 332 0
      source/math/Matrix3.js
  9. 40 0
      source/math/UUID.js
  10. 291 0
      source/math/Vector2.js
  11. 366 0
      source/math/Vector3.js

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+node_modules
+package-lock.json

+ 23 - 0
index.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="utf-8">
+	<title>diagram.js</title>
+</head>
+<body>
+	<script type="text/javascript" src="source/EventManager.js"></script>
+	<script type="text/javascript">
+		var canvas = document.createElement("canvas");
+		canvas.width = 800;
+		canvas.height = 600;
+		document.body.appendChild(canvas);
+
+		var context 
+
+		class CanvasObject()
+		{
+			
+		}
+	</script>
+</body>
+</html>

+ 29 - 0
package.json

@@ -0,0 +1,29 @@
+{
+	"name": "diagram.js",
+	"version": "0.1.0",
+	"description": "Web based diagram editing library.",
+	"main": "build/diagram.min.js",
+	"repository": {
+		"type": "git",
+		"url": "https://github.com/tentone/crosstabtalk.git"
+	},
+	"scripts": {
+		"build": "rollup -c && npm run uglify",
+		"docs": "jsdoc -d docs source",
+		"uglify": "uglifyjs --compress --mangle --output build/crosstabtalk.min.js -- build/crosstabtalk.js",
+		"pub": "npm run build && npm run docs && npm publish --access public ."
+	},
+	"keywords": [
+		"diagram",
+		"canvas"
+	],
+	"author": "Tentone",
+	"license": "MIT",
+	"dependencies": {},
+	"devDependencies": {
+		"jsdoc": "^3.5.0",
+		"rollup": "^1.0.0",
+		"rollup-plugin-strip": "^1.2.1",
+		"uglify-js": "^3.4.9"
+	}
+}

+ 3 - 0
source/Diagram.js

@@ -0,0 +1,3 @@
+"use strict";
+
+export {EventManager} from "./EventManager.js";

+ 69 - 0
source/EventManager.js

@@ -0,0 +1,69 @@
+"use strict";
+
+/**
+ * EventManager is used to manager DOM events creationg and destruction in a single function call.
+ *
+ * It is used by objects to make it easier to add and remove events from global DOM objects.
+ *
+ * @class
+ */
+function EventManager()
+{
+	/**
+	 * Stores all events in the manager, their target and callback.
+	 * 
+	 * Format [target, event, callback, active]
+	 * 
+	 * @type {Array}
+	 */
+	this.events = [];
+}
+
+/**
+ * Add new event to the manager.
+ *
+ * @param {DOM} target Event target element.
+ * @param {String} event Event name.
+ * @param {Function} callback Callback function.
+ */
+EventManager.prototype.add = function(target, event, callback)
+{
+	this.events.push([target, event, callback, false]);
+};
+
+/**
+ * Destroys this manager and remove all events.
+ */
+EventManager.prototype.clear = function()
+{
+	this.destroy();
+	this.events = [];
+};
+
+/**
+ * Creates all events in this manager.
+ */
+EventManager.prototype.create = function()
+{
+	for(var i = 0; i < this.events.length; i++)
+	{
+		var event = this.events[i];
+		event[0].addEventListener(event[1], event[2]);
+		event[3] = true;
+	}
+};
+
+/**
+ * Removes all events in this manager.
+ */
+EventManager.prototype.destroy = function()
+{
+	for(var i = 0; i < this.events.length; i++)
+	{
+		var event = this.events[i];
+		event[0].removeEventListener(event[1], event[2]);
+		event[3] = false;
+	}
+};
+
+//export {EventManager};

+ 18 - 0
source/Object2D.js

@@ -0,0 +1,18 @@
+"use strict";
+
+/**
+ * Base 2D object class, implements all the object positioning and scalling features.
+ *
+ * @class
+ */
+function Object2D()
+{
+	this.uuid = 
+
+	this.children = [];
+}
+
+Object2D.prototype.method_name = function()
+{
+	// body...
+};

+ 174 - 0
source/math/Box2.js

@@ -0,0 +1,174 @@
+
+function Box2(min, max)
+{
+	this.min = (min !== undefined) ? min : new Vector2();
+	this.max = (max !== undefined) ? max : new Vector2();
+}
+
+Object.assign(Box2.prototype,
+{
+	set: function(min, max)
+	{
+		this.min.copy(min);
+		this.max.copy(max);
+
+		return this;
+	},
+
+	setFromPoints: function(points)
+	{
+		this.min = new Vector2(+Infinity, +Infinity);
+		this.max = new Vector2(-Infinity, -Infinity);
+
+		for(var i = 0, il = points.length; i < il; i++)
+		{
+			this.expandByPoint(points[i]);
+		}
+
+		return this;
+	},
+
+	setFromCenterAndSize: function()
+	{
+		var v1 = new Vector2();
+
+		return function setFromCenterAndSize(center, size)
+		{
+			var halfSize = v1.copy(size).multiplyScalar(0.5);
+			this.min.copy(center).sub(halfSize);
+			this.max.copy(center).add(halfSize);
+
+			return this;
+		};
+	}(),
+
+	clone: function()
+	{
+		var box = new Box2();
+		box.copy(this);
+		return box;
+	},
+
+	copy: function(box)
+	{
+		this.min.copy(box.min);
+		this.max.copy(box.max);
+
+		return this;
+	},
+
+
+	isEmpty: function()
+	{
+		// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
+		return (this.max.x < this.min.x) || (this.max.y < this.min.y);
+	},
+
+	getCenter: function(target)
+	{
+		return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
+	},
+
+	getSize: function(target)
+	{
+		return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
+	},
+
+	expandByPoint: function(point)
+	{
+		this.min.min(point);
+		this.max.max(point);
+
+		return this;
+	},
+
+	expandByVector: function(vector)
+	{
+		this.min.sub(vector);
+		this.max.add(vector);
+
+		return this;
+	},
+
+	expandByScalar: function(scalar)
+	{
+		this.min.addScalar(-scalar);
+		this.max.addScalar(scalar);
+
+		return this;
+	},
+
+	containsPoint: function(point)
+	{
+		return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
+	},
+
+	containsBox: function(box)
+	{
+		return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
+	},
+
+	getParameter: function(point, target)
+	{
+		// This can potentially have a divide by zero if the box
+		// has a size dimension of 0.
+
+		return target.set(
+			(point.x - this.min.x) / (this.max.x - this.min.x),
+			(point.y - this.min.y) / (this.max.y - this.min.y)
+		);
+	},
+
+	intersectsBox: function(box)
+	{
+		// using 4 splitting planes to rule out intersections
+		return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
+	},
+
+	clampPoint: function(point, target)
+	{
+		return target.copy(point).clamp(this.min, this.max);
+	},
+
+	distanceToPoint: function()
+	{
+		var v1 = new Vector2();
+
+		return function distanceToPoint(point)
+		{
+			var clampedPoint = v1.copy(point).clamp(this.min, this.max);
+			return clampedPoint.sub(point).length();
+		};
+	}(),
+
+	intersect: function(box)
+	{
+		this.min.max(box.min);
+		this.max.min(box.max);
+
+		return this;
+	},
+
+	union: function(box)
+	{
+		this.min.min(box.min);
+		this.max.max(box.max);
+
+		return this;
+	},
+
+	translate: function(offset)
+	{
+		this.min.add(offset);
+		this.max.add(offset);
+
+		return this;
+	},
+
+	equals: function(box)
+	{
+		return box.min.equals(this.min) && box.max.equals(this.max);
+	}
+});
+
+export {Box2};

+ 332 - 0
source/math/Matrix3.js

@@ -0,0 +1,332 @@
+"use strict";
+
+function Matrix3()
+{
+	this.elements = [
+		1, 0, 0,
+		0, 1, 0,
+		0, 0, 1
+	];
+}
+
+Object.assign(Matrix3.prototype,
+{
+	isMatrix3: true,
+
+	set: function(n11, n12, n13, n21, n22, n23, n31, n32, n33)
+	{
+		var te = this.elements;
+
+		te[0] = n11;
+		te[1] = n21;
+		te[2] = n31;
+		te[3] = n12;
+		te[4] = n22;
+		te[5] = n32;
+		te[6] = n13;
+		te[7] = n23;
+		te[8] = n33;
+	},
+
+	identity: function()
+	{
+		this.set(
+			1, 0, 0,
+			0, 1, 0,
+			0, 0, 1
+		);
+	},
+
+	clone: function()
+	{
+		var m = new Matrix3();
+		m.fromArray(this.elements);
+		return m;
+	},
+
+	copy: function(m)
+	{
+		var te = this.elements;
+		var me = m.elements;
+
+		te[0] = me[0];
+		te[1] = me[1];
+		te[2] = me[2];
+		te[3] = me[3];
+		te[4] = me[4];
+		te[5] = me[5];
+		te[6] = me[6];
+		te[7] = me[7];
+		te[8] = me[8];
+	},
+
+	multiply: function(m)
+	{
+		return this.multiplyMatrices(this, m);
+	},
+
+	premultiply: function(m)
+	{
+		return this.multiplyMatrices(m, this);
+	},
+
+	multiplyMatrices: function(a, b)
+	{
+		var ae = a.elements;
+		var be = b.elements;
+		var te = this.elements;
+
+		var a11 = ae[0],
+			a12 = ae[3],
+			a13 = ae[6];
+		var a21 = ae[1],
+			a22 = ae[4],
+			a23 = ae[7];
+		var a31 = ae[2],
+			a32 = ae[5],
+			a33 = ae[8];
+
+		var b11 = be[0],
+			b12 = be[3],
+			b13 = be[6];
+		var b21 = be[1],
+			b22 = be[4],
+			b23 = be[7];
+		var b31 = be[2],
+			b32 = be[5],
+			b33 = be[8];
+
+		te[0] = a11 * b11 + a12 * b21 + a13 * b31;
+		te[3] = a11 * b12 + a12 * b22 + a13 * b32;
+		te[6] = a11 * b13 + a12 * b23 + a13 * b33;
+
+		te[1] = a21 * b11 + a22 * b21 + a23 * b31;
+		te[4] = a21 * b12 + a22 * b22 + a23 * b32;
+		te[7] = a21 * b13 + a22 * b23 + a23 * b33;
+
+		te[2] = a31 * b11 + a32 * b21 + a33 * b31;
+		te[5] = a31 * b12 + a32 * b22 + a33 * b32;
+		te[8] = a31 * b13 + a32 * b23 + a33 * b33;
+	},
+
+	multiplyScalar: function(s)
+	{
+		var te = this.elements;
+
+		te[0] *= s;
+		te[3] *= s;
+		te[6] *= s;
+		te[1] *= s;
+		te[4] *= s;
+		te[7] *= s;
+		te[2] *= s;
+		te[5] *= s;
+		te[8] *= s;
+	},
+
+	determinant: function()
+	{
+		var te = this.elements;
+
+		var a = te[0],
+			b = te[1],
+			c = te[2],
+			d = te[3],
+			e = te[4],
+			f = te[5],
+			g = te[6],
+			h = te[7],
+			i = te[8];
+
+		return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
+	},
+
+	getInverse: function(matrix, throwOnDegenerate)
+	{
+		var me = matrix.elements,
+			te = this.elements,
+
+			n11 = me[0],
+			n21 = me[1],
+			n31 = me[2],
+			n12 = me[3],
+			n22 = me[4],
+			n32 = me[5],
+			n13 = me[6],
+			n23 = me[7],
+			n33 = me[8],
+
+			t11 = n33 * n22 - n32 * n23,
+			t12 = n32 * n13 - n33 * n12,
+			t13 = n23 * n12 - n22 * n13,
+
+			det = n11 * t11 + n21 * t12 + n31 * t13;
+
+		if(det === 0)
+		{
+			if(throwOnDegenerate === true)
+			{
+				throw new Error(".getInverse() cant invert matrix, determinant is 0");
+			}
+
+			return this.identity();
+		}
+
+		var detInv = 1 / det;
+
+		te[0] = t11 * detInv;
+		te[1] = (n31 * n23 - n33 * n21) * detInv;
+		te[2] = (n32 * n21 - n31 * n22) * detInv;
+
+		te[3] = t12 * detInv;
+		te[4] = (n33 * n11 - n31 * n13) * detInv;
+		te[5] = (n31 * n12 - n32 * n11) * detInv;
+
+		te[6] = t13 * detInv;
+		te[7] = (n21 * n13 - n23 * n11) * detInv;
+		te[8] = (n22 * n11 - n21 * n12) * detInv;
+	},
+
+	transpose: function()
+	{
+		var tmp, m = this.elements;
+
+		tmp = m[1];
+		m[1] = m[3];
+		m[3] = tmp;
+		tmp = m[2];
+		m[2] = m[6];
+		m[6] = tmp;
+		tmp = m[5];
+		m[5] = m[7];
+		m[7] = tmp;
+	},
+
+	getNormalMatrix: function(matrix4)
+	{
+		return this.setFromMatrix4(matrix4).getInverse(this).transpose();
+	},
+
+	transposeIntoArray: function(r)
+	{
+		var m = this.elements;
+
+		r[0] = m[0];
+		r[1] = m[3];
+		r[2] = m[6];
+		r[3] = m[1];
+		r[4] = m[4];
+		r[5] = m[7];
+		r[6] = m[2];
+		r[7] = m[5];
+		r[8] = m[8];
+	},
+
+	setUvTransform: function(tx, ty, sx, sy, rotation, cx, cy)
+	{
+		var c = Math.cos(rotation);
+		var s = Math.sin(rotation);
+
+		this.set(
+			sx * c, sx * s, -sx * (c * cx + s * cy) + cx + tx,
+			-sy * s, sy * c, -sy * (-s * cx + c * cy) + cy + ty,
+			0, 0, 1
+		);
+	},
+
+	scale: function(sx, sy)
+	{
+		var te = this.elements;
+
+		te[0] *= sx;
+		te[3] *= sx;
+		te[6] *= sx;
+		te[1] *= sy;
+		te[4] *= sy;
+		te[7] *= sy;
+	},
+
+	rotate: function(theta)
+	{
+		var c = Math.cos(theta);
+		var s = Math.sin(theta);
+
+		var te = this.elements;
+
+		var a11 = te[0],
+			a12 = te[3],
+			a13 = te[6];
+		var a21 = te[1],
+			a22 = te[4],
+			a23 = te[7];
+
+		te[0] = c * a11 + s * a21;
+		te[3] = c * a12 + s * a22;
+		te[6] = c * a13 + s * a23;
+
+		te[1] = -s * a11 + c * a21;
+		te[4] = -s * a12 + c * a22;
+		te[7] = -s * a13 + c * a23;
+	},
+
+	translate: function(tx, ty)
+	{
+		var te = this.elements;
+
+		te[0] += tx * te[2];
+		te[3] += tx * te[5];
+		te[6] += tx * te[8];
+		te[1] += ty * te[2];
+		te[4] += ty * te[5];
+		te[7] += ty * te[8];
+	},
+
+	equals: function(matrix)
+	{
+		var te = this.elements;
+		var me = matrix.elements;
+
+		for(var i = 0; i < 9; i++)
+		{
+
+			if(te[i] !== me[i]) return false;
+
+		}
+
+		return true;
+	},
+
+	fromArray: function(array, offset)
+	{
+		if(offset === undefined) offset = 0;
+
+		for(var i = 0; i < 9; i++)
+		{
+			this.elements[i] = array[i + offset];
+		}
+	},
+
+	toArray: function(array, offset)
+	{
+		if(array === undefined) array = [];
+		if(offset === undefined) offset = 0;
+
+		var te = this.elements;
+
+		array[offset] = te[0];
+		array[offset + 1] = te[1];
+		array[offset + 2] = te[2];
+
+		array[offset + 3] = te[3];
+		array[offset + 4] = te[4];
+		array[offset + 5] = te[5];
+
+		array[offset + 6] = te[6];
+		array[offset + 7] = te[7];
+		array[offset + 8] = te[8];
+
+		return array;
+	}
+});
+
+export {Matrix3};

+ 40 - 0
source/math/UUID.js

@@ -0,0 +1,40 @@
+"use strict";
+
+/**
+ * Implements all UUID related methods.
+ *
+ * @class
+ */
+function UUID(){}
+
+/**
+ * Generate new random UUID v4 as string.
+ *
+ * http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
+ */
+UUID.generate = (function ()
+{
+	var lut = [];
+
+	for(var i = 0; i < 256; i++)
+	{
+		lut[i] = (i < 16 ? "0" : "") + (i).toString(16);
+	}
+
+	return function generateUUID()
+	{
+		var d0 = Math.random() * 0xffffffff | 0;
+		var d1 = Math.random() * 0xffffffff | 0;
+		var d2 = Math.random() * 0xffffffff | 0;
+		var d3 = Math.random() * 0xffffffff | 0;
+		var uuid = lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + "-" +
+			lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + "-" + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + "-" +
+			lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + "-" + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
+			lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ];
+
+		return uuid.toUpperCase();
+	};
+
+})();
+
+//export {UUID};

+ 291 - 0
source/math/Vector2.js

@@ -0,0 +1,291 @@
+"use strict";
+
+function Vector2(x, y)
+{
+	this.x = x || 0;
+	this.y = y || 0;
+}
+
+Object.assign(Vector2.prototype,
+{
+	isVector2: true,
+
+	set: function(x, y)
+	{
+		this.x = x;
+		this.y = y;
+	},
+
+	setScalar: function(scalar)
+	{
+		this.x = scalar;
+		this.y = scalar;
+	},
+
+	clone: function()
+	{
+		return new Vector2(this.x, this.y);
+	},
+
+	copy: function(v)
+	{
+		this.x = v.x;
+		this.y = v.y;
+	},
+
+	add: function(v, w)
+	{
+		this.x += v.x;
+		this.y += v.y;
+	},
+
+	addScalar: function(s)
+	{
+		this.x += s;
+		this.y += s;
+	},
+
+	addVectors: function(a, b)
+	{
+		this.x = a.x + b.x;
+		this.y = a.y + b.y;
+	},
+
+	addScaledVector: function(v, s)
+	{
+		this.x += v.x * s;
+		this.y += v.y * s;
+	},
+
+	sub: function(v, w)
+	{
+		this.x -= v.x;
+		this.y -= v.y;
+	},
+
+	subScalar: function(s)
+	{
+		this.x -= s;
+		this.y -= s;
+	},
+
+	subVectors: function(a, b)
+	{
+		this.x = a.x - b.x;
+		this.y = a.y - b.y;
+	},
+
+	multiply: function(v)
+	{
+		this.x *= v.x;
+		this.y *= v.y;
+	},
+
+	multiplyScalar: function(scalar)
+	{
+		this.x *= scalar;
+		this.y *= scalar;
+	},
+
+	divide: function(v)
+	{
+		this.x /= v.x;
+		this.y /= v.y;
+	},
+
+	divideScalar: function(scalar)
+	{
+		return this.multiplyScalar(1 / scalar);
+	},
+
+	applyMatrix3: function(m)
+	{
+		var x = this.x,
+			y = this.y;
+		var e = m.elements;
+
+		this.x = e[0] * x + e[3] * y + e[6];
+		this.y = e[1] * x + e[4] * y + e[7];
+	},
+
+	min: function(v)
+	{
+		this.x = Math.min(this.x, v.x);
+		this.y = Math.min(this.y, v.y);
+	},
+
+	max: function(v)
+	{
+		this.x = Math.max(this.x, v.x);
+		this.y = Math.max(this.y, v.y);
+	},
+
+	clamp: function(min, max)
+	{
+		// assumes min < max, componentwise
+		this.x = Math.max(min.x, Math.min(max.x, this.x));
+		this.y = Math.max(min.y, Math.min(max.y, this.y));
+	},
+
+	clampScalar: function(minVal, maxVal)
+	{
+
+		this.x = Math.max(minVal, Math.min(maxVal, this.x));
+		this.y = Math.max(minVal, Math.min(maxVal, this.y));
+	},
+
+	clampLength: function(min, max)
+	{
+		var length = this.length();
+		return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
+	},
+
+	floor: function()
+	{
+
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+	},
+
+	ceil: function()
+	{
+
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+	},
+
+	round: function()
+	{
+
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+	},
+
+	roundToZero: function()
+	{
+
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+	},
+
+	negate: function()
+	{
+		this.x = -this.x;
+		this.y = -this.y;
+
+		return this;
+	},
+
+	dot: function(v)
+	{
+		return this.x * v.x + this.y * v.y;
+	},
+
+	cross: function(v)
+	{
+		return this.x * v.y - this.y * v.x;
+	},
+
+	lengthSq: function()
+	{
+		return this.x * this.x + this.y * this.y;
+	},
+
+	length: function()
+	{
+		return Math.sqrt(this.x * this.x + this.y * this.y);
+	},
+
+	manhattanLength: function()
+	{
+		return Math.abs(this.x) + Math.abs(this.y);
+	},
+
+	normalize: function()
+	{
+		return this.divideScalar(this.length() || 1);
+	},
+
+	/**
+	 * Computes the angle in radians with respect to the positive x-axis
+	 */
+	angle: function()
+	{
+		var angle = Math.atan2(this.y, this.x);
+
+		if(angle < 0) angle += 2 * Math.PI;
+
+		return angle;
+	},
+
+	distanceTo: function(v)
+	{
+		return Math.sqrt(this.distanceToSquared(v));
+	},
+
+	distanceToSquared: function(v)
+	{
+		var dx = this.x - v.x,
+			dy = this.y - v.y;
+		return dx * dx + dy * dy;
+	},
+
+	manhattanDistanceTo: function(v)
+	{
+		return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
+	},
+
+	setLength: function(length)
+	{
+		return this.normalize().multiplyScalar(length);
+	},
+
+	lerp: function(v, alpha)
+	{
+		this.x += (v.x - this.x) * alpha;
+		this.y += (v.y - this.y) * alpha;
+	},
+
+	lerpVectors: function(v1, v2, alpha)
+	{
+		return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
+	},
+
+	equals: function(v)
+	{
+		return ((v.x === this.x) && (v.y === this.y));
+	},
+
+	fromArray: function(array, offset)
+	{
+		if(offset === undefined) offset = 0;
+
+		this.x = array[offset];
+		this.y = array[offset + 1];
+	},
+
+	toArray: function(array, offset)
+	{
+		if(array === undefined) array = [];
+		if(offset === undefined) offset = 0;
+
+		array[offset] = this.x;
+		array[offset + 1] = this.y;
+
+		return array;
+	},
+
+	rotateAround: function(center, angle)
+	{
+		var c = Math.cos(angle),
+			s = Math.sin(angle);
+
+		var x = this.x - center.x;
+		var y = this.y - center.y;
+
+		this.x = x * c - y * s + center.x;
+		this.y = x * s + y * c + center.y;
+	}
+});
+
+//export {Vector2};

+ 366 - 0
source/math/Vector3.js

@@ -0,0 +1,366 @@
+"use strict";
+
+function Vector3(x, y, z)
+{
+	this.x = x || 0;
+	this.y = y || 0;
+	this.z = z || 0;
+}
+
+Object.assign(Vector3.prototype,
+{
+	isVector3: true,
+
+	set: function(x, y, z)
+	{
+		this.x = x;
+		this.y = y;
+		this.z = z;
+	},
+
+	setScalar: function(scalar)
+	{
+		this.x = scalar;
+		this.y = scalar;
+		this.z = scalar;
+	},
+
+	clone: function()
+	{
+		return new Vector3(this.x, this.y, this.z);
+	},
+
+	copy: function(v)
+	{
+		this.x = v.x;
+		this.y = v.y;
+		this.z = v.z;
+	},
+
+	add: function(v, w)
+	{
+		this.x += v.x;
+		this.y += v.y;
+		this.z += v.z;
+	},
+
+	addScalar: function(s)
+	{
+		this.x += s;
+		this.y += s;
+		this.z += s;
+	},
+
+	addVectors: function(a, b)
+	{
+		this.x = a.x + b.x;
+		this.y = a.y + b.y;
+		this.z = a.z + b.z;
+	},
+
+	addScaledVector: function(v, s)
+	{
+		this.x += v.x * s;
+		this.y += v.y * s;
+		this.z += v.z * s;
+	},
+
+	sub: function(v, w)
+	{
+		this.x -= v.x;
+		this.y -= v.y;
+		this.z -= v.z;
+	},
+
+	subScalar: function(s)
+	{
+
+		this.x -= s;
+		this.y -= s;
+		this.z -= s;
+	},
+
+	subVectors: function(a, b)
+	{
+
+		this.x = a.x - b.x;
+		this.y = a.y - b.y;
+		this.z = a.z - b.z;
+	},
+
+	multiply: function(v, w)
+	{
+		this.x *= v.x;
+		this.y *= v.y;
+		this.z *= v.z;
+	},
+
+	multiplyScalar: function(scalar)
+	{
+		this.x *= scalar;
+		this.y *= scalar;
+		this.z *= scalar;
+	},
+
+	multiplyVectors: function(a, b)
+	{
+		this.x = a.x * b.x;
+		this.y = a.y * b.y;
+		this.z = a.z * b.z;
+	},
+
+	divide: function(v)
+	{
+		this.x /= v.x;
+		this.y /= v.y;
+		this.z /= v.z;
+	},
+
+	divideScalar: function(scalar)
+	{
+		return this.multiplyScalar(1 / scalar);
+	},
+
+	min: function(v)
+	{
+		this.x = Math.min(this.x, v.x);
+		this.y = Math.min(this.y, v.y);
+		this.z = Math.min(this.z, v.z);
+	},
+
+	max: function(v)
+	{
+		this.x = Math.max(this.x, v.x);
+		this.y = Math.max(this.y, v.y);
+		this.z = Math.max(this.z, v.z);
+	},
+
+	clamp: function(min, max)
+	{
+		// assumes min < max, componentwise
+
+		this.x = Math.max(min.x, Math.min(max.x, this.x));
+		this.y = Math.max(min.y, Math.min(max.y, this.y));
+		this.z = Math.max(min.z, Math.min(max.z, this.z));
+	},
+
+	clampScalar: function(minVal, maxVal)
+	{
+		this.x = Math.max(minVal, Math.min(maxVal, this.x));
+		this.y = Math.max(minVal, Math.min(maxVal, this.y));
+		this.z = Math.max(minVal, Math.min(maxVal, this.z));
+	},
+
+	clampLength: function(min, max)
+	{
+		var length = this.length();
+
+		return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
+	},
+
+	floor: function()
+	{
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+		this.z = Math.floor(this.z);
+	},
+
+	ceil: function()
+	{
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+		this.z = Math.ceil(this.z);
+	},
+
+	round: function()
+	{
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+		this.z = Math.round(this.z);
+	},
+
+	roundToZero: function()
+	{
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+		this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
+	},
+
+	negate: function()
+	{
+		this.x = -this.x;
+		this.y = -this.y;
+		this.z = -this.z;
+	},
+
+	dot: function(v)
+	{
+		return this.x * v.x + this.y * v.y + this.z * v.z;
+	},
+
+	lengthSquared: function()
+	{
+		return this.x * this.x + this.y * this.y + this.z * this.z;
+	},
+
+	length: function()
+	{
+		return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
+	},
+
+	manhattanLength: function()
+	{
+		return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
+	},
+
+	normalize: function()
+	{
+		return this.divideScalar(this.length() || 1);
+	},
+
+	setLength: function(length)
+	{
+		return this.normalize().multiplyScalar(length);
+	},
+
+	lerp: function(v, alpha)
+	{
+		this.x += (v.x - this.x) * alpha;
+		this.y += (v.y - this.y) * alpha;
+		this.z += (v.z - this.z) * alpha;
+	},
+
+	lerpVectors: function(v1, v2, alpha)
+	{
+		return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
+	},
+
+	cross: function(v, w)
+	{
+		return this.crossVectors(this, v);
+	},
+
+	crossVectors: function(a, b)
+	{
+
+		var ax = a.x,
+			ay = a.y,
+			az = a.z;
+		var bx = b.x,
+			by = b.y,
+			bz = b.z;
+
+		this.x = ay * bz - az * by;
+		this.y = az * bx - ax * bz;
+		this.z = ax * by - ay * bx;
+	},
+
+	projectOnVector: function(vector)
+	{
+		var scalar = vector.dot(this) / vector.lengthSq();
+
+		return this.copy(vector).multiplyScalar(scalar);
+	},
+
+	/**
+	 * Reflect incident vector off plane orthogonal to normal, normal is assumed to have unit length.
+	 */
+	reflect: function()
+	{
+		var v1 = new Vector3();
+
+		return function reflect(normal)
+		{
+			return this.sub(v1.copy(normal).multiplyScalar(2 * this.dot(normal)));
+		};
+
+	}(),
+
+	angleTo: function(v)
+	{
+		var theta = this.dot(v) / (Math.sqrt(this.lengthSq() * v.lengthSq()));
+		return Math.acos(theta);
+	},
+
+	distanceTo: function(v)
+	{
+		return Math.sqrt(this.distanceToSquared(v));
+	},
+
+	distanceToSquared: function(v)
+	{
+		var dx = this.x - v.x,
+			dy = this.y - v.y,
+			dz = this.z - v.z;
+
+		return dx * dx + dy * dy + dz * dz;
+	},
+
+	manhattanDistanceTo: function(v)
+	{
+		return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
+	},
+
+	setFromSpherical: function(s)
+	{
+		return this.setFromSphericalCoords(s.radius, s.phi, s.theta);
+	},
+
+	setFromSphericalCoords: function(radius, phi, theta)
+	{
+		var sinPhiRadius = Math.sin(phi) * radius;
+
+		this.x = sinPhiRadius * Math.sin(theta);
+		this.y = Math.cos(phi) * radius;
+		this.z = sinPhiRadius * Math.cos(theta);
+	},
+
+	setFromCylindrical: function(c)
+	{
+		return this.setFromCylindricalCoords(c.radius, c.theta, c.y);
+	},
+
+	setFromCylindricalCoords: function(radius, theta, y)
+	{
+		this.x = radius * Math.sin(theta);
+		this.y = y;
+		this.z = radius * Math.cos(theta);
+	},
+
+	setFromMatrixPosition: function(m)
+	{
+		var e = m.elements;
+
+		this.x = e[12];
+		this.y = e[13];
+		this.z = e[14];
+	},
+
+	equals: function(v)
+	{
+		return ((v.x === this.x) && (v.y === this.y) && (v.z === this.z));
+	},
+
+	fromArray: function(array, offset)
+	{
+		if(offset === undefined) offset = 0;
+
+		this.x = array[offset];
+		this.y = array[offset + 1];
+		this.z = array[offset + 2];
+	},
+
+	toArray: function(array, offset)
+	{
+		if(array === undefined) array = [];
+		if(offset === undefined) offset = 0;
+
+		array[offset] = this.x;
+		array[offset + 1] = this.y;
+		array[offset + 2] = this.z;
+
+		return array;
+	}
+});
+
+//export {Vector3};