Skip to content

Commit

Permalink
#6050: MaterialPreview is automatically detecting skybox materials wh…
Browse files Browse the repository at this point in the history
…ich are applied to the background instead.

A toggle tool button can be used to swap the background material with the object's.
  • Loading branch information
codereader committed Aug 6, 2022
1 parent 41168c0 commit 1591dba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
Binary file added install/bitmaps/swap_background.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 60 additions & 12 deletions radiant/ui/materials/editor/MaterialPreview.cpp
Expand Up @@ -23,6 +23,21 @@ namespace
const char* const FUNC_STATIC_CLASS = "func_static";
const char* const GKEY_DEFAULT_ROOM_MATERIAL = "/materialPreview/defaultRoomMaterial";
const char* const GKEY_DEFAULT_LIGHT_DEF = "/materialPreview/defaultLightDef";

inline bool isSkyboxMaterial(const MaterialPtr& material)
{
if (!material) return false;

for (const auto& layer : material->getAllLayers())
{
if (layer->getMapType() == IShaderLayer::MapType::CameraCubeMap)
{
return true;
}
}

return false;
}
}

MaterialPreview::MaterialPreview(wxWindow* parent) :
Expand Down Expand Up @@ -68,6 +83,15 @@ void MaterialPreview::setupToolbar()
toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onTestModelSelectionChanged, this, _testModelSphereButton->GetId());
toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onTestModelSelectionChanged, this, _testModelTilesButton->GetId());

toolbar->AddSeparator();

_swapBackgroundMaterialButton = toolbar->AddTool(wxID_ANY, "Swap", wxutil::GetLocalBitmap("swap_background.png", wxART_TOOLBAR));
_swapBackgroundMaterialButton->SetToggle(true);
toolbar->ToggleTool(_swapBackgroundMaterialButton->GetId(), false);
_swapBackgroundMaterialButton->SetShortHelp(_("Swap Background and Foreground Materials"));

toolbar->Bind(wxEVT_TOOL, &MaterialPreview::onSwapBackgroundMaterialChanged, this, _swapBackgroundMaterialButton->GetId());

toolbar->Realize();

addToolbar(toolbar);
Expand All @@ -78,8 +102,13 @@ const MaterialPtr& MaterialPreview::getMaterial()
return _material;
}

void MaterialPreview::updateModelSkin()
void MaterialPreview::updateModelSkin(const std::string& material)
{
// Assign the material to the temporary skin
_testModelSkin->setRemapMaterial(GlobalMaterialManager().getMaterial(material));

if (!_model) return;

// Hide the model if there's no material to preview
_model->setFiltered(_testModelSkin->isEmpty());

Expand All @@ -100,7 +129,7 @@ const std::string& MaterialPreview::getRoomMaterial()
void MaterialPreview::setRoomMaterial(const std::string& material)
{
_roomMaterial = material;
updateRoomSkin();
updateSceneMaterials();

signal_SceneChanged().emit();
}
Expand Down Expand Up @@ -155,9 +184,9 @@ std::string MaterialPreview::getDefaultLightDef()
return className;
}

void MaterialPreview::updateRoomSkin()
void MaterialPreview::updateRoomSkin(const std::string& roomMaterial)
{
_testRoomSkin->setRemapMaterial(GlobalMaterialManager().getMaterial(getRoomMaterial()));
_testRoomSkin->setRemapMaterial(GlobalMaterialManager().getMaterial(roomMaterial));

// Let the model update its remaps
auto skinnedRoom = std::dynamic_pointer_cast<SkinnedModel>(_room);
Expand All @@ -184,13 +213,11 @@ void MaterialPreview::setMaterial(const MaterialPtr& material)

_sceneIsReady = false;

if (_model)
{
// Assign the material to the temporary skin
_testModelSkin->setRemapMaterial(_material);
// Detect whether we should apply this material to the background
_swapBackgroundMaterial = isSkyboxMaterial(_material);
_swapBackgroundMaterialButton->GetToolBar()->ToggleTool(_swapBackgroundMaterialButton->GetId(), _swapBackgroundMaterial);

updateModelSkin();
}
updateSceneMaterials();

if (!hadMaterial && _material)
{
Expand All @@ -204,6 +231,20 @@ void MaterialPreview::setMaterial(const MaterialPtr& material)
queueDraw();
}

void MaterialPreview::updateSceneMaterials()
{
auto foregroundMaterial = _material ? _material->getName() : _roomMaterial;
auto backgroundMaterial = _roomMaterial;

if (_swapBackgroundMaterial)
{
std::swap(foregroundMaterial, backgroundMaterial);
}

updateModelSkin(foregroundMaterial);
updateRoomSkin(backgroundMaterial);
}

void MaterialPreview::enableFrobHighlight(bool enable)
{
if (!_entity) return;
Expand Down Expand Up @@ -313,7 +354,7 @@ void MaterialPreview::setupRoom()

roomEntity->addChildNode(_room);

updateRoomSkin();
updateRoomSkin(getRoomMaterial());
}

void MaterialPreview::setupTestModel()
Expand Down Expand Up @@ -343,7 +384,7 @@ void MaterialPreview::setupTestModel()
// The test model is a child of this entity
scene::addNodeToContainer(_model, _entity);

updateModelSkin();
updateSceneMaterials();
}

void MaterialPreview::onTestModelSelectionChanged(wxCommandEvent& ev)
Expand All @@ -364,6 +405,13 @@ void MaterialPreview::onTestModelSelectionChanged(wxCommandEvent& ev)
queueDraw();
}

void MaterialPreview::onSwapBackgroundMaterialChanged(wxCommandEvent& ev)
{
_swapBackgroundMaterial = ev.IsChecked();
updateSceneMaterials();
queueDraw();
}

sigc::signal<void>& MaterialPreview::signal_SceneChanged()
{
return _sigSceneChanged;
Expand Down
8 changes: 6 additions & 2 deletions radiant/ui/materials/editor/MaterialPreview.h
Expand Up @@ -35,12 +35,14 @@ class MaterialPreview :

std::string _roomMaterial;
std::string _lightClassname;
bool _swapBackgroundMaterial;

float _defaultCamDistanceFactor;

wxToolBarToolBase* _testModelCubeButton;
wxToolBarToolBase* _testModelSphereButton;
wxToolBarToolBase* _testModelTilesButton;
wxToolBarToolBase* _swapBackgroundMaterialButton;

sigc::signal<void> _sigSceneChanged;

Expand Down Expand Up @@ -91,10 +93,12 @@ class MaterialPreview :
void setupToolbar();
void setupTestModel();
void setupRoom();
void updateModelSkin();
void updateRoomSkin();
void updateModelSkin(const std::string& material);
void updateRoomSkin(const std::string& material);
void updateSceneMaterials();
std::string getDefaultLightDef();
void onTestModelSelectionChanged(wxCommandEvent& ev);
void onSwapBackgroundMaterialChanged(wxCommandEvent& ev);
};

}

0 comments on commit 1591dba

Please sign in to comment.