three-bvh-csg
Version:
A fast, flexible, dynamic CSG implementation on top of three-mesh-bvh
73 lines (40 loc) • 1.46 kB
JavaScript
import { BufferAttribute } from 'three';
export function areSharedArrayBuffersSupported() {
return typeof SharedArrayBuffer !== 'undefined';
}
export function convertToSharedArrayBuffer( array ) {
if ( array.buffer instanceof SharedArrayBuffer ) {
return array;
}
const cons = array.constructor;
const buffer = array.buffer;
const sharedBuffer = new SharedArrayBuffer( buffer.byteLength );
const uintArray = new Uint8Array( buffer );
const sharedUintArray = new Uint8Array( sharedBuffer );
sharedUintArray.set( uintArray, 0 );
return new cons( sharedBuffer );
}
export function getIndexArray( vertexCount, BufferConstructor = ArrayBuffer ) {
if ( vertexCount > 65535 ) {
return new Uint32Array( new BufferConstructor( 4 * vertexCount ) );
} else {
return new Uint16Array( new BufferConstructor( 2 * vertexCount ) );
}
}
export function ensureIndex( geo, options ) {
if ( ! geo.index ) {
const vertexCount = geo.attributes.position.count;
const BufferConstructor = options.useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer;
const index = getIndexArray( vertexCount, BufferConstructor );
geo.setIndex( new BufferAttribute( index, 1 ) );
for ( let i = 0; i < vertexCount; i ++ ) {
index[ i ] = i;
}
}
}
export function getVertexCount( geo ) {
return geo.index ? geo.index.count : geo.attributes.position.count;
}
export function getTriCount( geo ) {
return getVertexCount( geo ) / 3;
}