forked from qt-creator/qt-creator
Add "Add Texture" and "Add Light Probe" actions to Assets Library
When the user right-clicks on an image asset, "Add Light probe" now appears in the context menu. When the user right-clicks one or more image assets, "Add texture" or "Add textures" becomes available. Task-number: QDS-8344 Change-Id: Ia8d8379be2a0a285b33e4a230e08527c18756b47 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -4,9 +4,10 @@
|
||||
#include "materialbrowserview.h"
|
||||
|
||||
#include "bindingproperty.h"
|
||||
#include "materialbrowserwidget.h"
|
||||
#include "materialbrowsermodel.h"
|
||||
#include "materialbrowsertexturesmodel.h"
|
||||
#include "materialbrowserwidget.h"
|
||||
#include "modelnodeoperations.h"
|
||||
#include "nodeabstractproperty.h"
|
||||
#include "nodemetainfo.h"
|
||||
#include "qmlobjectnode.h"
|
||||
@@ -14,11 +15,11 @@
|
||||
|
||||
#include <designmodecontext.h>
|
||||
#include <nodeinstanceview.h>
|
||||
#include <nodemetainfo.h>
|
||||
#include <nodelistproperty.h>
|
||||
#include <qmldesignerconstants.h>
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/messagebox.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/qtcassert.h>
|
||||
@@ -449,9 +450,121 @@ void MaterialBrowserView::customNotification(const AbstractView *view,
|
||||
applyTextureToModel3D(nodeList.at(0), nodeList.at(1));
|
||||
} else if (identifier == "apply_texture_to_material") {
|
||||
applyTextureToMaterial({nodeList.at(0)}, nodeList.at(1));
|
||||
} else if (identifier == "add_textures") {
|
||||
if (data.size() != 4) {
|
||||
qWarning() << "Wrong number of arguments passed to add_textures: " << data.size();
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray identifier = data.at(0).toByteArray();
|
||||
QStringList filePaths = data.at(1).toStringList();
|
||||
AddTextureMode mode = data.at(2).value<AddTextureMode>();
|
||||
bool addToProject = data.at(3).toBool();
|
||||
|
||||
executeInTransaction(identifier, [&] {
|
||||
addTextures(filePaths, mode, addToProject);
|
||||
});
|
||||
} else if (identifier == "add_texture") {
|
||||
if (data.size() != 4) {
|
||||
qWarning() << "Wrong number of arguments passed to add_texture: " << data.size();
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray identifier = data.at(0).toByteArray();
|
||||
QString filePath = data.at(1).toString();
|
||||
AddTextureMode mode = data.at(2).value<AddTextureMode>();
|
||||
bool addToProject = data.at(3).toBool();
|
||||
|
||||
executeInTransaction(identifier, [&] {
|
||||
addOneTexture(filePath, mode, addToProject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void MaterialBrowserView::addOneTexture(const QString &texPath, AddTextureMode mode, bool addToProject)
|
||||
{
|
||||
if (addToProject) {
|
||||
// copy image to project
|
||||
AddFilesResult result = ModelNodeOperations::addImageToProject({texPath}, "images", false);
|
||||
|
||||
if (result.status() == AddFilesResult::Failed) {
|
||||
Core::AsynchronousMessageBox::warning(tr("Failed to Add Texture"),
|
||||
tr("Could not add %1 to project.").arg(texPath));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mode == AddTextureMode::Image)
|
||||
return;
|
||||
|
||||
// create a texture from the image
|
||||
ModelNode matLib = materialLibraryNode();
|
||||
if (!matLib.isValid())
|
||||
return;
|
||||
|
||||
NodeMetaInfo metaInfo = model()->metaInfo("QtQuick3D.Texture");
|
||||
|
||||
QString sourceVal = QLatin1String("images/%1").arg(texPath.split('/').last());
|
||||
ModelNode texNode = getTextureDefaultInstance(sourceVal);
|
||||
if (!texNode.isValid()) {
|
||||
texNode = createModelNode("QtQuick3D.Texture", metaInfo.majorVersion(),
|
||||
metaInfo.minorVersion());
|
||||
texNode.validId();
|
||||
VariantProperty sourceProp = texNode.variantProperty("source");
|
||||
sourceProp.setValue(sourceVal);
|
||||
matLib.defaultNodeListProperty().reparentHere(texNode);
|
||||
}
|
||||
|
||||
// assign the texture as scene environment's light probe
|
||||
if (mode == AddTextureMode::LightProbe && m_sceneId != -1) {
|
||||
QmlObjectNode sceneEnv = resolveSceneEnv();
|
||||
if (sceneEnv.isValid()) {
|
||||
sceneEnv.setBindingProperty("lightProbe", texNode.id());
|
||||
sceneEnv.setVariantProperty("backgroundMode",
|
||||
QVariant::fromValue(Enumeration("SceneEnvironment",
|
||||
"SkyBox")));
|
||||
}
|
||||
}
|
||||
QTimer::singleShot(0, this, [this, texNode]() {
|
||||
if (model() && texNode.isValid())
|
||||
emitCustomNotification("selected_texture_changed", {texNode});
|
||||
});
|
||||
}
|
||||
|
||||
void MaterialBrowserView::active3DSceneChanged(qint32 sceneId)
|
||||
{
|
||||
m_sceneId = sceneId;
|
||||
}
|
||||
|
||||
ModelNode MaterialBrowserView::resolveSceneEnv()
|
||||
{
|
||||
ModelNode activeSceneEnv;
|
||||
|
||||
if (m_sceneId != -1) {
|
||||
ModelNode activeScene = active3DSceneNode();
|
||||
if (activeScene.isValid()) {
|
||||
QmlObjectNode view3D;
|
||||
if (activeScene.metaInfo().isQtQuick3DView3D()) {
|
||||
view3D = activeScene;
|
||||
} else {
|
||||
ModelNode sceneParent = activeScene.parentProperty().parentModelNode();
|
||||
if (sceneParent.metaInfo().isQtQuick3DView3D())
|
||||
view3D = sceneParent;
|
||||
}
|
||||
if (view3D.isValid())
|
||||
activeSceneEnv = modelNodeForId(view3D.expression("environment"));
|
||||
}
|
||||
}
|
||||
|
||||
return activeSceneEnv;
|
||||
}
|
||||
|
||||
void MaterialBrowserView::addTextures(const QStringList &filePaths, AddTextureMode mode, bool addToProject)
|
||||
{
|
||||
for (const QString &texPath : filePaths)
|
||||
addOneTexture(texPath, mode, addToProject);
|
||||
}
|
||||
|
||||
void MaterialBrowserView::instancesCompleted(const QVector<ModelNode> &completedNodeList)
|
||||
{
|
||||
for (const ModelNode &node : completedNodeList) {
|
||||
|
||||
Reference in New Issue
Block a user