-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelPreviewWindow.xaml.cs
More file actions
377 lines (321 loc) · 10.8 KB
/
ModelPreviewWindow.xaml.cs
File metadata and controls
377 lines (321 loc) · 10.8 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
371
372
373
374
375
376
377
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using WpfPoint = System.Windows.Point;
using WpfMouseEventArgs = System.Windows.Input.MouseEventArgs;
namespace CFRezManager;
public partial class ModelPreviewWindow : Window
{
private const double InitialOrbitYaw = -35;
private const double InitialOrbitPitch = 22;
private const double InitialDistance = 8;
private const double InitialViewYaw = 145;
private const double InitialViewPitch = -22;
private const double MouseSensitivity = 0.16;
private const double MoveSpeed = 3.6;
private const double FastMoveMultiplier = 3.0;
private const double WheelMoveStep = 0.55;
private readonly PerspectiveCamera _camera = new();
private readonly HashSet<Key> _pressedKeys = new();
private Point3D _cameraPosition;
private TimeSpan? _lastMovementRenderingTime;
private double _yaw = InitialViewYaw;
private double _pitch = InitialViewPitch;
private bool _ignoreNextMouseMove;
private bool _isRenderingSubscribed;
private bool _isFreeLookActive;
public ModelPreviewWindow(
string fileName,
LithTechModelDocument document,
string? modelInfo = null,
Func<string, ImageSource?>? textureResolver = null)
{
InitializeComponent();
Rect workArea = SystemParameters.WorkArea;
MaxWidth = Math.Max(MinWidth, workArea.Width - 80);
MaxHeight = Math.Max(MinHeight, workArea.Height - 80);
LithTechModelDocument renderDocument = LithTechThumbnailGeometryReducer.ReduceForInteractivePreview(document);
ModelViewport.Camera = _camera;
BuildScene(renderDocument, textureResolver);
ResetCameraState();
UpdateCamera();
PreviewInfoText.Text = FormatDisplayInfo(document, renderDocument, modelInfo);
Title = $"{fileName} - {document.Name}";
}
private void BuildScene(LithTechModelDocument document, Func<string, ImageSource?>? textureResolver)
{
ModelViewport.Children.Add(new ModelVisual3D { Content = LithTechModelSceneBuilder.CreateScene(document, textureResolver) });
}
private void UpdateCamera()
{
_camera.Position = _cameraPosition;
_camera.LookDirection = GetForwardDirection();
_camera.UpDirection = new Vector3D(0, 1, 0);
_camera.FieldOfView = 45;
_camera.NearPlaneDistance = 0.01;
_camera.FarPlaneDistance = 1000;
}
private void ModelViewport_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
BeginFreeLook();
e.Handled = true;
}
private void ModelViewport_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
EndFreeLook();
e.Handled = true;
}
private void ModelViewport_MouseMove(object sender, WpfMouseEventArgs e)
{
if (!_isFreeLookActive)
{
return;
}
WpfPoint current = e.GetPosition(ModelViewport);
WpfPoint center = GetViewportCenter();
Vector delta = current - center;
if (_ignoreNextMouseMove)
{
_ignoreNextMouseMove = false;
if (Math.Abs(delta.X) < 1 && Math.Abs(delta.Y) < 1)
{
return;
}
}
if (Math.Abs(delta.X) < 0.5 && Math.Abs(delta.Y) < 0.5)
{
return;
}
_yaw -= delta.X * MouseSensitivity;
_pitch = Math.Clamp(_pitch - delta.Y * MouseSensitivity, -89, 89);
UpdateCamera();
CenterMouseInViewport();
}
private void ModelViewport_MouseWheel(object sender, MouseWheelEventArgs e)
{
double direction = e.Delta > 0 ? 1 : -1;
_cameraPosition += GetForwardDirection() * (direction * WheelMoveStep);
UpdateCamera();
}
private void ModelViewport_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
EndFreeLook();
e.Handled = true;
return;
}
Key key = e.Key;
if (IsMovementKey(key))
{
BeginFreeLook();
_pressedKeys.Add(key);
e.Handled = true;
}
}
private void ModelViewport_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (_pressedKeys.Remove(e.Key))
{
e.Handled = true;
}
}
private void ModelViewport_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EndFreeLook();
}
private void ModelViewport_LostMouseCapture(object sender, WpfMouseEventArgs e)
{
if (_isFreeLookActive)
{
EndFreeLook();
}
}
private void ResetViewButton_Click(object sender, RoutedEventArgs e)
{
ResetCameraState();
UpdateCamera();
if (_isFreeLookActive)
{
CenterMouseInViewport();
}
}
protected override void OnDeactivated(EventArgs e)
{
EndFreeLook();
base.OnDeactivated(e);
}
protected override void OnClosed(EventArgs e)
{
EndFreeLook();
StopMovementRendering();
base.OnClosed(e);
}
private void BeginFreeLook()
{
if (_isFreeLookActive)
{
return;
}
_isFreeLookActive = true;
_lastMovementRenderingTime = null;
ModelViewport.Focus();
ModelViewport.CaptureMouse();
Mouse.OverrideCursor = System.Windows.Input.Cursors.None;
CenterMouseInViewport();
StartMovementRendering();
}
private void EndFreeLook()
{
if (!_isFreeLookActive)
{
return;
}
_isFreeLookActive = false;
_ignoreNextMouseMove = false;
_lastMovementRenderingTime = null;
_pressedKeys.Clear();
StopMovementRendering();
if (ModelViewport.IsMouseCaptured)
{
ModelViewport.ReleaseMouseCapture();
}
Mouse.OverrideCursor = null;
}
private void StartMovementRendering()
{
if (_isRenderingSubscribed)
{
return;
}
CompositionTarget.Rendering += CompositionTarget_Rendering;
_isRenderingSubscribed = true;
}
private void StopMovementRendering()
{
if (!_isRenderingSubscribed)
{
return;
}
CompositionTarget.Rendering -= CompositionTarget_Rendering;
_isRenderingSubscribed = false;
}
private void CompositionTarget_Rendering(object? sender, EventArgs e)
{
TimeSpan renderingTime = e is RenderingEventArgs renderingEventArgs
? renderingEventArgs.RenderingTime
: TimeSpan.Zero;
double elapsedSeconds = 0;
if (_lastMovementRenderingTime is TimeSpan lastRenderingTime)
{
elapsedSeconds = Math.Clamp((renderingTime - lastRenderingTime).TotalSeconds, 0, 0.05);
}
_lastMovementRenderingTime = renderingTime;
if (!_isFreeLookActive || _pressedKeys.Count == 0 || elapsedSeconds <= 0)
{
return;
}
Vector3D movement = new();
Vector3D forward = GetForwardDirection();
Vector3D right = Vector3D.CrossProduct(forward, new Vector3D(0, 1, 0));
if (right.LengthSquared > 0)
{
right.Normalize();
}
if (_pressedKeys.Contains(Key.W))
{
movement += forward;
}
if (_pressedKeys.Contains(Key.S))
{
movement -= forward;
}
if (_pressedKeys.Contains(Key.D))
{
movement += right;
}
if (_pressedKeys.Contains(Key.A))
{
movement -= right;
}
if (movement.LengthSquared <= 0)
{
return;
}
movement.Normalize();
double speed = IsFastMovePressed() ? MoveSpeed * FastMoveMultiplier : MoveSpeed;
_cameraPosition += movement * (speed * elapsedSeconds);
UpdateCamera();
}
private void ResetCameraState()
{
_yaw = InitialViewYaw;
_pitch = InitialViewPitch;
_cameraPosition = CreateInitialCameraPosition();
}
private Vector3D GetForwardDirection()
{
double yawRadians = _yaw * Math.PI / 180;
double pitchRadians = _pitch * Math.PI / 180;
var forward = new Vector3D(
Math.Cos(pitchRadians) * Math.Sin(yawRadians),
Math.Sin(pitchRadians),
Math.Cos(pitchRadians) * Math.Cos(yawRadians));
forward.Normalize();
return forward;
}
private void CenterMouseInViewport()
{
if (ModelViewport.ActualWidth <= 0 || ModelViewport.ActualHeight <= 0)
{
return;
}
WpfPoint screenCenter = ModelViewport.PointToScreen(GetViewportCenter());
_ignoreNextMouseMove = true;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(
(int)Math.Round(screenCenter.X),
(int)Math.Round(screenCenter.Y));
}
private WpfPoint GetViewportCenter()
{
return new WpfPoint(ModelViewport.ActualWidth / 2, ModelViewport.ActualHeight / 2);
}
private static Point3D CreateInitialCameraPosition()
{
double pitchRadians = InitialOrbitPitch * Math.PI / 180;
double yawRadians = InitialOrbitYaw * Math.PI / 180;
double horizontal = InitialDistance * Math.Cos(pitchRadians);
return new Point3D(
horizontal * Math.Sin(yawRadians),
InitialDistance * Math.Sin(pitchRadians),
horizontal * Math.Cos(yawRadians));
}
private static bool IsMovementKey(Key key)
{
return key is Key.W or Key.A or Key.S or Key.D;
}
private static bool IsFastMovePressed()
{
return Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
}
private static string FormatDocumentInfo(LithTechModelDocument document)
{
return $"{document.StorageDescription} | {document.Meshes.Count:N0} mesh | {document.VertexCount:N0} vertices | {document.TriangleCount:N0} triangles";
}
private static string FormatDisplayInfo(
LithTechModelDocument originalDocument,
LithTechModelDocument renderDocument,
string? modelInfo)
{
string info = modelInfo ?? FormatDocumentInfo(originalDocument);
if (ReferenceEquals(originalDocument, renderDocument) ||
originalDocument.Meshes.Count == renderDocument.Meshes.Count &&
originalDocument.VertexCount == renderDocument.VertexCount &&
originalDocument.TriangleCount == renderDocument.TriangleCount)
{
return info;
}
return $"{info} | rendering {renderDocument.Meshes.Count:N0} mesh / {renderDocument.VertexCount:N0} vertices / {renderDocument.TriangleCount:N0} triangles";
}
}