Javascript 高效地连接串行生成的大型阵列
我将许多长数组(80个数组,每个数组包含80000个元素,最终更多)组合成几个单个数组(8个数组,每个数组包含1600000个元素),作为属性上传到一个Three.js BufferGeometry中。我努力使这个过程有足够的效率,而不是冻结浏览器。我已经过了那个点,但它仍然慢得令人痛苦。是否有任何方法可以加快这一优化?我尝试使用Javascript 高效地连接串行生成的大型阵列,javascript,arrays,google-chrome,three.js,concatenation,Javascript,Arrays,Google Chrome,Three.js,Concatenation,我将许多长数组(80个数组,每个数组包含80000个元素,最终更多)组合成几个单个数组(8个数组,每个数组包含1600000个元素),作为属性上传到一个Three.js BufferGeometry中。我努力使这个过程有足够的效率,而不是冻结浏览器。我已经过了那个点,但它仍然慢得令人痛苦。是否有任何方法可以加快这一优化?我尝试使用push.apply,这大大加快了进程,但最终超出了调用堆栈。我目前正在使用concat,但不知道将进程转换为字符串或其他数据结构,然后再转换回来是否有帮助?其他想法?
push.apply
,这大大加快了进程,但最终超出了调用堆栈。我目前正在使用concat
,但不知道将进程转换为字符串或其他数据结构,然后再转换回来是否有帮助?其他想法?我洗耳恭听
以下是有问题的代码块:
var motifMinBufferSize = 80000;
function setMinimumBufferSize( pointCloudAttributeArray, itemSize, fillValue ) {
// buffers cannot be resized once they've been sent to the graphics card, so I am emulating resizing by setting a minimum buffer size that exceeds the number of vertex positions in the largest known point cloud.
supplementalArray.fill( fillValue );
var fullArray = pointCloudAttributeArray.concat( supplementalArray );
return fullArray;
}
function flattenVertexArray( array ) {
var flattenedArray = [];
for ( var i = 0; i < array.length; i++ ) {
flattenedArray.push( array[i].x, array[i].y, array[i].z );
}
return flattenedArray;
}
function concatArrays( gridArray, motifArray ) {
var newGridArray = [];
newGridArray = gridArray.concat( motifArray );
return newGridArray;
}
function compileGridOfPointCloudAttributes( ... ) {
...code to compile the attributes for a BufferGeometry representing a grid of point clouds...
// Skipping ahead in the function:
for ( var i = 0; i < 80; i++ ) {
...
// I have 8 of these attributes that gradually accumulate representing 80,000 values each for 80 different particle clouds:
var position = flattenVertexArray( motif.position );
var aGridPosition = flattenVertexArray ( motif.aGridPosition );
pointCloudGridAttributes.aPointCloudIDPerVertex = concatArrays( pointCloudGridAttributes.aMotifIDPerVertex, setMinimumBufferSize( motif.aPointCloudIDPerVertex, 1, 0 ) );
pointCloudGridAttributes.position = concatArrays( pointCloudGridAttributes.position, setMinimumBufferSize( position, 3, gridDimensions.gridWidth ) );
pointCloudGridAttributes.aGridPosition = concatArrays( pointCloudAttributes.aGridPosition, setMinimumBufferSize( motif.aGridPosition, 1, 0 ) );
...continue like this for 5 more attributes...
}
}
var-minbuffersize=80000;
函数setMinimumBufferSize(pointCloudAttributeArray、itemSize、fillValue){
//缓冲区发送到图形卡后无法调整大小,因此我通过设置最小缓冲区大小来模拟调整大小,该大小超过已知最大点云中的顶点位置数。
补充阵列填充(填充值);
var fullArray=pointCloudAttributeArray.concat(补充阵列);
返回完整数组;
}
函数展平顶点阵列(阵列){
var FlattedArray=[];
对于(var i=0;i
上下文:
我正在使用三个.js进行可视化,它们由80个左右的粒子云组成,每个粒子云都有唯一数量的点(每个云有50000多个点),并且都组成一个
BufferGeometry
,以实现高效渲染。我定期在点云上交换另一个点云,但了解到缓冲区几何体中的缓冲区在实现后无法调整大小,因此我现在有一个固定的、超大的数组部分专用于每个点云。不要创建临时javascript数组。看见