| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSSGDEFAULTMATERIAL_H |
| 5 | #define QSSGDEFAULTMATERIAL_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 <QtQuick3D/private/qquick3dtexture_p.h> |
| 20 | |
| 21 | #include <QColor> |
| 22 | #include <QHash> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | class Q_QUICK3D_EXPORT QQuick3DDefaultMaterial : public QQuick3DMaterial |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | Q_PROPERTY(Lighting lighting READ lighting WRITE setLighting NOTIFY lightingChanged) |
| 30 | Q_PROPERTY(BlendMode blendMode READ blendMode WRITE setBlendMode NOTIFY blendModeChanged) |
| 31 | |
| 32 | Q_PROPERTY(QColor diffuseColor READ diffuseColor WRITE setDiffuseColor NOTIFY diffuseColorChanged) |
| 33 | Q_PROPERTY(QQuick3DTexture *diffuseMap READ diffuseMap WRITE setDiffuseMap NOTIFY diffuseMapChanged) |
| 34 | |
| 35 | Q_PROPERTY(QVector3D emissiveFactor READ emissiveFactor WRITE setEmissiveFactor NOTIFY emissiveFactorChanged) |
| 36 | Q_PROPERTY(QQuick3DTexture *emissiveMap READ emissiveMap WRITE setEmissiveMap NOTIFY emissiveMapChanged) |
| 37 | |
| 38 | Q_PROPERTY(QQuick3DTexture *specularReflectionMap READ specularReflectionMap WRITE setSpecularReflectionMap NOTIFY specularReflectionMapChanged) |
| 39 | Q_PROPERTY(QQuick3DTexture *specularMap READ specularMap WRITE setSpecularMap NOTIFY specularMapChanged) |
| 40 | Q_PROPERTY(SpecularModel specularModel READ specularModel WRITE setSpecularModel NOTIFY specularModelChanged) |
| 41 | Q_PROPERTY(QColor specularTint READ specularTint WRITE setSpecularTint NOTIFY specularTintChanged) |
| 42 | |
| 43 | Q_PROPERTY(float indexOfRefraction READ indexOfRefraction WRITE setIndexOfRefraction NOTIFY indexOfRefractionChanged) |
| 44 | Q_PROPERTY(float fresnelPower READ fresnelPower WRITE setFresnelPower NOTIFY fresnelPowerChanged) |
| 45 | Q_PROPERTY(float specularAmount READ specularAmount WRITE setSpecularAmount NOTIFY specularAmountChanged) |
| 46 | Q_PROPERTY(float specularRoughness READ specularRoughness WRITE setSpecularRoughness NOTIFY specularRoughnessChanged) |
| 47 | Q_PROPERTY(QQuick3DTexture *roughnessMap READ roughnessMap WRITE setRoughnessMap NOTIFY roughnessMapChanged) |
| 48 | Q_PROPERTY(QQuick3DMaterial::TextureChannelMapping roughnessChannel READ roughnessChannel WRITE setRoughnessChannel NOTIFY roughnessChannelChanged) |
| 49 | |
| 50 | Q_PROPERTY(float opacity READ opacity WRITE setOpacity NOTIFY opacityChanged) |
| 51 | Q_PROPERTY(QQuick3DTexture *opacityMap READ opacityMap WRITE setOpacityMap NOTIFY opacityMapChanged) |
| 52 | Q_PROPERTY(QQuick3DMaterial::TextureChannelMapping opacityChannel READ opacityChannel WRITE setOpacityChannel NOTIFY opacityChannelChanged) |
| 53 | |
| 54 | Q_PROPERTY(QQuick3DTexture *bumpMap READ bumpMap WRITE setBumpMap NOTIFY bumpMapChanged) |
| 55 | Q_PROPERTY(float bumpAmount READ bumpAmount WRITE setBumpAmount NOTIFY bumpAmountChanged) |
| 56 | |
| 57 | Q_PROPERTY(QQuick3DTexture *normalMap READ normalMap WRITE setNormalMap NOTIFY normalMapChanged) |
| 58 | |
| 59 | Q_PROPERTY(QQuick3DTexture *translucencyMap READ translucencyMap WRITE setTranslucencyMap NOTIFY translucencyMapChanged) |
| 60 | Q_PROPERTY(QQuick3DMaterial::TextureChannelMapping translucencyChannel READ translucencyChannel WRITE setTranslucencyChannel NOTIFY translucencyChannelChanged) |
| 61 | Q_PROPERTY(float translucentFalloff READ translucentFalloff WRITE setTranslucentFalloff NOTIFY translucentFalloffChanged) |
| 62 | |
| 63 | Q_PROPERTY(float diffuseLightWrap READ diffuseLightWrap WRITE setDiffuseLightWrap NOTIFY diffuseLightWrapChanged) |
| 64 | |
| 65 | Q_PROPERTY(bool vertexColorsEnabled READ vertexColorsEnabled WRITE setVertexColorsEnabled NOTIFY vertexColorsEnabledChanged) |
| 66 | |
| 67 | Q_PROPERTY(float pointSize READ pointSize WRITE setPointSize NOTIFY pointSizeChanged) |
| 68 | Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged) |
| 69 | |
| 70 | QML_NAMED_ELEMENT(DefaultMaterial) |
| 71 | |
| 72 | public: |
| 73 | enum Lighting { NoLighting = 0, FragmentLighting }; |
| 74 | Q_ENUM(Lighting) |
| 75 | |
| 76 | enum BlendMode { SourceOver = 0, Screen, Multiply }; |
| 77 | Q_ENUM(BlendMode) |
| 78 | |
| 79 | enum SpecularModel { Default = 0, KGGX }; |
| 80 | Q_ENUM(SpecularModel) |
| 81 | |
| 82 | explicit QQuick3DDefaultMaterial(QQuick3DObject *parent = nullptr); |
| 83 | ~QQuick3DDefaultMaterial() override; |
| 84 | |
| 85 | Lighting lighting() const; |
| 86 | BlendMode blendMode() const; |
| 87 | QColor diffuseColor() const; |
| 88 | QQuick3DTexture *diffuseMap() const; |
| 89 | QVector3D emissiveFactor() const; |
| 90 | QQuick3DTexture *emissiveMap() const; |
| 91 | QQuick3DTexture *specularReflectionMap() const; |
| 92 | QQuick3DTexture *specularMap() const; |
| 93 | SpecularModel specularModel() const; |
| 94 | QColor specularTint() const; |
| 95 | float indexOfRefraction() const; |
| 96 | float fresnelPower() const; |
| 97 | float specularAmount() const; |
| 98 | float specularRoughness() const; |
| 99 | QQuick3DTexture *roughnessMap() const; |
| 100 | float opacity() const; |
| 101 | QQuick3DTexture *opacityMap() const; |
| 102 | QQuick3DTexture *bumpMap() const; |
| 103 | float bumpAmount() const; |
| 104 | QQuick3DTexture *normalMap() const; |
| 105 | |
| 106 | QQuick3DTexture *translucencyMap() const; |
| 107 | float translucentFalloff() const; |
| 108 | float diffuseLightWrap() const; |
| 109 | bool vertexColorsEnabled() const; |
| 110 | TextureChannelMapping roughnessChannel() const; |
| 111 | TextureChannelMapping opacityChannel() const; |
| 112 | TextureChannelMapping translucencyChannel() const; |
| 113 | |
| 114 | float pointSize() const; |
| 115 | float lineWidth() const; |
| 116 | |
| 117 | public Q_SLOTS: |
| 118 | |
| 119 | void setLighting(QQuick3DDefaultMaterial::Lighting lighting); |
| 120 | void setBlendMode(QQuick3DDefaultMaterial::BlendMode blendMode); |
| 121 | void setDiffuseColor(QColor diffuseColor); |
| 122 | void setDiffuseMap(QQuick3DTexture *diffuseMap); |
| 123 | void setEmissiveFactor(QVector3D emissiveFactor); |
| 124 | void setEmissiveMap(QQuick3DTexture *emissiveMap); |
| 125 | |
| 126 | void setSpecularReflectionMap(QQuick3DTexture *specularReflectionMap); |
| 127 | void setSpecularMap(QQuick3DTexture *specularMap); |
| 128 | void setSpecularModel(QQuick3DDefaultMaterial::SpecularModel specularModel); |
| 129 | void setSpecularTint(QColor specularTint); |
| 130 | void setIndexOfRefraction(float indexOfRefraction); |
| 131 | void setFresnelPower(float fresnelPower); |
| 132 | void setSpecularAmount(float specularAmount); |
| 133 | void setSpecularRoughness(float specularRoughness); |
| 134 | void setRoughnessMap(QQuick3DTexture *roughnessMap); |
| 135 | void setOpacity(float opacity); |
| 136 | void setOpacityMap(QQuick3DTexture *opacityMap); |
| 137 | void setBumpMap(QQuick3DTexture *bumpMap); |
| 138 | void setBumpAmount(float bumpAmount); |
| 139 | void setNormalMap(QQuick3DTexture *normalMap); |
| 140 | |
| 141 | void setTranslucencyMap(QQuick3DTexture *translucencyMap); |
| 142 | void setTranslucentFalloff(float translucentFalloff); |
| 143 | void setDiffuseLightWrap(float diffuseLightWrap); |
| 144 | void setVertexColorsEnabled(bool vertexColorsEnabled); |
| 145 | |
| 146 | void setRoughnessChannel(QQuick3DMaterial::TextureChannelMapping channel); |
| 147 | void setOpacityChannel(QQuick3DMaterial::TextureChannelMapping channel); |
| 148 | void setTranslucencyChannel(QQuick3DMaterial::TextureChannelMapping channel); |
| 149 | |
| 150 | void setPointSize(float size); |
| 151 | void setLineWidth(float width); |
| 152 | |
| 153 | Q_SIGNALS: |
| 154 | void lightingChanged(QQuick3DDefaultMaterial::Lighting lighting); |
| 155 | void blendModeChanged(QQuick3DDefaultMaterial::BlendMode blendMode); |
| 156 | void diffuseColorChanged(QColor diffuseColor); |
| 157 | void diffuseMapChanged(QQuick3DTexture *diffuseMap); |
| 158 | void emissiveFactorChanged(QVector3D emissiveFactor); |
| 159 | void emissiveMapChanged(QQuick3DTexture *emissiveMap); |
| 160 | void specularReflectionMapChanged(QQuick3DTexture *specularReflectionMap); |
| 161 | void specularMapChanged(QQuick3DTexture *specularMap); |
| 162 | void specularModelChanged(QQuick3DDefaultMaterial::SpecularModel specularModel); |
| 163 | void specularTintChanged(QColor specularTint); |
| 164 | void indexOfRefractionChanged(float indexOfRefraction); |
| 165 | void fresnelPowerChanged(float fresnelPower); |
| 166 | void specularAmountChanged(float specularAmount); |
| 167 | void specularRoughnessChanged(float specularRoughness); |
| 168 | void roughnessMapChanged(QQuick3DTexture *roughnessMap); |
| 169 | void opacityChanged(float opacity); |
| 170 | void opacityMapChanged(QQuick3DTexture *opacityMap); |
| 171 | void bumpMapChanged(QQuick3DTexture *bumpMap); |
| 172 | void bumpAmountChanged(float bumpAmount); |
| 173 | void normalMapChanged(QQuick3DTexture *normalMap); |
| 174 | void translucencyMapChanged(QQuick3DTexture *translucencyMap); |
| 175 | void translucentFalloffChanged(float translucentFalloff); |
| 176 | void diffuseLightWrapChanged(float diffuseLightWrap); |
| 177 | void vertexColorsEnabledChanged(bool vertexColorsEnabled); |
| 178 | void roughnessChannelChanged(QQuick3DMaterial::TextureChannelMapping channel); |
| 179 | void opacityChannelChanged(QQuick3DMaterial::TextureChannelMapping channel); |
| 180 | void translucencyChannelChanged(QQuick3DMaterial::TextureChannelMapping channel); |
| 181 | void pointSizeChanged(); |
| 182 | void lineWidthChanged(); |
| 183 | |
| 184 | protected: |
| 185 | QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override; |
| 186 | void markAllDirty() override; |
| 187 | void itemChange(ItemChange, const ItemChangeData &) override; |
| 188 | private: |
| 189 | enum DirtyType { |
| 190 | LightingModeDirty = 0x00000001, |
| 191 | BlendModeDirty = 0x00000002, |
| 192 | DiffuseDirty = 0x00000004, |
| 193 | EmissiveDirty = 0x00000008, |
| 194 | SpecularDirty = 0x00000010, |
| 195 | OpacityDirty = 0x00000020, |
| 196 | BumpDirty = 0x00000040, |
| 197 | NormalDirty = 0x00000080, |
| 198 | TranslucencyDirty = 0x00000100, |
| 199 | VertexColorsDirty = 0x00000200, |
| 200 | PointSizeDirty = 0x00000400, |
| 201 | LineWidthDirty = 0x00000800 |
| 202 | }; |
| 203 | |
| 204 | void updateSceneManager(QQuick3DSceneManager *sceneManager); |
| 205 | Lighting m_lighting = FragmentLighting; |
| 206 | BlendMode m_blendMode = SourceOver; |
| 207 | QColor m_diffuseColor; |
| 208 | QQuick3DTexture *m_diffuseMap = nullptr; |
| 209 | QVector3D m_emissiveFactor; |
| 210 | QQuick3DTexture *m_emissiveMap = nullptr; |
| 211 | |
| 212 | QQuick3DTexture *m_specularReflectionMap = nullptr; |
| 213 | QQuick3DTexture *m_specularMap = nullptr; |
| 214 | SpecularModel m_specularModel = Default; |
| 215 | QColor m_specularTint; |
| 216 | float m_indexOfRefraction = 1.45f; |
| 217 | float m_fresnelPower = 0.0f; |
| 218 | float m_specularAmount = 0.0f; |
| 219 | float m_specularRoughness = 0.0f; |
| 220 | QQuick3DTexture *m_roughnessMap = nullptr; |
| 221 | float m_opacity = 1.0f; |
| 222 | QQuick3DTexture *m_opacityMap = nullptr; |
| 223 | QQuick3DTexture *m_bumpMap = nullptr; |
| 224 | float m_bumpAmount = 0.0f; |
| 225 | QQuick3DTexture *m_normalMap = nullptr; |
| 226 | |
| 227 | QQuick3DTexture *m_translucencyMap = nullptr; |
| 228 | float m_translucentFalloff = 0.0f; |
| 229 | float m_diffuseLightWrap = 0.0f; |
| 230 | bool m_vertexColorsEnabled = false; |
| 231 | |
| 232 | TextureChannelMapping m_roughnessChannel = QQuick3DMaterial::R; |
| 233 | TextureChannelMapping m_opacityChannel = QQuick3DMaterial::A; |
| 234 | TextureChannelMapping m_translucencyChannel = QQuick3DMaterial::A; |
| 235 | |
| 236 | float m_pointSize = 1.0f; |
| 237 | float m_lineWidth = 1.0f; |
| 238 | |
| 239 | quint32 m_dirtyAttributes = 0xffffffff; // all dirty by default |
| 240 | void markDirty(DirtyType type); |
| 241 | }; |
| 242 | |
| 243 | QT_END_NAMESPACE |
| 244 | |
| 245 | #endif // QSSGDEFAULTMATERIAL_H |
| 246 | |