-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLithTechModelThumbnailRenderer.cs
More file actions
94 lines (83 loc) · 2.73 KB
/
LithTechModelThumbnailRenderer.cs
File metadata and controls
94 lines (83 loc) · 2.73 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
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using MediaColor = System.Windows.Media.Color;
using WpfSize = System.Windows.Size;
namespace CFRezManager;
internal static class LithTechModelThumbnailRenderer
{
private const int ThumbnailSize = 192;
public static ImageSource? TryRender(LithTechModelDocument document)
{
ImageSource? thumbnail = null;
var renderThread = new Thread(() =>
{
try
{
thumbnail = RenderOnCurrentThread(document);
}
catch
{
thumbnail = null;
}
});
renderThread.SetApartmentState(ApartmentState.STA);
renderThread.IsBackground = true;
renderThread.Start();
renderThread.Join();
return thumbnail;
}
private static ImageSource RenderOnCurrentThread(LithTechModelDocument document)
{
var root = new Grid
{
Width = ThumbnailSize,
Height = ThumbnailSize,
Background = new SolidColorBrush(MediaColor.FromRgb(0x11, 0x18, 0x27))
};
var viewport = new Viewport3D
{
Width = ThumbnailSize,
Height = ThumbnailSize,
ClipToBounds = true,
Camera = CreateCamera()
};
viewport.Children.Add(new ModelVisual3D
{
Content = LithTechModelSceneBuilder.CreateScene(document)
});
root.Children.Add(viewport);
WpfSize renderSize = new(ThumbnailSize, ThumbnailSize);
root.Measure(renderSize);
root.Arrange(new Rect(renderSize));
root.UpdateLayout();
var bitmap = new RenderTargetBitmap(ThumbnailSize, ThumbnailSize, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(root);
bitmap.Freeze();
return bitmap;
}
private static PerspectiveCamera CreateCamera()
{
const double yaw = -35;
const double pitch = 22;
const double distance = 8;
double pitchRadians = pitch * Math.PI / 180;
double yawRadians = yaw * Math.PI / 180;
double horizontal = distance * Math.Cos(pitchRadians);
var position = new Point3D(
horizontal * Math.Sin(yawRadians),
distance * Math.Sin(pitchRadians),
horizontal * Math.Cos(yawRadians));
return new PerspectiveCamera
{
Position = position,
LookDirection = new Vector3D(-position.X, -position.Y, -position.Z),
UpDirection = new Vector3D(0, 1, 0),
FieldOfView = 45,
NearPlaneDistance = 0.01,
FarPlaneDistance = 1000
};
}
}