GithubHelp home page GithubHelp logo

funtechinc / react-three-offscreen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pmndrs/react-three-offscreen

0.0 0.0 0.0 2.44 MB

๐Ÿ“บ Offscreen worker canvas for react-three-fiber

Home Page: https://offscreen.pmnd.rs

License: MIT License

JavaScript 11.45% TypeScript 88.55%

react-three-offscreen's Introduction

react-three-offscreen

Version Downloads Twitter Discord Open Collective ETH BTC



npm install three @react-three/fiber @react-three/offscreen

This is an experimental package that allows you to render your react-three-fiber scene with an offscreen canvas in a web worker. This is mostly useful for self-contained webgl apps, and un-blocking the main thread.

What's the big deal, workers existed before

You only could never just run your existing WebGL/Threejs app in it. It had to be rewritten. Pointer-events wouldn't work, controls, textures, GLTFs, etc. Worse, thanks to Safari you needed to maintain two forks of your app, one that runs in a worker and one that runs on the main thread as a fallback. This is probably the main reason why Threejs with an offscreen canvas has never caught on.

This package tries to fix that! The goal is that your existing code will just work. It will forward DOM events to the worker, patch and shim Threejs as well as basic document/window interfaces. It will automatically fall back to main thread if a browser doesn't support offscreen canvas. Even the eco system will work (Drei, Rapier physics, Postprocessing, ...).

Usage

Instead of importing <Canvas> from @react-three/fiber you can import it from @react-three/offscreen and pass a worker prop. The fallback prop is optional, your scene will be rendered on the main thread when OffscreenCanvas is not supported.

It takes all other props that <Canvas> takes (dpr, shadows, etc), you can use it as a drop-in replacement.

// App.jsx (main thread)
import { lazy } from 'react'
import { Canvas } from '@react-three/offscreen'

// This is the fallback component that will be rendered on the main thread
// This will happen on systems where OffscreenCanvas is not supported
const Scene = lazy(() => import('./Scene'))

// This is the worker thread that will render the scene
const worker = new Worker(new URL('./worker.jsx', import.meta.url), { type: 'module' })

export default function App() {
  return (
    <Canvas
      worker={worker} fallback={<Scene />}
      shadows camera={{ position: [0, 5, 10], fov: 25 }} />
  )
}

Your worker thread will be responsible for rendering the scene. The render function takes a single argument, a ReactNode. React-three-fiber and its React-root/reconciler will run in that worker, rendering your contents.

// worker.jsx (worker thread)
import { render } from '@react-three/offscreen'
import Scene from './Scene'

render(<Scene />)

Your app or scene should idealy be self contained, it shouldn't postMessage with the DOM. This is because offscreen canvas + webgl is still not supported in Safari. If you must communicate with the DOM, you can use the web broadcast API.

// Scene.jsx (a self contained webgl app)
export default function App() {
  return (
    <mesh>
      <boxGeometry />
    </mesh>
  )
}

Troubleshooting

Compromises and defaults

For better interop all non-passive events (click, contextmenu, dlbclick) will preventDefault, pointerdown will capture, pointerup will release capture.

Nextjs

Just make sure to disable SSR for the canvas component because Worker only exists in the DOM:

// src/app/page.jsx
import dynamic from 'next/dynamic'

const App = dynamic(() => import('@/components/App'), { ssr: false })

Vite

Vites @vitejs/plugin-react tries to inject styles into document and assumes the presence of window, neither exist in a worker. As such you can consider the official React plugin faulty, it won't run React in a web worker. The workaround:

  1. yarn add @vitejs/[email protected]
  2. disable fast refresh (see: stackoverflow) (the option was removed in 4.x)
export default defineConfig({
  plugins: [react({ fastRefresh: false })],
  worker: { plugins: [react()] },
})

react-three-offscreen's People

Contributors

codyjasonbennett avatar drcmda avatar eyr1n avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.