forked from austinvaness/CameraLCD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraTSS.cs
More file actions
370 lines (325 loc) · 14 KB
/
CameraTSS.cs
File metadata and controls
370 lines (325 loc) · 14 KB
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
using System;
using System.IO;
using System.Reflection;
using avaness.CameraLCD.Wrappers;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Blocks;
using Sandbox.Game.Entities.Character;
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.GameSystems;
using Sandbox.Game.GameSystems.TextSurfaceScripts;
using Sandbox.Game.World;
using Sandbox.ModAPI;
using SharpDX.DXGI;
using VRage.Game.ModAPI;
using VRage.Game.Utils;
using VRage.ModAPI;
using VRage.Render.Scene;
using VRage.Utils;
using VRageMath;
using VRageRender;
using VRageRender.Messages;
namespace avaness.CameraLCD
{
[MyTextSurfaceScript("TSS_CameraDisplay", "Camera Display")]
public class CameraTSS : MyTSSCommon
{
private static readonly FieldInfo m_fov;
public override ScriptUpdate NeedsUpdate => ScriptUpdate.Update100;
private readonly MyTextPanel panel;
private readonly MyTextPanelComponent panelComponent;
private readonly MyTerminalBlock terminalBlock;
private DisplayId Id => new DisplayId(terminalBlock.EntityId, panelComponent.Area);
private MyCameraBlock camera;
private bool registered, functional;
private byte[] buffer = new byte[0];
private int bufferOffset = 0;
static CameraTSS()
{
m_fov = typeof(MyCameraBlock).GetField("m_fov", BindingFlags.NonPublic | BindingFlags.Instance);
}
public CameraTSS(IMyTextSurface surface, IMyCubeBlock block, Vector2 size) : base(surface, block, size)
{
panel = block as MyTextPanel;
panelComponent = (MyTextPanelComponent)surface;
terminalBlock = (MyTerminalBlock)block;
terminalBlock.OnMarkForClose += BlockMarkedForClose;
terminalBlock.CustomDataChanged += CustomDataChanged;
terminalBlock.IsWorkingChanged += IsWorkingChanged;
CustomDataChanged(terminalBlock);
}
public override void Dispose()
{
base.Dispose(); // do not remove
terminalBlock.OnMarkForClose -= BlockMarkedForClose;
terminalBlock.CustomDataChanged -= CustomDataChanged;
terminalBlock.IsWorkingChanged -= IsWorkingChanged;
Unregister();
}
void BlockMarkedForClose(IMyEntity ent)
{
Dispose();
}
private void Register(MyCameraBlock cameraBlock)
{
camera = cameraBlock;
camera.OnClose += Camera_OnClose;
camera.IsWorkingChanged += IsWorkingChanged;
CameraLCD.AddDisplay(Id, this);
registered = true;
IsWorkingChanged(camera);
}
private void IsWorkingChanged(MyCubeBlock b)
{
if (camera == null)
functional = false;
else
functional = terminalBlock.IsWorking && camera.IsWorking;
}
private void Unregister()
{
if (!registered)
return;
if(camera != null)
{
camera.OnClose -= Camera_OnClose;
camera.IsWorkingChanged -= IsWorkingChanged;
}
camera = null;
CameraLCD.RemoveDisplay(Id);
registered = false;
functional = false;
panelComponent.Reset();
}
private void CustomDataChanged(IMyTerminalBlock block)
{
if (string.IsNullOrWhiteSpace(block.CustomData))
{
Unregister();
return;
}
string text = GetCameraName(block.CustomData);
MyGridTerminalSystem terminal = (MyGridTerminalSystem)MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(block.CubeGrid);
if (terminal == null)
{
Unregister();
return;
}
foreach(var b in terminal.Blocks)
{
if (b is MyCameraBlock cameraBlock && b.CustomName.ToString() == text)
{
Register(cameraBlock);
return;
}
}
Unregister();
}
private string GetCameraName(string customData)
{
string prefix = panelComponent.Area + ":";
using (StringReader reader = new StringReader(customData))
{
string line = reader.ReadLine();
while(line != null)
{
if (line.StartsWith(prefix) && line.Length > prefix.Length)
return line.Substring(prefix.Length);
line = reader.ReadLine();
}
}
return customData;
}
private void Camera_OnClose(VRage.Game.Entity.MyEntity e)
{
Unregister();
}
public override void Run()
{
base.Run(); // do not remove
if (!registered)
CustomDataChanged(terminalBlock);
}
/// <summary>
/// This is the only method where calls to render can be made directly.
/// </summary>
public bool OnDrawScene()
{
if (!TryGetTextureName(out string screenName) || camera == null || !functional)
return false;
if (panel != null)
{
// Textures for panel rotation dont get unregistered
if (panel.PanelComponent != panelComponent)
return false;
// Block 90 and 270 degree rotation
// TODO: Figure out why they crash on wide lcd panel
if (panelComponent.Area % 2 == 1)
return false;
}
MyCamera renderCamera = MySector.MainCamera;
if (renderCamera == null)
return false;
if (renderCamera.GetDistanceFromPoint(terminalBlock.WorldMatrix.Translation) > CameraLCD.Settings.Range)
return false;
// Set temporary settings
bool initialLods = true;
if (!CameraLCD.Settings.UpdateLOD)
initialLods = SetLoddingEnabled(false);
MyRenderSettings renderSettings = MyRender11.Settings;
MyRenderSettings oldRenderSettings = renderSettings;
renderSettings.User.GrassDensityFactor = 0;
renderSettings.User.GrassDrawDistance = 0;
MyRender11.Settings = renderSettings;
MyRenderDebugOverrides debugOverrides = MyRender11.DebugOverrides;
debugOverrides.Flares = false;
debugOverrides.Bloom = false;
debugOverrides.SSAO = false;
// Set camera position
float fov = GetCameraFov();
SetCameraViewMatrix(camera.GetViewMatrix(), renderCamera.ProjectionMatrix, renderCamera.ProjectionMatrixFar, fov, fov, renderCamera.NearPlaneDistance, renderCamera.FarPlaneDistance, renderCamera.FarFarPlaneDistance, camera.WorldMatrix.Translation, smooth: false);
// Draw the game to the screen
BorrowedRtvTexture texture = DrawGame();
DrawOnScreen(screenName, texture);
texture.Release();
// Restore camera position
SetCameraViewMatrix(renderCamera.ViewMatrix, renderCamera.ProjectionMatrix, renderCamera.ProjectionMatrixFar, renderCamera.FieldOfView, renderCamera.FieldOfView, renderCamera.NearPlaneDistance, renderCamera.FarPlaneDistance, renderCamera.FarFarPlaneDistance, renderCamera.Position, lastMomentUpdateIndex: 0, smooth: false);
// Restore settings
if (!CameraLCD.Settings.UpdateLOD)
SetLoddingEnabled(initialLods);
MyRender11.Settings = oldRenderSettings;
debugOverrides.Flares = true;
debugOverrides.Bloom = true;
debugOverrides.SSAO = true;
return true;
}
private float GetCameraFov()
{
return (float)m_fov.GetValue(camera);
}
private BorrowedRtvTexture DrawGame()
{
Vector2I sourceResolution = MyRender11.ResolutionI;
Vector2I targetResolution = (Vector2I)panelComponent.TextureSize;
MyRender11.ResolutionI = targetResolution;
if (CameraLCD.Settings.LockAspectRatio)
{
Vector2 targetReal = panelComponent.SurfaceSize;
float targetRatio = targetReal.X / targetReal.Y;
// Ex 512/512 = 1
float sourceRatio = (float)sourceResolution.X / sourceResolution.Y;
// Ex 1920/1080 = 1.7777777
if (targetRatio < sourceRatio)
{
int newHeight = (int)((targetResolution.Y / sourceRatio) * targetRatio);
int barHeight = (targetResolution.Y - newHeight) / 2;
if (bufferOffset == 0)
buffer = new byte[0];
bufferOffset = barHeight * targetResolution.X * 4;
if (bufferOffset < 0)
bufferOffset = 0; // Theoretically this is impossible
else
targetResolution.Y = newHeight;
}
else
{
bufferOffset = 0;
}
}
else
{
bufferOffset = 0;
}
BorrowedRtvTexture texture = MyManagers.RwTexturesPool.BorrowRtv("CameraLCDRevivedRenderer", targetResolution.X, targetResolution.Y, Format.R8G8B8A8_UNorm_SRgb);
DrawCharacterHead();
MyRender11.DrawGameScene(texture, out _);
MyRender11.ResolutionI = sourceResolution;
return texture;
}
private void DrawCharacterHead()
{
if (CameraLCD.Settings.HeadFix && MySession.Static != null)
{
MyCharacter cha = MySession.Static.LocalCharacter;
if (cha != null && cha.Render != null && cha.IsInFirstPersonView)
{
// Reference: MyCharacter.UpdateHeadModelProperties(bool enabled)
uint id = cha.Render.RenderObjectIDs[0];
if (id != uint.MaxValue)
{
foreach (string materialName in cha.Definition.MaterialsDisabledIn1st)
{
// Reference: MyRenderProxy.UpdateModelProperties(id, mat, RenderFlags.Visible, 0, null, null);
MyRenderMessageUpdateModelProperties test = MyRenderProxy.MessagePool.Get<MyRenderMessageUpdateModelProperties>(MyRenderMessageEnum.UpdateModelProperties);
test.ID = id;
test.MaterialName = materialName;
test.FlagsChange = new RenderFlagsChange()
{
Add = RenderFlags.Visible,
Remove = 0,
};
test.DiffuseColor = null;
test.Emissivity = null;
MyRender11.ProcessMessage(test);
}
}
}
}
}
private bool SetLoddingEnabled(bool enabled)
{
// Reference: MyRender11.ProcessMessageInternal(MyRenderMessageBase message, int frameId)
// case MyRenderMessageEnum.UpdateNewLoddingSettings
MyNewLoddingSettings loddingSettings = MyCommon.LoddingSettings;
MyGlobalLoddingSettings globalSettings = loddingSettings.Global;
bool initial = globalSettings.IsUpdateEnabled;
if (initial == enabled)
return initial;
globalSettings.IsUpdateEnabled = enabled;
loddingSettings.Global = globalSettings;
MyManagers.GeometryRenderer.IsLodUpdateEnabled = enabled;
MyManagers.GeometryRenderer.Settings = globalSettings;
MyManagers.ModelFactory.OnLoddingSettingChanged();
return initial;
}
private void SetCameraViewMatrix(MatrixD viewMatrix, Matrix projectionMatrix, Matrix projectionFarMatrix, float fov, float fovSkybox, float nearPlane, float farPlane, float farFarPlane, Vector3D cameraPosition, float projectionOffsetX = 0f, float projectionOffsetY = 0f, int lastMomentUpdateIndex = 1, bool smooth = true)
{
MyRenderMessageSetCameraViewMatrix renderMessage = MyRenderProxy.MessagePool.Get<MyRenderMessageSetCameraViewMatrix>(MyRenderMessageEnum.SetCameraViewMatrix);
renderMessage.ViewMatrix = viewMatrix;
renderMessage.ProjectionMatrix = projectionMatrix;
renderMessage.ProjectionFarMatrix = projectionFarMatrix;
renderMessage.FOV = fov;
renderMessage.FOVForSkybox = fovSkybox;
renderMessage.NearPlane = nearPlane;
renderMessage.FarPlane = farPlane;
renderMessage.FarFarPlane = farFarPlane;
renderMessage.CameraPosition = cameraPosition;
renderMessage.LastMomentUpdateIndex = lastMomentUpdateIndex;
renderMessage.ProjectionOffsetX = projectionOffsetX;
renderMessage.ProjectionOffsetY = projectionOffsetY;
renderMessage.Smooth = smooth;
MyRender11.SetupCameraMatrices(renderMessage);
}
private bool TryGetTextureName(out string name)
{
try
{
name = panelComponent.GetRenderTextureName();
return true;
}
catch (NullReferenceException)
{
name = null;
return false;
}
}
private void DrawOnScreen(string textureName, BorrowedRtvTexture texture)
{
int requiredSize = panelComponent.TextureByteCount;
if (buffer.Length < requiredSize)
buffer = new byte[requiredSize];
texture.GetData(buffer, bufferOffset, buffer.Length - (2 * bufferOffset));
MyManagers.FileTextures.ResetGeneratedTexture(textureName, buffer);
}
}
}