-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I disable worker with config parameter? #63
Comments
The web worker is not native to Cannon, and the Cannon lib can be used without the worker ( However the worker is a core component of this It's possible to create your own import React, { createContext, useEffect, useRef, useContext, useState } from 'react'
import { useFrame } from 'react-three-fiber'
import * as CANNON from 'cannon'
const WorldContext = createContext({})
export const PhysicsProvider = ({ children }) => {
const [ world ] = useState(() => new CANNON.World())
useEffect(() => {
world.broadphase = new CANNON.NaiveBroadphase()
world.solver.iterations = 10
world.gravity.set(0, 0, -10)
}, [ world ])
// Run world stepper every frame
useFrame(() => world.step(1 / 60))
return (
<WorldContext.Provider value={world}>
{children}
</WorldContext.Provider>
)
}
export const useCannon = ({ ...props }, fn, deps = []) => {
const ref = useRef()
const world = useContext(WorldContext)
const [ body ] = useState(() => new CANNON.Body(props))
useEffect(() => {
// Call function so the user can add shapes, positions, etc. to the body
fn(body)
world.addBody(body)
return () => world.remove(body)
}, deps)
useFrame(() => {
if (ref.current) {
// Transport cannon physics into the referenced threejs object
const { position, quaternion } = body
const { x: px, y: py, z: pz } = position
const { x: qx, y: qy, z: qz, w: qw } = quaternion
ref.current.position.copy(new Vector3(px, py, pz))
ref.current.quaternion.copy(new Quaternion(qx, qy, qz, qw))
}
})
return ref
} then to consume: import React from 'react'
import { Canvas} from 'react-three-fiber'
import { useCannon } from './index'
const Box = ({ position, width, height, depth }) => {
const ref = useCannon({ mass: 10 }, body => {
body.addShape(
new CANNON.Box(
new CANNON.Vec3(width * 0.5, height * 0.5, depth * 0.5)
)
)
body.position.set(...position)
})
return (
<mesh ref={ref}>
<boxGeometry attach="geometry" args={[ width, height, depth ]} />
<meshStandardMaterial attach="material" />
</mesh>
)
}
const App = () => (
<Canvas>
<PhysicsProvider>
<Box position={[ 0, 0, 5 ]} width={2} height={2} depth={2} />
</PhysicsProvider>
</Canvas>
) Note that you will want a second physics object in the scene (such as a floor plane) for your box to collide with. You can combine this with a wireframe debugger (found here) like so: import { PhysicsBodyWireframes } from './debug'
const App = () => (
<Canvas>
<PhysicsProvider>
<PhysicsBodyWireframes />
<Box position={[ 0, 0, 5 ]} width={2} height={2} depth={2} />
</PhysicsProvider>
</Canvas>
) |
I would be curious to know what environment you can't use web workers in? They are supported pretty well even in older browsers, although I'm not sure if the API might be different in old versions of IE |
If you emulate the API of a web worker with a class on the main thread I guess it might technically be possible to run |
|
@drcmda do you just mean a way to pause the World simulation, or are we talking about a second Provider whose context isnt a web worker? |
Thank you for the detailed explaination. |
i think pausing would be a nice feature. similar to enabled/disabled on threejs controls. that would also solve @sidealice usecase since it's requesting the webworker runtime. |
I'm missing how pausing the simulation would help with the worker runtime, but I know we can implement pausing - it should be as easy as skipping the |
if it's not enabled, maybe it wouldn't have to create the worker. then again, there would be lots of awkward rewiring, looking over the code it's probably not worth it because everything expects the worker to be in place - if we change that it would affect everything. better yet to not add Physics to the scene graph at all, and that can easily be made conditional. |
I played around with a substitute for the worker API on the main thread the other day (specifically to address this issue). What would be the equivalent of |
Hi from React Native land... I've been using the useCannon hook from the examples folder but having access to the full api would be awesome. |
@jwrubel You technically have access to more of the Cannon API with that example hook than you do within Making |
It's possible I'm getting lost in the code a bit. The hook only returns
I can't access |
The The Since the hook from the example code you posted is already running on the main thread, you have access to the entire |
I don't personally need this, but thought I'd share some results from having implemented cannon.js support (both with and without a Web Worker) in a past project. When running a large simulation, or a simulation that isn't highly interactive, a Web Worker is a great choice. But there's a minimum to the latency of interaction with a physics simulation running in a web worker. This is particularly noticeable with WebXR input devices, where (1) the display is likely refreshing at 90-120fps, and (2) the user's perception of their own motion is quite precise. In that scenario, with interaction wired up to WebXR controllers using Cannon.JS constraints, I found that not using a Web Worker for lightweight simulations worked really well. Context: n5ro/aframe-physics-system#33 |
got previous |
I've simplified the original https://codesandbox.io/s/cannon-es-r3f-forked-66mobn?file=/src/App.js:1211-1225 |
It would be great to allow |
Agreed, this would be great for React Native support (afaik this is the only thing blocking) |
I just used this solution for my project (a few objects collisioning). On web works perfect, but on Android and iOS It gets super laggy. Any solution to the poor performance? |
I recently rewrote this library and removed the web worker functionality. I thought I could release another version for everyone to use directly. |
I got a problem that in some environment javascript worker api should be disabled.
Shall I disable it in cannon manually?
The text was updated successfully, but these errors were encountered: