Skip to content
Draft
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
1 change: 1 addition & 0 deletions draftlogs/XXXX_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add `scene.markerrender` to draw `scatter3d` circle markers as one-vertex discs instead of glyph meshes, which cuts the memory of a marker from 4,160 bytes to 36 bytes [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]
1 change: 1 addition & 0 deletions draftlogs/YYYY_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add `layout.lod` and `layout.scene.lod` to draw a preview of every k-th point first in `scattergl` plots and while the camera moves in `scatter3d` plots of many points, with the full picture drawn when the plot is at rest, the `plotly_lod` event, and `scene.hidden` / `scene.setHidden` to hide `scattergl` traces without a recalculation [[#YYYY](https://github.com/plotly/plotly.js/pull/YYYY)]
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"probe-image-size": "^7.5.0",
"regl-error2d": "^2.0.12",
"regl-line2d": "^3.1.4",
"regl-scatter2d": "github:settylab/regl-scatter2d#dd2fb74d0c2f964025a3054953723f2e533cbdd3",
"regl-scatter2d": "github:settylab/regl-scatter2d#55a33abbcc0b936b9fd68b72a86b3e6abebac512",
"regl-splom": "^1.0.14",
"strongly-connected-components": "^1.0.1",
"svg-path-sdf": "^2.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/lib/clear_gl_canvases.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = function clearGlCanvases(gd) {
var fullLayout = gd._fullLayout;

if(fullLayout._glcanvas && fullLayout._glcanvas.size()) {
// A scattergl scene with a level of detail can keep a copy of the full picture on screen.
for(const id in fullLayout._plots) {
const scene = fullLayout._plots[id]._scene;
if(scene?.lod?.beforeClear) scene.lod.beforeClear();
}

fullLayout._glcanvas.each(function(d) {
if(d.regl) d.regl.clear({color: true, depth: true});
});
Expand Down
7 changes: 7 additions & 0 deletions src/plots/gl3d/layout/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function handleGl3dDefaults(sceneLayoutIn, sceneLayoutOut, coerce, opts) {
*/

var bgcolor = coerce('bgcolor');
// A scene without `lod` gets no `lod` container.
if(Lib.isPlainObject(sceneLayoutIn.lod) && coerce('lod.minpoints')) {
coerce('lod.budget');
coerce('lod.maxstride');
coerce('lod.settle');
}
var bgColorCombined = Color.combine(bgcolor, opts.paper_bgcolor);

var cameraKeys = ['up', 'center', 'eye'];
Expand All @@ -62,6 +68,7 @@ function handleGl3dDefaults(sceneLayoutIn, sceneLayoutOut, coerce, opts) {
}

coerce('camera.projection.type');
coerce('markerrender');

/*
* coerce to positive number (min 0) but also do not accept 0 (>0 not >=0)
Expand Down
56 changes: 56 additions & 0 deletions src/plots/gl3d/layout/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ module.exports = {
yaxis: gl3dAxisAttrs,
zaxis: gl3dAxisAttrs,

markerrender: {
valType: 'enumerated',
values: ['glyph', 'disc', 'auto'],
dflt: 'glyph',
editType: 'plot',
description: [
'Sets how circle markers of *scatter3d* traces are drawn.',
'The symbols *circle* and *circle-open* have two forms.',
'*glyph* draws each marker as a mesh of 60 to 104 vertices. This is the default.',
'*disc* draws each marker as one round point sprite. It needs far less memory and draws faster.',
'The edge of a disc differs from the edge of a glyph by about one pixel.',
'A marker with another symbol, a projection on the axes, or a size above the point size limit of the GPU is drawn as a glyph.',
'*auto* is currently the same as *disc*.'
].join(' ')
},
dragmode: {
valType: 'enumerated',
values: ['orbit', 'turntable', 'zoom', 'pan', false],
Expand All @@ -150,6 +165,47 @@ module.exports = {
'Determines the mode of hover interactions for this scene.'
].join(' ')
},
lod: {
minpoints: {
valType: 'number',
min: 0,
dflt: 0,
editType: 'plot',
description: [
'Sets the number of `scatter3d` disc markers from which a camera move shows a preview.',
'A preview draws every k-th marker of every trace and no pick pass.',
'The scene draws every marker again `settle` milliseconds after the last camera move.',
'0 turns the preview off.',
'The graph emits `plotly_lod` when the state changes.'
].join(' ')
},
budget: {
valType: 'number',
min: 1,
dflt: 2500000,
editType: 'plot',
description: [
'Sets the largest number of markers in a preview.',
'The stride k is the smallest power of two, from 2 to `maxstride`,',
'that brings the markers within this number.'
].join(' ')
},
maxstride: {
valType: 'integer',
min: 2,
dflt: 64,
editType: 'plot',
description: 'Sets the largest stride of a preview.'
},
settle: {
valType: 'number',
min: 0,
dflt: 150,
editType: 'plot',
description: 'Sets the time in milliseconds between the last camera move and the full picture.'
},
editType: 'plot'
},
uirevision: {
valType: 'any',
editType: 'none',
Expand Down
24 changes: 23 additions & 1 deletion src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,26 @@ proto.initializeGLPlot = function() {
scene.render();
};

// A static scene gets no level of detail, because an image must show every point.
scene.glplot.onlod = (state) => {
const gd = scene.graphDiv;
if(gd?.emit) gd.emit('plotly_lod', { subplot: scene.id, ...state });
};
scene.glplot.lod = scene.staticMode ? null : scene.fullSceneLayout.lod;

return true;
};

/**
* Get the level of detail of the scene.
*
* @returns `{state, stride, shown, total, settle}`. `state` is 'off', 'preview' or 'full'. `settle()` draws every point now.
*/
proto.getLOD = function() {
const { lodState, lodSettle } = this.glplot;
return { ...lodState, settle: lodSettle };
};

proto.render = function() {
var scene = this;
var gd = scene.graphDiv;
Expand Down Expand Up @@ -580,6 +597,8 @@ proto.plot = function(sceneData, fullLayout, layout) {
scene.updateFx(fullSceneLayout.dragmode, fullSceneLayout.hovermode);
scene.camera.enableWheel = scene.graphDiv._context._scrollZoom.gl3d;

scene.glplot.lod = scene.staticMode ? null : fullSceneLayout.lod;

// Update scene background
scene.glplot.setClearColor(str2RGBAarray(fullSceneLayout.bgcolor));

Expand Down Expand Up @@ -1093,8 +1112,11 @@ proto.toImage = function(format) {
if(!format) format = 'png';
if(scene.staticMode) scene.container.appendChild(STATIC_CANVAS);

// Force redraw
// Force redraw. Disc markers take a slower, smoother edge path on targets without multisampling.
var exportObjs = scene.glplot.objects;
for(var oi = 0; oi < exportObjs.length; oi++) exportObjs[oi].discExport = true;
scene.glplot.redraw();
for(var oj = 0; oj < exportObjs.length; oj++) exportObjs[oj].discExport = false;

// Grab context and yank out pixels
var gl = scene.glplot.gl;
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ proto.update = function (data) {
glyph: options.scatterMarker,
opacity: scatterOpacity,
orthographic: true,
markerRender: this.scene.fullSceneLayout.markerrender,
lineWidth: options.scatterLineWidth,
lineColor: options.scatterLineColor,
project: options.project,
Expand Down
2 changes: 2 additions & 0 deletions src/traces/scattergl/base_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module.exports = {
categories: ['gl', 'regl', 'cartesian', 'symbols', 'errorBarsOK', 'showLegend', 'scatter-like'],

attributes: require('./attributes'),
layoutAttributes: require('./layout_attributes').default,
supplyDefaults: require('./defaults'),
supplyLayoutDefaults: require('./layout_defaults').default,
crossTraceDefaults: require('../scatter/cross_trace_defaults'),
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('./format_labels'),
Expand Down
77 changes: 77 additions & 0 deletions src/traces/scattergl/layout_attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { AttributeMap } from '../../types/lib/attributes';

/**
* Layout attributes of the scattergl level of detail.
* Consumer-facing types are generated from the schema by generate_schema_types.mjs.
*/
const attributes = {
lod: {
minpoints: {
valType: 'number',
min: 0,
dflt: 0,
editType: 'none',
description: [
'Sets the number of visible scattergl points from which a draw shows a preview first.',
'A preview draws every k-th point of every trace.',
'The full picture follows when no draw request came for `settle` milliseconds.',
'0 turns the preview off.',
'Applies to a graph where the scattergl markers of one subplot are the only WebGL traces.',
'The graph emits `plotly_lod` when the state changes.'
].join(' ')
},
budget: {
valType: 'number',
min: 1,
dflt: 2500000,
editType: 'none',
description: [
'Sets the largest number of points in a preview.',
'The stride k is the smallest power of two, from 2 to `maxstride`,',
'that brings the visible points within this number.'
].join(' ')
},
maxstride: {
valType: 'integer',
min: 2,
dflt: 64,
editType: 'none',
description: 'Sets the largest stride of a preview.'
},
settle: {
valType: 'number',
min: 0,
dflt: 150,
editType: 'none',
description: 'Sets the time in milliseconds between the last draw request and the full picture.'
},
slice: {
valType: 'number',
min: 0,
dflt: 5000000,
editType: 'none',
description: [
'Sets the number of points in one part of the full picture.',
'The plot draws the next part when the GPU finished the part before.',
'Thus a draw request between two parts does not wait for the full picture.',
'0 draws the full picture in one part.'
].join(' ')
},
edits: {
valType: 'enumerated',
values: ['hold', 'preview'] as const,
dflt: 'hold',
editType: 'none',
description: [
'Determines the picture during an edit of a fully drawn plot',
'that keeps the axis ranges, the plot area and the size.',
'*hold* keeps the previous full picture until the new full picture is ready.',
'*preview* shows a preview first, as a pan does.',
'Use *preview* for edits in quick succession, for example from a slider.'
].join(' ')
},
editType: 'none'
}
} as const satisfies AttributeMap;

export default attributes;
23 changes: 23 additions & 0 deletions src/traces/scattergl/layout_defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Lib from '../../lib';
import layoutAttributes from './layout_attributes';

/**
* Supply the `lod` container of the layout.
* A layout without `lod` gets no `lod` container.
*
* @param layoutIn - the user-supplied layout
* @param layoutOut - the full layout to write into
*/
export default function supplyLayoutDefaults(layoutIn: any, layoutOut: any) {
if (!Lib.isPlainObject(layoutIn.lod)) return;

const coerce = (attr: string) => Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr);

if (coerce('lod.minpoints')) {
coerce('lod.budget');
coerce('lod.maxstride');
coerce('lod.settle');
coerce('lod.slice');
coerce('lod.edits');
}
}
Loading
Loading