-
-
Notifications
You must be signed in to change notification settings - Fork 661
/
BillboardText3D.cs
410 lines (370 loc) · 14.2 KB
/
BillboardText3D.cs
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
The MIT License (MIT)
Copyright (c) 2018 Helix Toolkit contributors
*/
using Cyotek.Drawing.BitmapFont;
using SharpDX;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
#if CORE
#else
#if NETFX_CORE
using Media = Windows.UI.Xaml.Media;
#else
using Media = System.Windows.Media;
#endif
#endif
#if !NETFX_CORE
namespace HelixToolkit.Wpf.SharpDX
#else
#if CORE
namespace HelixToolkit.SharpDX.Core
#else
namespace HelixToolkit.UWP
#endif
#endif
{
using global::SharpDX.DirectWrite;
using System.Collections.ObjectModel;
public class TextInfoExt : TextInfo
{
public string FontFamily { get; set; } = "Arial";
public FontWeight FontWeight { get; set; } = FontWeight.Normal;
public FontStyle FontStyle { get; set; } = FontStyle.Normal;
public Vector4 Padding = Vector4.Zero;
public int Size { get; set; } = 12;
}
public class TextInfo
{
public string Text
{
get; set;
}
public Vector3 Origin
{
get; set;
}
public Color4 Foreground { set; get; } = Color.Black;
public Color4 Background { set; get; } = Color.Transparent;
public float ActualWidth
{
protected set; get;
}
public float ActualHeight
{
protected set; get;
}
public float Scale { set; get; } = 1;
/// <summary>
/// Gets or sets the rotation angle in radians.
/// </summary>
/// <value>
/// The angle in radians.
/// </value>
public float Angle { set; get; } = 0;
/// <summary>
/// Sets or gets the horizontal alignment. Default = <see cref="BillboardHorizontalAlignment.Center"/>
/// <para>
/// For example, when sets horizontal and vertical alignment to top/left,
/// billboard's bottom/right point will be anchored at the billboard origin.
/// </para>
/// </summary>
/// <value>
/// The horizontal alignment.
/// </value>
public BillboardHorizontalAlignment HorizontalAlignment
{
set; get;
} = BillboardHorizontalAlignment.Center;
/// <summary>
/// Sets or gets the vertical alignment. Default = <see cref="BillboardVerticalAlignment.Center"/>
/// <para>
/// For example, when sets horizontal and vertical alignment to top/left,
/// billboard's bottom/right point will be anchored at the billboard origin.
/// </para>
/// </summary>
/// <value>
/// The vertical alignment.
/// </value>
public BillboardVerticalAlignment VerticalAlignment
{
set; get;
} = BillboardVerticalAlignment.Center;
/// <summary>
/// Additional offset for billboard display location.
/// Behavior depends on whether billboard is fixed sized or not.
/// When billboard is fixed sized, the offset is screen spaced.
/// </summary>
public Vector2 Offset
{
set; get;
} = Vector2.Zero;
public TextInfo()
{
}
public TextInfo(string text, Vector3 origin)
{
Text = text;
Origin = origin;
}
public virtual void UpdateTextInfo(float actualWidth, float actualHeight)
{
ActualWidth = actualWidth;
ActualHeight = actualHeight;
BoundSphere = new BoundingSphere(Origin, Math.Max(actualWidth, actualHeight) / 2);
}
public BoundingSphere BoundSphere
{
get; private set;
}
}
#if !NETFX_CORE
[Serializable]
#endif
public class BillboardText3D : BillboardBase
{
private readonly static BitmapFont bmpFont;
public static Stream TextureStatic
{
get; private set;
}
const float textureScale = 0.66f;
const string FontName = "arial";
static BillboardText3D()
{
#if CORE
var assembly = typeof(BillboardText3D).GetTypeInfo().Assembly;
Stream fontInfo = assembly.GetManifestResourceStream($"HelixToolkit.SharpDX.Core.Resources.{FontName}.fnt");
bmpFont = new BitmapFont();
bmpFont.Load(fontInfo);
Stream font = assembly.GetManifestResourceStream($"HelixToolkit.SharpDX.Core.Resources.{FontName}.dds");
TextureStatic = font;
#else
#if !NETFX_CORE
var assembly = Assembly.GetExecutingAssembly();
//Read the texture description
var texDescriptionStream = assembly.GetManifestResourceStream($"HelixToolkit.Wpf.SharpDX.Textures.{FontName}.fnt");
bmpFont = new BitmapFont();
bmpFont.Load(texDescriptionStream);// BitmapFontLoader.LoadFontFromFile(texDescriptionFilePath);
texDescriptionStream.Dispose();
//Read the texture
var texImageStream = assembly.GetManifestResourceStream($"HelixToolkit.Wpf.SharpDX.Textures.{FontName}.dds");
TextureStatic = MemoryStream.Synchronized(texImageStream);
#else
var packageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
var sampleFile = global::SharpDX.IO.NativeFile.ReadAllBytes(packageFolder + $"\\HelixToolkit.UWP\\Resources\\{FontName}.fnt");
bmpFont = new BitmapFont();
var fileStream = new MemoryStream(sampleFile);
bmpFont.Load(fileStream);
var texFile = global::SharpDX.IO.NativeFile.ReadAllBytes(packageFolder + $"\\HelixToolkit.UWP\\Resources\\{FontName}.dds");
TextureStatic = new MemoryStream(texFile);
#endif
#endif
}
public override BillboardType Type
{
get
{
return BillboardType.MultipleText;
}
}
public BitmapFont BitmapFont
{
get;
}
private ObservableCollection<TextInfo> textInfo = new ObservableCollection<TextInfo>();
public ObservableCollection<TextInfo> TextInfo
{
set
{
var old = textInfo;
if (Set(ref textInfo, value))
{
old.CollectionChanged -= CollectionChanged;
IsInitialized = false;
if (value != null)
{
value.CollectionChanged += CollectionChanged;
}
}
}
get
{
return textInfo;
}
}
public BillboardText3D()
{
textInfo.CollectionChanged += CollectionChanged;
Texture = TextureStatic;
BitmapFont = bmpFont;
}
public BillboardText3D(BitmapFont bitmapFont, Stream fontTexture)
{
textInfo.CollectionChanged += CollectionChanged;
Texture = fontTexture;
BitmapFont = bitmapFont;
}
public BillboardText3D(BitmapFont bitmapFont, TextureModel fontTexture)
{
textInfo.CollectionChanged += CollectionChanged;
Texture = fontTexture;
BitmapFont = bitmapFont;
}
protected override void OnAssignTo(Geometry3D target)
{
base.OnAssignTo(target);
if (target is BillboardText3D billboard)
{
billboard.TextInfo = this.TextInfo;
}
}
private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
IsInitialized = false;
}
protected override void OnUpdateTextureAndBillboardVertices(IDeviceResources deviceResources)
{
Width = 0;
Height = 0;
// http://www.cyotek.com/blog/angelcode-bitmap-font-parsing-using-csharp
var tempList = new List<BillboardVertex>(100);
foreach (var textInfo in TextInfo)
{
var tempPrevCount = tempList.Count;
var x = 0;
var y = 0;
var w = BitmapFont.TextureSize.Width;
var h = BitmapFont.TextureSize.Height;
char previousCharacter;
previousCharacter = ' ';
var normalizedText = textInfo.Text;
var rect = new RectangleF(textInfo.Origin.X, textInfo.Origin.Y, 0, 0);
foreach (var character in normalizedText)
{
switch (character)
{
case '\n':
x = 0;
y -= BitmapFont.LineHeight;
break;
default:
var data = BitmapFont[character];
var kerning = BitmapFont.GetKerning(previousCharacter, character);
tempList.Add(DrawCharacter(data, new Vector3(x + data.XOffset, y - data.YOffset, 0), w, h, kerning, textInfo));
x += data.XAdvance + kerning;
break;
}
previousCharacter = character;
if (tempList.Count > 0)
{
rect.Width = Math.Max(rect.Width, x * textInfo.Scale * textureScale);
rect.Height = Math.Max(rect.Height, Math.Abs(tempList.Last().OffBR.Y));
}
}
var transform = textInfo.Angle != 0 ? Matrix3x2.Rotation(textInfo.Angle) : Matrix3x2.Identity;
GetQuadOffset(rect.Width, rect.Height, textInfo.HorizontalAlignment, textInfo.VerticalAlignment, out var tl, out var br);
var tr = new Vector2(br.X, tl.Y);
var bl = new Vector2(tl.X, br.Y);
//Add backbround vertex first. This is also used for hit test
BillboardVertices.Add(new BillboardVertex()
{
Position = textInfo.Origin.ToVector4(),
Background = textInfo.Background,
TexTL = Vector2.Zero,
TexBR = Vector2.Zero,
OffTL = Matrix3x2.TransformPoint(transform, tl) + textInfo.Offset,
OffBR = Matrix3x2.TransformPoint(transform, br) + textInfo.Offset,
OffTR = Matrix3x2.TransformPoint(transform, tr) + textInfo.Offset,
OffBL = Matrix3x2.TransformPoint(transform, bl) + textInfo.Offset,
});
textInfo.UpdateTextInfo(rect.Width, rect.Height);
var halfW = rect.Width / 2;
var halfH = rect.Height / 2;
for (var k = tempPrevCount; k < tempList.Count; ++k)
{
var v = tempList[k];
v.OffTL = Matrix3x2.TransformPoint(transform, v.OffTL + tl) + textInfo.Offset;
v.OffBR = Matrix3x2.TransformPoint(transform, v.OffBR + tl) + textInfo.Offset;
v.OffTR = Matrix3x2.TransformPoint(transform, v.OffTR + tl) + textInfo.Offset;
v.OffBL = Matrix3x2.TransformPoint(transform, v.OffBL + tl) + textInfo.Offset;
tempList[k] = v;
}
Width += rect.Width;
Height += rect.Height;
}
foreach (var v in tempList)
{
BillboardVertices.Add(v);
}
}
public override void UpdateBounds()
{
if (TextInfo.Count == 0)
{
Bound = new BoundingBox();
BoundingSphere = new BoundingSphere();
}
else
{
var sphere = TextInfo[0].BoundSphere;
var bound = BoundingBox.FromSphere(sphere);
foreach (var info in TextInfo)
{
sphere = BoundingSphere.Merge(sphere, info.BoundSphere);
bound = BoundingBox.Merge(bound, BoundingBox.FromSphere(info.BoundSphere));
}
BoundingSphere = sphere;
Bound = bound;
}
}
private BillboardVertex DrawCharacter(Character character, Vector3 origin, float w, float h, float kerning, TextInfo info)
{
var cw = character.Width;
var ch = character.Height;
var cu = character.X;
var cv = character.Y;
var tl = new Vector2(origin.X + kerning, origin.Y);
var br = new Vector2(origin.X + cw + kerning, origin.Y - ch);
var offTL = tl * info.Scale * textureScale;
var offBR = br * info.Scale * textureScale;
var offTR = new Vector2(offBR.X, offTL.Y);
var offBL = new Vector2(offTL.X, offBR.Y);
var uv_tl = new Vector2(cu / w, cv / h);
var uv_br = new Vector2((cu + cw) / w, (cv + ch) / h);
return new BillboardVertex()
{
Position = info.Origin.ToVector4(),
Foreground = info.Foreground,
Background = Color.Transparent,
TexTL = uv_tl,
TexBR = uv_br,
OffTL = offTL,
OffBL = offBL,
OffBR = offBR,
OffTR = offTR
};
}
public override bool HitTest(HitTestContext context, Matrix modelMatrix, ref List<HitTestResult> hits,
object originalSource, bool fixedSize)
{
var rayWS = context.RayWS;
if (!IsInitialized || context == null || Width == 0 || Height == 0
|| (!fixedSize && !BoundingSphere.TransformBoundingSphere(modelMatrix).Intersects(ref rayWS)))
{
return false;
}
return fixedSize ? HitTestFixedSize(context, ref modelMatrix, ref hits, originalSource, textInfo.Count)
: HitTestNonFixedSize(context, ref modelMatrix, ref hits, originalSource, textInfo.Count);
}
protected override void AssignResultAdditional(BillboardHitResult result, int index)
{
base.AssignResultAdditional(result, index);
result.TextInfo = textInfo[index];
result.TextInfoIndex = index;
}
}
}