improve build for workers
This commit is contained in:
parent
f49b0d3ac7
commit
5e67da4041
39
gulpfile.js
39
gulpfile.js
@ -88,23 +88,26 @@ var paths = {
|
||||
};
|
||||
|
||||
var workers = {
|
||||
"laslaz": [
|
||||
"LASLAZWorker": [
|
||||
"libs/plasio/workers/laz-perf.js",
|
||||
"libs/plasio/workers/laz-loader-worker.js"
|
||||
],
|
||||
"LASDecoder": [
|
||||
"LASDecoderWorker": [
|
||||
"src/workers/LASDecoderWorker.js"
|
||||
],
|
||||
"BinaryDecoder": [
|
||||
"BinaryDecoderWorker": [
|
||||
"src/workers/BinaryDecoderWorker.js",
|
||||
"src/Version.js",
|
||||
"src/loader/PointAttributes.js"
|
||||
],
|
||||
"GreyhoundBinaryDecoder": [
|
||||
"GreyhoundBinaryDecoderWorker": [
|
||||
"libs/plasio/workers/laz-perf.js",
|
||||
"src/workers/GreyhoundBinaryDecoderWorker.js",
|
||||
"src/Version.js",
|
||||
"src/loader/PointAttributes.js"
|
||||
],
|
||||
"DEMWorker": [
|
||||
"src/workers/DEMWorker.js"
|
||||
]
|
||||
};
|
||||
|
||||
@ -121,25 +124,16 @@ var shaders = [
|
||||
|
||||
|
||||
gulp.task("workers", function(){
|
||||
gulp.src(workers.laslaz)
|
||||
.pipe(encodeWorker('laslaz-worker.js', "Potree.workers.laslaz"))
|
||||
.pipe(size({showFiles: true}))
|
||||
.pipe(gulp.dest('build/potree/workers'));
|
||||
|
||||
gulp.src(workers.LASDecoder)
|
||||
.pipe(encodeWorker('lasdecoder-worker.js', "Potree.workers.lasdecoder"))
|
||||
.pipe(size({showFiles: true}))
|
||||
.pipe(gulp.dest('build/potree/workers'));
|
||||
for(let workerName of Object.keys(workers)){
|
||||
|
||||
gulp.src(workers[workerName])
|
||||
.pipe(concat(`${workerName}.js`))
|
||||
.pipe(size({showFiles: true}))
|
||||
.pipe(gulp.dest('build/potree/workers'));
|
||||
|
||||
}
|
||||
|
||||
gulp.src(workers.BinaryDecoder)
|
||||
.pipe(encodeWorker('BinaryDecoderWorker.js', "Potree.workers.binaryDecoder"))
|
||||
.pipe(size({showFiles: true}))
|
||||
.pipe(gulp.dest('build/potree/workers'));
|
||||
|
||||
gulp.src(workers.GreyhoundBinaryDecoder)
|
||||
.pipe(encodeWorker('GreyhoundBinaryDecoderWorker.js', "Potree.workers.greyhoundBinaryDecoder"))
|
||||
.pipe(size({showFiles: true}))
|
||||
.pipe(gulp.dest('build/potree/workers'));
|
||||
});
|
||||
|
||||
gulp.task("shaders", function(){
|
||||
@ -214,7 +208,7 @@ gulp.task('webserver', function() {
|
||||
});
|
||||
|
||||
|
||||
var encodeWorker = function(fileName, varname, opt){
|
||||
var encodeWorker = function(fileName, opt){
|
||||
if (!fileName) throw new PluginError('gulp-concat', 'Missing fileName option for gulp-concat');
|
||||
if (!opt) opt = {};
|
||||
if (!opt.newLine) opt.newLine = gutil.linefeed;
|
||||
@ -236,7 +230,6 @@ var encodeWorker = function(fileName, varname, opt){
|
||||
if (buffer.length === 0) return this.emit('end');
|
||||
|
||||
var joinedContents = buffer.join("");
|
||||
//var content = varname + " = new Potree.WorkerManager(atob(\"" + new Buffer(joinedContents).toString('base64') + "\"));";
|
||||
let content = joinedContents;
|
||||
|
||||
var joinedPath = path.join(firstFile.base, fileName);
|
||||
|
||||
@ -206,7 +206,7 @@
|
||||
var LAZLoader = function(arraybuffer) {
|
||||
this.arraybuffer = arraybuffer;
|
||||
|
||||
let workerPath = Potree.scriptPath + "/workers/laslaz-worker.js";
|
||||
let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
|
||||
this.ww = Potree.workerPool.getWorker(workerPath);
|
||||
|
||||
this.nextCB = null;
|
||||
@ -274,7 +274,7 @@
|
||||
|
||||
return new Promise(function(res, rej) {
|
||||
o.dorr({type:'close'}, function(r) {
|
||||
let workerPath = Potree.scriptPath + "/workers/laslaz-worker.js";
|
||||
let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
|
||||
Potree.workerPool.returnWorker(workerPath, o.ww);
|
||||
|
||||
if (r.status !== 1)
|
||||
|
||||
@ -657,7 +657,7 @@ Potree.PointCloudOctree = class extends Potree.PointCloudTree{
|
||||
*/
|
||||
pick(renderer, camera, ray, params = {}){
|
||||
|
||||
let pickWindowSize = params.pickWindowSize || 101;
|
||||
let pickWindowSize = params.pickWindowSize || 17;
|
||||
let pickOutsideClipRegion = params.pickOutsideClipRegion || false;
|
||||
let width = Math.ceil(renderer.domElement.clientWidth);
|
||||
let height = Math.ceil(renderer.domElement.clientHeight);
|
||||
|
||||
127
src/Potree.js
127
src/Potree.js
@ -307,6 +307,102 @@ function updateVisibilityStructures(pointclouds, camera, renderer){
|
||||
};
|
||||
}
|
||||
|
||||
Potree.getDEMWorkerInstance = function(){
|
||||
if(!Potree.DEMWorkerInstance){
|
||||
let workerPath = Potree.scriptPath + "/workers/DEMWorker.js";
|
||||
Potree.DEMWorkerInstance = Potree.workerPool.getWorker(workerPath);
|
||||
}
|
||||
|
||||
return Potree.DEMWorkerInstance;
|
||||
}
|
||||
|
||||
|
||||
function demHeight(pointcloud, position){
|
||||
|
||||
if(!pointcloud.root.dem){
|
||||
return;
|
||||
}
|
||||
|
||||
let node = pointcloud.root;
|
||||
let dem = pointcloud.root.dem;
|
||||
let demHeights = new Float32Array(dem.data);
|
||||
let boxSize = pointcloud.boundingBox.getSize();
|
||||
|
||||
let u = (position.x - pointcloud.position.x) / boxSize.x;
|
||||
let v = (position.y - pointcloud.position.y) / boxSize.y;
|
||||
|
||||
let demX = u * dem.width;
|
||||
let demY = v * dem.height;
|
||||
|
||||
let clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
||||
let sample = (x, y) => {
|
||||
x = parseInt(clamp(x, 0, dem.width - 1));
|
||||
y = parseInt(clamp(y, 0, dem.height - 1));
|
||||
let i = x + dem.width * y;
|
||||
|
||||
return demHeights[i];
|
||||
};
|
||||
|
||||
let p00 = sample(Math.floor(demX), Math.floor(demY));
|
||||
let p10 = sample(Math.ceil(demX), Math.floor(demY));
|
||||
let p01 = sample(Math.floor(demX), Math.ceil(demY));
|
||||
let p11 = sample(Math.ceil(demX), Math.ceil(demY));
|
||||
|
||||
let value =
|
||||
p00 * (demX % 1) * (demY % 1) +
|
||||
p10 * (1 - demX % 1) * (demY % 1) +
|
||||
p01 * (demX % 1) * (1 - demY % 1) +
|
||||
p11 * (1 - demX % 1) * (1 - demY % 1);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function createDemMesh(){
|
||||
let pointcloud = viewer.scene.pointclouds[0];
|
||||
let triangles = [];
|
||||
let steps = 200;
|
||||
for(let i = 0; i <= steps; i++){
|
||||
for(let j = 0; j <= steps; j++){
|
||||
let u0 = i / steps;
|
||||
let u1 = (i+1) / steps;
|
||||
let v0 = j / steps;
|
||||
let v1 = (j+1) / steps;
|
||||
|
||||
let x0 = pointcloud.position.x + u0 * pointcloud.boundingBox.getSize().x;
|
||||
let x1 = pointcloud.position.x + u1 * pointcloud.boundingBox.getSize().x;
|
||||
let y0 = pointcloud.position.y + v0 * pointcloud.boundingBox.getSize().y;
|
||||
let y1 = pointcloud.position.y + v1 * pointcloud.boundingBox.getSize().y;
|
||||
|
||||
let h00 = demHeight(pointcloud, new THREE.Vector2(x0, y0));
|
||||
let h10 = demHeight(pointcloud, new THREE.Vector2(x1, y0));
|
||||
let h01 = demHeight(pointcloud, new THREE.Vector2(x0, y1));
|
||||
let h11 = demHeight(pointcloud, new THREE.Vector2(x1, y1));
|
||||
|
||||
if(![h00, h10, h01, h11].every(n => isFinite(n))){
|
||||
continue;
|
||||
}
|
||||
|
||||
triangles.push(x0, y0, h00);
|
||||
triangles.push(x0, y1, h01);
|
||||
triangles.push(x1, y0, h10);
|
||||
|
||||
triangles.push(x0, y1, h01);
|
||||
triangles.push(x1, y1, h11);
|
||||
triangles.push(x1, y0, h10);
|
||||
}
|
||||
}
|
||||
|
||||
let geometry = new THREE.BufferGeometry();
|
||||
let positions = new Float32Array(triangles);
|
||||
geometry.addAttribute( 'position', new THREE.BufferAttribute(positions, 3));
|
||||
geometry.computeBoundingSphere();
|
||||
geometry.computeVertexNormals();
|
||||
let material = new THREE.MeshNormalMaterial({side: THREE.DoubleSide});
|
||||
let mesh = new THREE.Mesh(geometry, material);
|
||||
//mesh.position.copy(pointcloud.position);
|
||||
viewer.scene.scene.add(mesh);
|
||||
}
|
||||
|
||||
Potree.updateVisibility = function(pointclouds, camera, renderer){
|
||||
let numVisibleNodes = 0;
|
||||
let numVisiblePoints = 0;
|
||||
@ -316,7 +412,7 @@ Potree.updateVisibility = function(pointclouds, camera, renderer){
|
||||
let unloadedGeometry = [];
|
||||
|
||||
let lowestSpacing = Infinity;
|
||||
|
||||
|
||||
// calculate object space frustum and cam pos and setup priority queue
|
||||
let s = updateVisibilityStructures(pointclouds, camera, renderer);
|
||||
let frustums = s.frustums;
|
||||
@ -416,6 +512,35 @@ Potree.updateVisibility = function(pointclouds, camera, renderer){
|
||||
}else if(!pointcloud.showBoundingBox && node.boundingBoxNode){
|
||||
node.boundingBoxNode.visible = false;
|
||||
}
|
||||
|
||||
if(!node.dem){
|
||||
if(!Potree.getDEMWorkerInstance().working){
|
||||
Potree.getDEMWorkerInstance().onmessage = (e) => {
|
||||
|
||||
node.dem = e.data.dem;
|
||||
|
||||
Potree.getDEMWorkerInstance().working = false;
|
||||
};
|
||||
|
||||
let position = node.geometryNode.geometry.attributes.position.array;
|
||||
|
||||
let message = {
|
||||
boundingBox: {
|
||||
min: node.getBoundingBox().min.toArray(),
|
||||
max:node.getBoundingBox().max.toArray()
|
||||
},
|
||||
position: new Float32Array(position).buffer
|
||||
};
|
||||
|
||||
let transferables = [message.position];
|
||||
|
||||
Potree.getDEMWorkerInstance().working = true;
|
||||
|
||||
Potree.getDEMWorkerInstance().postMessage(message, transferables);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// add child nodes to priorityQueue
|
||||
|
||||
@ -152,7 +152,7 @@ Potree.LasLazLoader.prototype.handle = function(node, url){
|
||||
Potree.LasLazBatcher = function(node){
|
||||
this.push = function(lasBuffer){
|
||||
|
||||
let workerPath = Potree.scriptPath + "/workers/lasdecoder-worker.js";
|
||||
let workerPath = Potree.scriptPath + "/workers/LASDecoderWorker.js";
|
||||
let worker = Potree.workerPool.getWorker(workerPath);
|
||||
|
||||
var mins = new THREE.Vector3(lasBuffer.mins[0], lasBuffer.mins[1], lasBuffer.mins[2]);
|
||||
|
||||
49
src/workers/DEMWorker.js
Normal file
49
src/workers/DEMWorker.js
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
|
||||
onmessage = function(event){
|
||||
let start = new Date().getTime();
|
||||
|
||||
let boundingBox = event.data.boundingBox;
|
||||
let position = new Float32Array(event.data.position);
|
||||
let width = 64;
|
||||
let height = 64;
|
||||
let numPoints = position.length / 3;
|
||||
|
||||
let boxSize = {
|
||||
x: boundingBox.max[0] - boundingBox.min[0],
|
||||
y: boundingBox.max[1] - boundingBox.min[1],
|
||||
z: boundingBox.max[2] - boundingBox.min[2]
|
||||
};
|
||||
|
||||
let dem = new Float32Array(width * height);
|
||||
dem.fill(-Infinity);
|
||||
for(let i = 0; i < numPoints; i++){
|
||||
let x = position[3*i + 0];
|
||||
let y = position[3*i + 1];
|
||||
let z = position[3*i + 2];
|
||||
|
||||
let dx = x / boxSize.x;
|
||||
let dy = y / boxSize.y;
|
||||
|
||||
let ix = parseInt(Math.min(width * dx, width - 1));
|
||||
let iy = parseInt(Math.min(height * dy, height - 1));
|
||||
|
||||
let index = ix + width * iy;
|
||||
dem[index] = z;
|
||||
}
|
||||
|
||||
let message = {
|
||||
dem: {
|
||||
width: width,
|
||||
height: height,
|
||||
data: dem.buffer
|
||||
}
|
||||
};
|
||||
|
||||
let end = new Date().getTime();
|
||||
let duration = end - start;
|
||||
//console.log(`duration: ${duration}ms`);
|
||||
|
||||
postMessage(message, [message.dem.data]);
|
||||
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user