@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
57 lines (56 loc) • 1.85 kB
JavaScript
;
import { watch } from "@vue-reactivity/watch";
import { ThreejsCoreObject } from "./ThreejsCoreObject";
export class CoreObjectHelper {
constructor(options) {
this.options = options;
this._watchStopHandles = [];
this.scene = options.scene;
this.object = options.object;
}
dispose() {
for (const watchStopHandle of this._watchStopHandles) {
watchStopHandle();
}
}
onAttributeUpdate(attribName, attribType, defaultValue, callback) {
const attributeRef = ThreejsCoreObject.attributeRef(this.object, attribName, attribType, defaultValue);
if (attributeRef == null) {
console.error(`attrib not found:'${attribName}'`);
return;
}
const watchStopHandle = watch(attributeRef.current, callback);
this._watchStopHandles.push(watchStopHandle);
}
watch(source, cb, options) {
const watchHandle = watch(source, cb, options);
this._watchStopHandles.push(watchHandle);
}
}
export async function objectFromNode(node, objectName, creatFunction, options) {
const container = await node.compute();
const coreGroup = container.coreContent();
if (!coreGroup) {
console.error(`no core group found in node '${node.path()}'`);
console.log(node.states.error.message());
return;
}
const objects = coreGroup.threejsObjects();
let foundObject = objects.find((o) => o.name == objectName);
if (!foundObject && (options == null ? void 0 : options.traverse) == true) {
for (const object of objects) {
if (foundObject == null) {
object.traverse((child) => {
if (child.name == objectName) {
foundObject = child;
}
});
}
}
}
if (!foundObject) {
console.error(`no object with name '${objectName}'`);
return;
}
return creatFunction({ scene: node.scene(), object: foundObject });
}