import type { TresNodeOpsPlugin } from 'src/core/nodeOps' import { Body, Box, World, NaiveBroadphase, Vec3 as CannonVec3, } from 'cannon-es' import type { Object3D } from 'three' import type { TresContext } from '../composables' import { useRenderLoop } from '../composables' function setup() { const store = new Map() const world = new World() world.gravity.set(0, -9.8, 0) world.broadphase = new NaiveBroadphase() const loopAPI = useRenderLoop().onLoop(({ delta }) => { world.step(delta) for (const [body, object3D] of store) { object3D.position.copy(body.position) object3D.setRotationFromQuaternion(body.quaternion) } }) const dispose = () => { world.bodies.forEach(b => world.removeBody(b)) store.clear() loopAPI.off() } return { store, world, dispose } } export const plugin: TresNodeOpsPlugin
= ( context: TresContext, ) => { const { store, world, dispose } = setup() return { name: 'Cannon Physics plugin', filter: { tag: (tag: string) => tag === 'Collider', element: (a: any): a is Body => a && a.aabb, node: (a: any): a is Body => a && a.aabb, }, createElement: (_: any, __: any, ___: any, props: Record