UNPKG

mylingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

240 lines 7.36 kB
import { Group, Mesh } from "three"; import { boxGeometry } from "../primitives/Cube"; import { wireframeMaterial } from "../utils/reusables"; import ObjectManager from "./ObjectManager"; import { addOutline, deleteOutline } from "../../engine/renderLoop/effectComposer/outlinePass"; import { addBloom, deleteBloom } from "../../engine/renderLoop/effectComposer/selectiveBloomPass/renderSelectiveBloom"; import Reresolvable from "./utils/Reresolvable"; import { Cancellable } from "@lincode/promiselikes"; import toResolvable from "../utils/toResolvable"; export default class Loaded extends ObjectManager { loadedGroup = new Group(); constructor(unmounted) { super(new Mesh(boxGeometry, wireframeMaterial), unmounted); this.outerObject3d.add(this.loadedGroup); } loaded = new Reresolvable(); _src; get src() { return this._src; } set src(val) { this._src = val; this.loaded.done && this.loadedGroup.clear(); this.cancelHandle("src", val && (() => toResolvable(this.load(val)).then((loaded) => { const loadedObject3d = this.resolveLoaded(loaded, val); this.loadedGroup.add(loadedObject3d); this.loaded.resolve(loadedObject3d); this.object3d.visible = !!this._boxVisible; }))); } _onLoad; get onLoad() { return this._onLoad; } set onLoad(cb) { this._onLoad = cb; this.cancelHandle("onLoad", cb && (() => this.loaded.then(() => void cb()))); } widthSet; get width() { return super.width; } set width(val) { super.width = val; this.widthSet = true; } heightSet; get height() { return super.height; } set height(val) { super.height = val; this.heightSet = true; } depthSet; get depth() { return super.depth; } set depth(val) { super.depth = val; this.depthSet = true; } get innerRotationX() { return super.innerRotationX; } set innerRotationX(val) { super.innerRotationX = val; this.loadedGroup.rotation.x = this.object3d.rotation.x; } get innerRotationY() { return super.innerRotationY; } set innerRotationY(val) { super.innerRotationY = val; this.loadedGroup.rotation.y = this.object3d.rotation.y; } get innerRotationZ() { return super.innerRotationZ; } set innerRotationZ(val) { super.innerRotationZ = val; this.loadedGroup.rotation.z = this.object3d.rotation.z; } get innerX() { return super.innerX; } set innerX(val) { super.innerX = val; this.loadedGroup.position.x = this.object3d.position.x; } get innerY() { return super.innerY; } set innerY(val) { super.innerY = val; this.loadedGroup.position.y = this.object3d.position.y; } get innerZ() { return super.innerZ; } set innerZ(val) { super.innerZ = val; this.loadedGroup.position.z = this.object3d.position.z; } get innerVisible() { return this.loadedGroup.visible; } set innerVisible(val) { this.loadedGroup.visible = val; } get frustumCulled() { return super.frustumCulled; } set frustumCulled(val) { this.outerObject3d.frustumCulled = val; this.cancelHandle("frustumCulled", () => this.loaded.then(() => { super.frustumCulled = val; })); } get castShadow() { return super.castShadow; } set castShadow(val) { this._castShadow = val; this.cancelHandle("castShadow", () => this.loaded.then(() => { super.castShadow = val; })); } get receiveShadow() { return super.receiveShadow; } set receiveShadow(val) { this._receiveShadow = val; this.cancelHandle("receiveShadow", () => this.loaded.then(() => { super.receiveShadow = val; })); } get physics() { return super.physics; } set physics(val) { this._physics = val; const handle = this.cancelHandle("physics", () => this.loaded.then(() => { this.initPhysics(val, handle); })); } _boxVisible; get boxVisible() { return this._boxVisible ?? this.object3d.visible; } set boxVisible(val) { this._boxVisible = val; this.object3d.visible = val; } _outline; get outline() { return !!this._outline; } set outline(val) { this._outline = val; this.cancelHandle("outline", () => this.loaded.then((loaded) => { if (!val) return; addOutline(loaded); return () => { deleteOutline(loaded); }; })); } _bloom; get bloom() { return !!this._bloom; } set bloom(val) { this._bloom = val; this.cancelHandle("bloom", () => this.loaded.then((loaded) => { if (!val) return; addBloom(loaded); return () => { deleteBloom(loaded); }; })); } managerSet; addToRaycastSet(set) { const handle = new Cancellable(); queueMicrotask(() => { if (handle.done) return; if (this._physics === "map" || this._physics === "map-debug") handle.watch(this.loaded.then((loaded) => { if (!this.managerSet) { this.managerSet = true; loaded.traverse((child) => (child.userData.manager ??= this)); } set.add(loaded); return () => { set.delete(loaded); }; })); else handle.watch(super.addToRaycastSet(set)); }); return handle; } refreshFactors() { this.cancelHandle("refreshFactorsLoaded", () => this.loaded.then(() => void super.refreshFactors())); } materialCloned; tryCloneMaterial() { if (this.materialCloned) return; this.materialCloned = true; this.watch(this.loaded.then((loaded) => { const cloned = []; loaded.traverse((child) => { if (!child.material) return; cloned.push((child.material = child.material.clone())); }); return () => { for (const material of cloned) material.dispose(); }; })); } placeAt(object) { this.cancelHandle("placeAt", () => this.loaded.then(() => void super.placeAt(object))); } } export const getLoadedObject = (item) => { if ("loadedGroup" in item) return item.loadedGroup; if ("object3d" in item) return item.object3d; return item.outerObject3d; }; //# sourceMappingURL=Loaded.js.map