gl-basic-mesh v2.0.1
gl-basic-mesh
A basic mesh wrapper, similar to gl-simplicial-complex but a little lower level. It creates a new shader with gl-basic-shader.
A typical use case may be rendering a triangulated 2D or 3D mesh with custom shader effects, or inheriting this class for your custom sprite or polygon batcher. For example, fontpath-gl utilizes this to generate a default shader and create glyph meshes.
Usage
var mesh = require('gl-basic-mesh')(gl, {
positions: new Float32Array([ -1, -1, 0, 0, ... ])
})
//you can access the gl-buffer like so:
mesh.positions.update( ... )
//bind the mesh and its associated shader
mesh.bind()
//draw with a red tint
mesh.shader.uniforms.tint = [1, 0, 0, 1]
mesh.draw()
mesh.unbind()
You can find a full demo in the demo folder.
mesh = createMesh(gl, options)
Creates a mesh where options can be:
positions
a data type (anything that gl-buffer accepts, eg. Float32Array) for vertex positionspositionSize
the number of position components per vertex, default 3 (for XYZ)colors
a data type for colorscolorSize
the number of color components per vertex, default 4normals
a data type for normals, expects 3 components per vertextexcoords
a data type for texcoords, expects 2 components per vertexcells
optional elements data type (eg. Uint16Array), if specified they will be used for drawingusage
the usage type for vertex attribuets, such asgl.DYNAMIC_DRAW
. defaults togl.STATIC_DRAW
shader
a shader this mesh will be associated with, otherwise a new default shader will be created
If a shader is not specified, a new one will be compiled with gl-basic-shader. If you plan on creating multiple meshes you should try to find a way to use a common shader.
If colors, normals or texcoords is not specified, no buffers or attributes will be set up for those. If positions is not set up, the position buffer will be an empty array, and you will be expected to fill it yourself with mesh.positions.update
.
mesh.bind()
Binds the mesh VAO and its associated shader. You can set shader uniforms after this.
mesh.unbind()
Unbinds the mesh VAO.
mesh.draw([mode, count, offset])
Draws the mesh with the given mode, primitive count, and offset. These are passed along to vao.draw(..)
.
The mode defaults to gl.TRIANGLES
if unspecified. The count defaults to the total vertex count (if no elements were specified) or total element count (if elements were specified). The offset defaults to zero.
mesh.dispose()
Disposes of this mesh VAO and VBOs. If you specified a shader in the constructor, it will not dispose of it; otherwise it will dispose of its defaultShader
.
default shader
The default shader is created with gl-basic-shader, which gives you a few features out of the box.
Attributes:
position
normal
if you specifiednormals
color
if you specifiedcolors
texcoord0
if you specifiedtexcoords
Uniforms:
tint
, defaults to whitetexture0
a sampler2D fortexcoord0
, if you specifiedtexcoords
model
mat4view
mat4projection
mat4
License
MIT, see LICENSE.md for details.