【发布时间】:2021-07-12 20:48:05
【问题描述】:
我想在一个屏幕上同时使用来自react-native-gl-model-view 的 2 个“3d 对象模型”。当我使用单个 3d 对象时,它可以正常工作,但一旦我使用两个或两个以上,每个 3d 对象模型都会开始闪烁,并在几秒钟后应用程序崩溃。
这是它如何处理多个 3d 对象
代码如下:
import React, {Component} from 'react';
import {StyleSheet, View, Animated} from 'react-native';
import ModelView from 'react-native-gl-model-view';
const AnimatedModelView = Animated.createAnimatedComponent(ModelView);
export default class Multiple extends Component {
constructor() {
super();
this.state = {
rotateZ: new Animated.Value(0),
};
}
componentDidMount() {
this.animate(0);
}
animate(iteration) {
Animated.timing(this.state.rotateZ, {
toValue: ++iteration * 360,
useNativeDriver: true,
duration: 5000,
}).start(this.animate.bind(this, iteration++));
}
renderModel() {
return (
<AnimatedModelView
model={{
uri: 'demon.obj',
}}
texture={{
uri: 'demon.png',
}}
tint={{r: 1.0, g: 1.0, b: 1.0, a: 1.0}}
animate
scale={0.01}
translateZ={-2.5}
rotateX={270}
rotateZ={Animated.add(this.state.rotateZ, Math.random() * 360)}
style={styles.model}
/>
);
}
render() {
return (
<View style={styles.container}>
<View style={styles.row}>
{this.renderModel()}
{this.renderModel()}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
row: {
flex: 1,
flexDirection: 'row',
},
model: {
flex: 1,
backgroundColor: 'transparent',
},
});
注意 1:要在 Android 上加载模型,您需要将模型放在 android/app/src/main/assets 文件夹中。如果新文件夹不存在,请创建一个新文件夹。
assets/models which were used in the above code.
如果你想试试代码:
npm install react-native-gl-model-view --save- 在注释 1 所示的位置添加资产。
- 创建文件 Muliple.js 并粘贴上面的代码
npm run android
抱歉,我想向您展示 expo snap 中的代码,但它不起作用并显示以下错误:
requireNativeComponent: "RNGLModelView" was not found in the UIManager.
【问题讨论】:
-
似乎是一个错误,因为您的代码与example for showing multiple models at the same time 几乎相同。在崩溃日志中,我看到了
FATAL EXCEPTION: GLThread 4136 java.lang.IndexOutOfBoundsException: Index: 0, Size: 1。我认为 Android 的原生模块代码中存在错误。
标签: javascript android reactjs react-native 3d-model