Something went wrong. Try again.
Utility to sort THREE.js BufferGeometry
Something went wrong. Try again.
three-buffergeometry-sort index.js
2.2 kB · 65 lines
JavaScript
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566module.exports = (function () { "use strict";
var THREE = require("three");
var BufferGeometrySorter = function (frameSkip) { this.frameSkip = frameSkip || 0; };
BufferGeometrySorter.prototype.sort = (function () { var distances = [], tmpPos = new THREE.Vector3(0, 0, 0), currFrame = 0; // This will result in the first frame getting sorted
return function (bufferGeomAttributes, cameraPosition) { if (currFrame > 0) { if (currFrame > this.frameSkip) { // Render next frame currFrame = 0; } else { currFrame += 1; } return; } currFrame += 1;
var attributes = bufferGeomAttributes, numPoints = attributes.position.count;
for (var i = 0; i < numPoints; ++i) { tmpPos.set( attributes.position.array[i * 3], attributes.position.array[i * 3 + 1], attributes.position.array[i * 3 + 2] ); distances[i] = [ cameraPosition.distanceTo(tmpPos), i]; } distances.sort(function(a, b) { return b[0] - a[0]; });
for (var val in attributes) { if (!attributes.hasOwnProperty(val)) { continue; }
var itemSize = attributes[val].itemSize; var newArray = new Float32Array(itemSize * numPoints);
for (i = 0; i < numPoints; ++i){ var index = distances[i][1]; for (var j = 0; j < itemSize; ++j) { var srcIndex = index * itemSize + j; var dstIndex = i * itemSize + j; newArray[dstIndex] = attributes[val].array[srcIndex]; } }
attributes[val].array = newArray; attributes[val].needsUpdate = true; } }; }());
return BufferGeometrySorter;}());