From 6d422d9ded8315d80f2bc0bad0a5ffc5525b7bb0 Mon Sep 17 00:00:00 2001 From: m-schuetz Date: Sun, 22 Oct 2017 11:19:18 +0200 Subject: [PATCH] ... --- src/Potree.js | 16 +-- src/materials/shaders/edl.fs | 1 + src/materials/shaders/pointcloud.fs | 12 +- src/materials/shaders/pointcloud.vs | 97 +++++++++------ src/materials/shaders/pointcloud_sm.vs | 157 +++++++++++++++++++++++-- src/utils/PointCloudSM.js | 60 +++++++++- 6 files changed, 282 insertions(+), 61 deletions(-) diff --git a/src/Potree.js b/src/Potree.js index b6e4e9fa..8f05f415 100644 --- a/src/Potree.js +++ b/src/Potree.js @@ -1251,7 +1251,7 @@ Potree.Renderer = class{ if(visibilityTextureData){ let vnStart = visibilityTextureData.offsets.get(node); - shader.setUniform1f("vnStart", vnStart); + shader.setUniform1f("uVNStart", vnStart); } @@ -1272,9 +1272,9 @@ Potree.Renderer = class{ //shader.setUniformMatrix4("modelMatrix", world); //shader.setUniformMatrix4("modelViewMatrix", worldView); - shader.setUniform1f("level", level); + shader.setUniform1f("uLevel", level); - shader.setUniform1f("pcIndex", i); + shader.setUniform1f("uPCIndex", i); if(shadowMaps.length > 0){ @@ -1421,8 +1421,8 @@ Potree.Renderer = class{ shader.setUniformMatrix4("uViewInv", viewInv); shader.setUniformMatrix4("uProjInv", projInv); - shader.setUniform1f("screenHeight", material.screenHeight); - shader.setUniform1f("screenWidth", material.screenWidth); + shader.setUniform1f("uScreenHeight", material.screenHeight); + shader.setUniform1f("uScreenWidth", material.screenWidth); shader.setUniform1f("fov", Math.PI * camera.fov / 180); shader.setUniform1f("near", camera.near); shader.setUniform1f("far", camera.far); @@ -1455,9 +1455,9 @@ Potree.Renderer = class{ shader.setUniform1f("minSize", 1); - // uniform float pcIndex - shader.setUniform1f("spacing", material.spacing); - shader.setUniform("octreeSize", material.uniforms.octreeSize.value); + // uniform float uPCIndex + shader.setUniform1f("uSpacing", material.spacing); + shader.setUniform("uOctreeSize", material.uniforms.octreeSize.value); //uniform vec3 uColor; diff --git a/src/materials/shaders/edl.fs b/src/materials/shaders/edl.fs index 1deb9938..ec82d70d 100644 --- a/src/materials/shaders/edl.fs +++ b/src/materials/shaders/edl.fs @@ -47,6 +47,7 @@ void main(){ discard; }else{ gl_FragColor = vec4(color.rgb * shade, opacity); + //gl_FragColor = vec4(color.rgb * res, opacity); } } diff --git a/src/materials/shaders/pointcloud.fs b/src/materials/shaders/pointcloud.fs index cddb5bd7..c3b85c80 100644 --- a/src/materials/shaders/pointcloud.fs +++ b/src/materials/shaders/pointcloud.fs @@ -18,12 +18,12 @@ uniform float opacity; uniform float blendHardness; uniform float blendDepthSupplement; uniform float fov; -uniform float spacing; +uniform float uSpacing; uniform float near; uniform float far; -uniform float pcIndex; -uniform float screenWidth; -uniform float screenHeight; +uniform float uPCIndex; +uniform float uScreenWidth; +uniform float uScreenHeight; varying vec3 vColor; varying float vOpacity; @@ -70,7 +70,7 @@ void main() { { vec2 pc = vec2(gl_PointCoord.x - 0.5, (1.0 - gl_PointCoord.y) - 0.5); - vec2 offset = (pc * vPointSize) / vec2(screenWidth, screenHeight); + vec2 offset = (pc * vPointSize) / vec2(uScreenWidth, uScreenHeight); uv = 0.5 * (vSnapProjected[i].xy /vSnapProjected[i].w) + 0.5 + offset; @@ -123,7 +123,7 @@ void main() { #endif #if defined color_type_point_index - gl_FragColor = vec4(color, pcIndex / 255.0); + gl_FragColor = vec4(color, uPCIndex / 255.0); #else gl_FragColor = vec4(color, vOpacity); #endif diff --git a/src/materials/shaders/pointcloud.vs b/src/materials/shaders/pointcloud.vs index 27e92d65..2e57927c 100644 --- a/src/materials/shaders/pointcloud.vs +++ b/src/materials/shaders/pointcloud.vs @@ -20,8 +20,8 @@ uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; -uniform float screenWidth; -uniform float screenHeight; +uniform float uScreenWidth; +uniform float uScreenHeight; uniform float fov; uniform float near; uniform float far; @@ -45,12 +45,12 @@ uniform float size; // pixel size factor uniform float minSize; // minimum pixel size uniform float maxSize; // maximum pixel size -uniform float pcIndex; -uniform float spacing; -uniform float octreeSize; -uniform vec3 bbSize; -uniform float level; -uniform float vnStart; +uniform float uPCIndex; +uniform float uSpacing; +uniform float uOctreeSize; +uniform vec3 uBBSize; +uniform float uLevel; +uniform float uVNStart; uniform vec3 uColor; uniform float opacity; @@ -141,10 +141,10 @@ bool isBitSet(float number, float index){ float getLOD(){ vec3 offset = vec3(0.0, 0.0, 0.0); - float iOffset = vnStart; - float depth = level; + float iOffset = uVNStart; + float depth = uLevel; for(float i = 0.0; i <= 30.0; i++){ - float nodeSizeAtLevel = octreeSize / pow(2.0, i + level + 0.0); + float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0); vec3 index3d = (position-offset) / nodeSizeAtLevel; index3d = floor(index3d + 0.5); @@ -448,9 +448,9 @@ void main() { float pointSize = 1.0; float slope = tan(fov / 2.0); - float projFactor = -0.5 * screenHeight / (slope * vViewPosition.z); + float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z); - float r = spacing * 1.5; + float r = uSpacing * 1.5; vRadius = r; #if defined fixed_point_size pointSize = size; @@ -460,7 +460,7 @@ void main() { pointSize = pointSize * projFactor; #elif defined adaptive_point_size if(useOrthographicCamera) { - pointSize = size * r / (orthoRange * pow(2.0, getLOD())) * screenWidth; + pointSize = size * r / (orthoRange * pow(2.0, getLOD())) * uScreenWidth; } else { float worldSpaceSize = size * r / getPointSizeAttenuation(); pointSize = worldSpaceSize * projFactor; @@ -550,7 +550,7 @@ void main() { const float sm_near = 0.1; const float sm_far = 1000.0; - vColor = vec3(1.0, 1.0, 1.0); + //vColor = vec3(1.0, 1.0, 1.0); for(int i = 0; i < num_shadowmaps; i++){ vec3 viewPos = (uShadowWorldView[i] * vec4(position, 1.0)).xyz; @@ -562,36 +562,57 @@ void main() { vec2 uv = vec2(u, v) * 0.5 + 0.5; vec2 step = vec2(1.0 / 1024.0, 1.0 / 1024.0); - vec2 sampleLocations[5]; - sampleLocations[0] = vec2(0.0, 0.0); - sampleLocations[1] = step; - sampleLocations[2] = -step; - sampleLocations[3] = vec2(step.x, step.y); - sampleLocations[4] = vec2(-step.x, step.y); + + float sm_depth = sm_far * texture2D(uShadowMap[i], uv).r + sm_near; + float sm_depthx0 = sm_far * texture2D(uShadowMap[i], uv - vec2(step.x, 0.0)).r + sm_near; + float sm_depthx2 = sm_far * texture2D(uShadowMap[i], uv + vec2(step.x, 0.0)).r + sm_near; + float sm_depthy0 = sm_far * texture2D(uShadowMap[i], uv - vec2(0.0, step.y)).r + sm_near; + float sm_depthy2 = sm_far * texture2D(uShadowMap[i], uv + vec2(0.0, step.y)).r + sm_near; + + float dx = sm_depthx0 - sm_depthx2; + float dy = sm_depthy0 - sm_depthy2; - float visible_samples = 0.0; - float visibility = 0.0; - float sumSamples = 0.0; + //vColor = vec3(dx, dy, 0.0); + //vColor = vec3(1.0, 1.0, 1.0) * (distance - sm_depth); - for(int j = 0; j < 5; j++){ - float sm_depth = texture2D(uShadowMap[j], uv).r; - - if((depth - sm_depth) * sm_far > 0.1){ - visible_samples += 1.0; - visibility += (log2(depth * sm_far) - log2(sm_depth * sm_far)); - } - - sumSamples = sumSamples + 1.0; + if((distance - sm_depth) > 0.1){ + vColor = vColor * 0.2; } - float coverage = 1.0 - visible_samples / sumSamples; - vColor = vColor * (1.0-visibility); + + //vec2 sampleLocations[5]; + //sampleLocations[0] = vec2(0.0, 0.0); + //sampleLocations[1] = step; + //sampleLocations[2] = -step; + //sampleLocations[3] = vec2(step.x, step.y); + //sampleLocations[4] = vec2(-step.x, step.y); + + //float visible_samples = 0.0; + //float visibility = 0.0; + //float sumSamples = 0.0; + //float sum = 0.0; + + //for(int j = 0; j < 5; j++){ + // float sm_depth = texture2D(uShadowMap[i], uv).r; + + // if((depth - sm_depth) * sm_far > 0.05){ + // visible_samples += 1.0; + // visibility += (log2(depth * sm_far) - log2(sm_depth * sm_far)); + + // sum += max(0.0, log2(depth * sm_far) - log2(sm_depth * sm_far)); + // } + + // sumSamples = sumSamples + 1.0; + //} + + //float response = sum / sumSamples; + //float shade = exp(-response * 300.0); + + //float coverage = 1.0 - visible_samples / sumSamples; + //vColor = vColor * response; - //if((depth - sm_depth) * sm_far > 0.1){ - // vColor = vColor * 0.3; - //} } diff --git a/src/materials/shaders/pointcloud_sm.vs b/src/materials/shaders/pointcloud_sm.vs index 0a4d6949..1459ca66 100644 --- a/src/materials/shaders/pointcloud_sm.vs +++ b/src/materials/shaders/pointcloud_sm.vs @@ -7,26 +7,169 @@ attribute vec3 color; uniform mat4 uWorldView; +uniform float uScreenWidth; +uniform float uScreenHeight; uniform float near; uniform float far; +uniform float uSpacing; +uniform float uOctreeSize; +uniform float uLevel; +uniform float uVNStart; + +uniform sampler2D visibleNodes; + varying vec3 vColor; #define PI 3.141592653589793 + + +// --------------------- +// OCTREE +// --------------------- + +#if defined(adaptive_point_size) +/** + * number of 1-bits up to inclusive index position + * number is treated as if it were an integer in the range 0-255 + * + */ +float numberOfOnes(float number, float index){ + float tmp = mod(number, pow(2.0, index + 1.0)); + float numOnes = 0.0; + for(float i = 0.0; i < 8.0; i++){ + if(mod(tmp, 2.0) != 0.0){ + numOnes++; + } + tmp = floor(tmp / 2.0); + } + return numOnes; +} + + +/** + * checks whether the bit at index is 1 + * number is treated as if it were an integer in the range 0-255 + * + */ +bool isBitSet(float number, float index){ + return mod(floor(number / pow(2.0, index)), 2.0) != 0.0; +} + + +/** + * find the LOD at the point position + */ +float getLOD(){ + + vec3 offset = vec3(0.0, 0.0, 0.0); + float iOffset = uVNStart; + float depth = uLevel; + for(float i = 0.0; i <= 30.0; i++){ + float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0); + + vec3 index3d = (position-offset) / nodeSizeAtLevel; + index3d = floor(index3d + 0.5); + float index = 4.0 * index3d.x + 2.0 * index3d.y + index3d.z; + + vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0)); + float mask = value.r * 255.0; + if(isBitSet(mask, index)){ + // there are more visible child nodes at this position + iOffset = iOffset + value.g * 255.0 * 256.0 + value.b * 255.0 + numberOfOnes(mask, index - 1.0); + depth++; + }else{ + // no more visible child nodes at this position + return depth; + } + + offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d; + + } + + return depth; +} + +#endif + + +vec3 sphericalToPlanar(vec3 pos){ + float u = atan(pos.y, pos.x) / PI; + float v = atan(pos.z, length(pos.xy)) / PI; + float distance = length(pos); + + return vec3(u, v, distance); +} + void main() { vec3 viewPos = (uWorldView * vec4(position, 1.0)).xyz; - float u = atan(viewPos.y, viewPos.x) / PI; - float v = atan(viewPos.z, length(viewPos.xy)) / PI; - float distance = length(viewPos); - float t = 2.0 * ((distance - near) / (far - near)) - 1.0; + //float u = atan(viewPos.y, viewPos.x) / PI; + //float v = atan(viewPos.z, length(viewPos.xy)) / PI; + //float distance = length(viewPos); - gl_Position = vec4(u, v, t, 1.0); + vec3 planar = sphericalToPlanar(viewPos); + float distance = planar.z; - gl_PointSize = 2.0; + float t = 2.0 * ((planar.z - near) / (far - near)) - 1.0; - vColor = color; + gl_Position = vec4(planar.xy, t, 1.0); + + vColor = vec3(1.0, 1.0, 1.0) * (distance / 20.0); + + float pointSize = 2.0; + #if defined(adaptive_point_size) + float lod = getLOD(); + float r = uSpacing / pow(2.0, lod); + float pr = 0.5 * r / distance; + + pointSize = pr * uScreenWidth; + + + vColor = vec3(1.0, 1.0, 1.0) * lod * 0.2; + + #else + pointSize = 2.0; + #endif + + pointSize = min(pointSize, 20.0); + + gl_PointSize = pointSize; + + + // --------------------- + // POINT SIZE + // --------------------- + //float pointSize = 1.0; + // + //float slope = tan(fov / 2.0); + //float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z); + // + //float r = uSpacing * 1.5; + //vRadius = r; + //#if defined fixed_point_size + // pointSize = size; + //#elif defined attenuated_point_size + // pointSize = size; + // if(!useOrthographicCamera) + // pointSize = pointSize * projFactor; + //#elif defined adaptive_point_size + // if(useOrthographicCamera) { + // pointSize = size * r / (orthoRange * pow(2.0, getLOD())) * uScreenWidth; + // } else { + // float worldSpaceSize = size * r / getPointSizeAttenuation(); + // pointSize = worldSpaceSize * projFactor; + // } + //#endif +// + //pointSize = max(minSize, pointSize); + //pointSize = min(maxSize, pointSize); + // + //vRadius = pointSize / projFactor; + // + //gl_PointSize = pointSize; + //vPointSize = gl_PointSize; } diff --git a/src/utils/PointCloudSM.js b/src/utils/PointCloudSM.js index bc583f4f..cf2ed4b9 100644 --- a/src/utils/PointCloudSM.js +++ b/src/utils/PointCloudSM.js @@ -11,14 +11,42 @@ Potree.PointCloudSM = class PointCloudSM{ this.shader = new Potree.Shader(this.gl, "shadow_map", this.vsSource, this.fsSource); this.lightPos = new THREE.Vector3(); - this.target = new THREE.WebGLRenderTarget(1024, 1024, { + this.target = new THREE.WebGLRenderTarget(4*1024, 4*1024, { 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.target.depthTexture.type = THREE.UnsignedIntType; + + this.vnTexture = null; + + { + let gl = this.gl; + this.vnTexture = gl.createTexture(); + + gl.bindTexture(gl.TEXTURE_2D, this.vnTexture); + + const level = 0; + const internalFormat = gl.RGB; + const width = 2048; + const height = 1; + const border = 0; + const srcFormat = gl.RGB; + const srcType = gl.UNSIGNED_BYTE; + const pixel = new Uint8Array(3*width); + gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, + width, height, border, srcFormat, srcType, + pixel); + + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); + + gl.bindTexture(gl.TEXTURE_2D, null); + } } @@ -37,6 +65,11 @@ Potree.PointCloudSM = class PointCloudSM{ let gl = this.gl; let shader = this.shader; + let material = octree.material; + + let vsSource = `#define adaptive_point_size\n` + this.vsSource; + let fsSource = `#define adaptive_point_size\n` + this.fsSource; + shader.update(vsSource, fsSource); gl.useProgram(shader.program); @@ -46,11 +79,28 @@ Potree.PointCloudSM = class PointCloudSM{ let view = new THREE.Matrix4().makeTranslation(...this.lightPos.clone().multiplyScalar(-1).toArray()); + let visibilityTextureData = octree.computeVisibilityTextureData(nodes); + gl.bindTexture(gl.TEXTURE_2D, this.vnTexture); + let vnData = new Uint8Array(3*2048); + vnData.set(visibilityTextureData.data); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 2048, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, vnData); + gl.bindTexture(gl.TEXTURE_2D, null); + + + shader.setUniform1i("visibleNodes", 0); + gl.activeTexture(gl.TEXTURE0); + gl.bindTexture(gl.TEXTURE_2D, this.vnTexture); + let near = 0.1; let far = 1000.0; shader.setUniform1f("near", near); shader.setUniform1f("far", far); + shader.setUniform1f("uScreenWidth", this.target.width); + shader.setUniform1f("uScreenHeight", this.target.height); + + shader.setUniform1f("uSpacing", material.spacing); + shader.setUniform("uOctreeSize", material.uniforms.octreeSize.value); for(let node of nodes){ let world = node.sceneNode.matrixWorld; @@ -58,6 +108,12 @@ Potree.PointCloudSM = class PointCloudSM{ shader.setUniformMatrix4(`uWorldView`, worldView); + let vnStart = visibilityTextureData.offsets.get(node); + shader.setUniform1f("uVNStart", vnStart); + + let level = node.getLevel(); + shader.setUniform1f("uLevel", level); + let iBuffer = node.geometryNode.buffer; if(!this.potreeRenderer.buffers.has(iBuffer)){