Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Builds vertex and index arrays as needed for xeoEngine's color-indexed triangle picking.
/**
* Builds vertex and index arrays needed by color-indexed triangle picking.
*
* @method getPickPrimitives
* @static
* @param {Float32Array} positions One-dimensional flattened array of positions.
* @param {Float32Array} indices One-dimensional flattened array of indices.
* @returns {*} Object containing the arrays, created by this method or reused from 'pickTris' parameter.
*/
getPickPrimitives: function(positions, indices) {
var numIndices = indices.length;
var pickPositions = new Float32Array(numIndices * 30); // FIXME: Why do we need to extend size like this to make large meshes pickable?
var pickColors = new Float32Array(numIndices * 40);
var primIndex = 0;
// Positions array index
var vi;
// Picking positions array index
var pvi;
// Picking color array index
var pci;
// Triangle indices
var i;
var r;
var g;
var b;
var a;
for (var location = 0; location < numIndices; location += 3) {
pvi = location * 3;
pci = location * 4;
// Primitive-indexed triangle pick color
a = (primIndex >> 24 & 0xFF) / 255.0;
b = (primIndex >> 16 & 0xFF) / 255.0;
g = (primIndex >> 8 & 0xFF) / 255.0;
r = (primIndex & 0xFF) / 255.0;
// A
i = indices[location];
vi = i * 3;
pickPositions[pvi] = positions[vi];
pickPositions[pvi + 1] = positions[vi + 1];
pickPositions[pvi + 2] = positions[vi + 2];
pickColors[pci] = r;
pickColors[pci + 1] = g;
pickColors[pci + 2] = b;
pickColors[pci + 3] = a;
// B
i = indices[location + 1];
vi = i * 3;
pickPositions[pvi + 3] = positions[vi];
pickPositions[pvi + 4] = positions[vi + 1];
pickPositions[pvi + 5] = positions[vi + 2];
pickColors[pci + 4] = r;
pickColors[pci + 5] = g;
pickColors[pci + 6] = b;
pickColors[pci + 7] = a;
// C
i = indices[location + 2];
vi = i * 3;
pickPositions[pvi + 6] = positions[vi];
pickPositions[pvi + 7] = positions[vi + 1];
pickPositions[pvi + 8] = positions[vi + 2];
pickColors[pci + 8] = r;
pickColors[pci + 9] = g;
pickColors[pci + 10] = b;
pickColors[pci + 11] = a;
primIndex++;
}
return {
positions: pickPositions,
colors: pickColors
};
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment