UNPKG

@spearwolf/twopoint5d

Version:

Create 2.5D realtime graphics and pixelart with WebGL and three.js

63 lines 2.48 kB
import { Matrix4, Quaternion, Plane as THREE_Plane, Vector3 } from 'three/webgpu'; 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) { return this.plane.coplanarPoint(target ?? new Vector3()); } 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