• Stars
    star
    785
  • Rank 57,560 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal WebGL 2 rendering library

PicoGL.js

Build Status Coverage Status GZIP size Gitter License NPM

API Docs | Tutorial | Chat

PicoGL.js is a minimal WebGL 2 rendering library. It's meant for developers who understand the WebGL 2 rendering pipeline and want to use it, but with a more convenient API. Typical usage of PicoGL.js will involve creating programs, vertex buffers, vertex arrays, uniform buffers, framebuffers, textures, transform feedbacks, and combining them into draw calls.

    // Create App which manages all GL state
    let app = PicoGL.createApp(canvas)
    .clearColor(0.0, 0.0, 0.0, 1.0);
    
    // Create Program
    // Shaders are compiled in parallel if supported by the platform.
    app.createPrograms([vertexShaderSource, fragmentShaderSource]).then(([program]) => {
        // Create a buffer of vertex attributes
        let positions = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
            -0.5, -0.5,
             0.5, -0.5,
             0.0,  0.5
        ]));

        // VertexArray manages attribute buffer state
        let vertexArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions);

        // UniformBuffer allows multiple uniforms to be bound
        // as a single block of memory.
        // First part defines layout of the UniformBuffer.
        // Second part updates values.
        let uniformBuffer = app.createUniformBuffer([
            PicoGL.FLOAT_VEC4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, new Float32Array([1.0, 0.0, 0.0, 0.3]))
        .set(1, new Float32Array([0.0, 0.0, 1.0, 0.7]))
        .update();

        // Create DrawCall from Program and VertexArray (both required),
        // and a UniformBuffer.
        let drawCall = app.createDrawCall(program, vertexArray)
        .uniformBlock("ColorUniforms", uniformBuffer);

        // Draw
        app.clear();
        drawCall.draw();
    });

Note that PicoGL.js is not a scene graph library. There are no objects, hierarchies, transforms, materials, etc. It has been designed only to make management of GPU state more convenient. Its conceptual model maps fairly directly to the constructs one deals with when writing directly with the WebGL 2 API. The only higher-level construct is the draw call, which manages sets of related lower-level constructs.

Usage

PicoGL.js can be used directly by downloading the built source and loading it via a script tag:

    <script src="js/picogl.min.js"></script>

or it can be installed via npm:

    npm install picogl

and loaded via ES6-style import:

    import PicoGL from "picogl";

Features

PicoGL.js simplifies usage of some more complex WebGL 2 features, such as multiple render targets, uniform buffers, transform feedback and instanced drawing.

Multiple Render Targets

    let app = PicoGL.createApp(canvas)
    .clearColor(0.0, 0.0, 0.0, 1.0);


    // Texture render targets
    let colorTarget0 = app.createTexture2D(app.width, app.height);
    let colorTarget1 = app.createTexture2D(app.width, app.height);
    let depthTarget = app.createTexture2D(app.width, app.height, {
        internalFormat: PicoGL.DEPTH_COMPONENT16
    });


    // Create framebuffer with color targets at attachments 
    // 0 and 1, and a depth target.
    let framebuffer = app.createFramebuffer()
    .colorTarget(0, colorTarget0)
    .colorTarget(1, colorTarget1)
    .depthTarget(depthTarget);
    
    // ... set up programs and vertex arrays for offscreen and
    // main draw passes...
    
    let offscreenDrawCall = app.createDrawCall(offscreenProgram, offscreenVAO);

    // Bind main program texture samplers to framebuffer targets
    let mainDrawCall = app.createDrawCall(mainProgram, mainVAO)
    .texture("texture1", framebuffer.colorAttachments[0])
    .texture("texture2", framebuffer.colorAttachments[1])
    .texture("depthTexture", framebuffer.depthAttachment);

    // Offscreen pass
    app.drawFramebuffer(framebuffer).clear();
    offscreenDrawCall.draw();
    
    // Main draw pass
    app.defaultDrawFramebuffer().clear()
    mainDrawCall.draw();

Uniform Buffers

    let app = PicoGL.createApp(canvas)
    .clearColor(0.0, 0.0, 0.0, 1.0);
    
    // ... set up program and vertex array...

    // Layout is std140
    let uniformBuffer = app.createUniformBuffer([
        PicoGL.FLOAT_MAT4,
        PicoGL.FLOAT_VEC4,
        PicoGL.INT_VEC4,
        PicoGL.FLOAT
    ])
    .set(0, matrix)
    .set(1, float32Vector)
    .set(2, int32Vector)
    .set(3, scalar)
    .update();      // Data only sent to GPU when update() is called

    let drawCall = app.createDrawCall(program, vertexArray)
    .uniformBlock("UniformBlock", uniformBuffer);

