Skip to content

Instantly share code, notes, and snippets.

@SakuraHentai
Created November 7, 2023 11:50
Show Gist options
  • Save SakuraHentai/16d9bb5ec8a3b63fb27c5a0bfa720ce6 to your computer and use it in GitHub Desktop.
Save SakuraHentai/16d9bb5ec8a3b63fb27c5a0bfa720ce6 to your computer and use it in GitHub Desktop.
R3F ( React Three Fiber ) place plane on other mesh demo
import { useRef } from "react";
import { Canvas } from "react-three-fiber";
function Demo() {
const circleRef = useRef();
return (
<>
<mesh
rotation={[4, 0.3, -0.3]}
onPointerMove={(e) => {
if (circleRef.current) {
const pos = e.point.clone();
const dir = e.face.normal.clone();
dir.applyQuaternion(e.object.quaternion);
circleRef.current.lookAt(e.point.add(dir));
circleRef.current.position.copy(pos.add(dir.divideScalar(2 << 5)));
}
}}
>
<boxBufferGeometry args={[2, 2, 2]} />
<meshPhongMaterial color="red" />
</mesh>
<mesh ref={circleRef}>
<circleGeometry args={[0.2, 32]} />
<meshBasicMaterial color="yellow" />
</mesh>
</>
);
}
export default function App() {
return (
<div className="App" style={{ width: "100vw", height: "100vh" }}>
<Canvas>
<Demo />
<ambientLight intensity={0.1} />
<directionalLight />
</Canvas>
</div>
);
}
@SakuraHentai
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment