Skip to content

Commit f16bfd4

Browse files
ibgreengnavvy
authored andcommitted
Use gl_FragDepth extension to fix Voronoi Layer (visgl#186)
1 parent 28ebc73 commit f16bfd4

7 files changed

Lines changed: 48 additions & 14 deletions

File tree

example/layer-examples.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
} from '../src/layers/fp64';
1616

1717
import {
18-
EnhancedChoroplethLayer
18+
EnhancedChoroplethLayer,
19+
VoronoiLayer
1920
} from '../src/layers/samples';
2021

2122
const ArcLayerExample = props =>
@@ -193,6 +194,15 @@ const EnhancedChoroplethLayerExample = props =>
193194
onClick: props.onChoroplethClicked
194195
});
195196

197+
const VoronoiLayerExample = props =>
198+
new VoronoiLayer({
199+
...props,
200+
data: props.points,
201+
pickable: true,
202+
onHover: props.onScatterplotHovered,
203+
onClick: props.onScatterplotClicked
204+
});
205+
196206
// Returns new array N times larger than input array
197207
// filled with duplicate elements
198208
// Avoids Array.concat (which generates temporary huge arrays)
@@ -272,7 +282,8 @@ export default {
272282
},
273283

274284
'Sample Layers': {
275-
EnhancedChoroplethLayer: EnhancedChoroplethLayerExample
285+
EnhancedChoroplethLayer: EnhancedChoroplethLayerExample,
286+
VoronoiLayer: VoronoiLayerExample
276287
},
277288

278289
'Performance Tests': {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"babel-preset-stage-2": "^6.3.13",
5151
"babel-runtime": "^6.11.6",
5252
"babelify": "^7.2.0",
53+
"brfs-babel": "^1.0.0",
5354
"browserify": "13.0.0",
5455
"budo": "^8.0.3",
5556
"d3-color": "^1.0.1",
@@ -118,6 +119,6 @@
118119
"test-electron": "browserify dist/test/electron.js | testron | faucet",
119120
"test-shader": "npm run build-dist && budo src/test/shader.js:build/test-bundle.js --dir test --live --open --port 3001 --watch-glob '**/*.{html,css,scss,js,glsl}' -- -t babelify -t glslify",
120121
"profile-disc": "browserify src/bundle.js --full-paths -t babelify -t glslify | uglifyjs | discify --open",
121-
"start": "npm run build-watch && budo example/main.js:example/bundle.js --live --open --port 3000 --css example/main.css --title 'deck.gl' --watch-glob '**/*.{html,css,js,glsl}' -- -t babelify -t envify"
122+
"start": "npm run build-watch && budo example/main.js:example/bundle.js --live --open --port 3000 --css example/main.css --title 'deck.gl' --watch-glob '**/*.{html,css,js,glsl}' -- -t brfs-babel -t babelify -t envify"
122123
}
123124
}

src/layers/samples/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export {default as EnhancedChoroplethLayer} from './enhanced-choropleth-layer';
2+
export {default as VoronoiLayer} from './voronoi-layer';

src/layers/samples/voronoi-layer/voronoi-layer-fragment.glsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015 Uber Technologies, Inc.
1+
// Copyright (c) 2016 Uber Technologies, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -20,12 +20,16 @@
2020

2121
#define SHADER_NAME voronoi-layer-fragment-shader
2222

23+
#extension GL_EXT_frag_depth : enable
24+
2325
#ifdef GL_ES
2426
precision highp float;
2527
#endif
2628

2729
varying vec4 vColor;
30+
varying float vFragDepth;
2831

2932
void main(void) {
3033
gl_FragColor = vColor;
34+
gl_FragDepthEXT = vFragDepth;
3135
}

src/layers/samples/voronoi-layer/voronoi-layer-vertex.glsl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015 Uber Technologies, Inc.
1+
// Copyright (c) 2016 Uber Technologies, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -29,14 +29,23 @@ attribute vec3 instancePickingColors;
2929
uniform float opacity;
3030
uniform float radius;
3131
uniform float renderPickingBuffer;
32+
uniform mat4 flatMatrix;
3233

3334
varying vec4 vColor;
35+
varying float vFragDepth;
3436

3537
void main(void) {
36-
vec3 center = preproject(instancePositions.xyz);
38+
vec3 center = project_position(instancePositions.xyz);
3739
vec3 vertex = positions * scale(radius * instancePositions.w);
38-
gl_Position = project(vec4(center, 1.0)) +
39-
project(vec4(vertex, 0.0));
40+
vec4 position =
41+
project_to_clipspace(vec4(center, 1.0)) +
42+
project_to_clipspace(vec4(vertex, 0.0));
43+
vec4 positionFlat =
44+
project_to_clipspace(vec4(center.xy, 0.0, 1.0)) +
45+
project_to_clipspace(vec4(vertex.xy, 0.0, 0.0));
46+
47+
gl_Position = positionFlat;
48+
vFragDepth = position.z / position.w;
4049

4150
vec4 color = vec4(instanceColors / 255.0, opacity);
4251
vec4 pickingColor = vec4(instancePickingColors / 255.0, 1.);

src/layers/samples/voronoi-layer/voronoi-layer.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import {Layer} from '../../../lib';
2121
import {assembleShaders} from '../../../shader-utils';
2222
import {GL, Model, Geometry} from 'luma.gl';
23-
24-
const glslify = require('glslify');
23+
import {readFileSync} from 'fs';
24+
import {join} from 'path';
2525

2626
const DEFAULT_COLOR = [255, 0, 255];
2727

@@ -65,6 +65,10 @@ export default class VoronoiLayer extends Layer {
6565
instanceColors: {size: 3, update: this.calculateInstanceColors}
6666
});
6767

68+
if (!gl.getExtension('EXT_frag_depth')) {
69+
throw new Error('Voronoi needs gl_FragDepth');
70+
}
71+
6872
this.setState({model: this.getModel(gl)});
6973
}
7074

@@ -74,6 +78,10 @@ export default class VoronoiLayer extends Layer {
7478
});
7579
}
7680

81+
draw({uniforms}) {
82+
this.state.model.render(uniforms);
83+
}
84+
7785
getModel(gl) {
7886
const NUM_SEGMENTS = 32;
7987
const PI2 = Math.PI * 2;
@@ -95,8 +103,8 @@ export default class VoronoiLayer extends Layer {
95103
gl,
96104
id: this.props.id,
97105
...assembleShaders(gl, {
98-
vs: glslify('./voronoi-layer-vertex.glsl'),
99-
fs: glslify('./voronoi-layer-fragment.glsl')
106+
vs: readFileSync(join(__dirname, './voronoi-layer-vertex.glsl')),
107+
fs: readFileSync(join(__dirname, './voronoi-layer-fragment.glsl'))
100108
}),
101109
geometry: new Geometry({
102110
drawMode: GL.TRIANGLE_FAN,

src/viewport/old-viewport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ export default class Viewport {
337337
this._createMat4(),
338338
2 * Math.atan((this.height / 2) / this.altitude), // fov in radians
339339
this.width / this.height, // aspect ratio
340-
0.1, // near plane
341-
this.farZ * 10.0 // far plane
340+
0.2, // near plane
341+
this.farZ * 2 // far plane
342342
);
343343
/* eslint-enable no-inline-comments */
344344

0 commit comments

Comments
 (0)