UNPKG

@spearwolf/twopoint5d

Version:

a library to create 2.5d realtime graphics and pixelart with three.js

65 lines 2.58 kB
import { Matrix4, Object3D, Plane as THREE_Plane, Quaternion, Vector3 } from 'three'; export class ProjectionPlane { constructor(planeDescription, up) { if (planeDescription === 'xy|bottom-left') { this.plane = new THREE_Plane(new Vector3(0, 0, 1)); this.up = up?.clone() ?? new Vector3(0, 1, 0); } else if (planeDescription === 'xy|top-left') { this.plane = new THREE_Plane(new Vector3(0, 0, -1)); this.up = up?.clone() ?? new Vector3(0, -1, 0); } else if (planeDescription === 'xz|top-left') { this.plane = new THREE_Plane(new Vector3(0, 1, 0)); this.up = up?.clone() ?? new Vector3(0, 0, -1); } else if (planeDescription === 'xz|bottom-left') { this.plane = new THREE_Plane(new Vector3(0, -1, 0)); this.up = up?.clone() ?? new Vector3(0, 0, 1); } else { this.plane = planeDescription.clone(); if (up == null) { throw new Error('up is mandatory for a custom projection plane'); } this.up = up.clone(); } } static get(plane) { return plane instanceof ProjectionPlane ? plane : new ProjectionPlane(plane); } clone() { return new ProjectionPlane(this.plane.clone(), this.up.clone()); } equals(projectionPlane) { if (projectionPlane === this) return true; if (projectionPlane == null) return false; return this.plane.equals(projectionPlane.plane) && this.up.equals(projectionPlane.up); } applyRotation(obj3d) { obj3d.applyQuaternion(new Quaternion().setFromRotationMatrix(new Matrix4().lookAt(this.getPointByDistance(1), this.getPointByDistance(0), this.up))); } getPointByDistance(distanceToPlane, target) { return this.getOrigin(target).add(this.plane.normal.clone().multiplyScalar(distanceToPlane)); } getOrigin(target) { const { normal, constant } = this.plane; target = target ? target.copy(normal) : normal.clone(); return target.multiplyScalar(-constant); } getForward(target) { return this.getPointByDistance(1, target).negate(); } getRight(target) { return this.getForward(target).cross(this.up); } getPoint(x, y, target) { target = this.getOrigin(target); target.add(this.getRight().setLength(x)); target.add(this.up.clone().setLength(y)); return target; } } //# sourceMappingURL=ProjectionPlane.js.map