Skip to content
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

React Three Fiber (R3F) Integration Example #6

Open
koljam opened this issue Dec 9, 2024 · 0 comments
Open

React Three Fiber (R3F) Integration Example #6

koljam opened this issue Dec 9, 2024 · 0 comments

Comments

@koljam
Copy link

koljam commented Dec 9, 2024

First, thank you for creating this excellent library! I've been banging my head against the wall for some time trying to get proper transparency rendering.

While implementing three-wboit with React Three Fiber, I ran into some challenges and wanted to share a working solution that might help others.

Here's a minimal working example:

// WBOITPass.jsx - Separate component to handle WBOIT setup
function WBOITPass() {
  const { gl, scene, camera } = useThree();
  
  useEffect(() => {
    const wboitPass = new WboitPass(gl, scene, camera, 0, 1.0);
    const originalRender = gl.render.bind(gl);
    let isRendering = false;

    gl.render = function (...args) {
      if (isRendering) {
        return originalRender.apply(this, args);
      }
      isRendering = true;
      wboitPass.render(gl);
      isRendering = false;
    };

    return () => {
      gl.render = originalRender;
    };
  }, [gl, scene, camera]);

  return null;
}

// Usage in your scene
function App() {
  return (
    <Canvas gl={{ alpha: true }}>
      <WBOITPass />
      {/* Your scene content */}
    </Canvas>
  );
}

// Patching materials
const material = useMemo(() => {
  const material = new THREE.MeshStandardMaterial({
    transparent: true,
    opacity: 0.5
  });
  WboitUtils.patch(material);
  return material;
}, []);

The key points:

  • Create a separate component to handle WBOIT setup
  • Use a rendering flag to prevent recursion
  • Patch materials inside useMemo to prevent recreation
  • Add the WBOITPass component to your Canvas

Hope this helps future R3F users!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant