From d124576f7df0de96bf2832e278773e2986564678 Mon Sep 17 00:00:00 2001 From: Lukas Gersthofer Date: Sat, 15 Jul 2017 15:38:01 +0200 Subject: [PATCH] polygon clipping works now (surprisingly efficient) --- TODO.txt | 11 +++--- src/materials/PointCloudMaterial.js | 6 ++-- src/materials/shaders/pointcloud.vs | 52 +++++++++++++++++------------ src/navigation/OrbitControls.js | 2 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/TODO.txt b/TODO.txt index 01ce4136..775ef95c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -7,12 +7,11 @@ TODO orthographic cameras: TODO clipping: -- polygon clipping (define polygon in screenspace) - - lock camera when selecting polygon vertices - - vertices are distorted at the boundaries of the render area - - picking with ortho cameras +- polygon clipping + - lock camera when selecting polygon vertices (disable controls, navigationMode) + - vertices are distorted at the boundaries of the render area (caused by perspective projection) + - picking with ortho cameras (fix ortho camera picking first) - TODO: - + clipping in shader + render extruded clip polygon -- profile clipping +- clip planes - EDL renderer: no depth of volumes stored -> no depth test possible diff --git a/src/materials/PointCloudMaterial.js b/src/materials/PointCloudMaterial.js index 9810a8e5..5457d8c8 100644 --- a/src/materials/PointCloudMaterial.js +++ b/src/materials/PointCloudMaterial.js @@ -355,9 +355,9 @@ Potree.PointCloudMaterial = class PointCloudMaterial extends THREE.RawShaderMate this.uniforms.clipPolygonVCount.value[i] = poly.count; this.uniforms.clipPolygonVP.value.set(poly.view.elements, 16*i); for(let j = 0; j < poly.count; j++) { - this.uniforms.clipPolygons.value[i * 8 + (j * 3 + 0)] = poly.polygon[j].x; - this.uniforms.clipPolygons.value[i * 8 + (j * 3 + 1)] = poly.polygon[j].y; - this.uniforms.clipPolygons.value[i * 8 + (j * 3 + 2)] = poly.polygon[j].z; + this.uniforms.clipPolygons.value[i * 24 + (j * 3 + 0)] = poly.polygon[j].x; + this.uniforms.clipPolygons.value[i * 24 + (j * 3 + 1)] = poly.polygon[j].y; + this.uniforms.clipPolygons.value[i * 24 + (j * 3 + 2)] = poly.polygon[j].z; } } } diff --git a/src/materials/shaders/pointcloud.vs b/src/materials/shaders/pointcloud.vs index a09bf6c8..86a72eeb 100644 --- a/src/materials/shaders/pointcloud.vs +++ b/src/materials/shaders/pointcloud.vs @@ -319,6 +319,31 @@ vec3 getCompositeColor(){ return c; } +bool pointInClipPolygon(vec3 point, int polyIdx) { + vec4 screenClipPos = clipPolygonVP[polyIdx] * modelMatrix * vec4(point, 1.0); + screenClipPos.xy = screenClipPos.xy / screenClipPos.w * 0.5 + 0.5; + + int j = clipPolygonVCount[polyIdx] - 1; + bool c = false; + for(int i = 0; i < 8; i++) { + if(i == clipPolygonVCount[polyIdx]) { + break; + } + + vec4 verti = clipPolygonVP[polyIdx] * vec4(clipPolygons[polyIdx * 8 + i], 1); + vec4 vertj = clipPolygonVP[polyIdx] * vec4(clipPolygons[polyIdx * 8 + j], 1); + verti.xy = verti.xy / verti.w * 0.5 + 0.5; + vertj.xy = vertj.xy / vertj.w * 0.5 + 0.5; + if( ((verti.y > screenClipPos.y) != (vertj.y > screenClipPos.y)) && + (screenClipPos.x < (vertj.x-verti.x) * (screenClipPos.y-verti.y) / (vertj.y-verti.y) + verti.x) ) { + c = !c; + } + j = i; + } + + return c; +} + void main() { vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); vViewPosition = mvPosition.xyz; @@ -444,27 +469,12 @@ void main() { break; } - vec4 screenClipPos = clipPolygonVP[i] * modelMatrix * vec4(position, 1.0); - int k = clipPolygonVCount[i] - 1; - bool c = false; - for(int j = 0; j < 8; j++) { - if(j == clipPolygonVCount[i]) { - break; - } - - // TODO test if point in polygon - - vec4 vertj = clipPolygonVP[i] * vec4(clipPolygons[i * 8 + j], 1); - vec4 vertk = clipPolygonVP[i] * vec4(clipPolygons[i * 8 + k], 1); - if( ((vertj.y > screenClipPos.y) != (vertk.y > screenClipPos.y)) && - (screenClipPos.x < (vertk.x-vertj.x) * (screenClipPos.y-vertj.y) / (vertk.y-vertj.y) + vertj.x) ) { - c = !c; - } - k = j + 1; - } - if(c) { - vColor.r += 0.5; - } + polyInsideAny = polyInsideAny || pointInClipPolygon(position, i); + } + if(polyInsideAny && clipMode == 1 || !polyInsideAny && clipMode == 2) { + gl_Position = vec4(1000.0, 1000.0, 1000.0, 1.0); + } else if(clipMode == 0 && polyInsideAny) { + vColor.r += 0.5; } #endif diff --git a/src/navigation/OrbitControls.js b/src/navigation/OrbitControls.js index 63d455cd..43f12f6b 100644 --- a/src/navigation/OrbitControls.js +++ b/src/navigation/OrbitControls.js @@ -16,7 +16,7 @@ Potree.OrbitControls = class OrbitControls extends THREE.EventDispatcher{ constructor(viewer){ - super() + super(); this.viewer = viewer; this.renderer = viewer.renderer;