javascript - 防止点击 React-Fiber Three.js 中的对象

标签 javascript reactjs three.js react-three-fiber

这是我可以点击的平面立方体

到目前为止,这个问题的主要问题是当我只想单击鼠标悬停的平面时,单击平面会点击。我在 Three.js 平面上缺少什么?

我曾尝试在 three.js 中搜索与 collision 相关的内容,但到目前为止无济于事。

进一步研究,我认为与RayCasting有关?

import React, { useState, useRef } from "react";
import { OrbitControls, Plane } from "@react-three/drei";
import { Canvas, useFrame, useThree, extend } from "@react-three/fiber";
import styles from "../styles/game.module.css";
import { DoubleSide } from "three";

const Cell = (props) => {
  const [hovered, hover] = useState(false);
  const [checked, setChecked] = useState(false);

  const colorStyle = () => {
    if (hovered) return "hotpink";
    if (checked) return "lightblue";
    return "orange";
  };
  return (
    <Plane
      scale={1}
      onClick={() => setChecked(!checked)}
      onPointerEnter={() => hover(true)}
      onPointerLeave={() => hover(false)}
      position={props.position}
      rotation={props.rotation}
    >
      <meshPhongMaterial side={DoubleSide} color={colorStyle()} />
    </Plane>
  );
};

const Cube = () => {
  useFrame((state, delta) => {
  });

  return (
    <>
      {/* Back Face */}
      <Cell position={[-1, 1, -1.5]} rotation={[0, 0, 0]} />
      // other cells here

      {/* Front Face */}
      <Cell position={[-1, 1, 1.5]} rotation={[0, 0, 0]} />
      // other cells here

      {/* Left Face */}
      <Cell position={[-1.5, 1, 1]} rotation={[0, Math.PI / 2, 0]} />
      // other cells here

      {/* Right Face */}
      <Cell position={[1.5, 1, 1]} rotation={[0, Math.PI / 2, 0]} />
      // other cells here
      
      {/* Bottom Face */}
      <Cell position={[1, -1.5, 1]} rotation={[Math.PI / 2, 0, 0]} />
      // other cells here
      
      {/* Top */}
      <Cell position={[1, 1.5, 1]} rotation={[Math.PI / 2, 0, 0]} />
     // other cells here
    </>
  );
};

const SceneItems = () => {
  return (
    <>
      <OrbitControls minDistance={7.5} maxDistance={15} />
      <ambientLight intensity={0.5} />
      <spotLight position={[10, 15, 10]} angle={0.3} />
      <Cube position={[1, 1, 1]} />
    </>
  );
};

const CompleteScene = () => {
  return (
    <div id={styles.scene}>
      <Canvas>
        <SceneItems />
      </Canvas>
    </div>
  );
};

最佳答案

看来,要防止这种点击发生,我所需要做的就是将 event.stopPropagation() 添加到我的 Plane 上的事件监听器中。现在我不再点击 Plane


const Cell = (props) => {
  const [hovered, hover] = useState(false);
  const [checked, setChecked] = useState(false);

  useCursor(hovered);

  const colorStyle = () => {
    if (hovered) return "hotpink";
    if (checked) return "lightblue";
    return "orange";
  };
  return (
    <Plane
      scale={1}
      onClick={(e) => {
        e.stopPropagation();
        setChecked(!checked);
      }}
      onPointerEnter={(e) => {
        e.stopPropagation();
        hover(true);
      }}
      onPointerLeave={(e) => {
        e.stopPropagation();
        hover(false);
      }}
      position={props.position}
      rotation={props.rotation}
    >
      <meshPhongMaterial side={DoubleSide} color={colorStyle()} />
    </Plane>
  );
};

此处有更多详细信息:https://docs.pmnd.rs/react-three-fiber/api/events#pointer-capture

关于javascript - 防止点击 React-Fiber Three.js 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69938046/

相关文章:

javascript - JavaScript 中标识符名称中的 unicode 转义序列有什么意义?

java - GSON 将数组的数组解析为 Java 对象数组

javascript - Three.js 围绕对象中心轴旋转、缩放不发生

python-3.x - 如何使用 nibabel 修改 mgh/dicom/nifti 文件的方向

javascript - 连接商店时 Mobx.inject 和 Mobx.observer 之间的主要区别是什么?

javascript - 如何围绕边旋转三 Angular 形?

javascript - 检查某个项目是否已在数组中的正确 jquery 语法是什么?这是一个简单的例子

javascript - 如何在 Javascript 函数中显示图像

reactjs - 除非刷新页面,否则 React Router 将不会渲染 Route 组件

javascript - 安装后,ReactJS 组件可以接收新属性或新状态。组件内部发生了什么不同?