the-world-engine
Version:
three.js based, unity like game engine for browser
260 lines (253 loc) • 7.54 kB
JavaScript
import { OrthographicCamera as ThreeOrthographicCamera, PerspectiveCamera as ThreePerspectiveCamera } from "three/src/Three";
import { Transform } from "../../../engine/hierarchy_object/Transform";
import { Component } from "../../hierarchy_object/Component";
import { CameraInfo } from "../../render/CameraInfo";
import { Color } from "../../render/Color";
export var CameraType;
(function(t) {
t[t["Perspective"] = 0] = "Perspective";
t[t["Orthographic"] = 1] = "Orthographic";
})(CameraType || (CameraType = {}));
export class Camera extends Component {
th=null;
Ta=CameraType.Orthographic;
ya=75;
Oa=5;
Pa=1;
ba=.1;
za=1e3;
Dt=0;
oo=null;
i=null;
ka=(t, i) => {
const e = t / i;
if (this.th instanceof ThreePerspectiveCamera) {
this.th.aspect = e;
this.th.updateProjectionMatrix();
} else if (this.th instanceof ThreeOrthographicCamera) {
const t = this.Oa;
this.th.left = -t * e;
this.th.right = t * e;
this.th.top = t;
this.th.bottom = -t;
this.th.updateProjectionMatrix();
}
};
awake() {
this.i = this.engine.cameraContainer;
}
onEnable() {
this.engine.screen.onResize.addListener(this.ka);
this.Da();
}
onWorldMatrixUpdated() {
if (this.th) {
Transform.updateRawObject3DWorldMatrixRecursively(this.th);
this.th.matrixWorldInverse.copy(this.th.matrixWorld).invert();
}
}
Da() {
const t = this.engine.screen.width / this.engine.screen.height;
if (this.Ta === CameraType.Perspective) {
if (!this.th) {
this.th = this.Ia();
this.transform.unsafeGetObject3D().add(this.th);
} else {
if (this.th instanceof ThreePerspectiveCamera) {
this.th.aspect = t;
this.th.fov = this.ya;
this.th.near = this.ba;
this.th.far = this.za;
this.th.updateProjectionMatrix();
} else {
this.th.removeFromParent();
Camera.removeCameraFromDuckPool(this.th);
this.th = this.Ia();
this.transform.unsafeGetObject3D().add(this.th);
}
}
} else if (this.Ta === CameraType.Orthographic) {
if (!this.th) {
this.th = this.ja();
this.transform.unsafeGetObject3D().add(this.th);
} else {
if (this.th instanceof ThreeOrthographicCamera) {
const i = this.Oa;
this.th.left = -i * t;
this.th.right = i * t;
this.th.top = i;
this.th.bottom = -i;
this.th.near = this.ba;
this.th.far = this.za;
this.th.updateProjectionMatrix();
} else {
this.th.removeFromParent();
Camera.removeCameraFromDuckPool(this.th);
this.th = this.ja();
this.transform.unsafeGetObject3D().add(this.th);
}
}
} else {
throw new Error("Camera type not supported");
}
Transform.updateRawObject3DWorldMatrixRecursively(this.th);
this.i.addCamera(this, new CameraInfo(this.Dt, this.oo));
}
Ia() {
const t = this.engine.screen.width / this.engine.screen.height;
const i = new ThreePerspectiveCamera(this.ya, t, this.ba, this.za);
if (this.Pa !== 1) {
i.zoom = this.Pa;
i.updateProjectionMatrix();
}
return i;
}
ja() {
const t = this.engine.screen.width / this.engine.screen.height;
const i = this.Oa;
const e = new ThreeOrthographicCamera(-i * t, i * t, i, -i, this.ba, this.za);
if (this.Pa !== 1) {
e.zoom = this.Pa;
e.updateProjectionMatrix();
}
return e;
}
onDisable() {
this.engine.screen.onResize.removeListener(this.ka);
if (this.th) {
this.i.removeCamera(this);
}
}
onDestroy() {
if (this.th) {
this.th.removeFromParent();
Camera.removeCameraFromDuckPool(this.th);
}
}
get cameraType() {
return this.Ta;
}
set cameraType(t) {
if (this.Ta === t) return;
this.Ta = t;
if (this.th) {
this.Da();
}
}
get fov() {
if (this.th instanceof ThreePerspectiveCamera) {
return this.ya = this.th.fov;
}
return this.ya;
}
set fov(t) {
if (this.th instanceof ThreePerspectiveCamera) {
if (this.th.fov === t) return;
this.ya = this.th.fov = t;
this.th.updateProjectionMatrix();
} else {
this.ya = t;
}
}
get viewSize() {
if (this.th instanceof ThreeOrthographicCamera) {
return this.Oa = this.th.top;
}
return this.Oa;
}
set viewSize(t) {
if (this.th instanceof ThreeOrthographicCamera) {
if (this.th.top === t) return;
this.Oa = t;
const i = this.engine.screen.width / this.engine.screen.height;
const e = this.Oa;
this.th.left = -e * i;
this.th.right = e * i;
this.th.top = e;
this.th.bottom = -e;
this.th.updateProjectionMatrix();
} else {
this.Oa = t;
}
}
get zoom() {
if (this.th) {
return this.Pa = this.th.zoom;
}
return this.Pa;
}
set zoom(t) {
const i = this.th;
if (i) {
if (i.zoom === t) return;
this.Pa = i.zoom = t;
i.updateProjectionMatrix();
} else {
this.Pa = t;
}
}
get near() {
if (this.th) {
return this.ba = this.th.near;
}
return this.ba;
}
set near(t) {
const i = this.th;
if (i) {
if (i.near === t) return;
this.ba = i.near = t;
i.updateProjectionMatrix();
} else {
this.ba = t;
}
}
get far() {
if (this.th) {
return this.za = this.th.far;
}
return this.za;
}
set far(t) {
const i = this.th;
if (i) {
if (i.far === t) return;
this.za = i.far = t;
i.updateProjectionMatrix();
} else {
this.za = t;
}
}
get priority() {
return this.Dt;
}
set priority(t) {
this.Dt = t;
if (this.th) {
this.i.changeCameraPriority(this, t);
}
}
get backgroundColor() {
return this.oo;
}
set backgroundColor(t) {
if (t === null) {
this.oo = null;
} else if (t instanceof Color) {
if (this.oo instanceof Color) {
this.oo.copy(t);
} else {
this.oo = t.clone();
}
} else {
this.oo = t;
}
if (this.th) {
this.i.changeCameraBackgroundColor(this, t);
}
}
get threeCamera() {
return this.th;
}
static removeCameraFromDuckPool(t) {}
}