Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
How to assign different click function to each face of the cube in react three fibre

Hi I have a simple Cube component made with react three fibre.

 import {useState,useRef} from 'react';
   import { useFrame } from '@react-three/fiber';
   const Box = ({props}) =>{
    const ref = useRef()
    const [hovered, hover] = useState(false)
    
    const boxClick = () =>{
        alert('clicked the object')
    }
    return ( 
      <mesh
        {...props}
        ref={ref}
        onClick={boxClick}
        scale={2}
        onPointerOver={(event) => hover(true)}
        onPointerOut={(event) => hover(false)}>
        <boxGeometry args={[1.5, 1.5, 1.5]} />
        <meshStandardMaterial color={hovered ? 'yellow' : 'orange'} />
      </mesh>
    )
  }
  export default Box;

I am importing the Box compoent in App.js and using it this way-

    function App() {

  const [boxes,setBoxes] = useState(0);
  const box1Click = () =>{
    setBoxes(() =>boxes+1)
    alert(boxes)
  }
  return (
   <Canvas className='main-canvas'>

      <ambientLight intensity={0.5} />
      <spotLight position={[5, 155, 10]} angle={0.15} penumbra={1} />
      <pointLight position={[-100, -200, -100]} />
      <Box position = {[0 ,0,0]}  />  

    </Canvas>
  );
}

export default App;

Right now I can click the cube and it runs the boxClick function properly. What I want to assign different click function to each face of the cube.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can draw six planes and change the rotation or the position each time to construct a cube..

const Plane = (props) => {
    const ref = useRef();
    const texture = useLoader(THREE.TextureLoader, "/images/wood.png");

useFrame((state) => {
    if (props.rotateX) ref.current.rotation.x = props.rotateX;
    if (props.rotateY) ref.current.rotation.y = props.rotateY;
    if (props.rotateZ) ref.current.rotation.z = props.rotateZ;
    // ref.current.rotation.x += 0.01;
    // ref.current.rotation.y += 0.01;
});

return (
    <mesh ref={ref} {...props}>
        <planeGeometry />
        <meshBasicMaterial
            color="red"
            side={THREE.DoubleSide}
            opacity={props.opacity}
            transparent
            visible={props.visible}
            map={texture}
        />
    </mesh>
);

};

And then add them to your scene

<Suspense fallback={null}>
    <Box position={[3, 3, 0]} />

    <Plane
        args={[2, 2]}
        position={[0.5, 0, 0.5]}
        rotateX={Math.PI / 2}
        opacity="0.5"
        visible={true}
    />
    <Plane
        args={[2, 2]}
        position={[0.5, 0.5, 1]}
        opacity="0.8"
        visible={true}
    />
    <Plane
        args={[2, 2]}
        position={[0, 0.5, 0.5]}
        rotateY={Math.PI / 2}
        opacity="1"
        visible={true}
    />
    {/** ... */}
</Suspense>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!