Coder Social home page Coder Social logo

react-three-gpu-pathtracer's Introduction


react-three-gpu-pathtracer

⚡️ A React abstraction for the popular three-gpu-pathtracer


Chat on Twitter Chat on Twitter


This demo is real, you can click it! It contains full code, too. 📦 More examples here

3D model by KuayArts (@kuayarts) on Sketchfab.


react-three-gpu-pathtracer lets you render your react-three-fiber scenes using Path Tracing! It is as simple as

import { Pathtracer } from '@react-three/gpu-pathtracer'

function GradientSphere() {
  return (
    <Canvas>
      <Pathtracer>{/* Your scene */}</Pathtracer>
    </Canvas>
  )
}

The <Pathtracer /> component wraps your scene. The scene is then rendered using Path Tracing.

Props

Prop Type Default Description
alpha number 1 Alpha of the scene background. Must be set to <1 to see behind the canvas. Two extra render targets are used if <1.
samples number 1 Number of samples per-frame
frames number Infinity Number of frames to path trace. Will pause rendering once this number is reached.
tiles [number, number] / THREE.Vector2 / { x: number; y: number } / number 2 Number of tiles. Can be used to improve the responsiveness of a page while still rendering a high resolution target.
bounces number 1 The number of ray bounces to test. Higher is better quality but slower performance.
enabled boolean true Wether to enable pathtracing.
resolutionFactor number 1 Scaling factor for resolution.0.5 means the scene will be rendered at half of screen resolution. Higher is better quality but slower performance.
backgroundBlur number 0 Strength of blur on background env map.
backgroundIntensity number 1 Strength of the light cast by the env map.

Backgrounds

Env maps

Env maps can be added using Drei's <Environment /> component just like in a regular scene.

<Pathtracer>
  <Environment
    preset="..."
    background // Optional, set as scene background
  />
</Pathtracer>

Or you can use a solid color as the background

<Canvas>
    <color args={[0xff0000]} attach="background" />

    <Pathtracer>
        // ...

usePathtracer

This hook provides access to useful functions in the internal renderer. Can only be used within the <Pathtracer /> component.

const { renderer, update, reset } = usePathtracer()
Return value Type Description
renderer PathTracingRenderer Internal renderer. Can be used to access/edit internal properties
reset () => void Clear's the textures. Equiv to renderer.reset(). Must be called every time the camera moves.
update () => void Re-calculates and re-uploads BVH. Needed when new objects/materials are added to/removed from the scene.

react-three-gpu-pathtracer's People

Contributors

drcmda avatar farazzshaikh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-three-gpu-pathtracer's Issues

Uncaught Error : Cannot find module error (with NextJS 13)

  • three : ^0.149.0
  • @react-three/fiber : ^8.11.8
  • @react-three/drei : ^9.56.25
  • next : 13.2.3

Problem description:

Hello!

I'm now use this lib with @react-three/fiber, drei in nextjs.

When I use in NextJS 13, it gave me the not found module error (Cannot find module '.../node_modules/three/examples/jsm/postprocessing/Pass') which module actually exist.

Once I thought it is some kind of nextjs 'server side runtime issue' so that I tried 'use client' code to declare the component is not for server side but for client side component.

After I found out it doesn't work, I tried window undefined check. But it also doesn't work.

next/dynamic

const Pathtracer = dynamic(
  () => import("@react-three/gpu-pathtracer").then((mod) => mod.Pathtracer),
  {
    ssr: false,
  }
);

doesn't work.

Now I need some advise for handle this problem.
Is this the problem of server side runtime issue with nextjs or lib compatibility issue?
I want to know what the problem is and how I can solve the error.

Here's the jsx return code which from basic example.

Relevant code:

return (
    <>
      <Leva
        collapsed
        titleBar={{
          title: "Options",
        }}
      />
      <Canvas
        gl={{
          outputEncoding: sRGBEncoding,
          toneMapping: ACESFilmicToneMapping,
        }}
      >
        <Suspense fallback={null}>
 
            <Pathtracer
              samples={opts.Rendering_Samples}
              bounces={opts.Rendering_Bounces}
              resolutionFactor={opts.Rendering_Factor}
              tiles={opts.Rendering_Tiles}
              enabled={opts.Rendering_Enabled}
              backgroundIntensity={opts.Environment_Intensity}
              backgroundBlur={opts.Environment_Blur}
            >
              <Environment
                preset={"studio"}
                background={opts.Environment_Visible}
              />
              <Thing />
              <UI infoRef={infoRef} />
            </Pathtracer>
         
        </Suspense>
      </Canvas>
      <Stats />
    </>
  );

Here's the error msg I get.
Error Msg:

