Created
August 16, 2017 01:59
-
-
Save mattdesl/e399418558b2b52b58f5edeafea3c16c to your computer and use it in GitHub Desktop.
unindex + add barycentric coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import buffer from 'three-buffer-vertex-data'; | |
export default function addBarycentricCoordinates (bufferGeometry) { | |
const barycentric = []; | |
const attrib = bufferGeometry.getIndex() || bufferGeometry.getAttribute('position'); | |
const count = attrib.count / 3; | |
if (count % 2 !== 0) { | |
// Probably won't ever happen | |
console.error('WARN: Got a weird decimal result for attribute count, skipping wireframe...'); | |
return; | |
} | |
for (let i = 0; i < count; i++) { | |
barycentric.push( | |
0, 0, 1, | |
0, 1, 0, | |
1, 0, 0 | |
); | |
} | |
// attach barycentric attribute to geometry | |
buffer.attr(bufferGeometry, 'barycentric', barycentric, 3); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function unindexBufferGeometry (bufferGeometry) { | |
const index = bufferGeometry.getIndex(); | |
if (!index) return; // already un-indexed | |
const indexArray = index.array; | |
const triangleCount = indexArray.length / 3; | |
const attributes = bufferGeometry.attributes; | |
const newAttribData = Object.keys(attributes).map(key => { | |
return { | |
array: [], | |
attribute: bufferGeometry.getAttribute(key) | |
}; | |
}); | |
for (let i = 0; i < triangleCount; i++) { | |
// indices into attributes | |
const a = indexArray[i * 3 + 0]; | |
const b = indexArray[i * 3 + 1]; | |
const c = indexArray[i * 3 + 2]; | |
const indices = [ a, b, c ]; | |
// for each attribute, put vertex into unindexed list | |
newAttribData.forEach(data => { | |
const attrib = data.attribute; | |
const dim = attrib.itemSize; | |
// add [a, b, c] vertices | |
for (let i = 0; i < indices.length; i++) { | |
const index = indices[i]; | |
for (let d = 0; d < dim; d++) { | |
const v = attrib.array[index * dim + d]; | |
data.array.push(v); | |
} | |
} | |
}); | |
} | |
index.array = null; | |
bufferGeometry.setIndex(null); | |
// now copy over new data | |
newAttribData.forEach(data => { | |
const newArray = new data.attribute.array.constructor(data.array); | |
data.attribute.setArray(newArray); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment