UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

252 lines (242 loc) 6.16 kB
import { clamp } from "three/src/math/MathUtils"; import { Euler, Matrix4, Quaternion } from "three/src/Three"; export class ObservableEuler { isEuler=true; static DefaultOrder="XYZ"; static RotationOrders=[ "XYZ", "YZX", "ZXY", "XZY", "YXZ", "ZYX" ]; ti; ii; si; hi; _onChangeCallback; ei; ri; constructor(t = 0, i = 0, s = 0, h = ObservableEuler.DefaultOrder) { this.ti = t; this.ii = i; this.si = s; this.hi = h; this._onChangeCallback = () => {}; this.ei = () => {}; this.ri = () => {}; } onBeforeGetComponent(t) { this.ri = t; } onBeforeChange(t) { this.ei = t; } get _x() { this.ri(); return this.ti; } set _x(t) { this.ti = t; } get _y() { this.ri(); return this.ii; } set _y(t) { this.ii = t; } get _z() { this.ri(); return this.si; } set _z(t) { this.si = t; } get _order() { this.ri(); return this.hi; } set _order(t) { this.hi = t; } get x() { this.ri(); return this.ti; } set x(t) { this.ei(); this.ti = t; this._onChangeCallback(); } get y() { this.ri(); return this.ii; } set y(t) { this.ei(); this.ii = t; this._onChangeCallback(); } get z() { this.ri(); return this.si; } set z(t) { this.ei(); this.si = t; this._onChangeCallback(); } get order() { this.ri(); return this.hi; } set order(t) { this.ei(); this.hi = t; this._onChangeCallback(); } set(t, i, s, h) { this.ei(); this.ti = t; this.ii = i; this.si = s; if (h) { this.hi = h; } this._onChangeCallback(); return this; } clone() { this.ri(); return new Euler(this.ti, this.ii, this.si, this.hi); } copy(t) { this.ei(); this.ti = t._x; this.ii = t._y; this.si = t._z; this.hi = t._order; this._onChangeCallback(); return this; } setFromRotationMatrix(t, i, s = true) { this.ri(); const h = t.elements; const e = h[0], r = h[4], a = h[8]; const n = h[1], o = h[5], l = h[9]; const u = h[2], M = h[6], c = h[10]; if (!i) i = this.hi; this.ei(); switch (i) { case "XYZ": this.ii = Math.asin(clamp(a, -1, 1)); if (Math.abs(a) < .9999999) { this.ti = Math.atan2(-l, c); this.si = Math.atan2(-r, e); } else { this.ti = Math.atan2(M, o); this.si = 0; } break; case "YXZ": this.ti = Math.asin(-clamp(l, -1, 1)); if (Math.abs(l) < .9999999) { this.ii = Math.atan2(a, c); this.si = Math.atan2(n, o); } else { this.ii = Math.atan2(-u, e); this.si = 0; } break; case "ZXY": this.ti = Math.asin(clamp(M, -1, 1)); if (Math.abs(M) < .9999999) { this.ii = Math.atan2(-u, c); this.si = Math.atan2(-r, o); } else { this.ii = 0; this.si = Math.atan2(n, e); } break; case "ZYX": this.ii = Math.asin(-clamp(u, -1, 1)); if (Math.abs(u) < .9999999) { this.ti = Math.atan2(M, c); this.si = Math.atan2(n, e); } else { this.ti = 0; this.si = Math.atan2(-r, o); } break; case "YZX": this.si = Math.asin(clamp(n, -1, 1)); if (Math.abs(n) < .9999999) { this.ti = Math.atan2(-l, o); this.ii = Math.atan2(-u, e); } else { this.ti = 0; this.ii = Math.atan2(a, c); } break; case "XZY": this.si = Math.asin(-clamp(r, -1, 1)); if (Math.abs(r) < .9999999) { this.ti = Math.atan2(M, o); this.ii = Math.atan2(a, e); } else { this.ti = Math.atan2(-l, c); this.ii = 0; } break; default: console.warn("THREE.Euler: .setFromRotationMatrix() encountered an unknown order: " + i); } this.hi = i; if (s === true) this._onChangeCallback(); return this; } setFromQuaternion(t, i, s) { tempMatrix.makeRotationFromQuaternion(t); return this.setFromRotationMatrix(tempMatrix, i, s); } setFromVector3(t, i) { return this.set(t.x, t.y, t.z, i); } reorder(t) { tempQuaternion.setFromEuler(this); return this.setFromQuaternion(tempQuaternion, t); } equals(t) { this.ri(); return t._x === this.ti && t._y === this.ii && t._z === this.si && t._order === this.hi; } fromArray(t) { if (this.ti === t[0] && this.ii === t[1] && this.si === t[2] && this.hi === t[3]) return this; this.ei(); this.ti = t[0]; this.ii = t[1]; this.si = t[2]; if (t[3] !== undefined) this.hi = t[3]; this._onChangeCallback(); return this; } toArray(t = [], i = 0) { this.ri(); t[i] = this.ti; t[i + 1] = this.ii; t[i + 2] = this.si; t[i + 3] = this.hi; return t; } _onChange(t) { this._onChangeCallback = t; return this; } * [Symbol.iterator]() { this.ri(); yield this.ti; this.ri(); yield this.ii; this.ri(); yield this.si; this.ri(); yield this.hi; } } const tempMatrix = new Matrix4; const tempQuaternion = new Quaternion;