javascript - 修改 app.js 文件时,Expo.GLView 在 iOS 上不起作用
问题描述
我目前正在使用我的 iOS 设备上的 expo 开发 Web AR。在最初的过程中,当我修改我的 app.js 时,Expo.GLView 似乎无法在我的 iOS 上运行。
任何人都可以帮忙吗?
import React from 'react';
import Expo from 'expo';
import { StyleSheet, Text, View } from 'react-native';
import * as THREE from 'three';
import ExpoTHREE from 'expo-three';
export default class App extends React.Component {
render() {
return (
<Expo.GLView
style={{flex: 1}}
onContextCreate={this._onGLContextCreate}
/>
);
}
_onGLContextCreate = async (gl) => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, gl.drawingBufferWidth / gl.drawingBufferHeight, 0.1, 1000);
const renderer = ExpoTHREE.createRenderer({ gl });
renderer.setSize(gl.drawingBufferWidth, gl.drawingBufferHeight);
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
const animate = () => {
requestAnimationFrame(animate);
renderer.render(scene, camera);
gl.endFrameEXP();
}
animate();
}
}
这是我遇到的错误。我之前已经清楚地遵循了说明。怎么了?
[01:45:27] TypeError: undefined is not an object (evaluating '_expo.default.GLView')
解决方案
看起来只是导入时的一个小语法问题。Expo 必须有一个与其各种组件或类似的东西分开的默认导出。以下对我有用:
import { GLView } from 'expo'
我目前正在使用 expo-three 并为我的 GLView 元素使用包 expo-graphics,我发现它有点方便,因为它接受“onResize”和“onRender”的额外道具,所以你不需要单独处理这些.
npm i --save expo-graphics
然后不是从 expo 导入它,而是从 expo graphics 导入它(该组件在 expo-graphics 中只是称为“视图”,因此如果您正在使用同一个组件中的其他视图,您可能希望使用自己的名称导入它)
import { View as GraphicsView } from 'expo-graphics'
然后它是标准 GLView 的直接替代品
render() {
return (
<GraphicsView
style={{flex: 1}}
onContextCreate={this._onGLContextCreate}
/>
);
}
然后,您还可以通过各自的道具添加渲染循环功能和调整大小功能
render() {
return (
<GraphicsView
style={{flex: 1}}
onContextCreate={this._onGLContextCreate}
onResize={this._onResize}
onRender={this._onRender}
/>
);
}
推荐阅读
- xamarin - Xamarin 访问 ViewModel 属性
- html - 出现边框时如何保持位置?
- swiftui - 类型“Favorites.Type”不能符合“Encodable”;只有结构/枚举/类类型可以符合协议
- r - 在自己的 R 包中使用并行包函数
- sockets - C 套接字服务器:读取所有未知长度 XMLHttpRequest 的正确方法是什么?
- visual-studio - 我可以在 Visual Studio 2019 之外运行我的微软单元测试吗?
- android - 如何在 RecyclerView 中访问图像和背景颜色 - onBindViewHolder
- python - 使用 Python 以数字顺序将多个文件连接或附加到新文件
- sql - 在 Oracle SQL 临时脚本中声明函数
- php - 结合 Woocommerce 运输类