我正在使用react-three-fiber、three和nextjs来显示一些鸟类,这是threejs中常见的例子!下面是一个nextjs应用程序的索引文件:
import React, { useRef, useState, useEffect } from 'react';
import * as THREE from 'three';
import { Canvas, useFrame, useLoader } from 'react-three-fiber';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
function Box(props) {
// This reference will give us direct access to the mesh
const mesh = useRef();
// Set up state for the hovered and active state
const [hovered, setHover] = useState(false);
const [active, setActive] = useState(false);
// Rotate mesh every frame, this is outside of React without overhead
// eslint-disable-next-line no-return-assign,no-multi-assign
useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));
return (
<mesh
{...props}
ref={mesh}
scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
onClick={e => setActive(!active)}
onPointerOver={e => setHover(true)}
onPointerOut={e => setHover(false)}
>
<boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
<meshStandardMaterial
attach="material"
color={hovered ? 'hotpink' : 'orange'}
/>
</mesh>
);
}
// This component was auto-generated from GLTF by: https://github.com/react-spring/gltfjsx
function Bird({ speed, factor, url, ...props }) {
const gltf = useLoader(GLTFLoader, url);
const group = useRef();
const [mixer] = useState(() => new THREE.AnimationMixer());
useEffect(
() => void mixer.clipAction(gltf.animations[0], group.current).play(),
[gltf.animations, mixer],
);
useFrame((state, delta) => {
group.current.rotation.y +=
Math.sin((delta * factor) / 2) * Math.cos((delta * factor) / 2) * 1.5;
mixer.update(delta * speed);
});
return (
<group ref={group}>
<scene name="Scene" {...props}>
<mesh
name="Object_0"
morphTargetDictionary={gltf.__$[1].morphTargetDictionary}
morphTargetInfluences={gltf.__$[1].morphTargetInfluences}
rotation={[1.5707964611537577, 0, 0]}
>
<bufferGeometry attach="geometry" {...gltf.__$[1].geometry} />
<meshStandardMaterial
attach="material"
{...gltf.__$[1].material}
name="Material_0_COLOR_0"
/>
</mesh>
</scene>
</group>
);
}
function Birds() {
return new Array(2).fill().map((_, i) => {
const x = (15 + Math.random() * 30) * (Math.round(Math.random()) ? -1 : 1);
const y = -10 + Math.random() * 20;
const z = -5 + Math.random() * 10;
const bird = ['stork', 'parrot', 'flamingo'][Math.round(Math.random() * 2)];
const speed = bird === 'stork' ? 0.5 : bird === 'flamingo' ? 2 : 5;
const factor =
bird === 'stork'
? 0.5 + Math.random()
: bird === 'flamingo'
? 0.25 + Math.random()
: 1 + Math.random() - 0.5;
return (
<Bird
key={i}
position={[x, y, z]}
rotation={[0, x > 0 ? Math.PI : 0, 0]}
speed={speed}
factor={factor}
url={`/static/glb/${bird}.glb`}
/>
);
});
}
const MyComponent = props => {
return (
<Canvas>
<ambientLight />
<pointLight position={[10, 0, 10]} />
<Box position={[-1.2, 2, 0]} />
<Box position={[1.2, 0, 0]} />
<Box position={[3, -2, 0]} />
<Birds />
</Canvas>
);
};
export default MyComponent;我的文件在static/glb/...文件夹中!问题在于GLTFLoader加载程序。我尝试了扩展方法的"react-three-fiber",但没有帮助!
我想可能是因为。你认为这样对吗?
发布于 2020-04-14 15:37:24
嗯,我迟到了大约3个月,但我希望这能解决任何疑问,以防有人发现这个问题。
首先,您需要导入GLB呈现,您的应用程序目前不知道您要呈现哪个GLB文件。像这样的事情应该可以做到:
import MyCustomRender from 'src/static/glb/an_awesome_bird.glb'然后,每次实例化鸟组件时,您都可以将其作为道具传递,如
<Bird
key={i}
position={[x, y, z]}
rotation={[0, x > 0 ? Math.PI : 0, 0]}
speed={speed}
factor={factor}
url={MyCustomRender}
/>现在,由于您使用的是NextJS,这意味着您目前正在使用WebPack捆绑您的应用程序。
问题是,WebPack从一开始就不知道glb或GLTF文件是什么,您必须让这个小模块知道如何在导入具有上述扩展名的文件时进行操作。
您可以通过这样更新next.config.js文件来做到这一点:
module.exports = withCSS(withSass({
webpack (config, options) {
config.module.rules.push({
test: /\.(glb|gltf)$/,
use: {
loader: 'file-loader',
}
})
return config
}
}))这实际上意味着你告诉Webpack“嘿,如果你看到我导入任何.gltf或.glb文件,这意味着我想要他们的路径,所以给我他们的网址”。
如果您没有安装Webpack的文件加载程序,您可以始终这样做。
npm install file-loader --save-dev几天前,我使用完全相同的堆栈做了这个回购:
https://github.com/franco-roura/react-game
(但我没有使用NextJS,所以我的next.config.js实际上是webpack.config.js)
希望它能帮助你消除任何疑虑!
https://stackoverflow.com/questions/59912539
复制相似问题