diff --git a/TODO.txt b/TODO.txt index 2ad86b73..5198b9f4 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,11 +4,7 @@ TODO orthographic cameras: TODO clipping: -- polygon clipping - - vertices are distorted at the boundaries of the render area (caused by perspective projection) - - marker spheres not equally big (depneds on camera mode and distance) - - EDL renderer: no depth of volumes stored -> no depth test possible -- clip planes +- polygon clipping / clip planes / measurement volumes - EDL renderer: no depth of volumes stored -> no depth test possible TODO general: diff --git a/src/utils/ClippingTool.js b/src/utils/ClippingTool.js index 2e0fc143..cc9b22b4 100644 --- a/src/utils/ClippingTool.js +++ b/src/utils/ClippingTool.js @@ -22,6 +22,7 @@ Potree.ClippingTool = class ClippingTool extends THREE.EventDispatcher{ }); }); + this.sceneMarker = new THREE.Scene(); this.sceneVolume = new THREE.Scene(); this.sceneVolume.name = "scene_clip_volume"; this.viewer.inputHandler.registerInteractiveScene(this.sceneVolume); @@ -124,7 +125,8 @@ Potree.ClippingTool = class ClippingTool extends THREE.EventDispatcher{ this.dispatchEvent({"type": "start_inserting_clipping_volume"}); - this.viewer.scene.addPolygonClipVolume(polyClipVol); + //this.viewer.scene.addPolygonClipVolume(polyClipVol); + this.sceneMarker.add(polyClipVol); let cancel = { callback: null @@ -150,16 +152,28 @@ Potree.ClippingTool = class ClippingTool extends THREE.EventDispatcher{ }; cancel.callback = e => { + this.sceneMarker.remove(polyClipVol); if(polyClipVol.markers.length > 3) { polyClipVol.removeLastMarker(); + for(let i = 0; i < polyClipVol.markers.length; i++) { + polyClipVol.markers[i].position.copy(polyClipVol.markersPosWorld[i]); + polyClipVol.markers[i].visible = false; + polyClipVol.remove(polyClipVol.edges[i]); + } + polyClipVol.edges = []; + for(let i = 0; i < polyClipVol.markers.length - 1; i++) { + polyClipVol.addEdge(i, i+1); + } polyClipVol.addEdge(polyClipVol.markers.length - 1, 0); - polyClipVol.initialized = true; + polyClipVol.addExtrudedEdges(); + polyClipVol.initialized = true; + this.viewer.scene.addPolygonClipVolume(polyClipVol); } else { this.viewer.scene.removePolygonClipVolume(polyClipVol); } + this.viewer.renderer.domElement.removeEventListener("mouseup", insertionCallback, true); this.viewer.removeEventListener("cancel_insertions", cancel.callback); - polyClipVol.addExtrudedEdges(); this.viewer.inputHandler.enabled = true; }; diff --git a/src/utils/PolygonClipVolume.js b/src/utils/PolygonClipVolume.js index 4fa229fe..14b5edc5 100644 --- a/src/utils/PolygonClipVolume.js +++ b/src/utils/PolygonClipVolume.js @@ -12,9 +12,10 @@ Potree.PolygonClipVolume = class extends THREE.Object3D{ this.viewMatrix = this.camera.matrixWorldInverse.clone(); this.projMatrix = this.camera.projectionMatrix.clone(); this.markers = []; + this.markersPosWorld = []; this.edges = []; this.extrudedEdges = []; - this.sphereGeometry = new THREE.SphereGeometry(0.01, 5, 5); + this.sphereGeometry = new THREE.SphereGeometry(0.005, 10, 10); this.color = new THREE.Color( 0xff0000 ); this.initialized = false; @@ -47,35 +48,27 @@ Potree.PolygonClipVolume = class extends THREE.Object3D{ (e.drag.end.x / e.viewer.renderer.domElement.clientWidth) * 2 - 1, -(e.drag.end.y / e.viewer.renderer.domElement.clientHeight) * 2 + 1, 0.5); + + let posWorld = pos.clone(); let camera = e.viewer.scene.getActiveCamera(); if(camera.isPerspectiveCamera) { - pos.unproject(camera); - var dir = pos.sub(camera.position).normalize(); + posWorld.unproject(camera); + var dir = posWorld.clone().sub(camera.position).normalize(); var distance = -camera.position.z / dir.z; - pos = camera.position.clone().add( dir.multiplyScalar(1)); + posWorld = camera.position.clone().add( dir.multiplyScalar(1)); } else { - pos.unproject(camera); - pos.add(camera.getWorldDirection().clone().multiplyScalar(-(Math.abs(camera.near) - 1))); - let depthRange = camera.far - camera.near; + posWorld.unproject(camera); + posWorld.add(camera.getWorldDirection().clone().multiplyScalar(-(Math.abs(camera.near) - 1))); } + this.markersPosWorld[this.markersPosWorld.length - 1].copy(posWorld); + let width = e.viewer.scaleFactor * e.viewer.renderArea.clientWidth; + let height = e.viewer.scaleFactor * e.viewer.renderArea.clientHeight; + let aspect = width / height; + pos.y = pos.y * 1/aspect; + pos.z = -pos.z; marker.position.copy(pos); }; - - let addEdge = (start, end) => { - let lineGeometry = new THREE.Geometry(); - lineGeometry.vertices.push(this.markers[start].position, this.markers[end].position); - lineGeometry.colors.push(this.color, this.color, this.color); - let lineMaterial = new THREE.LineBasicMaterial( { - linewidth: 1 - }); - lineMaterial.depthTest = false; - let edge = new THREE.Line(lineGeometry, lineMaterial); - edge.visible = true; - - this.add(edge); - this.edges.push(edge); - }; let drop = e => { cancel.callback(); @@ -89,6 +82,8 @@ Potree.PolygonClipVolume = class extends THREE.Object3D{ marker.addEventListener("drag", drag); marker.addEventListener("drop", drop); + + this.markersPosWorld.push(new THREE.Vector3(0, 0, 0)); this.markers.push(marker); this.add(marker); } @@ -112,6 +107,7 @@ Potree.PolygonClipVolume = class extends THREE.Object3D{ if(this.markers.length > 0) { this.remove(this.markers[this.markers.length - 1]); this.markers.splice(this.markers.length - 1, 1); + this.markersPosWorld.splice(this.markersPosWorld.length -1, 1); } } @@ -119,22 +115,23 @@ Potree.PolygonClipVolume = class extends THREE.Object3D{ let dir = this.camera.getWorldDirection().normalize(); let camPos = this.camera.getWorldPosition(); for(let i = 0; i < this.markers.length; i++) { - let frontMarker = this.markers[i].position.clone(); + //let frontMarker = this.markers[i].position.clone(); let backMarker = this.markers[i].position.clone(); if(this.camera.isOrthographicCamera) { - frontMarker.add(dir.clone().multiplyScalar(1000)); - backMarker.add(dir.clone().multiplyScalar(-1000)); + //frontMarker.add(dir.clone().multiplyScalar(1000)); + backMarker.add(dir.clone().multiplyScalar(1000)); } else { let markerDir = backMarker.clone().sub(camPos); backMarker.add(markerDir.clone().multiplyScalar(1000)); - frontMarker.copy(camPos); + //frontMarker.copy(camPos); } let lineGeometry = new THREE.Geometry(); - lineGeometry.vertices.push(frontMarker, backMarker); - lineGeometry.colors.push(this.color, this.color, this.color); + lineGeometry.vertices.push(this.markers[i].position, backMarker); + //lineGeometry.colors.push(this.color, this.color, this.color); let lineMaterial = new THREE.LineBasicMaterial( { - linewidth: 1 + linewidth: 1, + color: this.color }); lineMaterial.depthWrite = false; lineMaterial.depthTest = true; diff --git a/src/viewer/viewer.js b/src/viewer/viewer.js index 1079e86c..969ede5d 100644 --- a/src/viewer/viewer.js +++ b/src/viewer/viewer.js @@ -139,6 +139,7 @@ Potree.Scene = class extends THREE.EventDispatcher{ this.cameraP = new THREE.PerspectiveCamera(this.fov, 1, 0.1, 1000*1000); this.cameraO = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 1000*1000); this.cameraBG = new THREE.Camera(); + this.cameraScreenSpace = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10); this.cameraMode = Potree.CameraMode.PERSPECTIVE; this.pointclouds = []; this.referenceFrame; @@ -374,6 +375,7 @@ Potree.Scene = class extends THREE.EventDispatcher{ this.cameraO.position.set(1000, 1000, 1000); //this.camera.rotation.y = -Math.PI / 4; //this.camera.rotation.x = -Math.PI / 6; + this.cameraScreenSpace.lookAt(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -1), new THREE.Vector3(0, 1, 0)); this.directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 ); this.directionalLight.position.set( 10, 10, 10 ); @@ -1644,11 +1646,8 @@ Potree.Viewer = class PotreeViewer extends THREE.EventDispatcher{ }); let clipPolygons = this.scene.polygonClipVolumes.filter(vol => vol.initialized).map(vol => { - let vertices = vol.markers.map(marker => { - return marker.position; - }); let vp = vol.projMatrix.clone().multiply(vol.viewMatrix); - return {polygon: vertices, count: vertices.length, view: vp}; + return {polygon: vol.markersPosWorld, count: vol.markersPosWorld.length, view: vp}; }); for(let pointcloud of this.scene.pointclouds){ @@ -1769,6 +1768,10 @@ class PotreeRenderer{ viewer.scene.cameraO.bottom = -frustumScale * 1/aspect; viewer.scene.cameraO.updateProjectionMatrix(); + viewer.scene.cameraScreenSpace.top = 1/aspect; + viewer.scene.cameraScreenSpace.bottom = -1/aspect; + viewer.scene.cameraScreenSpace.updateProjectionMatrix(); + // update frustum size for adaptive point size with ortho cameras for(let pointcloud of viewer.scene.pointclouds){ pointcloud.material.orthoRange = 2.0 * frustumScale; @@ -1993,6 +1996,7 @@ class PotreeRenderer{ viewer.renderer.render(viewer.volumeTool.sceneVolume, activeCam); viewer.clippingTool.update(); + viewer.renderer.render(viewer.clippingTool.sceneMarker, viewer.scene.cameraScreenSpace); //viewer.scene.cameraScreenSpace); viewer.renderer.render(viewer.clippingTool.sceneVolume, activeCam); viewer.renderer.render(viewer.controls.sceneControls, activeCam); @@ -2075,6 +2079,10 @@ class EDLRenderer{ viewer.scene.cameraO.top = frustumScale * 1/aspect; viewer.scene.cameraO.bottom = -frustumScale * 1/aspect; viewer.scene.cameraO.updateProjectionMatrix(); + + viewer.scene.cameraScreenSpace.top = 1/aspect; + viewer.scene.cameraScreenSpace.bottom = -1/aspect; + viewer.scene.cameraScreenSpace.updateProjectionMatrix(); viewer.renderer.setSize(width, height); this.rtColor.setSize(width, height);