UNPKG

@threlte/rapier

Version:

Components and hooks to use the Rapier physics engine in Threlte

33 lines (32 loc) 885 B
import { useTask } from '@threlte/core'; import { useRapier } from './useRapier'; const isKey = (value) => { return typeof value === 'string' || typeof value === 'symbol'; }; export function usePhysicsTask(keyOrFn, fnOrOptions, options) { const { simulationTask, simulationStage } = useRapier(); let key; let fn; let opts; if (isKey(keyOrFn)) { key = keyOrFn; fn = fnOrOptions; opts = options ?? {}; } else { key = Symbol('usePhysicsTask'); fn = keyOrFn; opts = (fnOrOptions ?? {}); } if (opts.before && Array.isArray(opts.before)) { opts.before.push(simulationTask); } else if (opts.before) { opts.before = [opts.before, simulationTask]; } else { opts.before = [simulationTask]; } opts.stage = simulationStage; return useTask(key, fn, opts); }