-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextOverlay.cs
477 lines (412 loc) · 15 KB
/
TextOverlay.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
using Valve.VR;
using TMPro;
using UnityEngine.UI;
using System.Collections.Generic;
using System.IO;
public class TextOverlay : MonoBehaviour
{
// Message class to track timing and fade
private class TimedMessage
{
public string Text { get; set; }
public float TimeReceived { get; set; }
public float Alpha { get; set; }
public TimedMessage(string text)
{
Text = text;
TimeReceived = Time.time;
Alpha = 1.0f;
}
}
[Header("Socket Settings")]
public string host = "127.0.0.1";
public int port = 65432;
[Header("Overlay Settings")]
public float width = 1.0f;
public float height = 0.5f;
public Vector3 position = new Vector3(0, 1.6f, -1f);
public Vector3 rotation = new Vector3(0, 0, 0);
[Header("Message Settings")]
public float messageFadeStartTime = 7.0f;
public float messageFadeDuration = 3.0f;
public float fadeUpdateInterval = 0.05f; // Reduced interval for smoother fading
[Header("TextMeshPro Settings")]
[Tooltip("Assign the TMP Font Asset for the TextMeshProUGUI component.")]
public TMP_FontAsset fontAsset;
private ulong overlayHandle = OpenVR.k_ulOverlayHandleInvalid;
private TcpClient client;
private NetworkStream stream;
private Thread receiveThread;
private volatile bool keepReading = false;
private StringBuilder messageBuilder = new StringBuilder();
private object lockObject = new object();
// For rendering text to texture
private RenderTexture renderTexture;
private Camera textCamera;
private TextMeshProUGUI textMesh;
private Texture_t overlayTexture;
private Queue<TimedMessage> lastSentences = new Queue<TimedMessage>();
private const int MAX_SENTENCES = 10;
// Thread-safe queue for incoming messages
private Queue<string> pendingMessages = new Queue<string>();
private StreamReader reader;
// Flag to indicate if a render is needed
private bool needsRender = false;
private Coroutine fadeCoroutine; // Reference to the fade coroutine
void Start()
{
InitializeOverlay();
ConnectToPythonServer();
fadeCoroutine = StartCoroutine(FadeMessagesRoutine()); // Start the fade coroutine
Application.targetFrameRate = 30; // Optional: Cap frame rate to reduce GPU load
}
private void InitializeOverlay()
{
// Initialize OpenVR if needed
if (OpenVR.System == null)
{
var error = EVRInitError.None;
OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay);
if (error != EVRInitError.None)
{
Debug.LogError($"Failed to initialize OpenVR: {error}");
return;
}
}
// Create overlay
var error2 = OpenVR.Overlay.CreateOverlay("TextOverlayKey", "Text Overlay", ref overlayHandle);
if (error2 != EVROverlayError.None)
{
Debug.LogError($"Failed to create overlay: {error2}");
return;
}
// Adjust render texture with more vertical space
renderTexture = new RenderTexture(2048, 1536, 24);
renderTexture.antiAliasing = 1;
renderTexture.Create();
// Create camera for rendering text
var cameraObj = new GameObject("TextCamera");
textCamera = cameraObj.AddComponent<Camera>();
textCamera.clearFlags = CameraClearFlags.SolidColor;
textCamera.backgroundColor = new Color(0, 0, 0, 0);
textCamera.orthographic = true;
textCamera.orthographicSize = 7;
textCamera.targetTexture = renderTexture;
textCamera.enabled = true;
// Create Canvas with proper dimensions
var canvasObj = new GameObject("OverlayCanvas");
var canvas = canvasObj.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = textCamera;
canvasObj.transform.SetParent(cameraObj.transform, false);
var canvasRect = canvasObj.GetComponent<RectTransform>();
canvasRect.sizeDelta = new Vector2(2200, 3000); // Adjusted width to be 30% narrower
var scaler = canvasObj.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
scaler.scaleFactor = 1;
// Create text object as UI element
var textObj = new GameObject("OverlayText");
textObj.transform.SetParent(canvasObj.transform, false);
textMesh = textObj.AddComponent<TextMeshProUGUI>();
textMesh.enableWordWrapping = true;
textMesh.horizontalAlignment = HorizontalAlignmentOptions.Center;
textMesh.verticalAlignment = VerticalAlignmentOptions.Bottom;
textMesh.fontSize = 48;
textMesh.color = Color.white;
textMesh.margin = new Vector4(20, 10, 20, 10);
textMesh.lineSpacing = 4;
// Assign the selected font from the Inspector
if (fontAsset != null)
{
textMesh.font = fontAsset;
}
else
{
Debug.LogWarning("No TMP Font Asset assigned. Using default font.");
}
var rectTransform = textObj.GetComponent<RectTransform>();
rectTransform.anchorMin = new Vector2(0, 0);
rectTransform.anchorMax = new Vector2(1, 1);
rectTransform.sizeDelta = Vector2.zero;
rectTransform.localPosition = Vector3.zero;
rectTransform.offsetMin = new Vector2(20, 40);
rectTransform.offsetMax = new Vector2(-20, -20);
canvasObj.transform.localPosition = new Vector3(0, 0.5f, 1);
canvasObj.transform.localRotation = Quaternion.Euler(0, 180, 180);
canvasObj.transform.localScale = new Vector3(0.003f, 0.003f, 0.003f);
overlayTexture = new Texture_t
{
handle = renderTexture.GetNativeTexturePtr(),
eType = ETextureType.DirectX,
eColorSpace = EColorSpace.Auto
};
var transformMatrix = new HmdMatrix34_t();
var pos = position;
var rot = Quaternion.Euler(rotation);
transformMatrix.m0 = (1f - 2f * (rot.y * rot.y + rot.z * rot.z));
transformMatrix.m1 = (2f * (rot.x * rot.y - rot.z * rot.w));
transformMatrix.m2 = (2f * (rot.x * rot.z + rot.y * rot.w));
transformMatrix.m3 = pos.x;
transformMatrix.m4 = (2f * (rot.x * rot.y + rot.z * rot.w));
transformMatrix.m5 = (1f - 2f * (rot.x * rot.x + rot.z * rot.z));
transformMatrix.m6 = (2f * (rot.y * rot.z - rot.x * rot.w));
transformMatrix.m7 = pos.y;
transformMatrix.m8 = (2f * (rot.x * rot.z - rot.y * rot.w));
transformMatrix.m9 = (2f * (rot.y * rot.z + rot.x * rot.w));
transformMatrix.m10 = (1f - 2f * (rot.x * rot.x + rot.y * rot.y));
transformMatrix.m11 = pos.z;
OpenVR.Overlay.SetOverlayTransformAbsolute(overlayHandle, ETrackingUniverseOrigin.TrackingUniverseStanding, ref transformMatrix);
OpenVR.Overlay.SetOverlayWidthInMeters(overlayHandle, width);
OpenVR.Overlay.ShowOverlay(overlayHandle);
}
private void ConnectToPythonServer()
{
try
{
client = new TcpClient(host, port);
stream = client.GetStream();
reader = new StreamReader(stream, Encoding.UTF8);
Debug.Log("Connected to Python server.");
keepReading = true;
receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.Start();
}
catch (Exception e)
{
Debug.LogError("Socket error: " + e.Message);
}
}
void ReceiveData()
{
try
{
while (keepReading && stream != null)
{
string completeMessage = reader.ReadLine();
if (completeMessage != null)
{
Debug.Log($"Complete message received: {completeMessage}");
lock (lockObject)
{
pendingMessages.Enqueue(completeMessage);
}
needsRender = true; // Trigger render for new message
}
else
{
Debug.LogWarning("Received null message. Server might have closed the connection.");
ReconnectToPythonServer();
}
}
}
catch (Exception e)
{
Debug.LogError($"Receive error: {e.Message}");
if (keepReading)
{
ReconnectToPythonServer();
}
}
}
private void UpdateDisplayText(string newText)
{
if (textMesh == null) return;
if (!string.IsNullOrEmpty(newText))
{
Debug.Log($"Processing text for display: {newText}");
lastSentences.Enqueue(new TimedMessage(newText));
while (lastSentences.Count > MAX_SENTENCES)
{
lastSentences.Dequeue();
}
}
// Flag to update render after adding new text
needsRender = true;
}
void Update()
{
if (overlayHandle != OpenVR.k_ulOverlayHandleInvalid)
{
lock (lockObject)
{
if (pendingMessages.Count > 0)
{
Debug.Log($"Processing {pendingMessages.Count} pending messages");
while (pendingMessages.Count > 0)
{
string message = pendingMessages.Dequeue();
UpdateDisplayText(message);
}
}
}
// Only render if needed
if (needsRender)
{
UpdateOverlayText();
needsRender = false;
}
}
}
/// <summary>
/// Updates the overlay text and renders the camera.
/// </summary>
private void UpdateOverlayText()
{
// Build final text with current alpha values
StringBuilder displayBuilder = new StringBuilder();
var currentTime = Time.time;
var messages = lastSentences.ToArray();
bool hasVisibleMessages = false;
foreach (var message in messages)
{
if (message.Alpha > 0)
{
string coloredText = $"<alpha=#{Mathf.FloorToInt(message.Alpha * 255):X2}>{message.Text}";
displayBuilder.AppendLine(coloredText);
hasVisibleMessages = true;
}
}
string finalText = displayBuilder.ToString();
Debug.Log($"Final display text:\n{finalText}");
textMesh.text = finalText;
textMesh.ForceMeshUpdate(true);
if (hasVisibleMessages)
{
textCamera.Render();
if (overlayHandle != OpenVR.k_ulOverlayHandleInvalid && overlayTexture.handle != IntPtr.Zero)
{
try
{
overlayTexture.handle = renderTexture.GetNativeTexturePtr();
OpenVR.Overlay.SetOverlayTexture(overlayHandle, ref overlayTexture);
}
catch (Exception e)
{
Debug.LogError($"Error updating overlay texture: {e.Message}");
}
}
}
}
/// <summary>
/// Coroutine to continuously update message fading smoothly.
/// </summary>
/// <returns></returns>
private System.Collections.IEnumerator FadeMessagesRoutine()
{
while (true)
{
bool requiresRender = false;
// Update message alphas based on elapsed time
var currentTime = Time.time;
var messages = lastSentences.ToArray();
foreach (var message in messages)
{
float messageAge = currentTime - message.TimeReceived;
if (messageAge > messageFadeStartTime)
{
float fadeProgress = (messageAge - messageFadeStartTime) / messageFadeDuration;
float newAlpha = Mathf.Clamp01(1.0f - fadeProgress);
if (Mathf.Abs(newAlpha - message.Alpha) > 0.01f)
{
message.Alpha = newAlpha;
requiresRender = true;
}
}
}
// Remove messages that have fully faded
while (lastSentences.Count > 0 && lastSentences.Peek().Alpha <= 0)
{
lastSentences.Dequeue();
requiresRender = true;
}
if (requiresRender)
{
needsRender = true;
}
yield return new WaitForSeconds(fadeUpdateInterval); // Wait for the specified interval
}
}
void OnDestroy()
{
try
{
keepReading = false;
if (receiveThread != null && receiveThread.IsAlive)
{
try
{
receiveThread.Join(1000);
if (receiveThread.IsAlive)
{
receiveThread.Abort();
}
}
catch (Exception e)
{
Debug.LogWarning($"Error closing receive thread: {e.Message}");
}
}
if (stream != null)
{
try { stream.Close(); } catch { }
stream = null;
}
if (client != null)
{
try { client.Close(); } catch { }
client = null;
}
if (overlayHandle != OpenVR.k_ulOverlayHandleInvalid && OpenVR.Overlay != null)
{
try { OpenVR.Overlay.DestroyOverlay(overlayHandle); } catch { }
}
if (renderTexture != null)
{
try { renderTexture.Release(); } catch { }
renderTexture = null;
}
// Destroy camera and canvas to free resources
if (textCamera != null)
{
Destroy(textCamera.gameObject);
textCamera = null;
}
// Stop the fade coroutine
if (fadeCoroutine != null)
{
StopCoroutine(fadeCoroutine);
}
}
catch (Exception e)
{
Debug.LogError($"Error in OnDestroy: {e.Message}");
}
}
private void ReconnectToPythonServer()
{
try
{
if (client != null)
{
client.Close();
client = null;
}
if (stream != null)
{
stream.Close();
stream = null;
}
Thread.Sleep(1000);
ConnectToPythonServer();
}
catch (Exception e)
{
Debug.LogError($"Reconnection error: {e.Message}");
}
}
}