polygon clipping works now (surprisingly efficient)

This commit is contained in:
Lukas Gersthofer 2017-07-15 15:38:01 +02:00
parent 8b5a7ab40a
commit d124576f7d
4 changed files with 40 additions and 31 deletions

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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

View File

@ -16,7 +16,7 @@
Potree.OrbitControls = class OrbitControls extends THREE.EventDispatcher{
constructor(viewer){
super()
super();
this.viewer = viewer;
this.renderer = viewer.renderer;