diff --git a/scene.js b/scene.js index 06c3565..6e62359 100644 --- a/scene.js +++ b/scene.js @@ -192,6 +192,14 @@ function createScene(options) { mouseListener: null, _stopped: false, + // The pick pass. 'lazy': drawn when a pointer event queries it, or pickIdleDelay ms after the last + // frame that changed it (0: never ahead of the query). 'eager': drawn in every frame that changed it (upstream). + pickMode: options.pickMode || 'lazy', + pickIdleDelay: options.pickIdleDelay === undefined ? 120 : options.pickIdleDelay, + // Query the pick buffers only when the main button goes down (hover is off): pointer moves draw nothing + pickOnPress: false, + pickStats: {draws: 0}, + getAspectratio: function() { return { x: this.aspect[0], @@ -293,6 +301,7 @@ function createScene(options) { } //Create new pick buffer var nbuffer = createSelect(gl, viewShape) + pickDrawn = false pickBufferIds[i] = numPick pickBuffers.push(nbuffer) pickBufferCount.push(pickCount) @@ -324,6 +333,7 @@ function createScene(options) { pickBufferIds.push(-1) dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -339,6 +349,7 @@ function createScene(options) { pickBufferIds.pop() dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -348,6 +359,14 @@ function createScene(options) { } scene._stopped = true + if(pickTimer) { + clearTimeout(pickTimer) + pickTimer = null + } + if(pickWait) { + clearTimeout(pickWait) + pickWait = null + } window.removeEventListener('resize', resizeListener) canvas.removeEventListener('webglcontextlost', checkContextLoss) @@ -412,7 +431,12 @@ function createScene(options) { } scene._mouseRotating = false - for(var i=0; i selection.distance) { @@ -440,17 +464,27 @@ function createScene(options) { } } + // A hover that changes nothing in the picture needs no frame: the axis spikes are off and the + // hovered cloud is not drawn larger (highlightScale 1, as plotly sets it). The render callback alone + // then gives the hover label and events, as it does from the frame. + var e = spikes.enabled + var still = !e[0] && !e[1] && !e[2] && !!scene.onrender && pickFrameSeen && + (!prevObj || prevObj.highlightScale === 1) && + (!selection.object || selection.object.highlightScale === 1) if(prevObj && prevObj !== selection.object) { if(prevObj.highlight) { prevObj.highlight(null) } - dirty = true + dirty = dirty || !still } if(selection.object) { if(selection.object.highlight) { selection.object.highlight(selection.data) } - dirty = true + dirty = dirty || !still + } + if(still && !dirty && (prevObj || selection.object)) { + scene.onrender() } change = change || (selection.object !== prevObj) @@ -481,12 +515,89 @@ function createScene(options) { canvas.addEventListener('webglcontextlost', checkContextLoss) + var pickTimer = null + var pickFrameSeen = false + var pickEmpty = false + // False until a pick pass has drawn every pick buffer. A new buffer holds memory from the pool, not a picture, + // so no query reads the buffers before that. + var pickDrawn = false + // The pick pass of a frame was read back 1 ms after the frame, so a pointer event right after a frame saw the + // picture of the frame before. A pass on demand waits as long: pickWait is set for 1 ms after a frame that changed it. + var pickWait = null + // True from a change of the objects (add, remove) to the next frame. Only that frame starts the wait: after a frame + // that moves the camera, the pass on demand answers at once, where the frame pass gave the picture of the frame before. + var pickNew = true + + // True if some object that is drawn into a pick buffer can be picked + function anyPickable() { + for(var i=0; i= 0 && objects[i].drawPick && !objects[i].noPick) { + return true + } + } + return false + } + + // Draw the pick buffers and read them back now, if they are out of date. + // Not while a change waits for its frame (an object added, removed or updated, a new size): a pass on demand + // shows what the last frame drew, as the pass in the frame did. That frame starts the idle timer again. + function ensurePick() { + if(pickTimer) { + clearTimeout(pickTimer) + pickTimer = null + } + if(!pickDirty || !pickFrameSeen || dirty || pickWait || scene._stopped || scene.contextLost) { + return + } + pickDirty = false + renderPick() + // the pick pass draws at pixel ratio 1; objects read their own ratio when a query follows (mesh pick) + for(var i=0; i 0) || scene.pickOnPress) { + return + } + pickTimer = setTimeout(function() { + pickTimer = null + if(scene._prevButtons) { + // nothing is queried while a button is held: look again later + schedulePick() + } else { + ensurePick() + } + }, scene.pickIdleDelay) + } + //Render the scene for mouse picking function renderPick() { if(checkContextLoss()) { return } + // Nothing can be picked (every trace has hoverinfo 'skip'): no pass, and no query reads the buffers + pickEmpty = !anyPickable() + if(pickEmpty) { + return + } + + scene.pickStats.draws++ + gl.colorMask(true, true, true, true) gl.depthMask(true) gl.disable(gl.BLEND) @@ -511,6 +622,7 @@ function createScene(options) { } buf.end() } + pickDrawn = true } var nBounds = [ @@ -651,9 +763,24 @@ function createScene(options) { } //If state changed, then redraw pick buffers + pickFrameSeen = true if(pickDirty) { - pickDirty = false - renderPick() + if(scene.pickMode === 'lazy') { + // Not in this frame: a pointer event that queries the buffer draws it, or the idle timer does + schedulePick() + if(pickNew) { + if(pickWait) { + clearTimeout(pickWait) + } + pickWait = setTimeout(function() { + pickWait = null + }, 1) + } + pickNew = false + } else { + pickDirty = false + renderPick() + } } //Recalculate pixel data