This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo GLView
一个提供 GLView 的库,GLView 可用作 OpenGL ES 渲染目标并提供 GLContext。适用于渲染 2D 和 3D 图形。
expo-gl 提供了一个充当 OpenGL ES 渲染目标的 View,可用于渲染 2D 和 3D 图形。挂载时会创建一个 OpenGL ES 上下文。每帧都会将其绘图缓冲区呈现为 View 的内容。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
高级 API
由于 WebGL API 的层级较低,因此使用通过底层 GLView 进行渲染的高级图形 API 会很有帮助。以下库集成了常用的图形 API:
任何需要 WebGLRenderingContext 的 WebGL 库都可以使用。有时,这类库会假设运行在 Web JavaScript 上下文中(例如假设存在 document)。通常这是为了加载资源或处理事件,其主要渲染逻辑仍只使用纯 WebGL。因此,这些库通常仍可通过一些变通方法使用。上述 Expo 专用集成包含了针对一些常用库的变通方法。
与 Reanimated worklet 集成
若要在 Reanimated worklet 中使用此 API,需要将 GL 上下文 ID 传递给 worklet,并像下面的示例一样重新创建 GL 对象。
如需了解如何将 expo-gl 与 Reanimated 和 Gesture Handler 搭配使用的更详细示例,请查看此示例。
限制
Worklet 运行时会对其中运行的代码施加一些限制,因此,如果你已有 WebGL 代码,要在 worklet 线程中运行,可能需要进行一些修改。
- Pixi.js 或 Three.js 等第三方库无法在 worklet 中运行,你只能使用在开头添加了
'worklet'的函数。 - 如果需要加载一些资源并传递给 WebGL 代码,必须在主线程上完成加载,然后通过某种引用传递给 worklet。如果使用
expo-assets,只需将Asset.fromModule返回的资源对象,或useAssets钩子返回的资源对象传递给runOnUI函数即可。 - 要实现渲染循环,需要使用
requestAnimationFrame,不支持setTimeout等 API。
请查看 Reanimated 文档以了解更多信息。
远程调试与 GLView
启用远程调试时,此 API 无法按预期运行。React Native 调试器会在你的计算机而非移动设备上运行 JavaScript。GLView 需要 Chrome 不支持的同步原生调用。
API
import { GLView } from 'expo-gl';
Component
Type: React.Component<GLViewProps>
A View that acts as an OpenGL ES render target. On mounting, an OpenGL ES context is created. Its drawing buffer is presented as the contents of the View every frame.
boolean • Default: falseEnables support for interacting with a gl object from code running on the Reanimated worklet thread.
number • Default: 4GLView can enable iOS's built-in multisampling.
This prop specifies the number of samples to use. Setting this to 0 turns off multisampling.
(gl: ExpoWebGLRenderingContext) => voidA function that will be called when the OpenGL ES context is created.
The function is passed a single argument gl that extends a WebGLRenderingContext interface.
Static methods
Imperative API that creates headless context which is devoid of underlying view.
It's useful for headless rendering or in case you want to keep just one context per application and share it between multiple components.
It is slightly faster than usual context as it doesn't swap framebuffers and doesn't present them on the canvas,
however it may require you to take a snapshot in order to present its results.
Note that the context created using createContextAsync has to be destroyed using destroyContextAsync.
Also, keep in mind that you need to set up a viewport and create your own framebuffer and texture that you will be drawing to, before you take a snapshot.
Promise<ExpoWebGLRenderingContext>A promise that resolves to WebGL context object. See WebGL API for more details.
Destroys given context. This method is intended to use to destroy headless context created with createContextAsync.
Promise<boolean>A promise that resolves to boolean value that is true if given context existed and has been destroyed successfully.
Takes a snapshot of the framebuffer and saves it as a file to app's cache directory.
Promise<GLSnapshot>A promise that resolves to GLSnapshot object.
Component methods
Same as static takeSnapshotAsync(),
but uses WebGL context that is associated with the view on which the method is called.
Promise<GLSnapshot>Methods
Interfaces
Extends: WebGL2RenderingContext
Types
Literal type: union
Acceptable values are: null | number | Component<any, any> | ComponentClass<any>
Enums
GLLoggingOption.GET_ERRORS = 2Calls gl.getError() after each other method call and prints an error if any is returned.
This option has a significant impact on the performance as this method is blocking.
GLLoggingOption.RESOLVE_CONSTANTS = 4Resolves parameters of type number to their constant names.
GLLoggingOption.TRUNCATE_STRINGS = 8When this option is enabled, long strings will be truncated. It's useful if your shaders are really big and logging them significantly reduces performance.
WebGL API
组件挂载并创建 OpenGL ES 上下文后,通过 onContextCreate 属性接收的 gl 对象便成为 OpenGL ES 上下文的接口,并提供 WebGL API。它与 WebGL 2 规范中的 WebGL2RenderingContext 类似。
一些较旧的 Android 设备可能不支持 WebGL2 功能。要检查设备是否支持 WebGL2,建议使用 gl instanceof WebGL2RenderingContext。
此外还提供了一个方法 gl.endFrameEXP(),用于通知上下文当前帧已准备好呈现。这类似于其他 OpenGL 平台中的“交换缓冲区”API 调用。
以下 WebGL2RenderingContext 方法目前尚未实现:
getFramebufferAttachmentParameter()getRenderbufferParameter()compressedTexImage2D()compressedTexSubImage2D()getTexParameter()getUniform()getVertexAttrib()getVertexAttribOffset()getBufferSubData()getInternalformatParameter()renderbufferStorageMultisample()compressedTexImage3D()compressedTexSubImage3D()fenceSync()isSync()deleteSync()clientWaitSync()waitSync()getSyncParameter()getActiveUniformBlockParameter()
texImage2D() 的 pixels 参数必须为 null、包含像素数据的 ArrayBuffer,或形式为 { localUri } 的对象,其中 localUri 是设备文件系统中图像的 file:// URI。因此,只有在调用 .downloadAsync() 并完成下载以获取资源后,才会使用 Asset 对象。
出于效率考虑,目前这些方法的实现不会对参数执行类型或边界检查。因此,传入无效参数可能会导致原生崩溃。计划在后续 SDK 版本中更新 API,以执行参数检查。
目前错误检查的优先级较低,因为引擎通常不会依赖 OpenGL API 执行参数检查;否则,底层 OpenGL ES 实现所执行的检查通常已足够。