Skip to content

Commit

Permalink
TORENAME(PointCloud): Use gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Sep 15, 2023
1 parent f5fad38 commit 7b18548
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
38 changes: 33 additions & 5 deletions src/Renderer/PointsMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,27 @@ class PointsMaterial extends THREE.RawShaderMaterial {
CommonMaterial.setUniformProperty(this, 'maxAttenuatedSize', maxAttenuatedSize);

// add classification texture to apply classification lut.
const data = new Uint8Array(256 * 4);
const texture = new THREE.DataTexture(data, 256, 1, THREE.RGBAFormat);
texture.needsUpdate = true;
texture.magFilter = THREE.NearestFilter;
CommonMaterial.setUniformProperty(this, 'classificationLUT', texture);
const classData = new Uint8Array(256 * 4);
const classTexture = new THREE.DataTexture(classData, 256, 1, THREE.RGBAFormat);
classTexture.needsUpdate = true;
classTexture.magFilter = THREE.NearestFilter;
CommonMaterial.setUniformProperty(this, 'classificationLUT', classTexture);

// Classification scheme
this.classification = classification;

// Update classification
this.recomputeClassification();

// TODO
const gradientCanvas = document.createElement('canvas');
gradientCanvas.width = 256;
gradientCanvas.height = 1;
const gradientTexture = new THREE.CanvasTexture(gradientCanvas);
CommonMaterial.setUniformProperty(this, 'gradient', gradientTexture);

this.recomputeGradient();

if (oiMaterial) {
this.uniforms.projectiveTextureAlphaBorder = oiMaterial.uniforms.projectiveTextureAlphaBorder;
this.uniforms.projectiveTextureDistortion = oiMaterial.uniforms.projectiveTextureDistortion;
Expand Down Expand Up @@ -217,6 +226,25 @@ class PointsMaterial extends THREE.RawShaderMaterial {
});
}

recomputeGradient() {
const width = this.gradient.source.data.width;
const context = this.gradient.source.data.getContext('2d');

context.rect(0, 0, width, 1);
const gradient = context.createLinearGradient(0, 0, width, 1);
gradient.addColorStop(0, '#000000');
gradient.addColorStop(1, '#ffffff');
context.fillStyle = gradient;
context.fill();

this.gradient.needsUpdate = true;

this.dispatchEvent({
type: 'material_property_changed',
target: this,
});
}


onBeforeCompile(shader, renderer) {
if (renderer.capabilities.isWebGL2) {
Expand Down
16 changes: 9 additions & 7 deletions src/Renderer/Shader/PointsVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ uniform mat4 modelMatrix;
uniform vec2 intensityRange;
uniform vec2 elevationRange;

uniform sampler2D classificationLUT;
uniform sampler2D gradient;

uniform float size;
uniform float preSSE;

Expand All @@ -34,7 +37,6 @@ uniform float opacity;
uniform vec4 overlayColor;
uniform bool applyOpacityClassication;
attribute vec4 unique_id;
uniform sampler2D classificationLUT;
uniform int sizeMode;
uniform float minAttenuatedSize;
uniform float maxAttenuatedSize;
Expand Down Expand Up @@ -104,30 +106,30 @@ void main() {
}

if (mode == PNTS_MODE_INTENSITY) {
// adapt the grayscale knowing the range
// adapt the gradient knowing the range
float i = (intensity - intensityRange.x) / (intensityRange.y - intensityRange.x);
vColor.rgb = vec3(i, i, i);
vColor.rgb = texture2D(gradient, vec2(i, 0.5)).rgb;
} else if (mode == PNTS_MODE_NORMAL) {
vColor.rgb = abs(normal);
} else if (mode == PNTS_MODE_COLOR) {
// default to color mode
vColor.rgb = mix(color, overlayColor.rgb, overlayColor.a);
} else if (mode == PNTS_MODE_RETURN_NUMBER) {
float n = returnNumber / RETURN_NUMBER_MAX;
vColor.rgb = vec3(n, n, n);
vColor.rgb = texture2D(gradient, vec2(n, 0.5)).rgb;
} else if (mode == PNTS_MODE_NUMBER_OF_RETURNS) {
float n = numberOfReturns / RETURN_NUMBER_MAX;
vColor.rgb = vec3(n, n, n);
vColor.rgb = texture2D(gradient, vec2(n, 0.5)).rgb;
} else if (mode == PNTS_MODE_POINT_SOURCE_ID) {
// group ids by their 4 least significant bits
float i = mod(pointSourceID, 16.) / 16.;
vColor.rgb = vec3(i, i, i);
vColor.rgb = texture2D(gradient, vec2(i, 0.5)).rgb;
} else if (mode == PNTS_MODE_ELEVATION) {
// apply scale and offset transform
vec4 model = modelMatrix * vec4(position, 1.0);
float z = (model.z - elevationRange.x) / (elevationRange.y - elevationRange.x);
// adapt the grayscale knowing the range
vColor.rgb = vec3(z, z, z);
vColor.rgb = texture2D(gradient, vec2(z, 0.5)).rgb;
}
}

Expand Down

0 comments on commit 7b18548

Please sign in to comment.