quad-indices
Version:
creates the indices for a quad (two triangles)
42 lines (35 loc) • 1.14 kB
JavaScript
var dtype = require('dtype')
var anArray = require('an-array')
var isBuffer = require('is-buffer')
var CW = [0, 2, 3]
var CCW = [2, 1, 3]
module.exports = function createQuadElements(array, opt) {
//if user didn't specify an output array
if (!array || !(anArray(array) || isBuffer(array))) {
opt = array || {}
array = null
}
if (typeof opt === 'number') //backwards-compatible
opt = { count: opt }
else
opt = opt || {}
var type = typeof opt.type === 'string' ? opt.type : 'uint16'
var count = typeof opt.count === 'number' ? opt.count : 1
var start = (opt.start || 0)
var dir = opt.clockwise !== false ? CW : CCW,
a = dir[0],
b = dir[1],
c = dir[2]
var numIndices = count * 6
var indices = array || new (dtype(type))(numIndices)
for (var i = 0, j = 0; i < numIndices; i += 6, j += 4) {
var x = i + start
indices[x + 0] = j + 0
indices[x + 1] = j + 1
indices[x + 2] = j + 2
indices[x + 3] = j + a
indices[x + 4] = j + b
indices[x + 5] = j + c
}
return indices
}