javascript - react-three-fiber multiple meshes 丢失模型,除了最后一个
问题描述
我一直在用 React 开发一个应用程序react-three-fiber
来直观地显示数据。我试图通过将表示其属性的数据作为道具传递给组件来创建不同的模型。我的问题出现在这里,因为在每个组件内部我能够添加一个简单的形状(例如立方体)并让它正确显示,当我在每个组件内部声明要使用的模型时,它似乎只被最后一个组件拾取创建;
顶级组件画布简化
return (
<div>
<Canvas>
<Suspense fallback={null}>
<Items items={this.state.data}/>
</Suspense>
</Canvas>
</div>
)
中层组件
// count is to identify the position in the loop so that the meshes do not overlap
import React, {Component} from 'react';
import ITEM from './ITEM';
class Items extends Component {
render() {
return this.props.items.map((item, i) => (
<ITEM key={i} item={item} count={i}/>
));
}
}
底层组件
// I have been using the drei GLTF loader to get the model
import React, {useRef} from 'react';
import {useFrame} from 'react-three-fiber';
import {useGLTF} from 'drei';
const Model = ({modelPath}) => {
const gltf = useGLTF(modelPath, true)
return <primitive object={gltf.scene} dispose={null}/>
};
const ITEM = ({item, count}) => {
const modelPath = '/model/scene.gltf';
const mesh = useRef(null);
useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));
return (
<mesh castShadow position={[1, 1, 1]} ref={mesh}>
<Model modelPath={modelPath}/>
</mesh>
);
};
解决方案
使用gltfjsx将您的 GLTF 模型转换为 react 组件,然后您可以使用 props 来编辑它们的某些属性。
推荐阅读
- python - ModuleNotFoundError:没有名为“dota”的模块
- arrays - 试图从 json/api 而不是数组中提取数据
- reactjs - 页面中的 MUIDataTable 默认行不起作用
- gitlab - Gitlab-CI 又名管道依赖项中的上游触发
- amazon-web-services - AWS CloudWatch 自定义指标在实例类型更改后未显示
- azure - 将 Cosmbos DB Sql Api 中 Container 的主键命名为不同于“id”
- flutter - ios 中的音频显示错误的持续时间计时,但在 android 颤振应用程序中正确
- r - 如何对矩阵内的向量进行排名?在 R 中
- python - 从python中的嵌套字典中打印唯一键
- python - 在 tf.keras 中使用 tensorflow 函数