MCPcopy
hub / github.com/gre/gl-react

github.com/gre/gl-react @6.0.0 sqlite

repository ↗ · DeepWiki ↗ · release 6.0.0 ↗
665 symbols 1,637 edges 184 files 12 documented · 2%
README

Build Status npm

icon gl-react (v6)

gl-react is a React library to write and compose WebGL shaders. Implement complex effects by composing React components.

This universal library must be coupled with one of the concrete implementations:

Links

References

They use gl-react 🙂

Gist

import React from "react";
import { Shaders, Node, GLSL } from "gl-react";
const shaders = Shaders.create({
  helloBlue: {
    frag: GLSL`
precision highp float;
varying vec2 uv;
uniform float blue;
void main() {
  gl_FragColor = vec4(uv.x, uv.y, blue, 1.0);
}`
  }
});
class HelloBlue extends React.Component {
  render() {
    const { blue } = this.props;
    return <Node shader={shaders.helloBlue} uniforms={{ blue }} />;
  }
}

import the correct implementation,

import { Surface } from "gl-react-dom"; // for React DOM
import { Surface } from "gl-react-expo"; // for React Native via Expo GLView
import { Surface } from "gl-react-native"; // for React Native
import { Surface } from "gl-react-headless"; // for Node.js!

and this code...

<Surface width={300} height={300}>
  <HelloBlue blue={0.5} />
</Surface>

...renders:

Usage with Vite

gl-react uses typedarray-pool which references Node.js's global. Add this to your vite.config.js:

export default defineConfig({
  // ...
  define: {
    global: "globalThis",
  },
});

Features

  • React, VDOM and immutable paradigm: OpenGL is a low level imperative and mutable API. This library takes the best of it and exposes it in an immutable, descriptive way with React.
  • React lifecycle allows partial GL re-rendering. Only a React Component update will trigger a redraw. Each Node holds a framebuffer state that get redrawn when component updates and schedule a Surface reflow.
  • Developer experience
  • React DevTools works like on DOM and allows you to inspect and debug your stack of effects.
  • Uniform bindings: bindings from JavaScript objects to OpenGL GLSL language types (bool, int, float, vec2, vec3, vec4, mat2, mat3, mat4, sampler2D...)
  • An extensible texture loader that allows to support any content that goes in the shader as a sampler2D texture.
  • support for images
  • support for videos (currently gl-react-dom)
  • support for canvas (gl-react-dom)
  • flowtype support.
  • Modular, Composable, Sharable. Write shaders once into components that you re-use everywhere! At the end, users don't need to write shaders.

Atom nice GLSL highlighting

If you are using Atom Editor, you can have JS inlined GLSL syntax highlighted.

To configure this:

  • add language-babel package.
  • Configure language-babel to add GLSL:source.glsl in settings "JavaScript Tagged Template Literal Grammar Extensions".
  • (Bonus) Add this CSS to your Atom > Stylesheet:
/* language-babel blocks */
atom-text-editor::shadow .line .ttl-grammar {
  /* NB: designed for dark theme. can be customized */
  background-color: rgba(0, 0, 0, 0.3);
}
atom-text-editor::shadow .line .ttl-grammar:first-child:last-child {
  display: block; /* force background to take full width only if ttl-grammar is alone in the line. */
}

Extension points exported contracts — how you extend this code

AnchorRef (Interface)
* AnchorRef holds a ref to the anchor DOM element and draw history data. * In the old code, Anchor was a React class co
packages/cookbook/src/components/GLInspector.tsx
VisitorLike (Interface)
(no doc) [2 implementers]
packages/gl-react/src/Visitor.ts
ExampleEntry (Interface)
(no doc)
packages/cookbook-expo/examples/index.ts
Example (Interface)
(no doc)
packages/cookbook/src/types/index.ts
Surface (Interface)
(no doc)
packages/gl-react/src/createSurface.tsx
NavigationItem (Interface)
(no doc)
packages/cookbook/src/types/index.ts
ShaderDefinition (Interface)
(no doc)
packages/gl-react/src/Shaders.ts
GLReactContextType (Interface)
(no doc)
packages/cookbook/src/types/index.ts

Core symbols most depended-on inside this repo

GLSL
called by 178
packages/gl-react/src/GLSL.ts
capture
called by 92
packages/gl-react/src/createSurface.tsx
expectToBeCloseToColorArray
called by 84
packages/tests/utils.js
flush
called by 77
packages/gl-react/src/createSurface.tsx
get
called by 40
packages/gl-react/src/types/webgltexture-loader.d.ts
create
called by 33
packages/tests/utils.js
update
called by 33
packages/gl-react/src/types/webgltexture-loader.d.ts
useTimeLoop
called by 23
packages/cookbook/src/hooks/useTimeLoop.ts

Shape

Function 334
Method 224
Class 84
Interface 23

Languages

TypeScript100%

Modules by API surface

packages/cookbook/src/components/GLInspector.tsx66 symbols
packages/tests/__tests__/all.js59 symbols
packages/gl-react/src/Node.tsx45 symbols
packages/tests/utils.js43 symbols
packages/gl-react/src/createSurface.tsx33 symbols
packages/gl-react/src/Visitor.ts27 symbols
packages/gl-react/src/Bus.tsx17 symbols
packages/gl-react-headless/src/index.tsx14 symbols
packages/gl-react/src/VisitorLogger.ts13 symbols
packages/gl-react/src/Shaders.ts13 symbols
packages/gl-react-dom/src/GLViewDOM.tsx11 symbols
packages/cookbook/src/controls/index.tsx10 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

@babel/cli7.26.0 · 1×
@babel/core7.26.0 · 1×
@babel/preset-env7.26.0 · 1×
@babel/preset-flow7.25.0 · 1×
@babel/preset-react7.26.0 · 1×
@babel/preset-typescript7.26.0 · 1×
@changesets/cli2.29.0 · 1×
@heroicons/react2.2.0 · 1×
@playwright/test1.58.2 · 1×
@react-native-community/cli19.0.0 · 1×
@types/babel__core7 · 1×

For agents

$ claude mcp add gl-react \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact