1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef CUSTOMMATERIAL_H |
5 | #define CUSTOMMATERIAL_H |
6 | |
7 | #include <QtQuick3DAssetUtils/private/qssgscenedesc_p.h> |
8 | |
9 | #include <QtCore/qpointer.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | class CustomMaterial |
14 | { |
15 | public: |
16 | struct Uniform |
17 | { |
18 | enum class Type |
19 | { |
20 | Bool, |
21 | Int, |
22 | Float, |
23 | Vec2, |
24 | Vec3, |
25 | Vec4, |
26 | Mat44, |
27 | Sampler, |
28 | Last |
29 | }; |
30 | union { |
31 | bool b; |
32 | int i; |
33 | float f; |
34 | QVector2D vec2; |
35 | QVector3D vec3; |
36 | QVector4D vec4; |
37 | quintptr d; |
38 | }; |
39 | Type type; |
40 | QByteArray name; |
41 | QMatrix4x4 m44; |
42 | QString imagePath; |
43 | }; |
44 | using UniformTable = QList<Uniform>; |
45 | using TextureStore = QList<QImage *>; |
46 | |
47 | struct Properties |
48 | { |
49 | QQuick3DCustomMaterial::CullMode cullMode { QQuick3DCustomMaterial::CullMode::BackFaceCulling }; |
50 | QQuick3DCustomMaterial::DepthDrawMode depthDrawMode { QQuick3DCustomMaterial::DepthDrawMode::OpaqueOnlyDepthDraw }; |
51 | QQuick3DCustomMaterial::ShadingMode shadingMode { QQuick3DCustomMaterial::ShadingMode::Shaded }; |
52 | QQuick3DCustomMaterial::BlendMode sourceBlend { QQuick3DCustomMaterial::BlendMode::NoBlend }; |
53 | QQuick3DCustomMaterial::BlendMode destinationBlend { QQuick3DCustomMaterial::BlendMode::NoBlend }; |
54 | } properties; |
55 | |
56 | struct Shaders |
57 | { |
58 | QUrl vert; |
59 | QUrl frag; |
60 | }; |
61 | |
62 | static void setUniform(QSSGSceneDesc::Material &material, const Uniform &uniform); |
63 | QPointer<QQuick3DCustomMaterial> create(QQuick3DNode &parent, const UniformTable &uniforms, const Properties &properties, const Shaders &shaders); |
64 | bool isValid() const; |
65 | friend QTextStream &operator<<(QTextStream &stream, const CustomMaterial &material) |
66 | { |
67 | using namespace QSSGSceneDesc; |
68 | const auto &scene = material.scene; |
69 | if (auto node = scene.root) { |
70 | Q_ASSERT(node->runtimeType == Material::RuntimeType::CustomMaterial); |
71 | writeQmlComponent(material: static_cast<const QSSGSceneDesc::Material &>(*node), stream); |
72 | } |
73 | return stream; |
74 | } |
75 | |
76 | friend QDataStream &operator<<(QDataStream &stream, const CustomMaterial::Uniform &uniform) { return writeToDataStream(stream, uniform); } |
77 | friend QDataStream &operator>>(QDataStream &stream, CustomMaterial::Uniform &uniform) { return readFromDataStream(stream, uniform); } |
78 | |
79 | private: |
80 | static QDataStream &readFromDataStream(QDataStream &stream, Uniform &uniform); |
81 | static QDataStream &writeToDataStream(QDataStream &stream, const Uniform &uniform); |
82 | static void writeQmlComponent(const QSSGSceneDesc::Material &material, QTextStream &stream); |
83 | QSSGSceneDesc::Scene scene; |
84 | }; |
85 | |
86 | QT_END_NAMESPACE |
87 | |
88 | #endif // CUSTOMMATERIAL_H |
89 |