UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

570 lines (560 loc) 15 kB
import { clamp } from "three/src/math/MathUtils"; import { Quaternion, Vector3 } from "three/src/Three"; export class ObservableVector3 { isVector3=true; Hs; zs; qs; ei; ri; constructor(t = 0, s = 0, i = 0) { this.Hs = t; this.zs = s; this.qs = i; this.ei = () => {}; this.ri = () => {}; } onBeforeChange(t) { this.ei = t; } onBeforeGetComponent(t) { this.ri = t; } get x() { this.ri(); return this.Hs; } set x(t) { this.ei(); this.Hs = t; } get y() { this.ri(); return this.zs; } set y(t) { this.ei(); this.zs = t; } get z() { this.ri(); return this.qs; } set z(t) { this.ei(); this.qs = t; } set(t, s, i) { if (i === undefined) { this.ri(); i = this.qs; } this.ei(); this.Hs = t; this.zs = s; this.qs = i; return this; } setScalar(t) { this.ei(); this.Hs = t; this.zs = t; this.qs = t; return this; } setX(t) { this.ei(); this.Hs = t; return this; } setY(t) { this.ei(); this.zs = t; return this; } setZ(t) { this.ei(); this.qs = t; return this; } setComponent(t, s) { switch (t) { case 0: this.ei(); this.Hs = s; break; case 1: this.ei(); this.zs = s; break; case 2: this.ei(); this.qs = s; break; default: throw new Error("index is out of range: " + t); } return this; } getComponent(t) { this.ri(); switch (t) { case 0: return this.Hs; case 1: return this.zs; case 2: return this.qs; default: throw new Error("index is out of range: " + t); } } clone() { this.ri(); return new Vector3(this.Hs, this.zs, this.qs); } copy(t) { this.ei(); this.Hs = t.x; this.zs = t.y; this.qs = t.z; return this; } add(t, s) { if (s !== undefined) { console.warn("THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead."); return this.addVectors(t, s); } if (t.x === 0 && t.y === 0 && t.z === 0) return this; this.ri(); this.ei(); this.Hs += t.x; this.zs += t.y; this.qs += t.z; return this; } addScalar(t) { if (t === 0) return this; this.ri(); this.ei(); this.Hs += t; this.zs += t; this.qs += t; return this; } addVectors(t, s) { this.ei(); this.Hs = t.x + s.x; this.zs = t.y + s.y; this.qs = t.z + s.z; return this; } addScaledVector(t, s) { this.ri(); this.ei(); this.Hs += t.x * s; this.zs += t.y * s; this.qs += t.z * s; return this; } sub(t, s) { if (s !== undefined) { console.warn("THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead."); return this.subVectors(t, s); } if (t.x === 0 && t.y === 0 && t.z === 0) return this; this.ri(); this.ei(); this.Hs -= t.x; this.zs -= t.y; this.qs -= t.z; return this; } subScalar(t) { if (t === 0) return this; this.ri(); this.ei(); this.Hs -= t; this.zs -= t; this.qs -= t; return this; } subVectors(t, s) { this.ei(); this.Hs = t.x - s.x; this.zs = t.y - s.y; this.qs = t.z - s.z; return this; } multiply(t, s) { if (s !== undefined) { console.warn("THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead."); return this.multiplyVectors(t, s); } if (t.x === 1 && t.y === 1 && t.z === 1) return this; this.ri(); this.ei(); this.Hs *= t.x; this.zs *= t.y; this.qs *= t.z; return this; } multiplyScalar(t) { if (t === 1) return this; this.ri(); this.ei(); this.Hs *= t; this.zs *= t; this.qs *= t; return this; } multiplyVectors(t, s) { this.ei(); this.Hs = t.x * s.x; this.zs = t.y * s.y; this.qs = t.z * s.z; return this; } applyEuler(t) { if (!(t && t.isEuler)) { console.error("THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order."); } return this.applyQuaternion(tempQuaternion.setFromEuler(t)); } applyAxisAngle(t, s) { return this.applyQuaternion(tempQuaternion.setFromAxisAngle(t, s)); } applyMatrix3(t) { this.ri(); const s = this.Hs, i = this.zs, h = this.qs; const r = t.elements; this.ei(); this.Hs = r[0] * s + r[3] * i + r[6] * h; this.zs = r[1] * s + r[4] * i + r[7] * h; this.qs = r[2] * s + r[5] * i + r[8] * h; return this; } applyNormalMatrix(t) { return this.applyMatrix3(t).normalize(); } applyMatrix4(t) { this.ri(); const s = this.Hs, i = this.zs, h = this.qs; const r = t.elements; const e = 1 / (r[3] * s + r[7] * i + r[11] * h + r[15]); this.ei(); this.Hs = (r[0] * s + r[4] * i + r[8] * h + r[12]) * e; this.zs = (r[1] * s + r[5] * i + r[9] * h + r[13]) * e; this.qs = (r[2] * s + r[6] * i + r[10] * h + r[14]) * e; return this; } applyQuaternion(t) { this.ri(); const s = this.Hs, i = this.zs, h = this.qs; const r = t.x, e = t.y, n = t.z, a = t.w; const o = a * s + e * h - n * i; const u = a * i + n * s - r * h; const c = a * h + r * i - e * s; const l = -r * s - e * i - n * h; this.ei(); this.Hs = o * a + l * -r + u * -n - c * -e; this.zs = u * a + l * -e + c * -r - o * -n; this.qs = c * a + l * -n + o * -e - u * -r; return this; } project(t) { return this.applyMatrix4(t.matrixWorldInverse).applyMatrix4(t.projectionMatrix); } unproject(t) { return this.applyMatrix4(t.projectionMatrixInverse).applyMatrix4(t.matrixWorld); } transformDirection(t) { this.ri(); const s = this.Hs, i = this.zs, h = this.qs; const r = t.elements; this.ei(); this.Hs = r[0] * s + r[4] * i + r[8] * h; this.zs = r[1] * s + r[5] * i + r[9] * h; this.qs = r[2] * s + r[6] * i + r[10] * h; return this.normalize(); } divide(t) { if (t.x === 1 && t.y === 1 && t.z === 1) return this; this.ri(); this.ei(); this.Hs /= t.x; this.zs /= t.y; this.qs /= t.z; return this; } divideScalar(t) { return this.multiplyScalar(1 / t); } min(t) { this.ri(); this.ei(); this.Hs = Math.min(this.Hs, t.x); this.zs = Math.min(this.zs, t.y); this.qs = Math.min(this.qs, t.z); return this; } max(t) { this.ri(); this.ei(); this.Hs = Math.max(this.Hs, t.x); this.zs = Math.max(this.zs, t.y); this.qs = Math.max(this.qs, t.z); return this; } clamp(t, s) { this.ri(); this.ei(); this.Hs = Math.max(t.x, Math.min(s.x, this.Hs)); this.zs = Math.max(t.y, Math.min(s.y, this.zs)); this.qs = Math.max(t.z, Math.min(s.z, this.qs)); return this; } clampScalar(t, s) { this.ri(); this.ei(); this.Hs = Math.max(t, Math.min(s, this.Hs)); this.zs = Math.max(t, Math.min(s, this.zs)); this.qs = Math.max(t, Math.min(s, this.qs)); return this; } clampLength(t, s) { const i = this.length(); return this.divideScalar(i || 1).multiplyScalar(Math.max(t, Math.min(s, i))); } floor() { this.ri(); this.ei(); this.Hs = Math.floor(this.Hs); this.zs = Math.floor(this.zs); this.qs = Math.floor(this.qs); return this; } ceil() { this.ri(); this.ei(); this.Hs = Math.ceil(this.Hs); this.zs = Math.ceil(this.zs); this.qs = Math.ceil(this.qs); return this; } round() { this.ri(); this.ei(); this.Hs = Math.round(this.Hs); this.zs = Math.round(this.zs); this.qs = Math.round(this.qs); return this; } roundToZero() { this.ri(); this.ei(); this.Hs = this.Hs < 0 ? Math.ceil(this.Hs) : Math.floor(this.Hs); this.zs = this.zs < 0 ? Math.ceil(this.zs) : Math.floor(this.zs); this.qs = this.qs < 0 ? Math.ceil(this.qs) : Math.floor(this.qs); return this; } negate() { this.ri(); this.ei(); this.Hs = -this.Hs; this.zs = -this.zs; this.qs = -this.qs; return this; } dot(t) { this.ri(); return this.Hs * t.x + this.zs * t.y + this.qs * t.z; } lengthSq() { this.ri(); return this.Hs * this.Hs + this.zs * this.zs + this.qs * this.qs; } length() { this.ri(); return Math.sqrt(this.Hs * this.Hs + this.zs * this.zs + this.qs * this.qs); } manhattanLength() { this.ri(); return Math.abs(this.Hs) + Math.abs(this.zs) + Math.abs(this.qs); } normalize() { return this.divideScalar(this.length() || 1); } setLength(t) { return this.normalize().multiplyScalar(t); } lerp(t, s) { this.ri(); this.ei(); this.Hs += (t.x - this.Hs) * s; this.zs += (t.y - this.zs) * s; this.qs += (t.z - this.qs) * s; return this; } lerpVectors(t, s, i) { this.ei(); this.Hs = t.x + (s.x - t.x) * i; this.zs = t.y + (s.y - t.y) * i; this.qs = t.z + (s.z - t.z) * i; return this; } cross(t, s) { if (s !== undefined) { console.warn("THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead."); return this.crossVectors(t, s); } return this.crossVectors(this, t); } crossVectors(t, s) { const i = t.x, h = t.y, r = t.z; const e = s.x, n = s.y, a = s.z; this.ei(); this.Hs = h * a - r * n; this.zs = r * e - i * a; this.qs = i * n - h * e; return this; } projectOnVector(t) { const s = t.lengthSq(); if (s === 0) return this.set(0, 0, 0); const i = t.dot(this) / s; return this.copy(t).multiplyScalar(i); } projectOnPlane(t) { tempVector.copy(this).projectOnVector(t); return this.sub(tempVector); } reflect(t) { return this.sub(tempVector.copy(t).multiplyScalar(2 * this.dot(t))); } angleTo(t) { const s = Math.sqrt(this.lengthSq() * t.lengthSq()); if (s === 0) return Math.PI / 2; const i = this.dot(t) / s; return Math.acos(clamp(i, -1, 1)); } distanceTo(t) { return Math.sqrt(this.distanceToSquared(t)); } distanceToSquared(t) { this.ri(); const s = this.Hs - t.x, i = this.zs - t.y, h = this.qs - t.z; return s * s + i * i + h * h; } manhattanDistanceTo(t) { this.ri(); return Math.abs(this.Hs - t.x) + Math.abs(this.zs - t.y) + Math.abs(this.qs - t.z); } setFromSpherical(t) { return this.setFromSphericalCoords(t.radius, t.phi, t.theta); } setFromSphericalCoords(t, s, i) { const h = Math.sin(s) * t; this.ei(); this.Hs = h * Math.sin(i); this.zs = Math.cos(s) * t; this.qs = h * Math.cos(i); return this; } setFromCylindrical(t) { return this.setFromCylindricalCoords(t.radius, t.theta, t.y); } setFromCylindricalCoords(t, s, i) { this.ei(); this.Hs = t * Math.sin(s); this.zs = i; this.qs = t * Math.cos(s); return this; } setFromMatrixPosition(t) { const s = t.elements; this.ei(); this.Hs = s[12]; this.zs = s[13]; this.qs = s[14]; return this; } setFromMatrixScale(t) { const s = this.setFromMatrixColumn(t, 0).length(); const i = this.setFromMatrixColumn(t, 1).length(); const h = this.setFromMatrixColumn(t, 2).length(); this.ei(); this.Hs = s; this.zs = i; this.qs = h; return this; } setFromMatrixColumn(t, s) { return this.fromArray(t.elements, s * 4); } setFromMatrix3Column(t, s) { return this.fromArray(t.elements, s * 3); } setFromEuler(t) { this.x = t._x; this.y = t._y; this.z = t._z; return this; } equals(t) { this.ri(); return t.x === this.Hs && t.y === this.zs && t.z === this.qs; } fromArray(t, s = 0) { if (this.Hs === t[s] && this.zs === t[s + 1] && this.qs === t[s + 2]) return this; this.ei(); this.Hs = t[s]; this.zs = t[s + 1]; this.qs = t[s + 2]; return this; } toArray(t = [], s = 0) { this.ri(); t[s] = this.Hs; t[s + 1] = this.zs; t[s + 2] = this.qs; return t; } fromBufferAttribute(t, s, i) { if (i !== undefined) { console.warn("THREE.Vector3: offset has been removed from .fromBufferAttribute()."); } this.ei(); this.Hs = t.getX(s); this.zs = t.getY(s); this.qs = t.getZ(s); return this; } random() { this.ei(); this.Hs = Math.random(); this.zs = Math.random(); this.qs = Math.random(); return this; } randomDirection() { const t = (Math.random() - .5) * 2; const s = Math.random() * Math.PI * 2; const i = Math.sqrt(1 - t ** 2); this.ei(); this.Hs = i * Math.cos(s); this.zs = i * Math.sin(s); this.qs = t; return this; } * [Symbol.iterator]() { this.ri(); yield this.Hs; this.ri(); yield this.zs; this.ri(); yield this.qs; } } const tempVector = new ObservableVector3; const tempQuaternion = new Quaternion;