gl-react
repository·master·Indexed 25 days ago
https://github.com/gre/gl-reactA universal React library for writing and composing WebGL shaders. It abstracts low-level imperative OpenGL calls into a declarative React component model. The library provides multiple implementation packages based on the target environment: gl-react-dom for web, gl-react-expo and gl-react-native for mobile, and gl-react-headless for Node.js/SSR. It includes utility packages such as gl-react-blur for Gaussian blur effects and gl-react-image for rendering images with various resize modes.
What's inside gl-react
- gl-react is a React library designed to write and compose WebGL shaders using a declarative, immutable paradigm. It allows you to implement complex visual effects by composing React components. Because it is a universal library, you must pair it with a specific implementation package depending on your target environment:
Overview of gl-react-headless
mastergl-react-headlessis a headless implementation ofgl-reactusingheadless-gl. It allows you to write and compose WebGL shaders in environments without a browser DOM. It is primarily used for running tests and performing server-side rendering (SSR) of shader-based compositions.Use gl-react-dom for WebGL shaders in React DOM
mastergl-react-domis the React DOM implementation ofgl-react. It allows you to write and compose WebGL shaders within a React application environment.Understand gl-react scope and limitations
mastergl-react is designed for composing fragment shaders for 2D effects on images, videos, and other 2D assets.
Limitations:
- No 3D support: Vertex shaders and vertex data are currently out of scope. It is not intended for 3D use-cases.
- Workaround: If you need arbitrary WebGL calls or 3D capabilities, you should use the WebGL API (in web) or
react-native-webgl(in React Native) and combine it with libraries like Three.js or PIXI.js.
Debug shaders with the WebGL Inspector
masterThe cookbook includes a WebGL Inspector designed for debugging shaders and uniforms. It provides:
- Real-time uniform values
- Shader source code display
- Performance metrics (FPS, draw calls, vertices)
- Toggle visibility for debugging
Install and use gl-react-image
masterUse
gl-react-imageto render images within agl-reactsurface. It provides variousresizeModeoptions implemented in OpenGL. It is compatible withgl-react-dom,gl-react-expo,gl-react-native, andgl-react-headlesssurfaces.Because
GLImagerenders aNode, it can be used as a texture input for other effects or as a source for effects.import { Surface } from "gl-react-dom"; // or gl-react-expo / gl-react-native / gl-react-headless import GLImage from "gl-react-image"; <Surface width={300} height={200}> <GLImage source="https://i.imgur.com/uTP9Xfr.jpg" resizeMode="cover" /> </Surface>;Install gl-react-native
masterTo use
gl-react-nativein a React Native project, you must first ensurereact-native-unimodulesis installed and configured according to its official documentation.Note: If you are using Expo, use
gl-react-expoinstead ofgl-react-native.Run the GL React Cookbook
masterYou can run the GL React Cookbook development server either from the monorepo root or directly from the package directory. The cookbook will be available at
http://localhost:3001.# From the root of the monorepo: pnpm install pnpm cookbook # Or directly from this package: cd packages/cookbook pnpm devEnable Camera support in gl-react-expo
masterTo use the device camera as a texture in your shaders, you must install
expo-cameraandwebgltexture-loader-expo-camera, then import the loader at your application's entry point.yarn add expo-camera webgltexture-loader-expo-camera// In your entry point file import "webgltexture-loader-expo-camera";Configure gl-react for use with Vite
masterBecause
gl-reactusestypedarray-pool, which relies on Node.js'sglobalobject, you must defineglobalasglobalThisin yourvite.config.jsto avoid errors.export default defineConfig({ // ... define: { global: "globalThis", }, });Install gl-react-expo
mastergl-react-expois the Expo implementation ofgl-react, allowing you to write and compose WebGL shaders in React Native via Expo.Select a gl-react implementation package
masterChoose the implementation package that matches your runtime environment:
- React DOM: Use
gl-react-domfor web applications. - React Native (Expo): Use
gl-react-expofor iOS/Android via Expo GLView. - React Native (Standard): Use
gl-react-nativefor iOS/Android via unimodules. - Node.js: Use
gl-react-headlessfor server-side or headless environments (backed byheadless-gl).
- React DOM: Use