Skip to content

Commit 96ba72e

Browse files
puxiaoMugen87
andauthored
ViewHelper: Make label text and style configurable. (#28688)
* ViewHelper: Add options to the constructor * Update ViewHelper.js Clean up. * add setLabels, setLabelStyle * update * Update ViewHelper.js --------- Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
1 parent 7e2f886 commit 96ba72e

1 file changed

Lines changed: 51 additions & 2 deletions

File tree

examples/jsm/helpers/ViewHelper.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ViewHelper extends Object3D {
3333
const color3 = new Color( '#4488ff' );
3434
const color4 = new Color( '#000000' );
3535

36+
const options = {};
37+
3638
const interactiveObjects = [];
3739
const raycaster = new Raycaster();
3840
const mouse = new Vector2();
@@ -166,6 +168,26 @@ class ViewHelper extends Object3D {
166168

167169
};
168170

171+
this.setLabels = function ( labelX, labelY, labelZ ) {
172+
173+
options.labelX = labelX;
174+
options.labelY = labelY;
175+
options.labelZ = labelZ;
176+
177+
updateLabels();
178+
179+
};
180+
181+
this.setLabelStyle = function ( font, color, radius ) {
182+
183+
options.font = font;
184+
options.color = color;
185+
options.radius = radius;
186+
187+
updateLabels();
188+
189+
};
190+
169191
this.update = function ( delta ) {
170192

171193
const step = delta * turnRate;
@@ -271,26 +293,53 @@ class ViewHelper extends Object3D {
271293

272294
}
273295

274-
function getSpriteMaterial( color ) {
296+
function getSpriteMaterial( color, text ) {
297+
298+
const { font = '24px Arial', color: labelColor = '#000000', radius = 14 } = options;
275299

276300
const canvas = document.createElement( 'canvas' );
277301
canvas.width = 64;
278302
canvas.height = 64;
279303

280304
const context = canvas.getContext( '2d' );
281305
context.beginPath();
282-
context.arc( 32, 32, 14, 0, 2 * Math.PI );
306+
context.arc( 32, 32, radius, 0, 2 * Math.PI );
283307
context.closePath();
284308
context.fillStyle = color.getStyle();
285309
context.fill();
286310

311+
if ( text ) {
312+
313+
context.font = font;
314+
context.textAlign = 'center';
315+
context.fillStyle = labelColor;
316+
context.fillText( text, 32, 41 );
317+
318+
}
319+
287320
const texture = new CanvasTexture( canvas );
288321
texture.colorSpace = SRGBColorSpace;
289322

290323
return new SpriteMaterial( { map: texture, toneMapped: false } );
291324

292325
}
293326

327+
function updateLabels() {
328+
329+
posXAxisHelper.material.map.dispose();
330+
posYAxisHelper.material.map.dispose();
331+
posZAxisHelper.material.map.dispose();
332+
333+
posXAxisHelper.material.dispose();
334+
posYAxisHelper.material.dispose();
335+
posZAxisHelper.material.dispose();
336+
337+
posXAxisHelper.material = getSpriteMaterial( color1, options.labelX );
338+
posYAxisHelper.material = getSpriteMaterial( color2, options.labelY );
339+
posZAxisHelper.material = getSpriteMaterial( color3, options.labelZ );
340+
341+
}
342+
294343
}
295344

296345
}

0 commit comments

Comments
 (0)