react-three-fiberreact-three-drei

Camera Helper Three Fiber


I'm trying to add a camera helper to visualize what my camera sees. I found a storybook from Drei that shows how to use useHelper, but I'm not seeing it show up. Any tips?

Story: https://drei.pmnd.rs/?path=/story/gizmos-usehelper--camera-story

Github: https://github.com/pmndrs/drei/blob/050c2abcfdc00fe455326567533fefffad179da8/.storybook/stories/useHelper.stories.tsx

My own Sandbox: https://codesandbox.io/s/visible-width-camera-helper-ycsxjn?file=/src/App.js

I may be wrong but my thinking was that I should be able to see the perspectiveCamera from Fiber's default camera.

import { Canvas } from '@react-three/fiber'
import { PerspectiveCamera, useHelper, CameraHelper, OrbitControls, Box } from '@react-three/drei'
import { useRef } from 'react'

export const App = () => {
  return (
    <Canvas tonemapped={'false'}>
      <color attach="background" args={['#fff']} />
      <OrbitControls />
      <Experience />
    </Canvas>
  )
}

const Experience = () => {
  const camera = useRef()
  useHelper(camera, CameraHelper)
  return (
    <>
      <ambientLight intensity={1} />
      <PerspectiveCamera makeDefault={false} ref={camera} near={1} far={4} position={[0, 0, 0]}>
        <meshBasicMaterial />
      </PerspectiveCamera>
      <Box />
    </>
  )
}

Solution

  • you should use THREE.Camera instead of CameraHelper from @react-three/drei

    code like

    import { Canvas } from '@react-three/fiber'
    import { PerspectiveCamera, useHelper, CameraHelper, OrbitControls, Box } from '@react-three/drei'
    import { useRef } from 'react'
    import * as THREE from 'three'
    
    export const App = () => {
      return (
        <Canvas tonemapped={'false'}>
          <color attach="background" args={['#fff']} />
          <OrbitControls />
          <Experience />
        </Canvas>
      )
    }
    
    const Experience = () => {
      const camera = useRef()
      useHelper(camera, THREE.CameraHelper)
      return (
        <>
          <ambientLight intensity={1} />
          <PerspectiveCamera ref={camera} near={1} far={4} position={[0, 0, 0]}></PerspectiveCamera>
          <mesh>
            <boxGeometry />
            <meshBasicMaterial color="red" />
          </mesh>
        </>
      )
    }
    

    I adjusted your Sandbox