-
Notifications
You must be signed in to change notification settings - Fork 3
/
LabelHelper.js
378 lines (284 loc) · 10.3 KB
/
LabelHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*Base class*/
//TODO: correctly map ogl units to fontSize to canvasSize etc
LabelHelper = function (options) {
BaseHelper.call( this );
var options = options || {};
this.text = options.text !== undefined ? options.text : " ";
this.color = options.color || "rgba(0, 0, 0, 1)";
//this.textColor = options.textColor !== undefined ? options.textColor : "rgba(0, 0, 0, 1)";
this.fontFace = options.fontFace || "Jura";
this.fontWeight = options.fontWeight || "bold";//"normal bolder";
this.fontSize = options.fontSize || 10;
this.fontStyle = "";
this.background = options.background !== undefined ? options.background : true;
this.bgColor = options.bgColor || "rgba(255, 255, 255, 1)";
this._resolutionMultiplier = 8;
this._alphaTest = 0.1;
this.borderSize = 0;
this.width = 0;
this.height = 0;
//texture upscaling ratio
this.upscale = 10;
//convertion of canvas to webglUnits
this.baseRatio = 4;
this.canvas = document.createElement('canvas');
this.canvas.style.position = "absolute";
this.canvas.width = 256;
this.canvas.height = 256;
this.context= this.canvas.getContext('2d');
var texture = new THREE.Texture(this.canvas);
this.texture = texture;
this.measureText();
this.drawText();
}
LabelHelper.prototype = Object.create( BaseHelper.prototype );
LabelHelper.prototype.constructor = LabelHelper;
LabelHelper.prototype.setText = function( text ){
this.text = text;
this.generateTextFromCanvas();
this.generate();
}
LabelHelper.prototype.applyFontStyleToContext = function( measureMode ){
var measureMode = measureMode!== undefined ? measureMode : true;
var fontSize = this.charHeight;
if(!measureMode) fontSize = this.scaledFontSize;
var font = this.fontWeight +" "+ this.fontStyle +" " + fontSize +"px "+ this.fontFace;
this.context.font = font;
this.context.textBaseline = "middle";
this.context.textAlign = "center"
}
LabelHelper.prototype.measureText = function( text ){
var pixelRatio = window.devicePixelRatio || 1;
var charWidth = 0;
var charHeight = pixelRatio * this.fontSize;
var canvas = this.canvas;
var context = this.context;
var fontFace = this.fontFace;
var fontWeight = this.fontWeight;
var fontStyle = this.fontStyle;
var borderSize = this.borderSize;
//canvas.width = canvas.clientWidth * pixelRatio;
//canvas.height = canvas.clientHeight * pixelRatio;
//console.log("canvas.width ",canvas.width ,"canvas.height",canvas.height);
//var font = fontWeight +" "+ fontStyle +" " + charHeight +"px "+ fontFace;
//font = font.trim()
//context.font = font;
//context.textBaseline = "center";
//context.textAlign = "center"
this.applyFontStyleToContext();
function getPowerOfTwo(value, pow) {
var pow = pow || 1;
while(pow<value) {
pow *= 2;
}
return pow;
}
//FIXME: hackery based on measurement of specific characters
charWidth = context.measureText( Array(100+1).join('M') ).width / 100;
this.charWidth = charWidth;
this.charHeight = charHeight;
var charWidth = (context.measureText( Array(100+1).join('M') ).width ) / 100;
var charHeight = this.fontSize;
var rWidth = charWidth * (this.text.length-1);
var rHeight = charHeight;
var textWidth = context.measureText(text).width;
var sqrWidth = getPowerOfTwo(textWidth);
var sqrHeight = getPowerOfTwo(2*this.fontSize);
var upscale = this.upscale;
var baseRatio = this.baseRatio;
sqrWidth *= upscale;
sqrHeight *= upscale;
this.canvasWidth = sqrWidth;
this.canvasHeight = sqrHeight;
this.width = sqrWidth/(upscale*baseRatio);
this.height = sqrHeight/(upscale*baseRatio);
this.scaledFontSize = this.fontSize * upscale;
this.textWidth = (textWidth*upscale)/(upscale*baseRatio);
this.textHeight = (rHeight*upscale)/(upscale*baseRatio);
//this.width = ( charWidth * this.text.length-1 ) * this.canvasGLRatio + borderSize*2;
//this.height = charHeight * this.canvasGLRatio + borderSize*2;
console.log("canvas",sqrWidth, sqrHeight,"Width/height",this.width,this.height,"text (glSizes)",this.textWidth,this.textHeight);
}
LabelHelper.prototype.drawText = function()
{
var canvas = this.canvas;
var context = this.context;
var text = this.text;
var color = this.color;
var fontWeight = this.fontWeight;
var fontStyle = this.fontStyle;
var fontFace = this.fontFace;
var charHeight = this.charHeight;
canvas.width = this.canvasWidth;
canvas.height = this.canvasHeight;
this.applyFontStyleToContext( false );
context.clearRect(0, 0, canvas.width, canvas.height);
//context.fillStyle = "#000000";
//context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "#ffffff";
context.fillText(text, canvas.width/2, canvas.height/2);
//textWidth
//ctx.strokeStyle="red";
//ctx.rect((canvas.width-rWidth)/2,(canvas.height-rHeight)/2,rWidth,rHeight);
//ctx.stroke();
texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
texture.generateMipmaps = true;
texture.magFilter = THREE.LinearFilter;
texture.minFilter = THREE.LinearFilter;
console.log("texture", texture);
//texture.magFilter = THREE.NearestFilter;
//texture.minFilter = THREE.LinearMipMapLinearFilter;
this._texture = texture;
}
LabelHelper.prototype.generateTextFromCanvas = function()
{
var canvas, context, material, plane, texture;
var fontFace = this.fontFace;
var fontWeight = this.fontWeight;
var fontSize = this.fontSize;
var text = this.text;
var color = this.color;
var background = this.background;
var bgColor = this.bgColor;
var borderThickness = 1;
//color= "rgba(1, 1, 1, 1)";
//var spriteAlignment = THREE.SpriteAlignment.topLeft;
//for background drawing
var bgRect = function(ctx, x, y, w, h) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + w, y);
ctx.lineTo(x + w, y + h);
ctx.lineTo(x, y + h);
ctx.lineTo(x, y);
ctx.closePath();
ctx.fill();
//ctx.stroke();
};
// function for drawing rounded rectangles
function roundRect(ctx, x, y, w, h, r)
{
ctx.beginPath();
ctx.moveTo(x+r, y);
ctx.lineTo(x+w-r, y);
ctx.quadraticCurveTo(x+w, y, x+w, y+r);
ctx.lineTo(x+w, y+h-r);
ctx.quadraticCurveTo(x+w, y+h, x+w-r, y+h);
ctx.lineTo(x+r, y+h);
ctx.quadraticCurveTo(x, y+h, x, y+h-r);
ctx.lineTo(x, y+r);
ctx.quadraticCurveTo(x, y, x+r, y);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
//this forces a higher resolution (for texts)
var resMult = this._resolutionMultiplier;
var borderSize = 5;
//background = false;
canvas = document.createElement('canvas');
// canvas.width = 340;
context = canvas.getContext('2d');
context.save();
var rawFont = fontWeight + " " + (fontSize* resMult * 1.5) +"px "+fontFace;
context.font = rawFont;
context.textBaseline = "top";
context.textAlign = "center";
var metrics = context.measureText(text);
var width = metrics.width ;
var height = fontSize * resMult * 1.5 + borderSize;
var fullWidth = width + borderSize*2;
var fullHeight = height + borderSize*2;
//console.log(" width",canvas.width, fullWidth);
//console.log(" width", width, "height", height,metrics.width, fontSize,resMult,context.font, rawFont );
this.width = (fullWidth)/resMult/4;
this.height = (fullHeight)/resMult/4;
if (background){
context.fillStyle = bgColor;
context.strokeStyle = bgColor;
context.fillRect(0, 0, fullWidth, fullHeight);
}
context.scale(0.9, 0.9);
context.fillStyle = color;
context.fillText(text, width/2+borderSize/2,0 )//width/2, height/2);
//context.lineWidth = 3;
context.strokeStyle = color;
context.strokeText(text, width/2+borderSize/2 ,0);
context.restore();
texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
texture.generateMipmaps = true;
texture.magFilter = THREE.LinearFilter;
texture.minFilter = THREE.LinearFilter;
this._texture = texture;
}
/*Perspective 3d helpers*/
LabelHelper3d = function(options)
{
LabelHelper.call( this, options );
this.generate();
}
LabelHelper3d.prototype = Object.create( LabelHelper.prototype );
LabelHelper3d.prototype.constructor = LabelHelper3d;
LabelHelper3d.prototype.generate = function() {
var spriteMaterial = new THREE.SpriteMaterial({
map: this._texture,
transparent: true,
alphaTest: this._alphaTest,
useScreenCoordinates: false,
scaleByViewport: false,
color: this.color ,
side : THREE.DoubleSide,
//depthTest:false,
//depthWrite:false,
//renderDepth : 1e20
});
var width = this.width;
var height = this.height;
var textSprite = new THREE.Sprite(spriteMaterial);
textSprite.scale.set( width, height, 1.0);
this.textSprite = textSprite;
this.add( textSprite );
//var testCube = new THREE.Mesh(new THREE.CubeGeometry(width, height, 0.1), new THREE.MeshBasicMaterial({color:0xFF0000}));
//this.add( testCube );
};
/*Flat (perspective not front facing) helper*/
LabelHelperPlane = function(options)
{
LabelHelper.call( this, options );
this.generate();
}
LabelHelperPlane.prototype = Object.create( LabelHelper.prototype );
LabelHelperPlane.prototype.constructor = LabelHelperPlane;
LabelHelperPlane.prototype.generate = function() {
var width = this.width;
var height = this.height;
//width = this.canvasWidth;
//height = this.canvasHeight;
console.log("width", width,"height", height);
/*var dynamicTexture = new DynamicTexture(width*10,height*10)
dynamicTexture.context.font = "bold "+10+"px Arial";
dynamicTexture.clear();
dynamicTexture.drawText(this.text, undefined, height/2, 'blue')
this._texture = dynamicTexture.texture;*/
var material = new GizmoMaterial({
map: this._texture,
transparent: true,
color: this.color,
alphaTest: this._alphaTest,
side : THREE.FrontSide,
shading: THREE.FlatShading,
});
/*depthTest:false,
depthWrite:false,
renderDepth : 1e20,*/
var textPlane = new THREE.Mesh(new THREE.PlaneBufferGeometry(width, height), material);
if( this.textMesh ) this.remove( this.textMesh );
this.textMesh = textPlane;
this.add( textPlane );
if( this.textPlaneBack ) this.remove( this.textPlaneBack );
this.textPlaneBack = textPlane.clone();
this.textPlaneBack.rotation.y = -Math.PI;
this.add( this.textPlaneBack );
};