1
1
#include < optional>
2
2
#include < tuple>
3
3
4
+ #include < DirectxColors.h>
5
+
4
6
#include < imgui.h>
5
7
#include < ImGuizmo.h>
6
8
50
52
51
53
#endif
52
54
55
+ #include " ../BackBufferRenderer.hpp"
53
56
#include " ObjectExplorer.hpp"
54
57
#include " ChainViewer.hpp"
55
58
@@ -59,6 +62,39 @@ std::optional<std::string> ChainViewer::on_initialize() {
59
62
return Mod::on_initialize ();
60
63
}
61
64
65
+ std::optional<std::string> ChainViewer::on_initialize_d3d_thread () {
66
+ if (g_framework->is_dx12 ()) {
67
+ DirectX::EffectPipelineStateDescription psd (
68
+ &DirectX::DX12::GeometricPrimitive::VertexType::InputLayout,
69
+ DirectX::DX12::CommonStates::AlphaBlend,
70
+ DirectX::DX12::CommonStates::DepthNone,
71
+ DirectX::DX12::CommonStates::CullCounterClockwise,
72
+ BackBufferRenderer::get ()->get_default_rt_state ()
73
+ );
74
+
75
+ auto device = g_framework->get_d3d12_hook ()->get_device ();
76
+
77
+ m_d3d12.effect = std::make_unique<DirectX::DX12::BasicEffect>(device, DirectX::EffectFlags::None, psd);
78
+ m_d3d12.effect ->SetWorld (DirectX::SimpleMath::Matrix::Identity);
79
+ m_d3d12.effect ->SetView (DirectX::SimpleMath::Matrix::Identity);
80
+ m_d3d12.effect ->SetProjection (DirectX::SimpleMath::Matrix::Identity);
81
+ m_d3d12.effect ->SetDiffuseColor (DirectX::Colors::Blue);
82
+ m_d3d12.effect ->SetAlpha (m_effect_alpha);
83
+
84
+ m_d3d12.cylinder = DirectX::GeometricPrimitive::CreateCylinder ();
85
+ m_d3d12.sphere = DirectX::GeometricPrimitive::CreateSphere ();
86
+
87
+ spdlog::info (" ChainViewer D3D12 initialized" );
88
+ } else {
89
+ // TODO
90
+ spdlog::info (" ChainViewer D3D11 initialized" );
91
+ }
92
+
93
+ // OK
94
+ return Mod::on_initialize ();
95
+ }
96
+
97
+
62
98
void ChainViewer::on_config_load (const utility::Config& cfg) {
63
99
for (IModValue& option : m_options) {
64
100
option.config_load (cfg);
@@ -82,6 +118,21 @@ void ChainViewer::on_draw_dev_ui() {
82
118
if (m_enabled->draw (" Enabled" ) && !m_enabled->value ()) {
83
119
// todo
84
120
}
121
+
122
+ if (ImGui::SliderFloat (" Effect Alpha" , &m_effect_alpha, 0 .0f , 1 .0f )) {
123
+ m_effect_dirty = true ;
124
+ }
125
+ }
126
+
127
+ void ChainViewer::on_present () {
128
+ if (g_framework->is_dx12 ()) {
129
+ if (m_effect_dirty) {
130
+ m_d3d12.effect ->SetAlpha (m_effect_alpha);
131
+ m_effect_dirty = false ;
132
+ }
133
+ } else {
134
+ // TODO
135
+ }
85
136
}
86
137
87
138
void ChainViewer::on_frame () {
@@ -145,13 +196,24 @@ void ChainViewer::on_frame() {
145
196
return ;
146
197
}
147
198
148
- Matrix4x4f proj{}, view{};
199
+ __declspec ( align ( 16 )) Matrix4x4f proj{}, view{};
149
200
150
201
const auto camera_origin = sdk::get_transform_position (camera_transform);
151
202
152
203
sdk::call_object_func<void *>(camera, " get_ProjectionMatrix" , &proj, context, camera);
153
204
sdk::call_object_func<void *>(camera, " get_ViewMatrix" , &view, context, camera);
154
205
206
+ auto proj_directx = DirectX::SimpleMath::Matrix{&proj[0 ][0 ]};
207
+ auto view_directx = DirectX::SimpleMath::Matrix{&view[0 ][0 ]};
208
+
209
+ std::vector<BackBufferRenderer::D3D12RenderWorkFn> d3d12_work{};
210
+
211
+ // Set the view and projection matrices for the effect once per frame
212
+ d3d12_work.emplace_back ([this , proj_directx, view_directx](ID3D12GraphicsCommandList* cmd_list) {
213
+ m_d3d12.effect ->SetProjection (proj_directx);
214
+ m_d3d12.effect ->SetView (view_directx);
215
+ });
216
+
155
217
/* view = view * Matrix4x4f {
156
218
-1, 0, 0, 0,
157
219
0, 1, 0, 0,
@@ -256,11 +318,23 @@ void ChainViewer::on_frame() {
256
318
257
319
// Draw spheres/capsules and imguizmo widgets
258
320
if (collider.pair_joint == nullptr ) {
259
- imgui::draw_sphere (adjusted_pos1, collider.sphere .r , ImGui::GetColorU32 (col), true );
260
321
261
322
Matrix4x4f mat = glm::scale (Vector3f{collider.sphere .r , collider.sphere .r , collider.sphere .r });
262
323
mat[3 ] = Vector4f{adjusted_pos1, 1 .0f };
263
324
325
+ if (g_framework->is_dx12 ()) {
326
+ d3d12_work.emplace_back ([this , adjusted_pos1, radius = collider.sphere .r ](ID3D12GraphicsCommandList* cmd_list){
327
+ DirectX::SimpleMath::Matrix world = DirectX::SimpleMath::Matrix::CreateScale (radius) * DirectX::SimpleMath::Matrix::CreateTranslation (adjusted_pos1.x , adjusted_pos1.y , adjusted_pos1.z );
328
+ m_d3d12.effect ->SetWorld (world);
329
+
330
+ m_d3d12.effect ->Apply (cmd_list);
331
+ m_d3d12.sphere ->Draw (cmd_list);
332
+ });
333
+ } else {
334
+ // TODO
335
+ imgui::draw_sphere (adjusted_pos1, collider.sphere .r , ImGui::GetColorU32 (col), true );
336
+ }
337
+
264
338
const auto screen_pos1 = sdk::renderer::world_to_screen (adjusted_pos1);
265
339
const auto screen_pos1_top = sdk::renderer::world_to_screen (adjusted_pos1 + Vector3f{0 .0f , collider.sphere .r , 0 .0f });
266
340
const auto cursor_pos = *(Vector2f*)&ImGui::GetIO ().MousePos ;
@@ -278,7 +352,27 @@ void ChainViewer::on_frame() {
278
352
}
279
353
} else {
280
354
// Capsule
281
- imgui::draw_capsule (adjusted_pos1, adjusted_pos2, collider.capsule .r , ImGui::GetColorU32 (col), true );
355
+ if (g_framework->is_dx12 ()) {
356
+ d3d12_work.emplace_back ([this , adjusted_pos1, adjusted_pos2, radius = collider.capsule .r ](ID3D12GraphicsCommandList* cmd_list){
357
+ const auto delta = adjusted_pos2 - adjusted_pos1;
358
+ const auto dir = glm::normalize (delta);
359
+ const auto length = glm::length (delta) + (radius * 2 .0f );
360
+ const auto center = (adjusted_pos1 + adjusted_pos2) * 0 .5f ;
361
+ DirectX::SimpleMath::Matrix world =
362
+ DirectX::SimpleMath::Matrix::CreateScale (radius * 2 .0f , radius * 2 .0f , length) *
363
+ DirectX::SimpleMath::Matrix::CreateLookAt (DirectX::SimpleMath::Vector3::Zero, DirectX::SimpleMath::Vector3 (dir.x , dir.y , dir.z ), DirectX::SimpleMath::Vector3::Up).Invert () *
364
+ DirectX::SimpleMath::Matrix::CreateTranslation (center.x , center.y , center.z );
365
+
366
+ m_d3d12.effect ->SetWorld (world);
367
+
368
+ m_d3d12.effect ->Apply (cmd_list);
369
+ m_d3d12.sphere ->Draw (cmd_list);
370
+ });
371
+ } else {
372
+ // TODO
373
+ imgui::draw_capsule (adjusted_pos1, adjusted_pos2, collider.capsule .r , ImGui::GetColorU32 (col), true );
374
+ }
375
+
282
376
283
377
const auto screen_pos1 = sdk::renderer::world_to_screen (adjusted_pos1);
284
378
const auto screen_pos1_top = sdk::renderer::world_to_screen (adjusted_pos1 + Vector3f{0 .0f , collider.capsule .r , 0 .0f });
@@ -428,5 +522,9 @@ void ChainViewer::on_frame() {
428
522
}
429
523
430
524
ImGui::End ();
525
+
526
+ if (g_framework->is_dx12 ()) {
527
+ BackBufferRenderer::get ()->submit_work_d3d12 (std::move (d3d12_work));
528
+ }
431
529
}
432
530
0 commit comments