Static indexed mesh drawing for WebGL
WebGL class for rendering static indexed geometry
var shell = require"gl-now"var createMesh = require"gl-mesh"var simple2DShader = require"simple-2d-shader"var mesh shadershellon"gl-init"shader = simple2DShadershellglmesh = createMeshshellgl0 1 22 1 3"position": -1-1 0 1 0 0 1 -1"color": 1 0 0 0 1 0 0 0 1 1 1 1shellon"gl-render"shaderbindmeshbindshadermeshdrawmeshunbind
And here is what it should look like:
Use npm to install it locally:
npm install gl-mesh
var createMesh = require"gl-mesh"
Creates a static mesh.
glis a webgl context
cellsis a list of representing indices into the geometry
attributesis an object of attributes to pass to the mesh
Each of these objects can be encoded as either an array-of-native-arrays or as a typed array using ndarrays. The first dimension in the shape is interepreted as the number of vertices in the attribute while the second dimension is interpreted as the size. For example, to pass in a packed array of 3d vertices in a typed array you could do:
var mesh = createMeshgl cells "positions": ndarrayposition_data numVertices 3
The drawing mode for the mesh is determined by the shape of the cells according to the following rule:
cells.length == 0: empty mesh
cells.length == 1:
cells.length == 2:
cells.length == 3:
You can also skip the
cells parameter, in which case the resulting mesh is drawn as a point cloud.
Also you can pass a single object with a
cells field. For example, here is the quickest way to create a Stanford bunny test mesh:
var bunnyMesh = createMeshgl require"bunny"
Where the module comes from the
Mesh object has the following methods:
Binds the mesh to the given shader updating attributes accordingly.
shaderis an instance of a shader created using
Draws an instance of the mesh once it is bound to a shader
Unbinds the mesh releasing the current vertex attribute state
Destroys the mesh and releases all of its associated resources
(c) 2013 Mikola Lysenko. MIT License