@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
48 lines • 1.99 kB
JavaScript
import { OrthographicCamera, Vector2 } from 'three';
import { ProjectionPlane } from './ProjectionPlane.js';
import { fitIntoRectangle } from './fitIntoRectangle.js';
export class OrthographicProjection {
#viewRect = new Vector2();
#pixelRatio = new Vector2();
#halfWidth;
#halfHeight;
#near;
#far;
#distanceToProjectionPlane;
constructor(projectionPlane, specs) {
this.projectionPlane = typeof projectionPlane === 'string' ? ProjectionPlane.get(projectionPlane) : projectionPlane;
this.viewSpecs = specs;
}
updateViewRect(width, height) {
fitIntoRectangle(new Vector2(width, height), this.viewSpecs, this.#viewRect);
this.#halfWidth = this.#viewRect.width / 2;
this.#halfHeight = this.#viewRect.height / 2;
this.#pixelRatio.set(width, height).divide(this.#viewRect);
this.#near = this.viewSpecs.near ?? 0.1;
this.#far = this.viewSpecs.far ?? 100000;
this.#distanceToProjectionPlane = this.viewSpecs.distanceToProjectionPlane ?? 100;
}
getViewRect() {
return [this.#viewRect.width, this.#viewRect.height, this.#pixelRatio.x, this.#pixelRatio.y];
}
createCamera() {
const camera = new OrthographicCamera(-this.#halfWidth, this.#halfWidth, this.#halfHeight, -this.#halfHeight, this.#near, this.#far);
this.projectionPlane.applyRotation(camera);
camera.position.copy(this.projectionPlane.getPointByDistance(this.#distanceToProjectionPlane));
camera.updateProjectionMatrix();
return camera;
}
updateCamera(camera) {
camera.left = -this.#halfWidth;
camera.right = this.#halfWidth;
camera.top = this.#halfHeight;
camera.bottom = -this.#halfHeight;
camera.near = this.#near;
camera.far = this.#far;
camera.updateProjectionMatrix();
}
getZoom(_distanceToProjectionPlane) {
return 1;
}
}
//# sourceMappingURL=OrthographicProjection.js.map