@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
69 lines (68 loc) • 2.49 kB
JavaScript
;
import { CoreSoftBodyAttribute, SoftBodyIdAttribute } from "./SoftBodyAttribute";
import { SoftBodyController } from "./SoftBodyController";
import { SoftBody } from "./SoftBody";
import { coreObjectClassFactory } from "../geometry/CoreObjectFactory";
const controllers = /* @__PURE__ */ new WeakMap();
const softBodies = /* @__PURE__ */ new WeakMap();
export function createOrFindSoftBodyController(scene, node, options) {
const { tetEmbed, threejsObjectInSceneTree } = options;
const { highResObject } = tetEmbed;
if (highResObject) {
tetEmbed.highResObject = threejsObjectInSceneTree;
tetEmbed.threejsObjectInSceneTree = threejsObjectInSceneTree;
} else {
tetEmbed.lowResObject = threejsObjectInSceneTree;
tetEmbed.threejsObjectInSceneTree = threejsObjectInSceneTree;
}
let controller = controllers.get(threejsObjectInSceneTree);
if (!controller) {
controller = new SoftBodyController(scene, {
node
});
controllers.set(threejsObjectInSceneTree, controller);
const { softBody } = createOrFindSoftBody(node, tetEmbed);
if (softBody) {
controller.setSoftBody(softBody);
} else {
console.warn("no softbody found");
}
}
return { controller };
}
export function createOrFindSoftBody(node, tetEmbed) {
const { threejsObjectInSceneTree } = tetEmbed;
if (!threejsObjectInSceneTree) {
throw "createOrFindSoftBody: threejsObjectInSceneTree is null";
}
let softBody = softBodies.get(threejsObjectInSceneTree);
if (!softBody) {
const highResSkinningLookupSpacing = CoreSoftBodyAttribute.getHighResSkinningLookupSpacing(tetEmbed.tetObject);
const highResSkinningLookupPadding = CoreSoftBodyAttribute.getHighResSkinningLookupPadding(tetEmbed.tetObject);
softBody = new SoftBody({
node,
tetEmbed,
highResSkinning: {
lookup: {
spacing: highResSkinningLookupSpacing,
padding: highResSkinningLookupPadding
}
}
});
softBodies.set(threejsObjectInSceneTree, softBody);
}
return { softBody };
}
export function softBodyControllerNodeIdFromObject(softBodyObject) {
const nodeId = coreObjectClassFactory(softBodyObject).attribValue(
softBodyObject,
SoftBodyIdAttribute.SOLVER_NODE
);
return nodeId;
}
export function softBodyControllerFromObject(softBodyObject) {
return controllers.get(softBodyObject);
}
export function softBodyFromObject(softBodyObject) {
return softBodies.get(softBodyObject);
}