diff --git a/gulpfile.js b/gulpfile.js index 9db624b5..89a93c9d 100644 --- a/gulpfile.js +++ b/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); diff --git a/libs/plasio/js/laslaz.js b/libs/plasio/js/laslaz.js index c42bc7cc..6c1a7120 100644 --- a/libs/plasio/js/laslaz.js +++ b/libs/plasio/js/laslaz.js @@ -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) diff --git a/src/PointCloudOctree.js b/src/PointCloudOctree.js index b73e2702..0792cbe0 100644 --- a/src/PointCloudOctree.js +++ b/src/PointCloudOctree.js @@ -657,7 +657,9 @@ Potree.PointCloudOctree = class extends Potree.PointCloudTree{ */ pick(renderer, camera, ray, params = {}){ - let pickWindowSize = params.pickWindowSize || 101; + //let start = new Date().getTime(); + + let pickWindowSize = params.pickWindowSize || 17; let pickOutsideClipRegion = params.pickOutsideClipRegion || false; let width = Math.ceil(renderer.domElement.clientWidth); let height = Math.ceil(renderer.domElement.clientHeight); @@ -930,6 +932,10 @@ Potree.PointCloudOctree = class extends Potree.PointCloudTree{ } } + //let end = new Date().getTime(); + //let duration = end - start; + //console.log(`pick duration: ${duration}ms`); + return point; diff --git a/src/Potree.js b/src/Potree.js index 9d0299e9..ddccad98 100644 --- a/src/Potree.js +++ b/src/Potree.js @@ -307,6 +307,363 @@ function updateVisibilityStructures(pointclouds, camera, renderer){ }; } +/* +// +// WAY TOO SLOW WITH SYNCHRONOUS READ PIXEL +// +Potree.DEMRenderer = class{ + constructor(renderer){ + this.renderer = renderer; + + this.tileWidth = 64; + this.tileHeight = 64; + + //this.target = new THREE.WebGLRenderTarget( 64, 64, { + // minFilter: THREE.NearestFilter, + // magFilter: THREE.NearestFilter, + // format: THREE.RGBAFormat, + // type: THREE.FloatType + //} ); + //this.target.depthTexture = new THREE.DepthTexture(); + //this.target.depthTexture.type = THREE.UnsignedIntType; + + this.targetElevation = new THREE.WebGLRenderTarget( this.tileWidth, this.tileHeight, { + minFilter: THREE.NearestFilter, + magFilter: THREE.NearestFilter, + format: THREE.RGBAFormat, + //type: THREE.FloatType + }); + + this.targetMedian = new THREE.WebGLRenderTarget( this.tileWidth, this.tileHeight, { + minFilter: THREE.NearestFilter, + magFilter: THREE.NearestFilter, + format: THREE.RGBAFormat, + //type: THREE.FloatType + }); + + this.vsElevation = ` + precision mediump float; + precision mediump int; + + attribute vec3 position; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + varying float vElevation; + + void main(){ + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + gl_Position = projectionMatrix * mvPosition; + gl_PointSize = 1.0; + + vElevation = position.z; + } + `; + + this.fsElevation = ` + precision mediump float; + precision mediump int; + + varying float vElevation; + + void main(){ + gl_FragColor = vec4(vElevation, 0.0, 0.0, 1.0); + } + `; + + this.vsMedian = ` + precision mediump float; + precision mediump int; + + attribute vec3 position; + attribute vec2 uv; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + varying vec2 vUV; + + void main() { + vUV = uv; + + vec4 mvPosition = modelViewMatrix * vec4(position,1.0); + + gl_Position = projectionMatrix * mvPosition; + } + `; + + this.fsMedian = ` + + precision mediump float; + precision mediump int; + + uniform float uWidth; + uniform float uHeight; + uniform sampler2D uTexture; + + varying vec2 vUV; + + void main(){ + vec2 uv = gl_FragCoord.xy / vec2(uWidth, uHeight); + + vec4 color = texture2D(uTexture, uv); + gl_FragColor = color; + if(color.a == 0.0){ + + vec4 sum; + + float minVal = 1.0 / 0.0; + + float sumA = 0.0; + for(int i = -1; i <= 1; i++){ + for(int j = -1; j <= 1; j++){ + vec2 n = gl_FragCoord.xy + vec2(i, j); + vec2 uv = n / vec2(uWidth, uHeight); + vec4 c = texture2D(uTexture, uv); + + if(c.a == 1.0){ + minVal = min(c.r, minVal); + } + + sumA += c.a; + } + } + + if(sumA > 0.0){ + gl_FragColor = vec4(minVal, 0.0, 0.0, 1.0); + }else{ + discard; + } + }else{ + //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); + gl_FragColor = vec4(color.rgb, 1.0); + } + + + } + + `; + + this.elevationMaterial = new THREE.RawShaderMaterial( { + vertexShader: this.vsElevation, + fragmentShader: this.fsElevation, + } ); + + this.medianFilterMaterial = new THREE.RawShaderMaterial( { + uniforms: { + uWidth: {value: 1.0}, + uHeight: {value: 1.0}, + uTexture: {type: "t", value: this.targetElevation.texture} + }, + vertexShader: this.vsMedian, + fragmentShader: this.fsMedian, + }); + + this.camera = new THREE.OrthographicCamera(0, 1, 1, 0, 0, 1); + + + } + + render(pointcloud, node){ + if(!node.geometryNode){ + return; + } + + Potree.timerQueriesEnabled = true; + let start = new Date().getTime(); + let queryAll = Potree.startQuery("All", this.renderer.getContext()); + + this.renderer.setClearColor(0x0000FF, 0); + this.renderer.clearTarget( this.target, true, true, true ); + this.renderer.clearTarget(this.targetElevation, true, true, false ); + this.renderer.clearTarget(this.targetMedian, true, true, false ); + + let box = node.geometryNode.boundingBox; + + this.camera.up.set(0, 0, 1); + //this.camera.rotation.x = Math.PI / 2; + this.camera.left = box.min.x; + this.camera.right = box.max.x; + this.camera.top = box.max.y; + this.camera.bottom = box.min.y; + this.camera.near = -1000; + this.camera.far = 1000; + this.camera.updateProjectionMatrix(); + + let scene = new THREE.Scene(); + //let material = new THREE.PointsMaterial({color: 0x00ff00, size: 0.0001}); + let material = this.elevationMaterial; + let points = new THREE.Points(node.geometryNode.geometry, material); + scene.add(points); + + this.renderer.render(points, this.camera, this.targetElevation); + + this.medianFilterMaterial.uniforms.uWidth.value = this.targetMedian.width; + this.medianFilterMaterial.uniforms.uHeight.value = this.targetMedian.height; + this.medianFilterMaterial.uniforms.uTexture.value = this.targetElevation.texture; + + Potree.utils.screenPass.render(this.renderer, this.medianFilterMaterial, this.targetMedian); + + Potree.endQuery(queryAll, this.renderer.getContext()); + Potree.resolveQueries(this.renderer.getContext()); + Potree.timerQueriesEnabled = false; + + + setTimeout( () => { + let start = new Date().getTime(); + + let pixelCount = this.tileWidth * this.tileHeight; + let buffer = new Uint8Array(4 * pixelCount); + this.renderer.readRenderTargetPixels(this.targetMedian, + 0, 0, this.tileWidth, this.tileHeight, + buffer); + + let end = new Date().getTime(); + let duration = end - start; + console.log(`read duration: ${duration}ms`); + }, 3000); + + let end = new Date().getTime(); + let duration = end - start; + + console.log(`duration: ${duration}ms`); + + //{ // open window with image + // + // let pixelCount = this.tileWidth * this.tileHeight; + // let buffer = new Float32Array(4 * pixelCount); + // this.renderer.readRenderTargetPixels(this.targetMedian, + // 0, 0, this.tileWidth, this.tileHeight, + // buffer); + // + // let uiBuffer = new Uint8Array(4 * pixelCount); + // for(let i = 0; i < pixelCount; i++){ + // uiBuffer[i] = buffer[i] / 1.0; + // } + // + // let img = Potree.utils.pixelsArrayToImage(uiBuffer, this.tileWidth, this.tileHeight); + // let screenshot = img.src; + // + // if(!this.debugDIV){ + // this.debugDIV = $(` + //
`); + // $(document.body).append(this.debugDIV); + // } + // + // this.debugDIV.empty(); + // this.debugDIV.append($(`