diff --git a/src/materials/NormalizationMaterial.js b/src/materials/NormalizationMaterial.js index 548f9136..5bfa119a 100644 --- a/src/materials/NormalizationMaterial.js +++ b/src/materials/NormalizationMaterial.js @@ -5,6 +5,12 @@ Potree.NormalizationMaterial = class NormalizationMaterial extends THREE.ShaderM super(); let uniforms = { + screenWidth: { type: 'f', value: 0 }, + screenHeight: { type: 'f', value: 0 }, + edlStrength: { type: 'f', value: 1.0 }, + radius: { type: 'f', value: 1.0 }, + neighbours: { type: '2fv', value: [] }, + uEDLMap: { type: 't', value: null }, uDepthMap: { type: 't', value: null }, uWeightMap: { type: 't', value: null }, }; @@ -15,11 +21,14 @@ Potree.NormalizationMaterial = class NormalizationMaterial extends THREE.ShaderM fragmentShader: this.getDefines() + Potree.Shaders['normalize.fs'], }); + this.neighbourCount = 8; } getDefines() { let defines = ''; + defines += '#define NEIGHBOUR_COUNT ' + this.neighbourCount + '\n'; + return defines; } @@ -33,8 +42,27 @@ Potree.NormalizationMaterial = class NormalizationMaterial extends THREE.ShaderM fragmentShader: fs }); + this.uniforms.neighbours.value = this.neighbours; + this.needsUpdate = true; } + + get neighbourCount(){ + return this._neighbourCount; + } + + set neighbourCount(value){ + if (this._neighbourCount !== value) { + this._neighbourCount = value; + this.neighbours = new Float32Array(this._neighbourCount * 2); + for (var c = 0; c < this._neighbourCount; c++) { + this.neighbours[2 * c + 0] = Math.cos(2 * c * Math.PI / this._neighbourCount); + this.neighbours[2 * c + 1] = Math.sin(2 * c * Math.PI / this._neighbourCount); + } + + this.updateShaderSource(); + } + } }; diff --git a/src/materials/shaders/normalize.fs b/src/materials/shaders/normalize.fs index b9c66e1f..448d40b1 100644 --- a/src/materials/shaders/normalize.fs +++ b/src/materials/shaders/normalize.fs @@ -2,19 +2,55 @@ #extension GL_EXT_frag_depth : enable uniform sampler2D uWeightMap; +uniform sampler2D uEDLMap; uniform sampler2D uDepthMap; +uniform float screenWidth; +uniform float screenHeight; +uniform vec2 neighbours[NEIGHBOUR_COUNT]; +uniform float edlStrength; +uniform float radius; + varying vec2 vUv; +float response(float depth){ + vec2 uvRadius = radius / vec2(screenWidth, screenHeight); + + float sum = 0.0; + + for(int i = 0; i < NEIGHBOUR_COUNT; i++){ + vec2 uvNeighbor = vUv + uvRadius * neighbours[i]; + + float neighbourDepth = texture2D(uEDLMap, uvNeighbor).a; + + if(neighbourDepth != 0.0){ + if(depth == 0.0){ + sum += 100.0; + }else{ + sum += max(0.0, depth - neighbourDepth); + } + } + } + + return sum / float(NEIGHBOUR_COUNT); +} + void main() { float depth = texture2D(uDepthMap, vUv).g; if(depth >= 1.0){ discard; } + + float edlDepth = texture2D(uEDLMap, vUv).a; + float res = response(edlDepth); + float shade = exp(-res * 300.0 * edlStrength); vec4 color = texture2D(uWeightMap, vUv); color = color / color.w; + color = color * shade; + //color.xyz = vec3(1.0, 1.0, 1.0) * res * 10000.0; + //color = color * edlDepth; gl_FragColor = vec4(color.xyz, 1.0); diff --git a/src/viewer/HQSplatRenderer.js b/src/viewer/HQSplatRenderer.js index e1b53ec6..da2cd9ef 100644 --- a/src/viewer/HQSplatRenderer.js +++ b/src/viewer/HQSplatRenderer.js @@ -22,6 +22,7 @@ class HQSplatRenderer { this.attributeMaterial = new Potree.PointCloudMaterial(); this.depthMaterial.setDefine("depth_pass", "#define hq_depth_pass"); + this.depthMaterial.setDefine("use_edl", "#define use_edl"); this.normalizationMaterial = new Potree.NormalizationMaterial(); this.normalizationMaterial.depthTest = true; @@ -111,6 +112,7 @@ class HQSplatRenderer { let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize().x; this.attributeMaterial.pointSizeType = pointcloud.material.pointSizeType; + this.attributeMaterial.pointColorType = pointcloud.material.pointColorType; this.attributeMaterial.visibleNodesTexture = pointcloud.material.visibleNodesTexture; this.attributeMaterial.weighted = true; this.attributeMaterial.screenWidth = width; @@ -157,7 +159,12 @@ class HQSplatRenderer { } { // NORMALIZATION PASS + this.normalizationMaterial.uniforms.edlStrength.value = viewer.edlStrength; + this.normalizationMaterial.uniforms.radius.value = viewer.edlRadius; + this.normalizationMaterial.uniforms.screenWidth.value = width; + this.normalizationMaterial.uniforms.screenHeight.value = height; this.normalizationMaterial.uniforms.uWeightMap.value = this.rtAttribute.texture; + this.normalizationMaterial.uniforms.uEDLMap.value = this.rtDepth.texture; this.normalizationMaterial.uniforms.uDepthMap.value = this.rtAttribute.depthTexture; Potree.utils.screenPass.render(viewer.renderer, this.normalizationMaterial);