Giter Site home page Giter Site logo

mr47 / gl-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gre/gl-react

1.0 2.0 0.0 104.81 MB

[gl-react v3 alpha] – a React library to write and compose WebGL shaders

Home Page: https://gl-react-cookbook.surge.sh

License: MIT License

JavaScript 94.65% HTML 0.19% CSS 3.05% Shell 1.05% GLSL 1.06%

gl-react's Introduction

NB: This is gl-react v3. For gl-react v2, please see ProjectSeptemberInc/gl-react.

gre/gl-react repository is a complete rewrite of ProjectSeptemberInc/gl-react library (gl-react v2). It plans to be the gl-react v3. We keep both repository at same time because (1) this work is not yet finished and (2) this repository is now a "multi libraries" repository (it's just easier to maintain that way).

See also this article: http://greweb.me/2016/12/gl-react-v3/ .

v3 alpha: development in progress

  • gl-react, universal implementation
  • gl-react-dom, DOM implementation
  • gl-react-headless, Node.js implementation
    • tests: 100% test coverage!
  • gl-react-native, React Native implementation

The main remaining work of v3 is the React Native implementation:

The long term plan is to rely on Exponent's WebGL implementation to implement the WebGL layer. The implementation is still very young and experimental (only implement a subset of WebGL), but as soon as this implementation guarantees a good conformance, the library should just work! I encourage everyone to contribute to make Exponent WebGL implementation robust, independently from the library you use at the end (Three.js / Pixi.js / regl / gl-react / whatever!).

Here is the parts we would like to focus on solving in that implementation:

  • Support for framebuffers. This is fundamental for gl-react.
  • interoperability with React Native Image source prop (basically should support same format as a input for gl.texImage2D)
  • The WebGL implementation should be a standalone implementation that we can depend as a library (shouldn't requires you to use Exponent if you just use React Native).
  • interoperability with more "pixel sources" (Video, Camera, ...)

Other remaining topics:

  • Flow type support: we are waiting the next flow version that should bring WebGL type support: facebook/flow#2764 .
  • make inspector a Chrome plugin.
  • the Game of Life example, when inspected AND enabling snapshot capture, seems to memleak.
  • Node should expose a more generic escape hatch to use gl and do any WebGL stuff. The main problem is most WebGL libs are intrusive and alter the gl state (e.g. they don't expect to just draw in the current bounded framebuffer).
  • more than ~12 simultaneous canvas won't work in web, because WebGL don't allow that. an idea to workaround that is to implement a <SurfaceContainer>, a super surface, that discover the underlying Surface, and will position an absolute canvas that render all surface in one... we can use "gl scissor" for that. inspiration: multi-regl.
  • re-using fbo in the tree, is a complex idea which might be premature optim', but it might make sense in some cases. We can introduce a new prop in Node that says it should not hold its own Framebuffer copy (which make it non-cachable).

icon gl-react

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

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-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:

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.
  • headless tests with Jest. We have reached 99.9% test coverage!
  • 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. */
}

gl-react's People

Contributors

gre avatar mr47 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.