@spearwolf/twopoint5d
Version:
Create 2.5D realtime graphics and pixelart with WebGL and three.js
54 lines • 1.56 kB
JavaScript
import { MathUtils } from 'three/webgpu';
import { findRootNode } from '../utils/findRootNode.js';
export class HelpersManager {
constructor() {
this.uuid = MathUtils.generateUUID();
}
#scene;
#root;
get scene() {
return this.#scene;
}
set scene(scene) {
if (this.#scene !== scene) {
this.remove();
this.#scene = scene;
this.#root = undefined;
}
}
get root() {
if (this.#root == null && this.#scene != null) {
this.#root = findRootNode(this.#scene);
}
return this.#root;
}
add(node, addToRoot = false) {
const target = addToRoot ? this.root : this.#scene;
if (target) {
node.userData['isHelper'] = true;
node.userData['createdBy'] = this.uuid;
target.add(node);
}
}
remove() {
if (this.#scene) {
this.removeFromScene(this.#scene);
}
}
removeFromScene(scene) {
const removeChildren = [];
for (const childNode of scene.children) {
if (childNode.userData['isHelper'] && childNode.userData['createdBy'] === this.uuid) {
removeChildren.push(childNode);
}
}
for (const childNode of removeChildren) {
childNode.removeFromParent();
childNode.dispose?.();
}
if (this.root && scene !== this.root) {
this.removeFromScene(this.root);
}
}
}
//# sourceMappingURL=HelpersManager.js.map