GET http://localhost:3000/ 500 (Internal Server Error)
Uncaught Error: Cannot find module '.../node_modules/three/examples/jsm/postprocessing/Pass'
at createEsmNotFoundErr (node:internal/modules/cjs/loader:1016:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1009:15)
    at resolveExports (node:internal/modules/cjs/loader:529:14)
    at Module._findPath (node:internal/modules/cjs/loader:569:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:981:27)
    at mod._resolveFilename (file:///Users/Documents/GitHub/module_PathTrace/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (file:///Users/Documents/GitHub/module_PathTrace/node_modules/@react-three/gpu-pathtracer/dist/react-three-gpu-pathtracer.cjs.dev.js:8:12)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (file:///Users/Documents/GitHub/module_PathTrace/node_modules/@react-three/gpu-pathtracer/dist/react-three-gpu-pathtracer.cjs.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at @react-three/gpu-pathtracer (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:131:18)
    at __webpack_require__ (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/components/canvas/etc/ui.tsx:7:85)
    at ./src/components/canvas/etc/ui.tsx (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:76:1)
    at __webpack_require__ (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/components/canvas/Scene.tsx:19:65)
    at __webpack_require__.a (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:97:13)
    at eval (webpack-internal:///./src/components/canvas/Scene.tsx:1:21)
    at ./src/components/canvas/Scene.tsx (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:32:1)
    at __webpack_require__ (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/pages/index.tsx:12:82)
    at __webpack_require__.a (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:97:13)
    at eval (webpack-internal:///./src/pages/index.tsx:1:21)
    at ./src/pages/index.tsx (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:98:1)
    at __webpack_require__ (file:///Users/Documents/GitHub/module_PathTrace/.next/server/webpack-runtime.js:33:42)
    at __webpack_exec__ (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:196:39)
    at <unknown> (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:197:28)
    at Object.<anonymous> (file:///Users/Documents/GitHub/module_PathTrace/.next/server/pages/index.js:200:3)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.requirePage (file:///Users/Documents/GitHub/module_PathTrace/node_modules/next/dist/server/require.js:88:12)
    at <unknown> (file:///Users/Documents/GitHub/module_PathTrace/node_modules/next/dist/server/load-components.js:49:73)
    at async Object.loadComponentsImpl [as loadComponents] (file:///Users/Documents/GitHub/module_PathTrace/node_modules/next/dist/server/load-components.js:49:26)

mergeGeometries vs mergeBufferGeometries with Three 0.161.0

When i want to use the path tracer component I always got this error:

import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
./node_modules/three-gpu-pathtracer/src/utils/GeometryPreparationUtils.js
Attempted import error: 'mergeBufferGeometries' is not exported from 'three/examples/jsm/utils/BufferGeometryUtils.js' (imported as 'mergeBufferGeometries').

I figured out that three/addons/utils/BufferGeometryUtils.js has no mergeBufferGeometries export just a mergeGeometries.

I used R3F 8.15.16 and Nextjs 14.1,0

Are those two exports identical? How can I eliminate the error?

glTF scene lights are ignored

Maybe I'm missing something but I couldn't find anything in API.ts that handles lights, similar to this sample code from the three-gpu-pathtracer repo.

// update the lights
ptMaterial.lights.updateFrom( lights );
ptMaterial.lightCount = lights.length;

I have a glTF scene with a single spotlight and it doesn't seem to be picked up. Am I missing something?

Demo shows error "Cannot read properties of undefined (reading 'count')"

How to reproduce?

See demo: https://se7i99.csb.app/

image

  • Tested on Chrome
    • Version 101.0.4951.64 (Official Build) (x86_64)
    • Personal profile
    • Incognito

Unfortunately, I don't have a chance to dive deeper and help debug this myself right now. Creating this issue now to see if anyone else experiences this error as well or if it is just me. Hopefully there's a quick fix 🤞 Really excited to use this package! 🎉

Can't add lights to pathtracing scene

I'm trying to figure out how to insert lights within a pathtracing generated scene. Link to the relevant codesanbox:

https://codesandbox.io/s/react-three-gpu-pathtracer-forked-g2l8l2?file=/src/App.js

In this example, I added a red RectAreaLight within lines 116-131. I copied the methods that were mentioned on one of the repo issues:
#4
https://codesandbox.io/s/react-three-gpu-pathttracer-gltf-lights-3t7bqu?file=/src/App.js

But with no luck. As you can see, the light only appears when you move the OrbitControls within the codesanbox, since that shows the normal render of the scene. When the pathtracing render is enabled (when you do not move the OrbitControls for half a second), the light dissapears. I want the light to also be visible whenever pathtracing is enabled, as in this example from the original pathtracing library, on vanilla Three.js.
https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/spotLights.html

Here, the light is visible on both modes.

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.