Git Product home page Git Product logo

gl-geometry's Introduction

gl-geometry experimental

A flexible wrapper for gl-vao and gl-buffer that you can use to set up renderable WebGL geometries from a variety of different formats.

Usage

NPM

geom = createGeometry(gl)

Creates a new geometry attached to the WebGL canvas context gl.

geom.attr(name, values)

Define a new attribute value, for example using a simplicial complex:

var createGeometry = require('gl-geometry')
var bunny = require('bunny')

var geom = createGeometry(gl)
  .attr('positions', bunny)

The following vertex formats are supported and will be normalized:

  • Arrays of arrays, e.g. [[0, 0, 0], [1, 0, 0], [1, 1, 0]].

  • Flat arrays, e.g. [0, 0, 0, 1, 0, 0, 1, 1, 0].

  • Typed arrays, preferably a Float32Array.

  • 1-dimensional ndarrays.

  • simplicial complexes, i.e. an object with a positions array and a cells array. The former is a list of unique vertices in the mesh (if you've used three.js, think THREE.Vector3), and the latter is an index mapping these vertices to faces (THREE.Face3) in the mesh. It looks something like this:

    {
      "positions": [
        [0.0, 0.0, 0.0],
        [1.5, 0.0, 0.0],
        [1.5, 1.5, 0.0],
        [0.0, 1.5, 0.0]
      ],
      "cells": [
        [0, 1, 2],
        [1, 2, 3]
      ]
    }

geom.faces(values)

Pass a simplicial complex's cells property here in any of the above formats to use it as your index when drawing the geometry. For example:

var createGeometry = require('gl-geometry')
var bunny = require('bunny')

bunny.normals = normals.vertexNormals(
    bunny.cells
  , bunny.positions
)

var geom = createGeometry(gl)
  .attr('positions', bunny.positions)
  .attr('normals', bunny.normals)
  .faces(bunny.cells)

geom.bind([shader])

Binds the underlying VAO โ€“ this must be called before calling geom.draw. Optionally, you can pass in a gl-shader or glslify shader instance to automatically set up your attribute locations for you.

geom.draw(mode, start, stop)

Draws the geometry to the screen using the currently bound shader.

Optionally, you can pass in the drawing mode, which should be one of the following:

  • gl.POINTS
  • gl.LINES
  • gl.LINE_STRIP
  • gl.LINE_LOOP
  • gl.TRIANGLES
  • gl.TRIANGLE_STRIP
  • gl.TRIANGLE_FAN

The default value is gl.TRIANGLES. You're also able to pass in a start and stop range for the points you want to render, just the same as you would with gl.drawArrays or gl.drawElements.

geom.unbind()

Unbinds the underlying VAO. This must be done when you're finished drawing, unless you're binding to another gl-geometry or gl-vao instance.

See Also

License

MIT. See LICENSE.md for details.

gl-geometry's People

Contributors

hughsk avatar

Stargazers

 avatar

Watchers

James Cloos avatar Matt DesLauriers 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.