Skip to content

Commit 73883a6

Browse files
committed
fix sorting, sort on each frame
1 parent 252f4b7 commit 73883a6

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

occlusion.html

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@
185185

186186
document.getElementById("occlusion-toggle").addEventListener("change", function() {
187187
occlusionCullingEnabled = this.checked;
188-
for(var i = 0, len = isDrawNodes.length; i < len; i++) {
189-
isDrawNodes[i] = true;
188+
for(var i = 0, len = nodes.length; i < len; i++) {
189+
nodes[i].isDrawNode = true;
190190
}
191191
});
192192

@@ -397,7 +397,8 @@
397397
vaobbox: sphereBBoxArray,
398398
bboxNumVertices: sphere.bbox.bbox.positions.length / 3,
399399

400-
query: gl.createQuery()
400+
query: gl.createQuery(),
401+
isDrawNode: true
401402
};
402403

403404
utils.xformMatrix(nodes[i].modelMatrix, nodes[i].translate, null, nodes[i].scale);
@@ -424,26 +425,20 @@
424425
var tmpVec4a = vec4.create();
425426
var tmpVec4b = vec4.create();
426427
var tmpMat4 = mat4.create();
427-
nodes.sort(function(a, b){
428+
function sortNodes(a, b) {
428429
// sort nodes by dist to eye (via pos in view space)
429430
// we assume their relative order is static in this demo
430-
vec4.set(tmpVec4a, a.translate.x, a.translate.y, a.translate.z, 1.0);
431-
vec4.set(tmpVec4b, b.translate.x, b.translate.y, b.translate.z, 1.0);
431+
vec4.set(tmpVec4a, a.translate[0], a.translate[1], a.translate[2], 1.0);
432+
vec4.set(tmpVec4b, b.translate[0], b.translate[1], b.translate[2], 1.0);
432433

433434
mat4.mul(tmpMat4, viewMatrix, a.modelMatrix);
434435
vec4.transformMat4(tmpVec4a, tmpVec4a, tmpMat4);
435436
mat4.mul(tmpMat4, viewMatrix, b.modelMatrix);
436437
vec4.transformMat4(tmpVec4b, tmpVec4b, tmpMat4);
437438

438439
vec4.sub(tmpVec4a, tmpVec4a, tmpVec4b);
439-
if (tmpVec4a[2] !== 0) {
440-
return tmpVec4a[2];
441-
}
442-
if (tmpVec4a[0] !== 0) {
443-
return tmpVec4a[0];
444-
}
445-
return -tmpVec4a[1];
446-
});
440+
return -tmpVec4a[2];
441+
}
447442

448443
// top down assist camera
449444
var projMatrixAssistCam = mat4.create();
@@ -486,10 +481,6 @@
486481

487482
var image = new Image();
488483
var firstFrame = true;
489-
var isDrawNodes = new Array(nodes.length);
490-
for (var i = 0, len = isDrawNodes.length; i < len; ++i) {
491-
isDrawNodes[i] = true;
492-
}
493484
var invisibleNodesCount = 0;
494485

495486
image.onload = function() {
@@ -520,28 +511,39 @@
520511
var doquery = true;
521512
var samplesPassed;
522513

514+
var i, len = nodes.length;
523515

524516
var t = 0;
525517
function draw() {
526518
t += 0.01;
527519
invisibleNodesCount = 0;
528520

521+
// transform nodes
522+
for (i = 0; i < len; ++i) {
523+
node = nodes[i];
524+
525+
node.rotate[1] = Math.PI / 6 * (Math.sin(t - Math.PI / 2) + 1);
526+
527+
utils.xformMatrix(node.modelMatrix, node.translate, null, node.scale);
528+
mat4.fromYRotation(rotationMatrix, node.rotate[1]);
529+
mat4.multiply(node.modelMatrix, rotationMatrix, node.modelMatrix);
530+
}
531+
532+
if (occlusionCullingEnabled) {
533+
// sort by view distance
534+
nodes.sort(sortNodes);
535+
}
536+
529537
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
530538
gl.clearColor(0, 0, 0, 1);
531539
gl.enable(gl.DEPTH_TEST);
532540
gl.colorMask(true, true, true, true);
533541
gl.depthMask(true);
534542
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
535543

536-
for (var i = 0, len = nodes.length; i < len; ++i) {
544+
for (i = 0; i < len; ++i) {
537545
node = nodes[i];
538546
bbox = node.bbox;
539-
540-
node.rotate[1] = Math.PI / 6 * (Math.sin(t - Math.PI / 2) + 1);
541-
542-
utils.xformMatrix(node.modelMatrix, node.translate, null, node.scale);
543-
mat4.fromYRotation(rotationMatrix, node.rotate[1]);
544-
mat4.multiply(node.modelMatrix, rotationMatrix, node.modelMatrix);
545547

546548
if (occlusionCullingEnabled) {
547549
// use occlusion query with drawing bounding box
@@ -565,17 +567,17 @@
565567
if (samplesPassed == 0) {
566568
// console.log('invisible node ' + i);
567569
isVisible = false;
568-
isDrawNodes[i] = false;
570+
node.isDrawNode = false;
569571
}
570572
else {
571573
isVisible = true;
572-
isDrawNodes[i] = true;
574+
node.isDrawNode = true;
573575
}
574576
}
575577
else {
576578
// query is not ready, use visible info from previous frame (to make assist cam looks correct)
577579
// if we want to be conservative, simple set isVisible to true
578-
isVisible = isDrawNodes[i];
580+
isVisible = node.isDrawNode;
579581
doquery = false;
580582
}
581583
}
@@ -629,9 +631,9 @@
629631
gl.clearColor(0.3, 0.3, 0.3, 1);
630632
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
631633
gl.useProgram(programAssistCam);
632-
for (i = 0, len = nodes.length; i < len; ++i) {
633-
if (isDrawNodes[i]) {
634-
node = nodes[i];
634+
for (i = 0; i < len; ++i) {
635+
node = nodes[i];
636+
if (node.isDrawNode) {
635637
gl.bindVertexArray(node.vao);
636638
gl.uniformMatrix4fv(modelMatrixLocationAssistCam, false, node.modelMatrix);
637639
gl.drawElements(gl.TRIANGLES, node.indicesLength, gl.UNSIGNED_SHORT, 0);

0 commit comments

Comments
 (0)