the-world-engine
Version:
three.js based, unity like game engine for browser
85 lines (83 loc) • 2.45 kB
JavaScript
import { Component } from "../../hierarchy_object/Component";
import { Transform } from "../../hierarchy_object/Transform";
export class Object3DContainer extends Component {
es=null;
sl=false;
hl=null;
awake() {
this.sl = true;
const t = this.es;
if (t) {
this.transform.unsafeGetObject3D().add(t);
Transform.updateRawObject3DWorldMatrixRecursively(t);
this.transform.enqueueRenderAttachedObject3D(t);
}
}
onEnable() {
const t = this.es;
if (!t) return;
this.rl(t, true);
this.transform.enqueueRenderAttachedObject3D(t);
}
onDisable() {
const t = this.es;
if (!t) return;
this.rl(t, false);
this.transform.enqueueRenderAttachedObject3D(t);
}
onDestroy() {
if (!this.es) return;
this.hl?.(this.es);
this.transform.dequeueRenderAttachedObject3D(this.es);
this.transform.unsafeGetObject3D().remove(this.es);
this.es = null;
}
onWorldMatrixUpdated() {
const t = this.es;
if (!t) return;
if (!this.gameObject.activeInHierarchy || !this.enabled) return;
Transform.updateRawObject3DWorldMatrixRecursively(t);
this.transform.enqueueRenderAttachedObject3D(t);
}
rl(t, i) {
t.visible = i;
const s = t.children;
for (let t = 0; t < s.length; ++t) {
this.rl(s[t], i);
}
}
get object3D() {
return this.es;
}
setObject3D(t, i) {
if (this.sl) {
if (this.es) {
this.hl?.(this.es);
this.transform.unsafeGetObject3D().remove(this.es);
}
this.es = t;
this.hl = i ?? null;
this.transform.unsafeGetObject3D().add(this.es);
Transform.updateRawObject3DWorldMatrixRecursively(this.es);
this.transform.enqueueRenderAttachedObject3D(this.es);
} else {
if (this.es) {
this.hl?.(this.es);
}
this.es = t;
this.hl = i ?? null;
}
}
clearObject3D() {
if (!this.es) return;
this.hl?.(this.es);
if (this.sl) this.transform.unsafeGetObject3D().remove(this.es);
this.es = null;
}
get ready() {
return this.sl;
}
updateWorldMatrix() {
this.onWorldMatrixUpdated();
}
}