Transform Feedback

    let app = PicoGL.createApp(canvas)
    .clearColor(0.0, 0.0, 0.0, 1.0);

    // Last argument is transform feedback varyings
    app.createPrograms([vertexShaderSource, fragmentShaderSource, ["vPosition"]]).then(([program]) => {
        let positions1 = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
            -0.5, -0.5,
             0.5, -0.5,
             0.0,  0.5
        ]));
        let vertexArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions1);

        // Empty destination buffer of 6 floats
        let positions2 = app.createVertexBuffer(PicoGL.FLOAT, 2, 6);  

        // Capture transform results into positions2 buffer
        let transformFeedback = app.createTransformFeedback()
        .feedbackBuffer(0, positions2);

        let drawCall = app.createDrawCall(program, vertexArray)
        .transformFeedback(transformFeedback);

        app.clear();
        drawCall.draw();
    });

Instanced Drawing

    let app = PicoGL.createApp(canvas)
    .clearColor(0.0, 0.0, 0.0, 1.0);

    // The starting positions of the triangle. Each pair of coordinates
    // will be passed per-vertex
    let positions = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
        -0.3, -0.3,
         0.3, -0.3,
         0.0,  0.3
    ]));

    // This is an instance buffer meaning each pair of numbers will be passed
    // per-instance, rather than per-vertex
    let offsets = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
        -0.5, 0.0,
         0.0, 0.2,
         0.5, 0.0
    ]));

    // This vertex array is set up to draw 3 instanced triangles 
    // with the offsets given above
    let vertexArray = app.createVertexArray()
    .vertexAttributeBuffer(0, positions); // Pass positions per-vertex
    .instanceAttributeBuffer(1, offset); // Pass offsets per-instance

More Repositories

1

space-shooter.c

A cross-platform, top-down 2D space shooter written in C using only platform libraries.
C
1,331
star
2

webgl2examples

Rendering algorithms implemented in raw WebGL 2.
HTML
525
star
3

simple-opengl-loader

An extensible, cross-platform, single-header C/C++ OpenGL loader library.
C
85
star
4

webgpu-examples

Rendering algorithms implemented in WebGPU.
JavaScript
76
star
5

glcheck

A testing framework for WebGL 1 and 2 applications
JavaScript
62
star
6

nano-server

An ultra-lightweight node.js HTTP server for web development.
JavaScript
60
star
7

mercator-gl

Tiny library for GLSL web mercator projections
JavaScript
44
star
8

xogl

Minimal OpenGL loader for X11.
C
23
star
9

cervit

Minimal, multi-threaded POSIX HTTP 1.1 server written in C using only system libraries.
C
19
star
10

scroll-em

A JavaScript library for creating scrolling animations.
JavaScript
18
star
11

xaudio2-c-demo

A small example of using the Windows XAudio2 API in C
C
18
star
12

gl-utils

A bare-bones WebGL library.
JavaScript
16
star
13

tesseract-explorer

Interactive visualization of a 4-dimensional tesseract
JavaScript
15
star
14

mesh-quantization-example

A minimal example of vertex quantization.
JavaScript
11
star
15

weekendraytracerjs

JavaScript port of the path tracing algorithm from Peter Shirley's "Ray Tracing in One Weekend"
JavaScript
11
star
16

gaza-data

Open JSON and CSV formatted data documenting the Palestinian and Israeli lives lost in the July 2014 attack on Gaza.
JavaScript
6
star
17

sketches

Daily graphics sketches
C
6
star
18

mesh-compression-examples

JavaScript
5
star
19

oFactory

A simple JavaScript library for creating factories.
JavaScript
4
star
20

picogl-tutorial

PicoGL.js tutorial source code.
HTML
3
star
21

webglx

Unified context API for WebGL 1 and 2.
JavaScript
3
star
22

simple-live-server

A mininal HTTP server with live reload capabilities.
TypeScript
2
star
23

climb

An HTML5 minimalist vertical platforming game.
JavaScript
2
star
24

khronos-webgl-workshop1

CSS
2
star
25

nywebperf-biodigital

CSS
2
star
26

in-gaza

An interactive memorial to the Gazans killed during Israeli Operation Protective Edge, July 2014.
JavaScript
2
star
27

siggraph-2018

CSS
1
star
28

tgame

A very basic JavaScript game engine.
JavaScript
1
star
29

space-music

JavaScript
1
star
30

nanogl.js

Moved to https://github.com/tsherif/picogl.js
HTML
1
star
31

egypt-population

Population density map of Egypt
JavaScript
1
star
32

tareksherif

Code for tareksherif.net
JavaScript
1
star
33

webgl2bugs

Minimal examples of bugs found in WebGL 2
HTML
1
star
34

asm-experiments

Random assembly sketches
Assembly
1
star