1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QSSGCUSTOMMATERIAL_H
5#define QSSGCUSTOMMATERIAL_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick3D/private/qquick3dmaterial_p.h>
19#include <QtCore/qurl.h>
20
21#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
22#include <QtQuick3DRuntimeRender/private/qssgrendergraphobject_p.h>
23#include <QtQuick3D/private/qquick3dshaderutils_p.h>
24
25QT_BEGIN_NAMESPACE
26
27class Q_QUICK3D_EXPORT QQuick3DCustomMaterial : public QQuick3DMaterial
28{
29 Q_OBJECT
30 Q_PROPERTY(ShadingMode shadingMode READ shadingMode WRITE setShadingMode NOTIFY shadingModeChanged)
31 Q_PROPERTY(QUrl fragmentShader READ fragmentShader WRITE setFragmentShader NOTIFY fragmentShaderChanged)
32 Q_PROPERTY(QUrl vertexShader READ vertexShader WRITE setVertexShader NOTIFY vertexShaderChanged)
33 Q_PROPERTY(BlendMode sourceBlend READ srcBlend WRITE setSrcBlend NOTIFY srcBlendChanged)
34 Q_PROPERTY(BlendMode destinationBlend READ dstBlend WRITE setDstBlend NOTIFY dstBlendChanged)
35 Q_PROPERTY(bool alwaysDirty READ alwaysDirty WRITE setAlwaysDirty NOTIFY alwaysDirtyChanged)
36 Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged)
37
38 QML_NAMED_ELEMENT(CustomMaterial)
39
40public:
41 enum class ShadingMode
42 {
43 Unshaded,
44 Shaded
45 };
46 Q_ENUM(ShadingMode)
47
48 enum class BlendMode
49 {
50 NoBlend,
51 Zero,
52 One,
53 SrcColor,
54 OneMinusSrcColor,
55 DstColor,
56 OneMinusDstColor,
57 SrcAlpha,
58 OneMinusSrcAlpha,
59 DstAlpha,
60 OneMinusDstAlpha,
61 ConstantColor,
62 OneMinusConstantColor,
63 ConstantAlpha,
64 OneMinusConstantAlpha,
65 SrcAlphaSaturate
66 };
67 Q_ENUM(BlendMode)
68
69 explicit QQuick3DCustomMaterial(QQuick3DObject *parent = nullptr);
70 ~QQuick3DCustomMaterial() override;
71
72 ShadingMode shadingMode() const;
73 QUrl vertexShader() const;
74 QUrl fragmentShader() const;
75 BlendMode srcBlend() const;
76 BlendMode dstBlend() const;
77 bool alwaysDirty() const;
78 float lineWidth() const;
79
80public Q_SLOTS:
81 void setShadingMode(QQuick3DCustomMaterial::ShadingMode mode);
82 void setVertexShader(const QUrl &url);
83 void setFragmentShader(const QUrl &url);
84 void setSrcBlend(QQuick3DCustomMaterial::BlendMode mode);
85 void setDstBlend(QQuick3DCustomMaterial::BlendMode mode);
86 void setAlwaysDirty(bool alwaysDirty);
87 void setLineWidth(float width);
88
89Q_SIGNALS:
90 void shadingModeChanged();
91 void vertexShaderChanged();
92 void fragmentShaderChanged();
93 void srcBlendChanged();
94 void dstBlendChanged();
95 void alwaysDirtyChanged();
96 void lineWidthChanged();
97
98protected:
99 enum Dirty : quint32 {
100 TextureDirty = 0x1,
101 PropertyDirty = 0x2,
102 ShaderSettingsDirty = 0x4,
103 DynamicPropertiesDirty = 0x8, // Special case for custom materials created manually
104 AllDirty = std::numeric_limits<quint32>::max() ^ DynamicPropertiesDirty // (DynamicPropertiesDirty is intentionally excluded from AllDirty)
105 };
106
107 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
108 void itemChange(ItemChange, const ItemChangeData &) override;
109 void markAllDirty() override;
110 static void markDirty(QQuick3DCustomMaterial &that, QQuick3DCustomMaterial::Dirty type);
111
112private Q_SLOTS:
113 void onPropertyDirty();
114 void onTextureDirty();
115
116private:
117 friend class QQuick3DShaderUtilsTextureInput;
118 friend class QQuick3DViewport;
119
120 void setDynamicTextureMap(QQuick3DShaderUtilsTextureInput *textureMap);
121
122 QSet<QQuick3DShaderUtilsTextureInput *> m_dynamicTextureMaps;
123 quint32 m_dirtyAttributes = Dirty::AllDirty;
124 BlendMode m_srcBlend = BlendMode::NoBlend;
125 BlendMode m_dstBlend = BlendMode::NoBlend;
126 ShadingMode m_shadingMode = ShadingMode::Shaded;
127 QUrl m_vertexShader;
128 QUrl m_fragmentShader;
129 bool m_alwaysDirty = false;
130 float m_lineWidth = 1.0f;
131};
132
133QT_END_NAMESPACE
134
135#endif // QSSGCUSTOMMATERIAL_H
136

source code of qtquick3d/src/quick3d/qquick3dcustommaterial_p.h