【发布时间】:2021-12-23 03:33:48
【问题描述】:
我正在绘制具有以下配置的 3d 模型。
以下是绘制模型的 CodeSandBox。
CodeSandBox-A
绘制的模型靠近画布顶部,我想居中。
您能告诉我如何在画布上居中放置模型吗?
这是我第一次接触 Three.js,所以我确定我可能犯了一些基本错误。
代码如下。
Canvas组件的props参考了以下文档。
React Three Fiber Documentation
import { useMemo, Suspense } from "react";
import "./style.css";
import { Canvas } from "@react-three/fiber";
const { useGLTF, OrbitControls, Stage } = require("@react-three/drei");
const CanvasContainer = () => {
const { scene } = useGLTF("/scene.glb");
return (
<Canvas camera={{ fov: 70, position: [0, 1000, 1000] }}>
// No matter what I put in this "position" value, I could not see any change in the canvas.
<OrbitControls enablePan={true} enableZoom={true} enableRotate={true} />
<Stage>
<group dispose={null}>
<primitive scale={[1, 1, 1]} object={scene} />
</group>
</Stage>
</Canvas>
);
};
const App = () => {
const isWindow = typeof window === "undefined";
const memoCanvas = useMemo(() => <CanvasContainer />, []);
return (
<>
<h1>Canvas</h1>
<div className="canvas_container">
{!isWindow && <Suspense fallback={<div />}>{memoCanvas}</Suspense>}
</div>
</>
);
};
export default App;
setDefaultCamera removed in v6.x. · Issue #1147 · pmndrs/react-three-fiber
我还尝试了使用useThree 的代码,如上述问题中所述。
以下是它的CodeSandBox。
CodeSandBox-B
从 CodeSandBox 可以看出,我无法移动相机。
【问题讨论】:
标签: reactjs typescript three.js react-three-fiber drei