1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15
16#ifndef PROCEDURALSKYTEXTURE_H
17#define PROCEDURALSKYTEXTURE_H
18
19#include <QtQuick3D/QQuick3DTextureData>
20#include <QtQml/QQmlEngine>
21
22#include <QtGui/QColor>
23#include <QtCore/QByteArray>
24
25QT_BEGIN_NAMESPACE
26
27class ProceduralSkyTextureData : public QQuick3DTextureData
28{
29 Q_OBJECT
30 Q_PROPERTY(QColor skyTopColor READ skyTopColor WRITE setSkyTopColor NOTIFY skyTopColorChanged)
31 Q_PROPERTY(QColor skyHorizonColor READ skyHorizonColor WRITE setSkyHorizonColor NOTIFY skyHorizonColorChanged)
32 Q_PROPERTY(float skyCurve READ skyCurve WRITE setSkyCurve NOTIFY skyCurveChanged)
33 Q_PROPERTY(float skyEnergy READ skyEnergy WRITE setSkyEnergy NOTIFY skyEnergyChanged)
34
35 Q_PROPERTY(QColor groundBottomColor READ groundBottomColor WRITE setGroundBottomColor NOTIFY groundBottomColorChanged)
36 Q_PROPERTY(QColor groundHorizonColor READ groundHorizonColor WRITE setGroundHorizonColor NOTIFY groundHorizonColorChanged)
37 Q_PROPERTY(float groundCurve READ groundCurve WRITE setGroundCurve NOTIFY groundCurveChanged)
38 Q_PROPERTY(float groundEnergy READ groundEnergy WRITE setGroundEnergy NOTIFY groundEnergyChanged)
39
40 Q_PROPERTY(QColor sunColor READ sunColor WRITE setSunColor NOTIFY sunColorChanged)
41 Q_PROPERTY(float sunLatitude READ sunLatitude WRITE setSunLatitude NOTIFY sunLatitudeChanged)
42 Q_PROPERTY(float sunLongitude READ sunLongitude WRITE setSunLongitude NOTIFY sunLongitudeChanged)
43 Q_PROPERTY(float sunAngleMin READ sunAngleMin WRITE setSunAngleMin NOTIFY sunAngleMinChanged)
44 Q_PROPERTY(float sunAngleMax READ sunAngleMax WRITE setSunAngleMax NOTIFY sunAngleMaxChanged)
45 Q_PROPERTY(float sunCurve READ sunCurve WRITE setSunCurve NOTIFY sunCurveChanged)
46 Q_PROPERTY(float sunEnergy READ sunEnergy WRITE setSunEnergy NOTIFY sunEnergyChanged)
47 Q_PROPERTY(SkyTextureQuality textureQuality READ textureQuality WRITE setTextureQuality NOTIFY textureQualityChanged)
48 QML_ELEMENT
49
50public:
51 enum class SkyTextureQuality {
52 SkyTextureQualityLow, // 512
53 SkyTextureQualityMedium, // 1024
54 SkyTextureQualityHigh, // 2048
55 SkyTextureQualityVeryHigh, // 4096
56 };
57 Q_ENUM(SkyTextureQuality)
58
59 ProceduralSkyTextureData();
60 ~ProceduralSkyTextureData();
61
62 QColor skyTopColor() const;
63 QColor skyHorizonColor() const;
64 float skyCurve() const;
65 float skyEnergy() const;
66
67 QColor groundBottomColor() const;
68 QColor groundHorizonColor() const;
69 float groundCurve() const;
70 float groundEnergy() const;
71
72 QColor sunColor() const;
73 float sunLatitude() const;
74 float sunLongitude() const;
75 float sunAngleMin() const;
76 float sunAngleMax() const;
77 float sunCurve() const;
78 float sunEnergy() const;
79
80 SkyTextureQuality textureQuality() const;
81
82public Q_SLOTS:
83 void setSkyTopColor(QColor skyTopColor);
84 void setSkyHorizonColor(QColor skyHorizonColor);
85 void setSkyCurve(float skyCurve);
86 void setSkyEnergy(float skyEnergy);
87
88 void setGroundBottomColor(QColor groundBottomColor);
89 void setGroundHorizonColor(QColor groundHorizonColor);
90 void setGroundCurve(float groundCurve);
91 void setGroundEnergy(float groundEnergy);
92
93 void setSunColor(QColor sunColor);
94 void setSunLatitude(float sunLatitude);
95 void setSunLongitude(float sunLongitude);
96 void setSunAngleMin(float sunAngleMin);
97 void setSunAngleMax(float sunAngleMax);
98 void setSunCurve(float sunCurve);
99 void setSunEnergy(float sunEnergy);
100
101 void setTextureQuality(SkyTextureQuality textureQuality);
102
103 void generateRGBA16FTexture();
104
105Q_SIGNALS:
106 void skyTopColorChanged(QColor skyTopColor);
107 void skyHorizonColorChanged(QColor skyHorizonColor);
108 void skyCurveChanged(float skyCurve);
109 void skyEnergyChanged(float skyEnergy);
110
111 void groundBottomColorChanged(QColor groundBottomColor);
112 void groundHorizonColorChanged(QColor groundHorizonColor);
113 void groundCurveChanged(float groundCurve);
114 void groundEnergyChanged(float groundEnergy);
115
116 void sunColorChanged(QColor sunColor);
117
118 void sunLatitudeChanged(float sunLatitude);
119 void sunLongitudeChanged(float sunLongitude);
120 void sunAngleMinChanged(float sunAngleMin);
121 void sunAngleMaxChanged(float sunAngleMax);
122 void sunCurveChanged(float sunCurve);
123 void sunEnergyChanged(float sunEnergy);
124
125 void textureQualityChanged(SkyTextureQuality textureQuality);
126
127private:
128
129 struct LinearColor {
130 float r = 0.0f;
131 float g = 0.0f;
132 float b = 0.0f;
133 float a = 0.0f;
134 LinearColor(const QColor &color);
135 LinearColor() {}
136
137 LinearColor interpolate(const LinearColor &color, float value) const;
138 LinearColor blend(const LinearColor &color) const;
139 quint32 toRGBE9995() const;
140 quint32 toRGBA8() const;
141 quint32 toRGBE8() const;
142 };
143
144 QByteArray generateSkyTexture(int width, int height, QByteArray &imageData, bool isRGBE) const;
145 void scheduleTextureUpdate();
146 QColor m_skyTopColor = QColor(165, 214, 241);
147 QColor m_skyHorizonColor = QColor(214, 234, 250);
148 float m_skyCurve = 0.09f;
149 float m_skyEnergy = 1.0f;
150
151 QColor m_groundBottomColor = QColor(40, 47, 54);
152 QColor m_groundHorizonColor = QColor(108, 101, 95);
153 float m_groundCurve = 0.02f;
154 float m_groundEnergy = 1.0f;
155
156 QColor m_sunColor = QColor(255, 255, 255);
157 float m_sunLatitude = 35.0f;
158 float m_sunLongitude = 0.0f;
159 float m_sunAngleMin = 1.0f;
160 float m_sunAngleMax = 100.0f;
161 float m_sunCurve = 0.05f;
162 float m_sunEnergy = 1.0f;
163
164 SkyTextureQuality m_textureQuality = SkyTextureQuality::SkyTextureQualityMedium;
165};
166
167QT_END_NAMESPACE
168
169#endif // PROCEDURALSKYTEXTURE_H
170

source code of qtquick3d/src/helpers/proceduralskytexturedata_p.h