Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda