-
Notifications
You must be signed in to change notification settings - Fork 19
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
Feature request: add ability to handle onmessage in worker #10
Comments
First off: thank you for making this library. I think it's a fantastic idea, and now that OffscreenCanvas is supported in all major browsers (check out latest support in Safari), you'll likely be getting more traction here. As @wmaurer mentioned, it would be nice to be able to hook into the Any users interested in using this package are likely doing so because they want/need every ounce of performance they can get in the browser. |
Note for any others: after looking more, it's also possible to use transferable objects via the ^ This may in fact be the best way to interact with your rendering WebWorker, since you are able to "transfer" objects that exist in the same thread OR in different threads, allowing you to isomorphically interact with your rendering thread/scene |
Hi, I'm late to the party but I came up with a solution that worked for me so I thought I'd leave it here: // worker.jsx
import React from "react";
import { render } from "@react-three/offscreen";
import Scene from "./Scene";
import mitt from "mitt";
// emitter to pass coords into our Scene component
const emitter = mitt();
// render method sets up worker `onmessage` handler
render(<Scene emitter={emitter} />);
// save the `onmessage` handler
const offscreenMessageHandler = self.onmessage || function () {};
// implement our own `onmessage` handler with routing to avoid
// intercepting offscreen stuff
onmessage = (event) => {
const { type, payload } = event.data;
if (type === "coords") {
// pointer coords, pass them to the Scene with mitt emitter
emitter.emit("coords", payload);
} else {
// react-three-offscreen messages, pass them to original handler
offscreenMessageHandler(event);
}
}; // Scene.tsx
interface SceneProps {
emitter: Emitter<Event>;
}
function Scene({ emitter }: SceneProps) {
const [coords, setCoords] = useState<Coords | null>(null);
useEffect(() => {
if (emitter) emitter.on("coords", (newCoords) => setCoords(newCoords));
}, [emitter]);
... |
I'd like to be able to handle
onmessage
in the worker. Thereact-three-offscreen
render
function already handlesonmessage
. I'd like to be able to hook into this.(My use case is that I'm sending
ImageBitmap
s from the video camera to the worker which are passed on to theScene
component as a stream)As an experiment, I've changed the signature of
render
to accept a callback:I then call the callback inside
render
's handler:Would you accept a PR for this feature request?
The text was updated successfully, but these errors were encountered: