@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
34 lines (33 loc) • 1.35 kB
JavaScript
;
import { Vector3, Quaternion, Matrix4, MathUtils } from "three";
const POLAR_TRANSFORM_AXIS_VERTICAL = new Vector3(0, 1, 0);
const POLAR_TRANSFORM_AXIS_HORIZONTAL = new Vector3(-1, 0, 0);
const centerMatrix = new Matrix4();
const longitudeMatrix = new Matrix4();
const latitudeMatrix = new Matrix4();
const depthMatrix = new Matrix4();
const decomposed = {
t: new Vector3(),
q: new Quaternion(),
s: new Vector3()
};
export class CorePolarTransform {
static matrix(params, target) {
centerMatrix.identity();
longitudeMatrix.identity();
latitudeMatrix.identity();
depthMatrix.identity();
centerMatrix.makeTranslation(params.center.x, params.center.y, params.center.z);
longitudeMatrix.makeRotationAxis(POLAR_TRANSFORM_AXIS_VERTICAL, MathUtils.degToRad(params.longitude));
latitudeMatrix.makeRotationAxis(POLAR_TRANSFORM_AXIS_HORIZONTAL, MathUtils.degToRad(params.latitude));
depthMatrix.makeTranslation(0, 0, params.depth);
target.copy(centerMatrix).multiply(longitudeMatrix).multiply(latitudeMatrix).multiply(depthMatrix);
}
static applyMatrixToObject(object, matrix) {
matrix.decompose(decomposed.t, decomposed.q, decomposed.s);
object.position.copy(decomposed.t);
object.quaternion.copy(decomposed.q);
object.scale.copy(decomposed.s);
object.updateMatrix();
}
}