Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Created June 26, 2014 20:07
Show Gist options
  • Save bpostlethwaite/fcc3cbe0cf43282a1d17 to your computer and use it in GitHub Desktop.
Save bpostlethwaite/fcc3cbe0cf43282a1d17 to your computer and use it in GitHub Desktop.
thingy = function () {
this.model = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
}
proto.onRender = function () {
/*
* On each render animation cycle reset camera parameters
* in case view has changed.
* This can probably be optimized
*/
var cameraParameters = {
view: this.camera.view(),
projection: mat4.perspective(
new Array(16),
Math.PI/4.0,
this.shell.width/this.shell.height,
0.1, 10000.0
),
model: this.model // mutated during plot updates by autoScaleModel (see below)
};
var i, glObject, ticks = [],
gl = this.shell.gl,
sceneLayout = this.sceneLayout,
nticks, autoTickCached;
var glRange, axes,
width = this.shell.width,
height = this.shell.height;
// turns on depth rendering order.
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.DEPTH_BUFFER_BIT);
if (this.axis) {
glRange = getAxesPixelRange(this.axis,
cameraParameters,
width,
height);
for (i = 0; i < 3; ++i) {
axes = sceneLayout[this.axesNames[i]];
axes._length = (glRange[i].hi - glRange[i].lo) *
glRange[i].pixelsPerDataUnit;
if (Math.abs(axes._length) === Infinity) {
ticks[i] = [];
}
else {
axes.range[0] = glRange[i].lo;
axes.range[1] = glRange[i].hi;
axes._m = 1 / glRange[i].pixelsPerDataUnit;
// this is necessary to short-circuit the 'y' handling
// in autotick part of calcTicks... Treating all axes as 'y' in this case
// running the autoticks here, then setting
// autoticks to false to get around the 2D handling in calcTicks.
autoTickCached = axes.autotick;
if (axes.autotick) {
axes.autotick = false;
nticks = axes.nticks || this.Plotly.Lib.constrain((axes._length/40), 4, 9);
this.Plotly.Axes.autoTicks(axes, Math.abs(axes.range[1]-axes.range[0])/nticks);
}
ticks[i] = this.Plotly.Axes.calcTicks(axes);
axes.autotick = autoTickCached;
}
}
// need to profile to see if its faster to check if ticks are the same vs
// calling update everytime.
this.axesOpts.ticks = ticks;
this.axis.update(this.axesOpts);
this.axis.draw(cameraParameters);
}
/*
* Draw all objects in the render queue
*/
for (i = 0; i < this.renderQueue.length; ++i) {
glObject = this.renderQueue[i];
glObject.draw(cameraParameters);
}
};
proto.autoScaleModel = function () {
var range = this.range;
// get the data range
var ranges = [range[1][0] - range[0][0],
range[1][1] - range[0][1],
range[1][2] - range[0][2]];
var median = Math.min(Math.max(ranges[0], ranges[1]), ranges[2]);
// assign the parameters that expand or contract the dimension ranges
// so their ranges are equal in pixel coords.
for (var i = 0; i < 3; i++) {
this.model[i*4 + i] = median/ranges[i];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment