Skip to content

Commit 34aa5a8

Browse files
committed
Support text colors for the latex 3d
1 parent aecb4ad commit 34aa5a8

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

modules/hist/hist3d.mjs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ import { kCARTESIAN, kPOLAR, kCYLINDRICAL, kSPHERICAL, kRAPIDITY } from '../hist
1010
import { buildHist2dContour, buildSurf3D } from '../hist2d/TH2Painter.mjs';
1111

1212

13-
function createLatexGeometry(painter, lbl, size) {
13+
function createLatexGeometry(painter, lbl, size, as_array) {
1414
const geom_args = { font: getHelveticaFont(), size, height: 0, curveSegments: 5 };
1515
if (THREE.REVISION > 162)
1616
geom_args.depth = 0;
1717
else
1818
geom_args.height = 0;
1919

20-
if (isPlainText(lbl))
21-
return new THREE.TextGeometry(translateLaTeX(lbl), geom_args);
20+
if (isPlainText(lbl)) {
21+
const res = new THREE.TextGeometry(translateLaTeX(lbl), geom_args);
22+
return as_array ? [res] : res;
23+
}
2224

2325
const font_size = size * 100, geoms = [];
2426
let stroke_width = 5;
@@ -92,6 +94,8 @@ function createLatexGeometry(painter, lbl, size) {
9294
this.x += Number.parseInt(value)*0.01;
9395
else if ((name === 'y') && (this.kind === 'text'))
9496
this.y -= Number.parseInt(value)*0.01;
97+
else if ((name === 'fill') && (this.kind === 'text'))
98+
this.fill = value;
9599
else if ((name === 'd') && (this.kind === 'path')) {
96100
if (get() !== 'M')
97101
return console.error('Not starts with M');
@@ -122,6 +126,7 @@ function createLatexGeometry(painter, lbl, size) {
122126
const pos = new Float32Array(pnts);
123127

124128
this.geom = new THREE.BufferGeometry();
129+
this.geom._fill = this.fill;
125130
this.geom.setAttribute('position', new THREE.BufferAttribute(pos, 3));
126131
this.geom.scale(0.01, -0.01, 0.01);
127132
this.geom.computeVertexNormals();
@@ -135,6 +140,7 @@ function createLatexGeometry(painter, lbl, size) {
135140
if (this.kind === 'text') {
136141
geom_args.size = Math.round(0.01*this.font_size);
137142
this.geom = new THREE.TextGeometry(v, geom_args);
143+
this.geom._fill = this.fill;
138144
geoms.push(this.geom);
139145
}
140146
}
@@ -148,11 +154,15 @@ function createLatexGeometry(painter, lbl, size) {
148154

149155
if (!geoms.length) {
150156
geom_args.size = size;
151-
return new THREE.TextGeometry(translateLaTeX(lbl), geom_args);
157+
const res = new THREE.TextGeometry(translateLaTeX(lbl), geom_args);
158+
return as_array ? [res] : res;
152159
}
153160

154161
node.translate(); // apply translate attributes
155162

163+
if (as_array)
164+
return geoms;
165+
156166
if (geoms.length === 1)
157167
return geoms[0];
158168

@@ -187,12 +197,17 @@ function build3dlatex(obj) {
187197
handle = painter.createAttText({ attr: obj }),
188198
valign = handle.align % 10,
189199
halign = handle.align - valign,
190-
text3d = createLatexGeometry(painter, obj.fTitle, handle.getSize() || 10);
200+
arr3d = createLatexGeometry(painter, obj.fTitle, handle.getSize() || 10, true),
201+
bb = new THREE.Box3().makeEmpty();
191202

192-
text3d.computeBoundingBox();
203+
arr3d.forEach(geom => {
204+
geom.computeBoundingBox();
205+
bb.expandByPoint(geom.boundingBox.max);
206+
bb.expandByPoint(geom.boundingBox.min);
207+
})
193208

194-
let width = text3d.boundingBox.max.x - text3d.boundingBox.min.x,
195-
height = text3d.boundingBox.max.y - text3d.boundingBox.min.y;
209+
let width = bb.max.x - bb.min.x,
210+
height = bb.max.y - bb.min.y;
196211

197212
if (halign === 1)
198213
width = 0;
@@ -204,11 +219,26 @@ function build3dlatex(obj) {
204219
else if (valign === 2)
205220
height *= 0.5;
206221

207-
text3d.translate(-width, -height, 0);
222+
const materials = [],
223+
getMaterial = color => {
224+
if (!color)
225+
color = 'black';
226+
if (!materials[color])
227+
materials[color] = new THREE.MeshBasicMaterial(getMaterialArgs(color, { vertexColors: false }));
228+
return materials[color];
229+
};
230+
208231

209-
const material = new THREE.MeshBasicMaterial(getMaterialArgs(handle.color || 'black', { vertexColors: false }));
232+
const material0 = new THREE.MeshBasicMaterial(getMaterialArgs(handle.color || 'black', { vertexColors: false }));
233+
234+
const obj3d = new THREE.Object3D();
235+
236+
arr3d.forEach(geom => {
237+
geom.translate(-width, -height, 0);
238+
obj3d.add(new THREE.Mesh(geom, getMaterial(geom._fill || handle.color)));
239+
});
210240

211-
return new THREE.Mesh(text3d, material);
241+
return arr3d.length === 1 ? obj3d.children[0] : obj3d;
212242
}
213243

214244
/** @summary Text 3d axis visibility

0 commit comments

Comments
 (0)