Skip to content

Commit b1342ff

Browse files
committed
Fixed depth ordering without perspective
1 parent a06ea37 commit b1342ff

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

js/src/graph3d/examples/example14_styles.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
var style = document.getElementById("style").value;
2929
var showPerspective = document.getElementById("perspective").checked;
3030
var barWidth = parseFloat(document.getElementById("barWidth").value) || 1;
31-
3231
var withValue = ['bar-color', 'dot-size', 'dot-color'].indexOf(style) != -1;
3332

3433
// Create and populate a data table.
@@ -67,12 +66,16 @@
6766
verticalRatio: 0.5
6867
};
6968

69+
var camera = graph ? graph.getCameraPosition() : null;
70+
7071
// Instantiate our graph object.
7172
graph = new links.Graph3d(document.getElementById('mygraph'));
7273

7374
// Draw our graph with the created data and options
7475
graph.draw(data, options);
7576

77+
if (camera) graph.setCameraPosition(camera); // restore camera position
78+
7679
document.getElementById("style").onchange = drawVisualization;
7780
document.getElementById("perspective").onchange = drawVisualization;
7881
document.getElementById("barWidth").onchange = drawVisualization
@@ -102,7 +105,7 @@
102105

103106
<p>
104107
<label for="perspective">
105-
<input type="checkbox" id="perspective" checked> Perspective
108+
<input type="checkbox" id="perspective" checked> Show perspective
106109
</label>
107110
</p>
108111

js/src/graph3d/graph3d.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ links.Graph3d.prototype._redrawDataGrid = function() {
15301530

15311531
// calculate the translation of the point at the bottom (needed for sorting)
15321532
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
1533-
this.dataPoints[i].dist = transBottom.length();
1533+
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
15341534
}
15351535

15361536
// sort the points on depth of their (x,y) position (not on z)
@@ -1668,7 +1668,7 @@ links.Graph3d.prototype._redrawDataDot = function() {
16681668

16691669
// calculate the distance from the point at the bottom to the camera
16701670
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
1671-
this.dataPoints[i].dist = transBottom.length();
1671+
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
16721672
}
16731673

16741674
// order the translated points by depth
@@ -1764,7 +1764,7 @@ links.Graph3d.prototype._redrawDataBar = function() {
17641764

17651765
// calculate the distance from the point at the bottom to the camera
17661766
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
1767-
this.dataPoints[i].dist = transBottom.length();
1767+
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
17681768
}
17691769

17701770
// order the translated points by depth
@@ -1844,12 +1844,20 @@ links.Graph3d.prototype._redrawDataBar = function() {
18441844
(corners[0].point.z + corners[1].point.z + corners[2].point.z + corners[3].point.z) / 4
18451845
);
18461846
var trans = this._convertPointToTranslation(center);
1847-
surface.dist = trans.length();
1847+
surface.dist = this.showPerspective ? trans.length() : -trans.z;
18481848
}
18491849

18501850
// order the surfaces by their (translated) depth
18511851
surfaces.sort(function (a, b) {
1852-
return b.dist - a.dist;
1852+
var diff = b.dist - a.dist;
1853+
if (diff) return diff;
1854+
1855+
// if equal depth, sort the top surface last
1856+
if (a.corners === top) return 1;
1857+
if (b.corners === top) return -1;
1858+
1859+
// both are equal
1860+
return 0;
18531861
});
18541862

18551863
// draw the ordered surfaces

0 commit comments

Comments
 (0)