/
TextSceneEntity.cs
136 lines (118 loc) · 5.04 KB
/
TextSceneEntity.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
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2024. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// TextSceneEntity.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System;
using System.ComponentModel;
using System.Windows.Media;
using SciChart.Charting3D;
using SciChart.Charting3D.Interop;
using SciChart.Charting3D.Primitives;
using SciChart.Charting3D.Visuals.Primitives;
using SciChart.Core.Extensions;
namespace SciChart.Examples.Examples.Charts3D.Customize3DChart.AddGeometry3D
{
/// <summary>
/// A class to demonstrate a 3D Text Elements added to the SciChart3D Scene. Created using our BaseSceneEntity and Font3D APIs
/// </summary>
public class TextSceneEntity : BaseSceneEntity<SCRTSceneEntity>
{
private readonly Color _textColor;
private readonly int _fontSize;
private readonly string _fontFamily;
private Font3D _font;
public string Text { get; set; }
[TypeConverter(typeof(StringToVector3TypeConverter))]
public Vector3 Location { get; set; }
static TextSceneEntity()
{
SCRTDllLoader.InitNativeLibs();
}
/// <summary>
/// Creates a representation of a Text Label in the 3D space, defined in world coordinates
/// </summary>
/// <param name="location">Point in the 3D space that determines the top-left corner of a label</param>
public TextSceneEntity(string text, Color textColor, Vector3 location,
int fontSize = 8, string fontFamily = "Arial")
: base(new SCRTSceneEntity())
{
Text = text;
_textColor = textColor;
Location = location;
_fontSize = fontSize;
_fontFamily = fontFamily;
// Set requires SelectionId to False to exclude this item from selection, tooltips and also
// prevent issues with maximum number of selectable meshes
RequiresSelectionId = false;
}
/// <summary>
/// Called when the 3D Engine wishes to render this element. This is where geometry must be drawn to the 3D scene
/// </summary>
/// <param name="rpi">The <see cref="IRenderPassInfo3D" /> containing parameters for the current render pass.</param>
public override void RenderScene(IRenderPassInfo3D rpi)
{
var currentCamera = RootSceneEntity?.Viewport3D.CameraController;
if (currentCamera == null || Location == null) return;
// Display billboarded text using camera vectors
Vector3 cameraFwd = currentCamera.Forward;
Vector3 cameraUp = currentCamera.Up;
// Compute a side vector
Vector3 cameraSide = cameraFwd ^ cameraUp;
cameraSide.Normalize();
// Compute orthogonal up vector
cameraUp = cameraSide ^ cameraFwd;
cameraUp.Normalize();
// Display text billboarded using camera side, up vectors (text will always face camera)
_font.BeginBillboard(cameraSide, cameraUp);
_font.AddText(Text, _textColor, Location.X, Location.Y, Location.Z);
_font.End();
}
/// <summary>
/// Called when the 3D Engine wishes to update the geometry in this element. This is where we need to cache geometry before draw.
/// </summary>
/// <param name="rpi">The <see cref="IRenderPassInfo3D" /> containing parameters for the current render pass.</param>
public override void UpdateScene(IRenderPassInfo3D rpi)
{
_font ??= new Font3D(_fontFamily, (uint)_fontSize);
}
/// <summary>
/// Called when the D3DEngine Restarts. Meshes and DirectX related objects should be recreated
/// </summary>
public override void OnEngineRestart()
{
DisposeFont();
base.OnEngineRestart();
}
private void DisposeFont()
{
_font.SafeDispose();
_font = null;
}
public override eSCRTSceneEntityKind GetKind()
{
return eSCRTSceneEntityKind.SCRT_SCENE_ENTITY_KIND_HUD3D;
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeFont();
}
base.Dispose(disposing);
}
}
}