Skip to content

Commit d030360

Browse files
committed
Fix markers scaling in node.js, harmonize createPoints
1 parent 9067e87 commit d030360

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
12. Support custom mouse click/dblcklick handlers for lego plots
1616
13. Implement marker styles 35 - 49
1717
14. Let switch orthographic camera in geometry via control gui (#217)
18-
15. Fix drawing of custom markers on 3D (#205)
18+
15. Fix drawing of custom markers on 3D, also in node.js (#205)
1919

2020

2121
## Changes in 6.1.1

scripts/JSRoot.base3d.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ JSROOT.define(['d3', 'threejs_jsroot', 'painter'], (d3, THREE, jsrp) => {
13101310
this.pos[this.indx] = x;
13111311
this.pos[this.indx+1] = y;
13121312
this.pos[this.indx+2] = z;
1313-
this.indx+=3;
1313+
this.indx += 3;
13141314
}
13151315

13161316
/** @summary Create points */
@@ -1329,61 +1329,48 @@ JSROOT.define(['d3', 'threejs_jsroot', 'painter'], (d3, THREE, jsrp) => {
13291329
if (args.style === 6) k = 0.5; else
13301330
if (args.style === 7) k = 0.7;
13311331

1332-
let material;
1333-
1334-
if ((k !== 1) || (JSROOT.nodejs && !args.promise)) {
1335-
// this is plain creation of points, no texture loading, which does not work in node.js
1336-
material = new THREE.PointsMaterial({ size: (this.webgl ? 3 : 1) * this.scale * k, color: args.color });
1337-
1338-
} else {
1339-
1340-
let handler = new JSROOT.TAttMarkerHandler({ style: args.style, color: args.color, size: 7 }),
1341-
w = handler.fill ? 1 : 7,
1342-
dataUrl = 'data:image/svg+xml;utf8,' +
1343-
'<svg width="64" height="64" xmlns="http://www.w3.org/2000/svg">' +
1344-
`<path d="${handler.create(32,32)}" stroke="${handler.getStrokeColor()}" stroke-width="${w}" fill="${handler.getFillColor()}"/>` +
1345-
'</svg>',
1346-
loader = new THREE.TextureLoader();
1347-
1348-
if (args.promise && !JSROOT.nodejs)
1349-
return new Promise(resolveFunc => {
1350-
loader.load(dataUrl, texture => {
1351-
material = new THREE.PointsMaterial({ size: 3*this.scale, map: texture, transparent: true });
1352-
let pnts = new THREE.Points(this.geom, material);
1353-
pnts.nvertex = 1;
1354-
resolveFunc(pnts);
1355-
});
1356-
});
1332+
let makePoints = (material, skip_promise) => {
1333+
let pnts = new THREE.Points(this.geom, material);
1334+
pnts.nvertex = 1;
1335+
return !args.promise || skip_promise ? pnts : Promise.resolve(pnts);
1336+
};
13571337

1358-
if (args.promise && JSROOT.nodejs) {
1338+
// this is plain creation of points, no need for texture loading
1339+
if ((k !== 1) || (JSROOT.nodejs && !args.promise))
1340+
return makePoints(new THREE.PointsMaterial({ size: 3*this.scale * k, color: args.color }));
1341+
1342+
let handler = new JSROOT.TAttMarkerHandler({ style: args.style, color: args.color, size: 7 }),
1343+
w = handler.fill ? 1 : 7,
1344+
dataUrl = 'data:image/svg+xml;utf8,' +
1345+
'<svg width="64" height="64" xmlns="http://www.w3.org/2000/svg">' +
1346+
`<path d="${handler.create(32,32)}" stroke="${handler.getStrokeColor()}" stroke-width="${w}" fill="${handler.getFillColor()}"/>` +
1347+
'</svg>',
1348+
loader = new THREE.TextureLoader();
1349+
1350+
if (args.promise) {
1351+
let texture_promise;
1352+
if (JSROOT.nodejs) {
13591353
const { createCanvas, loadImage } = require('canvas');
13601354

1361-
return new Promise(resolveFunc => {
1362-
loadImage(dataUrl).then(img => {
1363-
const canvas = createCanvas(64, 64);
1364-
const ctx = canvas.getContext('2d');
1365-
ctx.drawImage(img, 0, 0, 64, 64);
1366-
let texture = new THREE.CanvasTexture(canvas);
1367-
texture.needsUpdate = true;
1368-
material = new THREE.PointsMaterial({ size: 3*this.scale, map: texture, transparent: true });
1369-
let pnts = new THREE.Points(this.geom, material);
1370-
pnts.nvertex = 1;
1371-
resolveFunc(pnts);
1372-
});
1355+
texture_promise = loadImage(dataUrl).then(img => {
1356+
const canvas = createCanvas(64, 64);
1357+
const ctx = canvas.getContext('2d');
1358+
ctx.drawImage(img, 0, 0, 64, 64);
1359+
return new THREE.CanvasTexture(canvas);
1360+
});
1361+
1362+
} else {
1363+
texture_promise = new Promise(resolveFunc => {
1364+
loader.load(dataUrl, texture => resolveFunc(texture));
13731365
});
13741366
}
13751367

1376-
let texture = loader.load(dataUrl);
1377-
1378-
material = new THREE.PointsMaterial({ size: (this.webgl ? 3 : 1) * this.scale, map: texture, transparent: true });
1368+
return texture_promise.then(texture => makePoints(new THREE.PointsMaterial({ size: 3*this.scale, map: texture, transparent: true }), true));
13791369
}
13801370

1381-
let pnts = new THREE.Points(this.geom, material);
1382-
pnts.nvertex = 1;
1383-
return args.promise ? Promise.resolve(pnts) : pnts;
1371+
return makePoints(new THREE.PointsMaterial({ size: 3*this.scale, map: loader.load(dataUrl), transparent: true }));
13841372
}
13851373

1386-
13871374
// ==============================================================================
13881375

13891376
/** @summary Create material for 3D line

0 commit comments

Comments
 (0)