@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
42 lines (31 loc) • 1.29 kB
JavaScript
import { BufferAttribute } from "three";
/**
*
* @param {THREE.InterleavedBufferAttribute|THREE.BufferAttribute} source_attribute
* @returns {THREE.BufferAttribute}
*/
export function buffer_attribute_deinterleave(source_attribute) {
if (source_attribute.isInterleavedBufferAttribute !== true && source_attribute.isBufferAttribute) {
// already isn't interleaved
return source_attribute;
}
const source_data = source_attribute.data;
const source_data_array = source_data.array;
const stride = source_data.stride;
const item_count = source_attribute.count;
const itemSize = source_attribute.itemSize;
const array_size = item_count * itemSize;
const offset = source_attribute.offset;
const destination_array = new source_data_array.constructor(array_size);
const attribute = new BufferAttribute(destination_array, itemSize);
attribute.normalized = source_attribute.normalized;
attribute.name = source_attribute.name;
let i = 0, j = 0;
// copy data
for (; i < item_count; i++) {
for (j = 0; j < itemSize; j++) {
destination_array[i * itemSize + j] = source_data_array[i * stride + offset + j];
}
}
return attribute;
}