UNPKG

@polygonjs/polygonjs

Version:

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

574 lines (573 loc) 17.4 kB
"use strict"; import { Vector3 } from "three"; import { getPhysicsWorldNodeFromWorldObject } from "../nodes/sop/PhysicsWorld"; import { NamedFunction1, ObjectNamedFunction0, ObjectNamedFunction1, ObjectNamedFunction2, ObjectNamedFunction3, ObjectNamedFunction4 } from "./_Base"; import { physicsCreateRBDFromWorldObject, setWorldGravity, stepWorld } from "../../core/physics/PhysicsWorld"; import { RBDProperty, _getRBDFromObject, _getRBDFromId, _physicsRBDAddForce, _physicsRBDAddForceAtPoint, _physicsRBDAddTorque, _physicsRBDApplyImpulse, _physicsRBDApplyImpulseAtPoint, _physicsRBDDelete, _physicsRBDResetAll, _physicsRBDApplyTorqueImpulse, _physicsRBDResetForces, _physicsRBDResetTorques, _setPhysicsRBDAngularVelocity, _setPhysicsRBDLinearVelocity, _setPhysicsRBDPosition, _setPhysicsRBDRotation } from "../../core/physics/PhysicsRBD"; import { _createPhysicsRBDKinematicConstraint, _deletePhysicsRBDKinematicConstraint, _physicsRBDDeleteConstraints } from "../../core/physics/PhysicsJoint"; import { RBDCapsuleProperty, _getPhysicsRBDCapsuleRadius, _setPhysicsRBDCapsuleProperty } from "../../core/physics/shapes/RBDCapsule"; import { _getPhysicsRBDConeHeight, _getPhysicsRBDConeRadius, _setPhysicsRBDConeProperty } from "../../core/physics/shapes/RBDCone"; import { RBDCuboidProperty, _getPhysicsRBDCuboidSizes, _setPhysicsRBDCuboidProperty } from "../../core/physics/shapes/RBDCuboid"; import { _getPhysicsRBDCylinderHeight, _getPhysicsRBDCylinderRadius, _setPhysicsRBDCylinderProperty } from "../../core/physics/shapes/RBDCylinder"; import { _getPhysicsRBDSphereRadius, _setPhysicsRBDSphereProperty } from "../../core/physics/shapes/RBDSphere"; import { getOrCreatePropertyRef } from "../../core/reactivity/RBDPropertyReactivity"; import { RBDCommonProperty } from "../../core/physics/shapes/_CommonHeightRadius"; import { dummyReadRefVal } from "../../core/reactivity/CoreReactivity"; import { _matchArrayLength } from "./_ArrayUtils"; import { updatePhysicsDebugObject } from "../../core/physics/PhysicsDebug"; export class physicsWorldReset extends ObjectNamedFunction0 { static type() { return "physicsWorldReset"; } func(object3D) { const physicsWorldNode = getPhysicsWorldNodeFromWorldObject(object3D, this.scene); if (!physicsWorldNode) { return; } physicsWorldNode.setDirty(); } } export class physicsWorldStepSimulation extends ObjectNamedFunction0 { static type() { return "physicsWorldStepSimulation"; } func(object3D) { stepWorld(object3D); } } export class physicsDebugUpdate extends ObjectNamedFunction0 { static type() { return "physicsDebugUpdate"; } func(object3D) { updatePhysicsDebugObject(object3D); } } export class setPhysicsWorldGravity extends ObjectNamedFunction2 { static type() { return "setPhysicsWorldGravity"; } func(object3D, gravity, lerp) { setWorldGravity(object3D, gravity, lerp); } } export class getPhysicsRBD extends NamedFunction1 { static type() { return "getPhysicsRBD"; } func(rbdId) { dummyReadRefVal(this.timeController.timeUniform().value); return _getRBDFromId(rbdId); } } export class getPhysicsRBDCapsuleRadius extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDCapsuleRadius"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCapsuleProperty.RADIUS).value); return _getPhysicsRBDCapsuleRadius(object3D); } } export class getPhysicsRBDCapsuleHeight extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDCapsuleHeight"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCapsuleProperty.HEIGHT).value); return _getPhysicsRBDCapsuleRadius(object3D); } } export class getPhysicsRBDConeHeight extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDConeHeight"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCommonProperty.HEIGHT).value); return _getPhysicsRBDConeHeight(object3D); } } export class getPhysicsRBDConeRadius extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDConeRadius"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCommonProperty.RADIUS).value); return _getPhysicsRBDConeRadius(object3D); } } export class getPhysicsRBDCuboidSizes extends ObjectNamedFunction1 { static type() { return "getPhysicsRBDCuboidSizes"; } func(object3D, target) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCuboidProperty.SIZES).value); _getPhysicsRBDCuboidSizes(object3D, target); return target; } } export class getPhysicsRBDCylinderHeight extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDCylinderHeight"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCommonProperty.HEIGHT).value); return _getPhysicsRBDCylinderHeight(object3D); } } export class getPhysicsRBDCylinderRadius extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDCylinderRadius"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCommonProperty.RADIUS).value); return _getPhysicsRBDCylinderRadius(object3D); } } export class getPhysicsRBDSphereRadius extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDSphereRadius"; } func(object3D) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDCommonProperty.RADIUS).value); return _getPhysicsRBDSphereRadius(object3D); } } export class setPhysicsRBDCapsuleProperty extends ObjectNamedFunction3 { static type() { return "setPhysicsRBDCapsuleProperty"; } func(object3D, scale, lerp, updateMatrix) { _setPhysicsRBDCapsuleProperty(object3D, scale, lerp, updateMatrix); } } export class setPhysicsRBDConeProperty extends ObjectNamedFunction4 { static type() { return "setPhysicsRBDConeProperty"; } func(object3D, radius, height, lerp, updateMatrix) { _setPhysicsRBDConeProperty(object3D, radius, height, lerp, updateMatrix); } } export class setPhysicsRBDCuboidProperty extends ObjectNamedFunction4 { static type() { return "setPhysicsRBDCuboidProperty"; } func(object3D, sizes, size, lerp, updateMatrix) { _setPhysicsRBDCuboidProperty(object3D, sizes, size, lerp, updateMatrix); } } export class setPhysicsRBDCylinderProperty extends ObjectNamedFunction4 { static type() { return "setPhysicsRBDCylinderProperty"; } func(object3D, radius, height, lerp, updateMatrix) { _setPhysicsRBDCylinderProperty(object3D, radius, height, lerp, updateMatrix); } } export class setPhysicsRBDSphereProperty extends ObjectNamedFunction3 { static type() { return "setPhysicsRBDSphereProperty"; } func(object3D, radius, lerp, updateMatrix) { _setPhysicsRBDSphereProperty(object3D, radius, lerp, updateMatrix); } } export class getPhysicsRBDAngularVelocity extends ObjectNamedFunction1 { static type() { return "getPhysicsRBDAngularVelocity"; } func(object3D, target) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.ANGULAR_VELOCITY).value); const angvel = body.angvel(); return target.set(angvel.x, angvel.y, angvel.z); } else { return target.set(0, 0, 0); } } } export class getChildrenPhysicsRBDPropertiesAngularVelocity extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesAngularVelocity"; } func(object3D, values) { dummyReadRefVal(this.timeController.timeUniform().value); _matchArrayLength(object3D.children, values, () => new Vector3()); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); if (body) { const angvel = body.angvel(); values[i].set(angvel.x, angvel.y, angvel.z); } else { values[i].set(0, 0, 0); } i++; } return values; } } export class getPhysicsRBDLinearVelocity extends ObjectNamedFunction1 { static type() { return "getPhysicsRBDLinearVelocity"; } func(object3D, target) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.LINEAR_VELOCITY).value); const linvel = body.linvel(); return target.set(linvel.x, linvel.y, linvel.z); } else { return target.set(0, 0, 0); } } } export class getChildrenPhysicsRBDPropertiesLinearVelocity extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesLinearVelocity"; } func(object3D, values) { dummyReadRefVal(this.timeController.timeUniform().value); _matchArrayLength(object3D.children, values, () => new Vector3()); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); if (body) { const linvel = body.linvel(); values[i].set(linvel.x, linvel.y, linvel.z); } else { values[i].set(0, 0, 0); } i++; } return values; } } export class getPhysicsRBDAngularDamping extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDAngularDamping"; } func(object3D) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.ANGULAR_DAMPING).value); return body.angularDamping(); } return 0; } } export class getChildrenPhysicsRBDPropertiesAngularDamping extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesAngularDamping"; } func(object3D, values) { _matchArrayLength(object3D.children, values, () => 0); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); values[i] = body ? body.angularDamping() : 0; i++; } return values; } } export class getPhysicsRBDLinearDamping extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDLinearDamping"; } func(object3D) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.LINEAR_DAMPING).value); return body.linearDamping(); } return 0; } } export class getChildrenPhysicsRBDPropertiesLinearDamping extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesLinearDamping"; } func(object3D, values) { _matchArrayLength(object3D.children, values, () => 0); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); values[i] = body ? body.linearDamping() : 0; i++; } return values; } } export class getPhysicsRBDIsSleeping extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDIsSleeping"; } func(object3D) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.IS_SLEEPING).value); return body.isSleeping(); } return false; } } export class getChildrenPhysicsRBDPropertiesIsSleeping extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesIsSleeping"; } func(object3D, values) { _matchArrayLength(object3D.children, values, () => false); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); values[i] = (body == null ? void 0 : body.isSleeping()) || false; i++; } return values; } } export class getPhysicsRBDIsMoving extends ObjectNamedFunction0 { static type() { return "getPhysicsRBDIsMoving"; } func(object3D) { const body = _getRBDFromObject(object3D); if (body) { dummyReadRefVal(getOrCreatePropertyRef(this.timeController, object3D, RBDProperty.IS_MOVING).value); return body.isMoving(); } return false; } } export class getChildrenPhysicsRBDPropertiesIsMoving extends ObjectNamedFunction1 { static type() { return "getChildrenPhysicsRBDPropertiesIsMoving"; } func(object3D, values) { _matchArrayLength(object3D.children, values, () => false); let i = 0; const children = object3D.children; for (let child of children) { const body = _getRBDFromObject(child); values[i] = (body == null ? void 0 : body.isMoving()) || false; i++; } return values; } } export class setPhysicsRBDPosition extends ObjectNamedFunction2 { static type() { return "setPhysicsRBDPosition"; } func(object3D, position, lerp) { _setPhysicsRBDPosition(object3D, position, lerp); } } export class setPhysicsRBDRotation extends ObjectNamedFunction2 { static type() { return "setPhysicsRBDRotation"; } func(object3D, quat, lerp) { _setPhysicsRBDRotation(object3D, quat, lerp); } } export class setPhysicsRBDAngularVelocity extends ObjectNamedFunction2 { static type() { return "setPhysicsRBDAngularVelocity"; } func(object3D, velocity, lerp) { _setPhysicsRBDAngularVelocity(object3D, velocity, lerp); } } export class setPhysicsRBDLinearVelocity extends ObjectNamedFunction2 { static type() { return "setPhysicsRBDLinearVelocity"; } func(object3D, velocity, lerp) { _setPhysicsRBDLinearVelocity(object3D, velocity, lerp); } } export class physicsRBDAddForce extends ObjectNamedFunction1 { static type() { return "physicsRBDAddForce"; } func(object3D, force) { _physicsRBDAddForce(object3D, force); } } export class physicsRBDAddForceAtPoint extends ObjectNamedFunction2 { static type() { return "physicsRBDAddForceAtPoint"; } func(object3D, force, point) { _physicsRBDAddForceAtPoint(object3D, force, point); } } export class physicsRBDAddTorque extends ObjectNamedFunction1 { static type() { return "physicsRBDAddTorque"; } func(object3D, force) { _physicsRBDAddTorque(object3D, force); } } export class physicsRBDApplyTorqueImpulse extends ObjectNamedFunction1 { static type() { return "physicsRBDApplyTorqueImpulse"; } func(object3D, impulse) { _physicsRBDApplyTorqueImpulse(object3D, impulse); } } export class physicsRBDApplyImpulse extends ObjectNamedFunction1 { static type() { return "physicsRBDApplyImpulse"; } func(object3D, impulse) { _physicsRBDApplyImpulse(object3D, impulse); } } export class physicsRBDApplyImpulseAtPoint extends ObjectNamedFunction2 { static type() { return "physicsRBDApplyImpulseAtPoint"; } func(object3D, impulse, point) { _physicsRBDApplyImpulseAtPoint(object3D, impulse, point); } } export class physicsRBDResetAll extends ObjectNamedFunction1 { static type() { return "physicsRBDResetAll"; } func(object3D, wakeup) { _physicsRBDResetAll(object3D, wakeup); } } export class physicsRBDResetForces extends ObjectNamedFunction1 { static type() { return "physicsRBDResetForces"; } func(object3D, wakeup) { _physicsRBDResetForces(object3D, wakeup); } } export class physicsRBDResetTorques extends ObjectNamedFunction1 { static type() { return "physicsRBDResetTorques"; } func(object3D, wakeup) { _physicsRBDResetTorques(object3D, wakeup); } } export class createPhysicsRBD extends ObjectNamedFunction2 { static type() { return "createPhysicsRBD"; } func(worldObject, object, rbdId) { const newRBDIds = physicsCreateRBDFromWorldObject(worldObject, object); newRBDIds == null ? void 0 : newRBDIds.forEach((id) => { rbdId.value = id; }); } } export class createPhysicsRBDs extends ObjectNamedFunction2 { static type() { return "createPhysicsRBDs"; } func(worldObject, objects, rbdIds) { for (let object of objects) { const newRBDIds = physicsCreateRBDFromWorldObject(worldObject, object); newRBDIds == null ? void 0 : newRBDIds.forEach((id) => { rbdIds.value.push(id); }); } } } export class physicsRBDDelete extends ObjectNamedFunction0 { static type() { return "physicsRBDDelete"; } func(object3D) { _physicsRBDDelete(this.scene, object3D); } } export class createPhysicsRBDKinematicConstraint extends ObjectNamedFunction2 { static type() { return "createPhysicsRBDKinematicConstraint"; } func(object3D, anchor, rbdIdRef) { const rbdId = _createPhysicsRBDKinematicConstraint(object3D, anchor); if (rbdId) { rbdIdRef.value = rbdId; } } } export class deletePhysicsRBDKinematicConstraint extends ObjectNamedFunction0 { static type() { return "deletePhysicsRBDKinematicConstraint"; } func(object3D) { _deletePhysicsRBDKinematicConstraint(this.scene, object3D); } } export class deletePhysicsRBDConstraints extends ObjectNamedFunction0 { static type() { return "deletePhysicsRBDConstraints"; } func(object3D) { _physicsRBDDeleteConstraints(object3D); } }