Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize picking performance by drawing only one pixel #218

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Xbim.WeXplorer/Viewer/viewer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions Xbim.WeXplorer/Viewer/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1736,12 +1736,14 @@ export class Viewer {
});

//it is not necessary to render the image in full resolution so this factor is used for less resolution.
var factor = 2;
var factor = 8;
var gl = this.gl;

var width = this._width / factor;
var height = this._height / factor;
x = x / factor;
y = y / factor;

var xRatio = (x / this._width);
var yRatio = (y / this._height);

//create framebuffer
var frameBuffer = gl.createFramebuffer();
Expand All @@ -1751,18 +1753,24 @@ export class Viewer {
var renderBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer);
// allocate renderbuffer
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 1, 1);

var texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
// Set the parameters so we can render any image size.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

const pickingPMatrix = mat4.copy(mat4.create(), this._pMatrix)

pickingPMatrix[2 * 4] = xRatio * 2.0;
pickingPMatrix[(2 * 4) + 1] = yRatio * 2.0;

gl.uniformMatrix4fv(this._pMatrixUniformPointer, false, pickingPMatrix);

// attach renderbuffer and texture
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderBuffer);
Expand All @@ -1777,6 +1785,8 @@ export class Viewer {
gl.viewport(0, 0, width, height);

gl.enable(gl.DEPTH_TEST); //we don't use any kind of blending or transparency
gl.enable(gl.SCISSOR_TEST);
gl.scissor(0, 0, 1, 1);
gl.disable(gl.BLEND);
gl.clearColor(0, 0, 0, 0); //zero colour for no-values
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
Expand Down Expand Up @@ -1805,7 +1815,7 @@ export class Viewer {

//get colour in of the pixel
var result = new Uint8Array(4);
gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, result);
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, result);


//reset framebuffer to render into canvas again
Expand All @@ -1819,6 +1829,7 @@ export class Viewer {
//set back blending
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
gl.disable(gl.SCISSOR_TEST);

//decode ID (bit shifting by multiplication)
var hasValue = result[3] != 0; //0 transparency is only for no-values
Expand Down