1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef MATERIALADAPTER_H
5#define MATERIALADAPTER_H
6
7#include <QtCore/qstring.h>
8#include <QtCore/qvariant.h>
9#include <QtCore/qobject.h>
10#include <QtCore/qpointer.h>
11
12#include <QtQml/qqmlregistration.h>
13
14#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
15#include <QtQuick3DRuntimeRender/private/qssgrendercustommaterial_p.h>
16
17#include <QtQuick3D/private/qquick3dcustommaterial_p.h>
18#include <QtQuick3D/private/qquick3dmodel_p.h>
19
20#include <QtCore/qabstractitemmodel.h>
21#include <QtCore/qstringlistmodel.h>
22
23#include <QtQuick3DAssetUtils/private/qssgscenedesc_p.h>
24
25#include "custommaterial.h"
26#include "buildmessage.h"
27#include "uniformmodel.h"
28
29QT_BEGIN_NAMESPACE
30
31class MaterialAdapter : public QObject
32{
33 Q_OBJECT
34
35 using CullMode = QQuick3DCustomMaterial::CullMode;
36 using DepthDrawMode = QQuick3DCustomMaterial::DepthDrawMode;
37 using ShadingMode = QQuick3DCustomMaterial::ShadingMode;
38 using BlendMode = QQuick3DCustomMaterial::BlendMode;
39
40 Q_PROPERTY(QQuick3DCustomMaterial * material READ material NOTIFY materialChanged)
41 Q_PROPERTY(QQuick3DNode * rootNode READ rootNode WRITE setRootNode NOTIFY rootNodeChanged)
42 Q_PROPERTY(QString fragmentShader READ fragmentShader WRITE setFragmentShader NOTIFY fragmentShaderChanged)
43 Q_PROPERTY(QString vertexShader READ vertexShader WRITE setVertexShader NOTIFY vertexShaderChanged)
44 Q_PROPERTY(ShaderBuildMessage vertexStatus READ vertexStatus NOTIFY vertexStatusChanged)
45 Q_PROPERTY(ShaderBuildMessage fragmentStatus READ fragmentStatus NOTIFY fragmentStatusChanged)
46 Q_PROPERTY(UniformModel * uniformModel READ uniformModel WRITE setUniformModel NOTIFY uniformModelChanged)
47 Q_PROPERTY(bool unsavedChanges READ unsavedChanges WRITE setUnsavedChanges NOTIFY unsavedChangesChanged)
48 Q_PROPERTY(QUrl materialSaveFile READ materialSaveFile WRITE setMaterialSaveFile NOTIFY materialSaveFileChanged)
49
50 Q_PROPERTY(QQuick3DMaterial::CullMode cullMode READ cullMode WRITE setCullMode NOTIFY cullModeChanged)
51 Q_PROPERTY(QQuick3DMaterial::DepthDrawMode depthDrawMode READ depthDrawMode WRITE setDepthDrawMode NOTIFY depthDrawModeChanged)
52 Q_PROPERTY(QQuick3DCustomMaterial::ShadingMode shadingMode READ shadingMode WRITE setShadingMode NOTIFY shadingModeChanged)
53 Q_PROPERTY(QQuick3DCustomMaterial::BlendMode sourceBlend READ srcBlend WRITE setSrcBlend NOTIFY srcBlendChanged)
54 Q_PROPERTY(QQuick3DCustomMaterial::BlendMode destinationBlend READ dstBlend WRITE setDstBlend NOTIFY dstBlendChanged)
55
56 QML_ELEMENT
57public:
58 explicit MaterialAdapter(QObject *parent = nullptr);
59
60 QQuick3DCustomMaterial *material() const;
61 QString fragmentShader() const;
62 void setFragmentShader(const QString &newFragmentShader);
63
64 QString vertexShader() const;
65 void setVertexShader(const QString &newVertexShader);
66
67 UniformModel *uniformModel() const;
68
69 ShaderBuildMessage vertexStatus() const;
70 ShaderBuildMessage fragmentStatus() const;
71
72 bool unsavedChanges() const;
73 void setUnsavedChanges(bool newUnsavedChanges);
74
75 const QUrl &materialSaveFile() const;
76 void setMaterialSaveFile(const QUrl &newMaterialSaveFile);
77
78 QQuick3DNode *rootNode() const;
79 void setRootNode(QQuick3DNode *newResourceNode);
80
81 CullMode cullMode() const;
82 void setCullMode(CullMode newCullMode);
83
84 DepthDrawMode depthDrawMode() const;
85 void setDepthDrawMode(DepthDrawMode newDepthDrawMode);
86
87 ShadingMode shadingMode() const;
88 void setShadingMode(ShadingMode newShadingMode);
89
90 BlendMode srcBlend() const;
91 void setSrcBlend(BlendMode newSourceBlend);
92
93 BlendMode dstBlend() const;
94 void setDstBlend(BlendMode newDestinationBlend);
95
96 void setUniformModel(UniformModel *newUniformModel);
97
98 Q_INVOKABLE QString getSupportedImageFormatsFilter() const;
99
100public Q_SLOTS:
101 void importFragmentShader(const QUrl &shaderFile);
102 void importVertexShader(const QUrl &shaderFile);
103 bool save();
104 bool saveMaterial(const QUrl &materialFile);
105 bool loadMaterial(const QUrl &materialFile);
106 bool exportQmlComponent(const QUrl &componentFile, const QString &vertName, const QString &fragName);
107 void reset();
108
109Q_SIGNALS:
110 void materialChanged();
111 void fragmentShaderChanged();
112 void vertexShaderChanged();
113 void vertexStatusChanged();
114 void uniformModelChanged();
115 void fragmentStatusChanged();
116 void unsavedChangesChanged();
117 void materialSaveFileChanged();
118
119 void errorOccurred();
120 void postMaterialSaved();
121 void rootNodeChanged();
122 void cullModeChanged();
123 void depthDrawModeChanged();
124 void shadingModeChanged();
125 void srcBlendChanged();
126 void dstBlendChanged();
127
128private:
129 static void updateShader(QQuick3DMaterial &target);
130 static void bakerStatusCallback(const QByteArray &descKey, QtQuick3DEditorHelpers::ShaderBaker::Status status, const QString &err, QShader::Stage stage);
131 void updateMaterialDescription(CustomMaterial::Shaders shaders);
132 void updateMaterialDescription();
133
134 QString importShader(const QUrl &shaderFile);
135 QFile resolveFileFromUrl(const QUrl &fileUrl);
136 QPointer<QQuick3DCustomMaterial> m_material;
137 UniformModel *m_uniformModel = nullptr;
138 QUrl m_vertexUrl;
139 QUrl m_fragUrl;
140 QString m_fragmentShader;
141 QString m_vertexShader;
142 ShaderBuildMessage m_vertexMsg;
143 ShaderBuildMessage m_fragmentMsg;
144 bool m_ready = false;
145 bool m_unsavedChanges = true;
146 QUrl m_materialSaveFile;
147 QPointer<QQuick3DNode> m_rootNode;
148 CustomMaterial m_materialDescr;
149 CustomMaterial::UniformTable uniformTable;
150 CustomMaterial::Properties m_properties;
151};
152
153QT_END_NAMESPACE
154
155#endif // MATERIALADAPTER_H
156

source code of qtquick3d/tools/materialeditor/materialadapter.h