如何在React-Three-Fiber中为从Rhino3dm文件加载的每个网格部分添加鼠标事件?

huangapple go评论81阅读模式
英文:

How to add mouse events to each mesh part loaded from Rhino3dm file in React-Three-Fiber?

问题

以下是代码部分的翻译:

import "./styles.css";
import * as THREE from "three";
import { useEffect, useMemo, useRef, useState } from "react";
import { Canvas } from "@react-three/fiber";
import { useLoader } from "@react-three/fiber";
import { Rhino3dmLoader } from "three/examples/jsm/loaders/3DMLoader";
import { Environment, OrbitControls} from "@react-three/drei";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { Suspense } from "react";

const Part = (child) => {
  const objRef = useRef();
  const [hovered, setHover] = useState(false);
  const material = new THREE.MeshStandardMaterial({
    color: hovered ? "hotpink" : "orange"
  });
  return (
    <mesh
      key={child.id}
      ref={objRef}
      castShadow
      receiveShadow
      onPointerOver={(e) => {
        e.stopPropagation();
        setHover(true);
      }}
      onPointerOut={(e) => {
        e.stopPropagation();
        setHover(false);
      }}
    >
      <primitive object={child} material={material} />
    </mesh>
  );
};

const Model = () => {
  // For some reason mouse events don't work well in Rhino3dm.
  const object = useLoader(Rhino3dmLoader, "./rhino_logo.3dm", (loader) => {
    loader.setLibraryPath("https://cdn.jsdelivr.net/npm/rhino3dm@0.15.0-beta/");
  });
  
  const model = object.children.map((child) => Part(child));

  // For gltf the below code worked fine.
  /* 
  const gltf = useLoader(GLTFLoader, "./rhino_logo.glb");
  const model = Object.values(gltf.nodes).map((child) => {
    if(child.isMesh){
     return Part(child)
    }
  });
  */
  return model;
};

export default function App() {
  return (
    <div className="App">
      <Canvas>
        <Suspense fallback={null}>
          <Model />
          <OrbitControls />
          <Environment preset="sunset" background />
        </Suspense>
      </Canvas>
    </div>
  );
}

注意:我只翻译了代码部分,没有包括其他文本内容。

英文:

I want to realize a function that changes color when the mouse is over each mesh part loaded from an Rhino3dm file with react-three-fiber.But in the code below mouse events are not working properly for some reason.

For GLTF, using Object.values(gltf.nodes).map worked fine.
enter image description here

For Rhino3dm, I use object.children.map and I think this is bad.

Do you know the cause and solution?

This is the sandbox URL.
https://codesandbox.io/s/test-rhino3dmloader-wnrty6?file=/src/App.js

import &quot;./styles.css&quot;;
import * as THREE from &quot;three&quot;;
import { useEffect, useMemo, useRef, useState } from &quot;react&quot;;
import { Canvas } from &quot;@react-three/fiber&quot;;
import { useLoader } from &quot;@react-three/fiber&quot;;
import { Rhino3dmLoader } from &quot;three/examples/jsm/loaders/3DMLoader&quot;;
import { Environment, OrbitControls} from &quot;@react-three/drei&quot;;
import { GLTFLoader } from &quot;three/examples/jsm/loaders/GLTFLoader&quot;;
import { Suspense } from &quot;react&quot;;
const Part = (child) =&gt; {
const objRef = useRef();
const [hovered, setHover] = useState(false);
const material = new THREE.MeshStandardMaterial({
color: hovered ? &quot;hotpink&quot; : &quot;orange&quot;
});
return (
&lt;mesh
key={child.id}
ref={objRef}
castShadow
receiveShadow
onPointerOver={(e) =&gt; {
e.stopPropagation();
setHover(true);
}}
onPointerOut={(e) =&gt; {
e.stopPropagation();
setHover(false);
}}
&gt;
&lt;primitive object={child} material={material} /&gt;
&lt;/mesh&gt;
);
};
const Model = () =&gt; {
// For some reason mouse events don&#39;t work well in Rhino3dm.
const object = useLoader(Rhino3dmLoader, &quot;./rhino_logo.3dm&quot;, (loader) =&gt; {
loader.setLibraryPath(&quot;https://cdn.jsdelivr.net/npm/rhino3dm@0.15.0-beta/&quot;);
});
const model = object.children.map((child) =&gt; Part(child));
// For gltf the below code worked fine.
/* 
const gltf = useLoader(GLTFLoader, &quot;./rhino_logo.glb&quot;);
const model = Object.values(gltf.nodes).map((child) =&gt; {
if(child.isMesh){
return Part(child)
}
});
*/
return model;
};
export default function App() {
return (
&lt;div className=&quot;App&quot;&gt;
&lt;Canvas&gt;
&lt;Suspense fallback={null}&gt;
&lt;Model /&gt;
&lt;OrbitControls /&gt;
&lt;Environment preset=&quot;sunset&quot; background /&gt;
&lt;/Suspense&gt;
&lt;/Canvas&gt;
&lt;/div&gt;
);
}

答案1

得分: 0

我已经能够在下面解决它!谢谢!!

https://discourse.threejs.org/t/how-to-add-mouse-events-to-each-mesh-part-loaded-from-rhino3dm-file-in-react-three-fiber/48191/2

英文:

I was able to solve it below! Thank you!!

https://discourse.threejs.org/t/how-to-add-mouse-events-to-each-mesh-part-loaded-from-rhino3dm-file-in-react-three-fiber/48191/2

huangapple
  • 本文由 发表于 2023年2月18日 13:45:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定