Skip to content

OriginalSin/regl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

regl

Join the chat at https://gitter.im/ark-lang/ark Circle CI js-standard-style npm version file size

regl is a fast functional reactive abstraction for WebGL.

Example

In regl, there are two fundamental abstractions, resources and commands:

  • A resource is a handle to a GPU resident object, like a texture, FBO or buffer.
  • A command is a complete representation of the WebGL state required to perform some draw call.

To define a command you specify a mixture of static and dynamic data for the object. Once this is done, regl takes this description and then compiles it into optimized JavaScript code. For example, here is a simple regl program to draw a colored triangle:

// Calling the regl module with no arguments creates a full screen canvas and
// WebGL context, and then uses this context to initialize a new REGL instance
const regl = require('regl')()

// Calling regl() creates a new partially evaluated draw command
const drawTriangle = regl({

  // Shaders in regl are just strings.  You can use glslify or whatever you want
  // to define them.  No need to manually create shader objects.
  frag: `
    precision mediump float;
    uniform vec4 color;
    void main() {
      gl_FragColor = color;
    }`,

  vert: `
    precision mediump float;
    attribute vec2 position;
    void main() {
      gl_Position = vec4(position, 0, 1);
    }`,

  // Here we define the vertex attributes for the above shader
  attributes: {
    // regl.buffer creates a new array buffer object
    position: regl.buffer([
      [-2, -2],   // no need to flatten nested arrays, regl automatically
      [4, -2],    // unrolls them into a typedarray (default Float32)
      [4,  4]
    ])
    // regl automatically infers sane defaults for the vertex attribute pointers
  },

  uniforms: {
    // This defines the color of the triangle to be a dynamic variable
    color: regl.prop('color')
  },

  // This tells regl the number of vertices to draw in this command
  count: 3
})

// regl.frame() wraps requestAnimationFrame and also handles viewport changes
regl.frame(({time}) => {
  // clear contents of the drawing buffer
  regl.clear({
    color: [0, 0, 0, 0],
    depth: 1
  })

  // draw a triangle using the command defined above
  drawTriangle({
    color: [
      Math.cos(time * 0.001),
      Math.sin(time * 0.0008),
      Math.cos(time * 0.003),
      1
    ]
  })
})

See this example live

More examples

Check out the gallery. The source code of all the gallery examples can be found here.

Setup

regl has no dependencies, so setting it up is pretty easy

Live editing

To try out regl right away, you can use the live editor in the gallery.

npm

The easiest way to use regl in a project is via npm. Once you have node set up, you can install and use regl in your project using the following command:

npm i -S regl

For more info on how to use npm, check out the official docs.

Standalone script tag

You can also use regl as a standalone script if you are really stubborn. The most recent versions can be found in the dist/ folder. Alternatively, you can directly import regl using npm cdn.

  • Unminified:
<script src="https://npmcdn.com/regl/dist/regl.js"></script>
  • Minified:
<script src="https://npmcdn.com/regl/dist/regl.min.js"></script>

Why use regl?

regl is basically all of WebGL without all of the shared state. You can do anything you could in regular WebGL with little overhead and way less debugging. Selling points of regl are:

  • regl makes it easy to load extensions and to adapt the program after the limits of the target device, and exposes many WebGL extensions for easy usage. See API.md for more info.
  • regl, in difference to many other WebGL frameworks, has support for easy usage of instanced rendering. See this example for more details.
  • regl integrates easily with modules from stack.gl, such gl-mat4 and gl-vec3.
  • regl is small and bloat-free; A minimized version of three.js is ~500Kb, while a minimized version of regl is only 71Kb.
  • regl has little overhead, and is near as fast as hand-optimized WebGL. You can compare the performance at the interactive benchmarks. The benchmark cube measures the performance of rendering a textured cube in regl, and cube-webgl does the same thing, but in raw WebGL. And cube-threejs does the same thing, but in three.js. In particular, notice how much faster regl is than three.js
  • regl performs strong error validation and sanity checking in debug builds. But for production builds of regl, all validation will be stripped away.

Comparisons

Benchmarks

You can run benchmarks locally using npm run bench or check them out here:

You can also check out our benchmarking results for the last couple of days:

The benchmarking results were created by using our custom scripts bench-history and bench-graph. You can read more about them in DEVELOPING.md.

Development

The latest changes in regl can be found in the CHANGELOG.

For info on how to build and test headless, see the contributing guide here

License

All code (c) 2016 MIT License

Development supported by the Freeman Lab and the Howard Hughes Medical Institute (@freeman-lab on GitHub)

Asset licenses

Many examples use creative commons or public domain artwork for illustrative purposes. These assets are not included in any of the redistributable packages of regl.

  • Test video (doggie-chromakey.ogv) by L0ckergn0me, used under creative commons license
  • Cube maps (posx.jpeg, negx.jpeg, posy.jpeg, negy.jpeg, posz.jpeg, negz.jpeg) by Humus, used under creative commons 3 license
  • Environment map of Oregon (ogd-oregon-360.jpg) due to Max Ogden (@maxogd on GitHub)
  • DDS test images (alpine_cliff_a, alpine_cliff_a_norm, alpine_cliff_a_spec) taken from the CC0 license 0-AD texture pack by Wildfire games
  • Tile set for tile mapping demo (tiles.png) from CC0 licensed cobblestone paths pack
  • Audio track for audio.js example is "Bamboo Cactus" by 8bitpeoples. CC BY-ND-NC 1.0 license

About

πŸ‘‘ Functional WebGL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 94.3%
  • TypeScript 4.6%
  • HTML 1.1%