Skip to content

Commit

Permalink
feature(point): option to link OrientedImageMaterial to PointsMaterial.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Aug 19, 2019
1 parent dd8c6b5 commit f89f1c5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 40 deletions.
6 changes: 4 additions & 2 deletions src/Process/PointCloudProcessing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as THREE from 'three';
import CancelledCommandException from 'Core/Scheduler/CancelledCommandException';

const point = new THREE.Vector3();
// Draw a cube with lines (12 lines).
function cube(size) {
var h = size.clone().multiplyScalar(0.5);
Expand Down Expand Up @@ -165,6 +166,7 @@ export default {
}

elt.notVisibleSince = undefined;
point.copy(context.camera.camera3D.position).sub(layer.object3d.position);

// only load geometry if this elements has points
if (elt.numPoints > 0) {
Expand All @@ -185,7 +187,7 @@ export default {
}
}
} else if (!elt.promise) {
const distance = Math.max(0.001, bbox.distanceToPoint(context.camera.camera3D.position));
const distance = Math.max(0.001, bbox.distanceToPoint(point));
// Increase priority of nearest node
const priority = computeScreenSpaceError(context, layer, elt, distance) / distance;
elt.promise = context.scheduler.execute({
Expand Down Expand Up @@ -220,7 +222,7 @@ export default {
}

if (elt.children && elt.children.length) {
const distance = bbox.distanceToPoint(context.camera.camera3D.position);
const distance = bbox.distanceToPoint(point);
elt.sse = computeScreenSpaceError(context, layer, elt, distance) / layer.sseThreshold;
if (elt.sse >= 1) {
return elt.children;
Expand Down
37 changes: 36 additions & 1 deletion src/Renderer/PointsMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector4, Uniform, NoBlending, NormalBlending, RawShaderMaterial } from
import PointsVS from 'Renderer/Shader/PointsVS.glsl';
import PointsFS from 'Renderer/Shader/PointsFS.glsl';
import Capabilities from 'Core/System/Capabilities';
import ShaderUtils from 'Renderer/Shader/ShaderUtils';

export const MODE = {
COLOR: 0,
Expand All @@ -12,9 +13,10 @@ export const MODE = {

class PointsMaterial extends RawShaderMaterial {
constructor(options = {}) {
const oiMaterial = options.orientedImageMaterial;
delete options.orientedImageMaterial;
super(options);
this.vertexShader = PointsVS;
this.fragmentShader = PointsFS;

this.size = options.size || 0;
this.scale = options.scale || 0.05 * 0.5 / Math.tan(1.0 / 2.0); // autosizing scale
Expand All @@ -34,6 +36,23 @@ class PointsMaterial extends RawShaderMaterial {
this.uniforms.opacity = new Uniform(this.opacity);
this.uniforms.overlayColor = new Uniform(this.overlayColor);

if (oiMaterial) {
this.uniforms.projectiveTextureAlphaBorder = oiMaterial.uniforms.projectiveTextureAlphaBorder;
this.uniforms.projectiveTextureDistortion = oiMaterial.uniforms.projectiveTextureDistortion;
this.uniforms.projectiveTextureMatrix = oiMaterial.uniforms.projectiveTextureMatrix;
this.uniforms.projectiveTexture = oiMaterial.uniforms.projectiveTexture;
this.uniforms.mask = oiMaterial.uniforms.mask;
this.uniforms.boostLight = oiMaterial.uniforms.boostLight;
this.defines.ORIENTED_IMAGES_COUNT = oiMaterial.defines.ORIENTED_IMAGES_COUNT;
this.defines.USE_DISTORTION = oiMaterial.defines.USE_DISTORTION;
this.defines.DEBUG_ALPHA_BORDER = oiMaterial.defines.DEBUG_ALPHA_BORDER;
this.defines.USE_TEXTURES_PROJECTIVE = true;
this.defines.USE_BASE_MATERIAL = true;
this.fragmentShader = ShaderUtils.unrollLoops(PointsFS, this.defines);
} else {
this.fragmentShader = PointsFS;
}

if (Capabilities.isLogDepthBufferSupported()) {
this.defines.USE_LOGDEPTHBUF = 1;
this.defines.USE_LOGDEPTHBUF_EXT = 1;
Expand All @@ -42,9 +61,25 @@ class PointsMaterial extends RawShaderMaterial {
if (__DEBUG__) {
this.defines.DEBUG = 1;
}

this.updateUniforms();
}

copy(source) {
super.copy(source);
if (source.uniforms.projectiveTextureAlphaBorder) {
// Don't copy oriented image because, it's a link to oriented image material.
// It needs a reference to oriented image material.
this.uniforms.projectiveTextureAlphaBorder = source.uniforms.projectiveTextureAlphaBorder;
this.uniforms.projectiveTextureDistortion = source.uniforms.projectiveTextureDistortion;
this.uniforms.projectiveTextureMatrix = source.uniforms.projectiveTextureMatrix;
this.uniforms.projectiveTexture = source.uniforms.projectiveTexture;
this.uniforms.mask = source.uniforms.mask;
this.uniforms.boostLight = source.uniforms.boostLight;
}
return this;
}

enablePicking(picking) {
this.picking = picking;
this.blending = picking ? NoBlending : NormalBlending;
Expand Down
24 changes: 17 additions & 7 deletions src/Renderer/Shader/Chunk/projective_texturing_pars_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void distort(inout vec2 p, vec4 polynom, vec2 pps)
}

void distort(inout vec2 p, vec4 polynom, vec3 l1l2, vec2 pps)
{
{
if ((l1l2.x == 0.) && (l1l2.y == 0.)) {
distort(p, polynom, pps);
} else {
Expand All @@ -51,8 +51,18 @@ void distort(inout vec2 p, vec4 polynom, vec3 l1l2, vec2 pps)
}
#endif

vec4 projectiveTextureColor(vec4 coords, Distortion distortion, sampler2D texture, sampler2D mask)
{
vec4 mixBaseColor(vec4 aColor, vec4 baseColor) {
#ifdef USE_BASE_MATERIAL
baseColor.rgb = aColor.a == 1.0 ? aColor.rgb : mix(baseColor, aColor, aColor.a).rgb;
baseColor.a = min(1.0, aColor.a + baseColor.a);
#else
baseColor += aColor * aColor.a;
baseColor.a += aColor.a;
#endif
return baseColor;
}

vec4 projectiveTextureColor(vec4 coords, Distortion distortion, sampler2D texture, sampler2D mask, vec4 baseColor) {
vec3 p = coords.xyz / coords.w;
if(p.z * p.z < 1.) {
#if USE_DISTORTION
Expand All @@ -67,18 +77,18 @@ vec4 projectiveTextureColor(vec4 coords, Distortion distortion, sampler2D textur

#if DEBUG_ALPHA_BORDER
vec3 r = texture2D(texture, p.xy).rgb;
return vec4( r.r * d, r.g, r.b, 1.0);
return mixBaseColor(vec4( r.r * d, r.g, r.b, 1.0), baseColor);
#else
vec4 color = texture2D(texture, p.xy);
color.a *= d;
if (boostLight) {
return vec4(sqrt(color.rgb), color.a);
return mixBaseColor(vec4(sqrt(color.rgb), color.a), baseColor);
} else {
return color;
return mixBaseColor(color, baseColor);
}
#endif

}
}
return vec4(0.);
return mixBaseColor(vec4(0.), baseColor);
}
25 changes: 19 additions & 6 deletions src/Renderer/Shader/PointsFS.glsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
precision highp float;
precision highp int;

#include <itowns/precision_qualifier>
#include <logdepthbuf_pars_fragment>
#if defined(USE_TEXTURES_PROJECTIVE)
#include <itowns/projective_texturing_pars_fragment>
#endif

varying vec4 vColor;

uniform bool pickingMode;
void main() {
#include <logdepthbuf_fragment>
// circular point rendering
if(length(gl_PointCoord - 0.5) > 0.5){
discard;
}

#if defined(USE_TEXTURES_PROJECTIVE)
vec4 color = vColor;
if (!pickingMode) {
#pragma unroll_loop
for (int i = 0; i < ORIENTED_IMAGES_COUNT; i++) {
color = projectiveTextureColor(projectiveTextureCoords[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTextureDistortion[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTexture[ ORIENTED_IMAGES_COUNT - 1 - i ], mask[ORIENTED_IMAGES_COUNT - 1 - i], color);
}
gl_FragColor = vec4(color.rgb, color.a * opacity);
} else {
gl_FragColor = color;
}
#else
gl_FragColor = vColor;

#include <logdepthbuf_fragment>
#endif
}
18 changes: 10 additions & 8 deletions src/Renderer/Shader/PointsVS.glsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
precision highp float;
precision highp int;

#include <itowns/precision_qualifier>
#include <itowns/project_pars_vertex>
#if defined(USE_TEXTURES_PROJECTIVE)
#include <itowns/projective_texturing_pars_vertex>
#endif
#include <logdepthbuf_pars_vertex>
#define EPSILON 1e-6

attribute vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform float size;

uniform bool pickingMode;
Expand Down Expand Up @@ -81,13 +79,17 @@ void main() {
vColor = vec4(mix(color, overlayColor.rgb, overlayColor.a), opacity);
}

gl_Position = projectionMatrix * (modelViewMatrix * vec4( position, 1.0 ));
#include <begin_vertex>
#include <project_vertex>

if (size > 0.) {
gl_PointSize = size;
} else {
gl_PointSize = clamp(-size / gl_Position.w, 3.0, 10.0);
}

#if defined(USE_TEXTURES_PROJECTIVE)
#include <itowns/projective_texturing_vertex>
#endif
#include <logdepthbuf_vertex>
}
24 changes: 8 additions & 16 deletions src/Renderer/Shader/ProjectiveTextureFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,36 @@
#include <itowns/projective_texturing_pars_fragment>
varying vec3 vNormal;

#ifdef USE_BASE_MATERIAL
struct noPT {
vec3 lightDirection;
vec3 ambient;
float opacity;
};

uniform noPT noProjectiveMaterial;
#endif

void main(void)
{
#include <logdepthbuf_fragment>
#ifdef USE_BASE_MATERIAL
float nDotVP = (max(0.1, dot(vNormal, normalize(noProjectiveMaterial.lightDirection))));
vec3 color = vec3(nDotVP) + noProjectiveMaterial.ambient;
vec4 color = vec4(noProjectiveMaterial.ambient + nDotVP, 0.0);
#else
vec3 color = vec3(0.0);
vec4 color = vec4(0.0);
#endif
vec4 aColor;
float alpha = 0.0;

#pragma unroll_loop
for (int i = 0; i < ORIENTED_IMAGES_COUNT; i++) {
aColor = projectiveTextureColor(projectiveTextureCoords[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTextureDistortion[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTexture[ ORIENTED_IMAGES_COUNT - 1 - i ], mask[ORIENTED_IMAGES_COUNT - 1 - i]);

#ifdef USE_BASE_MATERIAL
color = aColor.a == 1.0 ? aColor.rgb : mix(color, aColor.rgb, aColor.a);
alpha = min(1.0, aColor.a + alpha);
#else
color += aColor.rgb * aColor.a;
alpha += aColor.a;
#endif
color = projectiveTextureColor(projectiveTextureCoords[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTextureDistortion[ ORIENTED_IMAGES_COUNT - 1 - i ], projectiveTexture[ ORIENTED_IMAGES_COUNT - 1 - i ], mask[ORIENTED_IMAGES_COUNT - 1 - i], color);
}

#ifdef USE_BASE_MATERIAL
alpha = alpha < 1.0 ? max(noProjectiveMaterial.opacity, alpha) : 1.0 ;
gl_FragColor = vec4(color, alpha * opacity);
color.a = color.a < 1.0 ? max(noProjectiveMaterial.opacity, color.a) : 1.0 ;
gl_FragColor = vec4(color.rgb, color.a * opacity);
#else
gl_FragColor = vec4(color / alpha, opacity);
gl_FragColor = vec4(color.rgb / color.a, opacity);
#endif

}

0 comments on commit f89f1c5

Please sign in to comment.