UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

125 lines (124 loc) 5.22 kB
"use strict"; import { JsConnectionPointType } from "../../../../engine/nodes/utils/io/connections/Js"; import { ClothSolverStepSimulationOutput } from "../../../../engine/nodes/js/ClothSolverStepSimulation"; import { stringTailDigits } from "../../../String"; import { SopOnCreateHookRegister } from "./_Base"; import { SopType } from "../../../../engine/poly/registers/nodes/types/Sop"; const ACTOR_NODE_BASE_NAME = "actor_clothSolver"; const MAT_NAME = "MAT"; const MESH_PHYSICAL_MAT_NAME = "meshPhysical_CLOTH"; function _onCreatePrepareActorNode(node) { function createActorNodeChildren(actorNode) { const onScenePause = actorNode.createNode("onScenePause"); const onTick = actorNode.createNode("onTick"); const clothSolverReset1 = actorNode.createNode("clothSolverReset"); const softBodySolverStepSimulation1 = actorNode.createNode("clothSolverStepSimulation"); const clothSolverUpdateMaterial1 = actorNode.createNode("clothSolverUpdateMaterial"); clothSolverReset1.setInput(0, onScenePause); softBodySolverStepSimulation1.setInput(JsConnectionPointType.TRIGGER, onTick); const sharedInputs = [ JsConnectionPointType.TRIGGER, ClothSolverStepSimulationOutput.TEXTURE_SIZE, ClothSolverStepSimulationOutput.TEXTURE_POSITION0, ClothSolverStepSimulationOutput.TEXTURE_POSITION1, ClothSolverStepSimulationOutput.TEXTURE_NORMAL ]; for (const sharedInput of sharedInputs) { clothSolverUpdateMaterial1.setInput(sharedInput, softBodySolverStepSimulation1, sharedInput); } onScenePause.uiData.setPosition(-100, -100); clothSolverReset1.uiData.setPosition(100, -100); onTick.uiData.setPosition(-100, 100); softBodySolverStepSimulation1.uiData.setPosition(100, 100); clothSolverUpdateMaterial1.uiData.setPosition(300, 100); } function createActorNode(geoNode2) { const actor12 = geoNode2.createNode("actor"); actor12.setName(`${ACTOR_NODE_BASE_NAME}${stringTailDigits(node.name())}`); const nodePos = node.uiData.position(); actor12.uiData.setPosition(nodePos.x, nodePos.y - 200); createActorNodeChildren(actor12); return actor12; } const geoNode = node.parent(); const actor1 = ( /*geoNode.nodesByType('actor')[0] ||*/ createActorNode(geoNode) ); return { actor1 }; } function _createGlNodes(node) { const current_global = node.nodesByType("globals")[0]; const current_output = node.nodesByType("output")[0]; if (current_global || current_output) { return; } const globals1 = node.createNode("globals"); const output1 = node.createNode("output"); output1.setInput(0, globals1); globals1.uiData.setPosition(-200, 0); output1.uiData.setPosition(200, 0); return { globals1, output1 }; } function _createMat(node) { const nodePos = node.uiData.position(); const geoNode = node.parent(); const materialNode = geoNode.createNode("material"); function createMatNetwork(geoNode2) { const MAT2 = geoNode2.createNode("materialsNetwork"); MAT2.setName(MAT_NAME); const nodePos2 = node.uiData.position(); MAT2.uiData.setPosition(nodePos2.x - 200, nodePos2.y); return MAT2; } const MAT = geoNode.nodesByType("materialsNetwork")[0] || createMatNetwork(geoNode); function createMeshMaterial(MAT2) { const existingMatNodes = MAT2.children().filter( (node2) => node2.name() == MESH_PHYSICAL_MAT_NAME ); function _createMatBuilder() { const matBuilder = MAT2.createNode("meshPhysicalBuilder"); const globals = matBuilder.createNode("globals"); const output1 = matBuilder.createNode("output"); const clothSolverPosition1 = matBuilder.createNode("clothSolverPosition"); const attribute1 = matBuilder.createNode("attribute"); output1.setInput(0, clothSolverPosition1); clothSolverPosition1.setInput(0, attribute1); attribute1.setAttribSize(1); attribute1.p.name.set("id"); globals.uiData.setPosition(-400, 0); output1.uiData.setPosition(400, 0); clothSolverPosition1.uiData.setPosition(100, 0); attribute1.uiData.setPosition(-100, 0); matBuilder.setName(MESH_PHYSICAL_MAT_NAME); return matBuilder; } const materialNode2 = existingMatNodes[0] || _createMatBuilder(); return materialNode2; } const matNode = MAT.node(MESH_PHYSICAL_MAT_NAME) || createMeshMaterial(MAT); materialNode.p.material.setNode(matNode, { relative: true }); materialNode.setInput(0, node); materialNode.uiData.setPosition(nodePos.x, nodePos.y + 200); return { materialNode }; } function onCreateHook(node) { const geoNode = node.parent(); const nodePos = node.uiData.position(); const clothPrepare1 = geoNode.createNode("clothPrepare"); clothPrepare1.uiData.setPosition(nodePos.x, nodePos.y - 400); const glNodes = _createGlNodes(node); const matNodes = _createMat(node); const { actor1 } = _onCreatePrepareActorNode(node); actor1.setInput(0, clothPrepare1); node.setInput(0, actor1); return { actor1, clothPrepare1, matNodes, glNodes }; } export class ClothSolverSopOnCreateRegister extends SopOnCreateHookRegister { type() { return SopType.CLOTH_SOLVER; } onCreate(node) { return onCreateHook(node); } }