Debian Codegraph @

radiant/ui/materials/editor/MaterialPreview.cpp

raw
1 #include "MaterialPreview.h"
2
3 #include "i18n.h"
4 #include "ibrush.h"
5 #include "ientity.h"
6 #include "imodelcache.h"
7 #include "ieclass.h"
8 #include "ishaders.h"
9 #include "string/convert.h"
10 #include "math/pi.h"
11 #include "wxutil/dialog/MessageBox.h"
12 #include "wxutil/Bitmap.h"
13 #include <wx/toolbar.h>
14 #include "entitylib.h"
15 #include "gamelib.h"
16 #include "scenelib.h"
17
18 namespace ui
19 {
20
21 namespace
22 {
23 const char* const FUNC_STATIC_CLASS = "func_static";
24 const char* const GKEY_DEFAULT_ROOM_MATERIAL = "/materialPreview/defaultRoomMaterial";
25 const char* const GKEY_DEFAULT_LIGHT_DEF = "/materialPreview/defaultLightDef";
26
27 inline bool isSkyboxMaterial(const MaterialPtr& material)
28 {
29 if (!material) return false;
30
31 auto result = false;
32
33 material->foreachLayer([&](const IShaderLayer::Ptr& layer)
34 {
35 if (layer->getMapType() == IShaderLayer::MapType::CameraCubeMap)
36 {
37 result = true;
38 return false;
39 }
40
41 return true;
42 });
43
44 return result;
45 }
46 }
47
48 MaterialPreview::MaterialPreview(wxWindow* parent) :
49 RenderPreview(parent, true),
50 _sceneIsReady(false),
51 _roomMaterial(game::current::getValue<std::string>(GKEY_DEFAULT_ROOM_MATERIAL)),
52 _defaultCamDistanceFactor(2.0f),
53 _lightClassname(getDefaultLightDef())
54 {
55 _testModelSkin = std::make_unique<TestModelSkin>("model");
56 _testRoomSkin = std::make_unique<TestModelSkin>("room");
57
58 setupToolbar();
59
60 setStartupLightingMode(true);
61 setViewOrigin(Vector3(1, 1, 1) * 100);
62 setViewAngles(Vector3(37, 135, 0));
63 }
64
65 MaterialPreview::~MaterialPreview()
66 {
67 _testModelSkin.reset();
68 _testRoomSkin.reset();
69 }
70
71 void MaterialPreview::setupToolbar()
72 {
73 // Add one additional toolbar
74 wxToolBar* toolbar = new wxToolBar(_mainPanel, wxID_ANY);
75 toolbar->SetToolBitmapSize(wxSize(16, 16));
76
77 _testModelCubeButton = toolbar->AddRadioTool(wxID_ANY, "", wxutil::GetLocalBitmap("cube.png", wxART_TOOLBAR));
78 _testModelCubeButton->SetShortHelp(_("Show Cube"));
79 toolbar->ToggleTool(_testModelCubeButton->GetId(), true);
80
81 _testModelSphereButton = toolbar->AddRadioTool(wxID_ANY, "", wxutil::GetLocalBitmap("sphere.png", wxART_TOOLBAR));
82 _testModelSphereButton->SetShortHelp(_("Show Sphere"));
83
84 _testModelTilesButton = toolbar->AddRadioTool(wxID_ANY, "", wxutil::GetLocalBitmap("tiles.png", wxART_TOOLBAR));
85 _testModelTilesButton->SetShortHelp(_("Show Tiles"));
86
87 toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onTestModelSelectionChanged, this, _testModelCubeButton->GetId());
88 toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onTestModelSelectionChanged, this, _testModelSphereButton->GetId());
89 toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onTestModelSelectionChanged, this, _testModelTilesButton->GetId());
90
91 toolbar->AddSeparator();
92
93 _swapBackgroundMaterialButton = toolbar->AddTool(wxID_ANY, "Swap", wxutil::GetLocalBitmap("swap_background.png", wxART_TOOLBAR));
94 _swapBackgroundMaterialButton->SetToggle(true);
95 toolbar->ToggleTool(_swapBackgroundMaterialButton->GetId(), false);
96 _swapBackgroundMaterialButton->SetShortHelp(_("Swap Background and Foreground Materials"));
97
98 toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onSwapBackgroundMaterialChanged, this, _swapBackgroundMaterialButton->GetId());
99
100 toolbar->Realize();
101
102 addToolbar(toolbar);
103 }
104
105 const MaterialPtr& MaterialPreview::getMaterial()
106 {
107 return _material;
108 }
109
110 void MaterialPreview::updateModelSkin(const std::string& material)
111 {
112 // Assign the material to the temporary skin
113 _testModelSkin->setRemapMaterial(GlobalMaterialManager().getMaterial(material));
114
115 if (!_model) return;
116
117 // Hide the model if there's no material to preview
118 _model->setFiltered(_testModelSkin->isEmpty());
119
120 // Let the model update its remaps
121 auto skinnedModel = std::dynamic_pointer_cast<SkinnedModel>(_model);
122
123 if (skinnedModel)
124 {
125 skinnedModel->skinChanged(_testModelSkin->getSkinName());
126 }
127 }
128
129 const std::string& MaterialPreview::getRoomMaterial()
130 {
131 return _roomMaterial;
132 }
133
134 void MaterialPreview::setRoomMaterial(const std::string& material)
135 {
136 _roomMaterial = material;
137 updateSceneMaterials();
138
139 signal_SceneChanged().emit();
140 }
141
142 MaterialPreview::TestModel MaterialPreview::getTestModelType()
143 {
144 if (_testModelCubeButton->IsToggled()) return TestModel::Cube;
145 if (_testModelSphereButton->IsToggled()) return TestModel::Sphere;
146 if (_testModelTilesButton->IsToggled()) return TestModel::Tiles;
147
148 return TestModel::Cube;
149 }
150
151 void MaterialPreview::setTestModelType(TestModel type)
152 {
153 _testModelCubeButton->GetToolBar()->ToggleTool(_testModelCubeButton->GetId(), type == TestModel::Cube);
154 _testModelSphereButton->GetToolBar()->ToggleTool(_testModelSphereButton->GetId(), type == TestModel::Sphere);
155 _testModelTilesButton->GetToolBar()->ToggleTool(_testModelTilesButton->GetId(), type == TestModel::Tiles);
156
157 setupTestModel();
158 }
159
160 std::string MaterialPreview::GetTestModelTypeName(TestModel type)
161 {
162 switch (type)
163 {
164 case TestModel::Cube: return "Cube";
165 case TestModel::Sphere: return "Sphere";
166 case TestModel::Tiles: return "Tiles";
167 default: throw std::invalid_argument("Unknown preview model type");
168 }
169 }
170
171 MaterialPreview::TestModel MaterialPreview::GetTestModelType(const std::string& typeName)
172 {
173 if (typeName == "Cube") return TestModel::Cube;
174 if (typeName == "Sphere") return TestModel::Sphere;
175 if (typeName == "Tiles") return TestModel::Tiles;
176
177 return TestModel::Cube; // in case bad serialization data reaches this point
178 }
179
180 std::string MaterialPreview::getDefaultLightDef()
181 {
182 auto className = game::current::getValue<std::string>(GKEY_DEFAULT_LIGHT_DEF);
183
184 if (className.empty() || !GlobalEntityClassManager().findClass(className))
185 {
186 className = "light";
187 }
188
189 return className;
190 }
191
192 void MaterialPreview::updateRoomSkin(const std::string& roomMaterial)
193 {
194 _testRoomSkin->setRemapMaterial(GlobalMaterialManager().getMaterial(roomMaterial));
195
196 // Let the model update its remaps
197 auto skinnedRoom = std::dynamic_pointer_cast<SkinnedModel>(_room);
198
199 if (skinnedRoom)
200 {
201 skinnedRoom->skinChanged(_testRoomSkin->getSkinName());
202 }
203 }
204
205 void MaterialPreview::setMaterial(const MaterialPtr& material)
206 {
207 bool hadMaterial = _material != nullptr;
208
209 _materialChanged.disconnect();
210
211 _material = material;
212
213 if (_material)
214 {
215 _materialChanged = _material->sig_materialChanged().connect(
216 sigc::mem_fun(this, &MaterialPreview::onMaterialChanged));
217 }
218
219 _sceneIsReady = false;
220
221 // Detect whether we should apply this material to the background
222 _swapBackgroundMaterial = isSkyboxMaterial(_material);
223 _swapBackgroundMaterialButton->GetToolBar()->ToggleTool(_swapBackgroundMaterialButton->GetId(), _swapBackgroundMaterial);
224
225 updateSceneMaterials();
226
227 if (!hadMaterial && _material)
228 {
229 startPlayback();
230 }
231 else if (hadMaterial && !_material)
232 {
233 stopPlayback();
234 }
235
236 queueDraw();
237 }
238
239 void MaterialPreview::updateSceneMaterials()
240 {
241 auto foregroundMaterial = _material ? _material->getName() : _roomMaterial;
242 auto backgroundMaterial = _roomMaterial;
243
244 if (_swapBackgroundMaterial)
245 {
246 std::swap(foregroundMaterial, backgroundMaterial);
247 }
248
249 updateModelSkin(foregroundMaterial);
250 updateRoomSkin(backgroundMaterial);
251 }
252
253 void MaterialPreview::enableFrobHighlight(bool enable)
254 {
255 if (!_entity) return;
256
257 Node_getEntity(_entity)->setKeyValue("shaderParm11", enable ? "1" : "0");
258 queueDraw();
259 }
260
261 void MaterialPreview::onMaterialChanged()
262 {
263 queueDraw();
264 }
265
266 bool MaterialPreview::onPreRender()
267 {
268 if (!_sceneIsReady)
269 {
270 prepareScene();
271 }
272
273 // Update the rotation of the func_static
274 if (_model)
275 {
276 auto time = _renderSystem->getTime();
277
278 // one full rotation per 10 seconds
279 auto newAngle = 2 * math::PI * time / 10000;
280
281 Node_getEntity(_entity)->setKeyValue("angle", string::to_string(radians_to_degrees(newAngle)));
282 }
283
284 return RenderPreview::onPreRender();
285 }
286
287 void MaterialPreview::prepareScene()
288 {
289 _sceneIsReady = true;
290 }
291
292 bool MaterialPreview::canDrawGrid()
293 {
294 return false;
295 }
296
297 RenderStateFlags MaterialPreview::getRenderFlagsFill()
298 {
299 return RenderPreview::getRenderFlagsFill() | RENDER_DEPTHWRITE | RENDER_DEPTHTEST;
300 }
301
302 void MaterialPreview::setupSceneGraph()
303 {
304 RenderPreview::setupSceneGraph();
305
306 try
307 {
308 _rootNode = std::make_shared<scene::BasicRootNode>();
309
310 setupRoom();
311
312 _entity = GlobalEntityModule().createEntity(
313 GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS));
314
315 // Make sure the shaderParm11 spawnarg is present
316 Node_getEntity(_entity)->setKeyValue("shaderParm11", "0");
317 Node_getEntity(_entity)->setKeyValue("model", "-");
318
319 _rootNode->addChildNode(_entity);
320
321 setupTestModel();
322
323 // Hide all models that are not the test model
324 _entity->foreachNode([&](const scene::INodePtr& child)
325 {
326 if (Node_getModel(child) && child != _model)
327 {
328 child->enable(scene::Node::eHidden);
329 }
330 return true;
331 });
332
333 getScene()->setRoot(_rootNode);
334
335 // Set up the light
336 _light = GlobalEntityModule().createEntity(GlobalEntityClassManager().findClass(_lightClassname));
337
338 Node_getEntity(_light)->setKeyValue("light_radius", "750 750 750");
339 Node_getEntity(_light)->setKeyValue("origin", "150 150 150");
340
341 scene::addNodeToContainer(_light, _rootNode);
342
343 // Initialise the view origin and angles, if thery're still empty
344 if (getViewOrigin() == Vector3(0,0,0))
345 {
346 // Reset the default view, facing down to the model from diagonally above the bounding box
347 double distance = _model->localAABB().getRadius() * _defaultCamDistanceFactor;
348
349 setViewOrigin(Vector3(1, 1, 1) * distance);
350 setViewAngles(Vector3(37, 135, 0));
351 }
352
353 signal_SceneChanged().emit();
354 }
355 catch (std::runtime_error&)
356 {
357 wxutil::Messagebox::ShowError(fmt::format(_("Unable to setup the preview,\n"
358 "could not find the entity class {0}"), FUNC_STATIC_CLASS));
359 }
360 }
361
362 void MaterialPreview::setupRoom()
363 {
364 _room = GlobalModelCache().getModelNodeForStaticResource("preview/room_cuboid.ase");
365
366 auto roomEntity = GlobalEntityModule().createEntity(
367 GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS));
368
369 _rootNode->addChildNode(roomEntity);
370
371 roomEntity->addChildNode(_room);
372
373 updateRoomSkin(getRoomMaterial());
374 }
375
376 void MaterialPreview::setupTestModel()
377 {
378 if (!_entity) return; // too early
379
380 if (_entity && _model)
381 {
382 scene::removeNodeFromParent(_model);
383 _model.reset();
384 }
385
386 // Load the pre-defined model from the resources path
387 if (_testModelCubeButton->IsToggled())
388 {
389 _model = GlobalModelCache().getModelNodeForStaticResource("preview/cube.ase");
390 }
391 else if (_testModelSphereButton->IsToggled()) // sphere
392 {
393 _model = GlobalModelCache().getModelNodeForStaticResource("preview/sphere.ase");
394 }
395 else // Tiles
396 {
397 _model = GlobalModelCache().getModelNodeForStaticResource("preview/tiles.ase");
398 }
399
400 // The test model is a child of this entity
401 scene::addNodeToContainer(_model, _entity);
402
403 updateSceneMaterials();
404 }
405
406 void MaterialPreview::onTestModelSelectionChanged(wxCommandEvent& ev)
407 {
408 if (ev.GetId() == _testModelCubeButton->GetId())
409 {
410 setTestModelType(TestModel::Cube);
411 }
412 else if (ev.GetId() == _testModelSphereButton->GetId())
413 {
414 setTestModelType(TestModel::Sphere);
415 }
416 else if (ev.GetId() == _testModelTilesButton->GetId())
417 {
418 setTestModelType(TestModel::Tiles);
419 }
420
421 queueDraw();
422 }
423
424 void MaterialPreview::onSwapBackgroundMaterialChanged(wxCommandEvent& ev)
425 {
426 _swapBackgroundMaterial = ev.IsChecked();
427 updateSceneMaterials();
428 queueDraw();
429 }
430
431 sigc::signal<void>& MaterialPreview::signal_SceneChanged()
432 {
433 return _sigSceneChanged;
434 }
435
436 std::string MaterialPreview::getLightClassname()
437 {
438 return _lightClassname;
439 }
440
441 void MaterialPreview::setLightClassname(const std::string& className)
442 {
443 // Store this value locally, even if we don't have any light to adjust yet
444 _lightClassname = className;
445
446 if (!_light || _lightClassname.empty()) return;
447
448 _light = changeEntityClassname(_light, _lightClassname);
449 signal_SceneChanged().emit();
450 }
451
452 Vector3 MaterialPreview::getLightColour()
453 {
454 if (!_light) return Vector3(0,0,0);
455
456 auto value = Node_getEntity(_light)->getKeyValue("_color");
457
458 if (value.empty())
459 {
460 value = "1 1 1";
461 }
462
463 return string::convert<Vector3>(value, Vector3(0,0,0));
464 }
465
466 void MaterialPreview::setLightColour(const Vector3& colour)
467 {
468 if (!_light) return;
469
470 Node_getEntity(_light)->setKeyValue("_color", string::to_string(colour));
471 signal_SceneChanged().emit();
472 }
473
474 void MaterialPreview::resetLightColour()
475 {
476 if (!_light) return;
477
478 Node_getEntity(_light)->setKeyValue("_color", "");
479 signal_SceneChanged().emit();
480 }
481
482 }