gl-geometry
Advanced tools
+38
-32
@@ -8,5 +8,6 @@ var normalize = require('./normalize') | ||
| function GLGeometry(gl) { | ||
| if (!(this instanceof GLGeometry)) | ||
| function GLGeometry (gl) { | ||
| if (!(this instanceof GLGeometry)) { | ||
| return new GLGeometry(gl) | ||
| } | ||
@@ -17,3 +18,4 @@ this._elementsType = 5123 | ||
| this._dirty = true | ||
| this._length = 0 | ||
| this._attrLength = 0 | ||
| this._facesLength = 0 | ||
| this._index = null | ||
@@ -25,3 +27,3 @@ this._vao = null | ||
| GLGeometry.prototype.dispose = function() { | ||
| GLGeometry.prototype.dispose = function () { | ||
| for (var i = 0; i < this._attributes.length; i++) { | ||
@@ -33,3 +35,4 @@ this._attributes[i].buffer.dispose() | ||
| this._keys = [] | ||
| this._length = 0 | ||
| this._attrLength = 0 // Length of this attribute (the number of vertices it feeds) | ||
| this._facesLength = 0 // Number of vertices needed to draw all faces | ||
| this._dirty = true | ||
@@ -48,3 +51,3 @@ | ||
| GLGeometry.prototype.faces = function faces(attr, opts) { | ||
| GLGeometry.prototype.faces = function faces (attr, opts) { | ||
| var size = opts && opts.size || 3 | ||
@@ -66,3 +69,3 @@ attr = attr.cells ? attr.cells : attr | ||
| this._length = this._index.length * size | ||
| this._facesLength = this._index.length * size | ||
| this._index = this._index.buffer | ||
@@ -73,3 +76,8 @@ | ||
| GLGeometry.prototype.attr = function attr(name, attr, opts) { | ||
| GLGeometry.prototype.attr = function attr (name, attr, opts) { | ||
| // If we get a simplicial complex | ||
| if (attr.cells && attr.positions) { | ||
| return this.attr(name, attr.positions).faces(attr.cells, opts) | ||
| } | ||
| opts = opts || {} | ||
@@ -85,4 +93,4 @@ this._dirty = true | ||
| throw new Error( | ||
| 'Unexpected attribute format: needs an ndarray, array, typed array, ' | ||
| + 'gl-buffer or simplicial complex' | ||
| 'Unexpected attribute format: needs an ndarray, array, typed array, ' + | ||
| 'gl-buffer or simplicial complex' | ||
| ) | ||
@@ -93,22 +101,23 @@ } | ||
| var length = attribute.length | ||
| var index = attribute.index | ||
| this._keys.push(name) | ||
| this._attributes.push({ | ||
| size: size | ||
| , buffer: buffer | ||
| size: size, | ||
| buffer: buffer | ||
| }) | ||
| if (first) { | ||
| this._length = length | ||
| this._attrLength = length | ||
| } else | ||
| if (this._attrLength !== length) { | ||
| throw new Error( | ||
| 'Unexpected discrepancy in attributes size (was ' + this._attrLength + | ||
| ', now ' + length + ')' | ||
| ) | ||
| } | ||
| if (first && index) { | ||
| this._index = index | ||
| } | ||
| return this | ||
| } | ||
| GLGeometry.prototype.bind = function bind(shader) { | ||
| GLGeometry.prototype.bind = function bind (shader) { | ||
| this.update() | ||
@@ -118,2 +127,4 @@ this._vao.bind() | ||
| if (!this._keys) return | ||
| if (!shader) return | ||
| for (var i = 0; i < this._keys.length; i++) { | ||
@@ -124,10 +135,8 @@ var attr = shader.attributes[this._keys[i]] | ||
| if (!shader) return | ||
| shader.bind() | ||
| } | ||
| GLGeometry.prototype.draw = function draw(mode, start, stop) { | ||
| GLGeometry.prototype.draw = function draw (mode, start, stop) { | ||
| start = typeof start === 'undefined' ? 0 : start | ||
| stop = typeof stop === 'undefined' ? this._length : stop | ||
| mode = typeof mode === 'undefined' ? this.gl.TRIANGLES : mode | ||
| mode = typeof mode === 'undefined' ? this.gl.TRIANGLES : mode | ||
@@ -137,4 +146,6 @@ this.update() | ||
| if (this._vao._useElements) { | ||
| this.gl.drawElements(mode, stop - start, this._elementsType, start * this._elementsBytes) // "2" is sizeof(uint16) | ||
| stop = typeof stop === 'undefined' ? this._facesLength : stop | ||
| this.gl.drawElements(mode, stop - start, this._elementsType, start * this._elementsBytes) | ||
| } else { | ||
| stop = typeof stop === 'undefined' ? this._attrLength : stop | ||
| this.gl.drawArrays(mode, start, stop - start) | ||
@@ -144,3 +155,3 @@ } | ||
| GLGeometry.prototype.unbind = function unbind() { | ||
| GLGeometry.prototype.unbind = function unbind () { | ||
| this.update() | ||
@@ -150,3 +161,3 @@ this._vao.unbind() | ||
| GLGeometry.prototype.update = function update() { | ||
| GLGeometry.prototype.update = function update () { | ||
| if (!this._dirty) return | ||
@@ -156,8 +167,3 @@ this._dirty = false | ||
| this._vao = createVAO( | ||
| this.gl | ||
| , this._attributes | ||
| , this._index | ||
| ) | ||
| this._vao = createVAO(this.gl, this._attributes, this._index) | ||
| this._elementsType = this._vao._elementsType | ||
@@ -164,0 +170,0 @@ this._elementsBytes = dtype( |
+21
-35
@@ -1,15 +0,15 @@ | ||
| var pack = require('array-pack-2d') | ||
| var ista = require('is-typedarray') | ||
| var pack = require('array-pack-2d') | ||
| var ista = require('is-typedarray') | ||
| var createBuffer = require('gl-buffer') | ||
| var isnd = require('isndarray') | ||
| var dtype = require('dtype') | ||
| var isnd = require('isndarray') | ||
| var dtype = require('dtype') | ||
| module.exports = normalize | ||
| function normalize(gl, attr, size, mode, type) { | ||
| function normalize (gl, attr, size, mode, type) { | ||
| // if we get a nested 2D array | ||
| if (Array.isArray(attr) && Array.isArray(attr[0])) { | ||
| return { | ||
| buffer: createBuffer(gl, pack(attr, type), mode) | ||
| , length: attr.length | ||
| buffer: createBuffer(gl, pack(attr, type), mode), | ||
| length: attr.length | ||
| } | ||
@@ -21,4 +21,4 @@ } | ||
| return { | ||
| buffer: createBuffer(gl, pack(attr, type), mode) | ||
| , length: ( attr.length * attr[0].length ) / size | ||
| buffer: createBuffer(gl, pack(attr, type), mode), | ||
| length: (attr.length * attr[0].length) / size | ||
| } | ||
@@ -30,4 +30,4 @@ } | ||
| return { | ||
| buffer: createBuffer(gl, new (dtype(type))(attr), mode) | ||
| , length: attr.length / size | ||
| buffer: createBuffer(gl, new (dtype(type))(attr), mode), | ||
| length: attr.length / size | ||
| } | ||
@@ -39,24 +39,12 @@ } | ||
| return { | ||
| buffer: attr | ||
| , length: attr.length / size / 4 | ||
| buffer: attr, | ||
| length: attr.length / size / 4 | ||
| } | ||
| } | ||
| // if we get a simplicial complex | ||
| if (attr.cells && attr.positions) { | ||
| return { | ||
| length: attr.cells.length * size | ||
| , buffer: createBuffer(gl, pack(attr.positions, type), mode) | ||
| , index : createBuffer(gl | ||
| , pack(attr.cells, 'uint16') | ||
| , gl.ELEMENT_ARRAY_BUFFER | ||
| ) | ||
| } | ||
| } | ||
| // if we get an ndarray | ||
| if (isnd(attr)) { | ||
| return { | ||
| buffer: createBuffer(gl, attr, mode) | ||
| , length: ndlength(attr.shape) / size | ||
| buffer: createBuffer(gl, attr, mode), | ||
| length: ndlength(attr.shape) / size | ||
| } | ||
@@ -72,4 +60,4 @@ } | ||
| return { | ||
| buffer: createBuffer(gl, attr, mode) | ||
| , length: attr.length / size | ||
| buffer: createBuffer(gl, attr, mode), | ||
| length: attr.length / size | ||
| } | ||
@@ -79,14 +67,12 @@ } | ||
| function ndlength(shape) { | ||
| function ndlength (shape) { | ||
| var length = 1 | ||
| for (var i = 0; i < shape.length; i++) | ||
| length *= shape[i] | ||
| for (var i = 0; i < shape.length; i++) length *= shape[i] | ||
| return length | ||
| } | ||
| function convert(a, b) { | ||
| b = new b(a.length) | ||
| function convert (a, B) { | ||
| var b = new B(a.length) | ||
| for (var i = 0; i < a.length; i++) b[i] = a[i] | ||
| return b | ||
| } |
+9
-2
| { | ||
| "name": "gl-geometry", | ||
| "version": "2.0.0", | ||
| "version": "3.0.0", | ||
| "description": "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.", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "start": "budo test.js --open -- -t glslify" | ||
| "start": "budo test.js --open -- -t glslify", | ||
| "test": "standard" | ||
| }, | ||
| "standard": { | ||
| "globals": [ | ||
| "WebGLBuffer" | ||
| ] | ||
| }, | ||
| "author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughsk.io/)", | ||
@@ -33,2 +39,3 @@ "license": "MIT", | ||
| "normals": "^0.1.0", | ||
| "standard": "^5.4.1", | ||
| "unindex-mesh": "0.0.0" | ||
@@ -35,0 +42,0 @@ }, |
+1
-1
@@ -64,3 +64,3 @@ # gl-geometry [](http://github.com/badges/stability-badges) | ||
| Pass a simplicial complex's `cells` property here in any of the above formats | ||
| to use it as your index when drawing the geometry. This can only be called after all of the `.attr` calls. For example: | ||
| to use it as your index when drawing the geometry. For example: | ||
@@ -67,0 +67,0 @@ ``` javascript |
+37
-30
@@ -1,19 +0,18 @@ | ||
| var createCamera = require('canvas-orbit-camera') | ||
| var mat4 = require('gl-matrix').mat4 | ||
| var pack = require('array-pack-2d') | ||
| var unindex = require('unindex-mesh') | ||
| var faceNormals = require('face-normals') | ||
| var createCamera = require('canvas-orbit-camera') | ||
| var mat4 = require('gl-matrix').mat4 | ||
| var pack = require('array-pack-2d') | ||
| var unindex = require('unindex-mesh') | ||
| var faceNormals = require('face-normals') | ||
| var createContext = require('gl-context') | ||
| var fit = require('canvas-fit') | ||
| var ndarray = require('ndarray') | ||
| var normals = require('normals') | ||
| var glslify = require('glslify') | ||
| var bunny = require('bunny') | ||
| var createShader = require('gl-shader') | ||
| var ndarray = require('ndarray') | ||
| var normals = require('normals') | ||
| var glslify = require('glslify') | ||
| var bunny = require('bunny') | ||
| var createShader = require('gl-shader') | ||
| var createGeom = require('./') | ||
| var clear = require('gl-clear')({ | ||
| color: [0xF0/255, 0xF1/255, 0xF2/255, 1] | ||
| , depth: true | ||
| , stencil: false | ||
| var createGeom = require('./') | ||
| var clear = require('gl-clear')({ | ||
| color: [0xF0 / 255, 0xF1 / 255, 0xF2 / 255, 1], | ||
| depth: true, | ||
| stencil: false | ||
| }) | ||
@@ -24,3 +23,3 @@ | ||
| var scNor = normals.vertexNormals(bunny.cells, bunny.positions) | ||
| createExample(scPos, scNor) | ||
| createExample(scPos, scNor).title = 'Simplicial Complex' | ||
@@ -30,3 +29,3 @@ // handles Float32Arrays | ||
| var uiNor = faceNormals(uiPos) | ||
| createExample(uiPos, uiNor) | ||
| createExample(uiPos, uiNor).title = 'Unindexed Mesh, Float32Arrays' | ||
@@ -36,20 +35,23 @@ // handles (flat) ndarrays | ||
| var ndNor = ndarray(uiNor, [uiNor.length]) | ||
| createExample(ndPos, ndNor) | ||
| createExample(ndPos, ndNor).title = 'Flat ndarrays' | ||
| // also supports .faces() method | ||
| createExample(scPos.positions, scNor, scPos.cells) | ||
| createExample(scPos.positions, scNor, scPos.cells).title = '.faces(), Last Call' | ||
| // also supports .faces() method with packed data | ||
| createExample(pack(scPos.positions), scNor, pack(scPos.cells)) | ||
| createExample(pack(scPos.positions), scNor, pack(scPos.cells)).title = '.faces(), Packed Data' | ||
| function createExample(pos, norm, cells) { | ||
| var canvas = document.body.appendChild(document.createElement('canvas')) | ||
| var gl = createContext(canvas, render) | ||
| var camera = createCamera(canvas) | ||
| // .faces(), order-independant | ||
| createExample(scPos.positions, scNor, scPos.cells, true).title = '.faces(), First Call' | ||
| function createExample (pos, norm, cells, facesFirst) { | ||
| var canvas = document.body.appendChild(document.createElement('canvas')) | ||
| var gl = createContext(canvas, render) | ||
| var camera = createCamera(canvas) | ||
| var projection = mat4.create() | ||
| var shader = createShader(gl, | ||
| var shader = createShader(gl, | ||
| glslify('./test.vert'), | ||
| glslify('./test.frag') | ||
| ) | ||
| canvas.width = 300 | ||
@@ -60,9 +62,12 @@ canvas.height = 300 | ||
| var geom = createGeom(gl) | ||
| if (cells && facesFirst) geom.faces(cells) | ||
| geom | ||
| .attr('position', pos) | ||
| .attr('normal', norm) | ||
| if (cells) geom.faces(cells) | ||
| if (cells && !facesFirst) geom.faces(cells) | ||
| function render() { | ||
| var width = canvas.width | ||
| function render () { | ||
| var width = canvas.width | ||
| var height = canvas.height | ||
@@ -91,2 +96,4 @@ | ||
| } | ||
| return canvas | ||
| } |
14870
2.67%14
7.69%266
-0.75%