question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot add OrbitControl from drei to this starter.

See original GitHub issue

I try to add the OrbitControl from Drei to this project. With next-transpile-modules setup, the same code works well if I create the next.js project then import react-three-fiber, drei, etc. but I cannot make it work with this template. Here is how I edit the _canvas.jsx file. I’m I missing something? https://codesandbox.io/s/bold-monad-dqfr3?file=/src/components/layout/_canvas.jsx:0-1402

import { Canvas, useFrame } from '@react-three/fiber'
import { Preload } from '@react-three/drei'
import { A11yUserPreferences } from '@react-three/a11y'
import useStore from '@/helpers/store'
import { OrbitControls } from '@react-three/drei'
import React, { useRef, useState } from 'react'

function Box(props) {
  // This reference will give us direct access to the THREE.Mesh object
  const mesh = useRef()
  // Set up state for the hovered and active state
  const [hovered, setHover] = useState(false)
  const [active, setActive] = useState(false)
  // Subscribe this component to the render-loop, rotate the mesh every frame
  useFrame((state, delta) => (mesh.current.rotation.x += 0.01))
  // Return the view, these are regular Threejs elements expressed in JSX
  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? 1.5 : 1}
      onClick={(event) => setActive(!active)}
      onPointerOver={(event) => setHover(true)}
      onPointerOut={(event) => setHover(false)}
    >
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}

const LCanvas = ({ children }) => {
  return (
    <Canvas>
      <ambientLight />
      <pointLight position={[10, 10, 10]} />
      <Box position={[-1.2, 0, 0]} />
      <Box position={[1.2, 0, 0]} />
      <OrbitControls />
    </Canvas>
  )
}

export default LCanvas

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
njm222commented, Jun 19, 2021

The sandbox was broken earlier… i.e. did not run (it is working now)

So if you inspect the elements you will see the dom element you referenced here:

import useStore from '@/helpers/store'
import { useRef } from 'react'

const Dom = ({ children }) => {
  const ref = useRef(null)
  useStore.setState({ dom: ref })
  return (
    <div
      className='absolute left-0 z-10 w-screen h-screen overflow-hidden dom'
      ref={ref}
    >
      {children}
    </div>
  )
}

export default Dom

is covering the canvas. so if you remove h-screen it should work

0reactions
pfortescommented, Aug 11, 2021

If anyone encounters this error you should look at the changes for /layout/canvas.jsx in the commit: https://github.com/pmndrs/react-three-next/commit/3aa88a174035e0f3b1a69783d56d4f470745e251 This has the correct way to pass the dom events to the orbit controls.

const LControl = () => {
  const dom = useStore((state) => state.dom)
  const control = useRef(null)

  useEffect(() => {
    if (control) {
      dom.current.style['touch-action'] = 'none'
    }
  }, [dom, control])
  return <OrbitControls ref={control} domElement={dom.current} />
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Three.js - OrbitControls doesn't work - Stack Overflow
In your HTML the order matters when adding OrbitControls as it needs certain things from Three.js. It should be <script src="../build/three.min.js"></script> ...
Read more >
Add Controls in ThreeJs | OrbitControls in ThreeJs - YouTube
Controlling objects in ThreeJs is a pretty important topic so by doing that we can create a pretty good interactive 3D view, ...
Read more >
OrbitControls – three.js docs
Orbit controls allow the camera to orbit around a target. To use this, as with all files in the /examples directory, you will...
Read more >
Adding OrbitControls to React Three Fiber - Code Workshop
Three.JS has an import we can use called OrbitControls that adds a lot of free camera movement functionality. It is very configurable and...
Read more >
@react-three/drei - npm
Some drei controls already take it into account, like CameraShake, Gizmo and TransformControls. Drei currently exports OrbitControls , MapControls , ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found