gl-react

repository·master·Indexed 25 days ago

https://github.com/gre/gl-react

A 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.

Tokens
7.1K
Snippets
10
Records
59
Agent score
83%

What's inside gl-react

  1. Overview of gl-react

    master
    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:
  2. Understand gl-react scope and limitations

    master

    gl-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.
  3. Install and use gl-react-image

    master

    Use gl-react-image to render images within a gl-react surface. It provides various resizeMode options implemented in OpenGL. It is compatible with gl-react-dom, gl-react-expo, gl-react-native, and gl-react-headless surfaces.

    Because GLImage renders a Node, 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>;
  4. Run the GL React Cookbook

    master

    You 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 dev
  5. Enable Camera support in gl-react-expo

    master

    To use the device camera as a texture in your shaders, you must install expo-camera and webgltexture-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";
  6. Configure gl-react for use with Vite

    master

    Because gl-react uses typedarray-pool, which relies on Node.js's global object, you must define global as globalThis in your vite.config.js to avoid errors.

    export default defineConfig({
      // ...
      define: {
        global: "globalThis",
      },
    });
  7. Select a gl-react implementation package

    master

    Choose the implementation package that matches your runtime environment:

    • React DOM: Use gl-react-dom for web applications.
    • React Native (Expo): Use gl-react-expo for iOS/Android via Expo GLView.
    • React Native (Standard): Use gl-react-native for iOS/Android via unimodules.
    • Node.js: Use gl-react-headless for server-side or headless environments (backed by headless-gl).