Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda