1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dtexturedatafrontend_p.h"
5#include <QSize>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype ProceduralTextureData
11 \inqmlmodule QtQuick3D.Helpers
12 \inherits TextureData
13 \brief Allows creation of TextureData from QML.
14 \since 6.6
15
16 ProceduralTextureData is a helper type that allows creation of TextureData from QML.
17 The TextureData component iself is Abstract, and is usually created from C++. With
18 ProceduralTextureData, it is possible to populate a TextureData from QML.
19
20 \qml
21 ProceduralTextureData {
22 id: dynamicTextureData
23 property color color1: "red"
24 property color color2: "black"
25 width: 32
26 height: 32
27 hasTransparency: false
28 format: TextureData.RGBA8
29 textureData: generateTextureData(color1, color2)
30
31 function generateTextureData(newColor1: color, newColor2 : color) : ArrayBuffer {
32 let dataBuffer = new ArrayBuffer(width * height * 4)
33 let data = new Uint8Array(dataBuffer)
34 // Create a checkered pattern using newColor1 and newColor2
35 for (let x = 0; x < width; x++) {
36 for (let y = 0; y < height; y++) {
37 let index = (x + y * width) * 4
38 let color = (x % 2 === y % 2) ? newColor1 : newColor2
39 data[index + 0] = color.r * 255
40 data[index + 1] = color.g * 255
41 data[index + 2] = color.b * 255
42 data[index + 3] = 255
43 }
44 }
45 return dataBuffer
46 }
47 }
48 \endqml
49
50 In the above code snippet, the function generateTextureData is used to generate a
51 checkerboard pattern using the two colors color1 and color2. By filling an
52 ArrayBuffer with the generated data, the textureData property of the TextureData
53 is populated.
54*/
55
56/*!
57 \qmlproperty int ProceduralTextureData::width
58
59 This property holds the width of the texture data in pixels. The value defaults to 0.
60
61*/
62
63/*!
64 \qmlproperty int ProceduralTextureData::height
65
66 This property holds the height of the texture data in pixels. The value defaults to 0.
67*/
68
69/*!
70 \qmlproperty int ProceduralTextureData::depth
71
72 This property holds the depth of the texture data in pixels. The value defaults to 0.
73 Setting the depth above 0 means that the texture is handled as a 3D texture.
74*/
75
76/*!
77 \qmlproperty bool ProceduralTextureData::hasTransparency
78
79 This property holds whether the texture data has transparency.
80*/
81
82/*!
83 \qmlproperty enumeration ProceduralTextureData::format
84
85 This property holds the format of the texture data. The default format is
86 /c TexureData.RGBA8
87
88 \value TexureData.None The color format is not defined
89 \value TexureData.RGBA8 The color format is considered as 8-bit integer in R, G, B and alpha channels.
90 \value TexureData.RGBA16F The color format is considered as 16-bit float in R,G,B and alpha channels.
91 \value TexureData.RGBA32F The color format is considered as 32-bit float in R, G, B and alpha channels.
92 \value TexureData.RGBE8 The color format is considered as 8-bit mantissa in the R, G, and B channels and 8-bit shared exponent.
93 \value TexureData.R8 The color format is considered as 8-bit integer in R channel.
94 \value TexureData.R16 The color format is considered as 16-bit integer in R channel.
95 \value TexureData.R16F The color format is considered as 16-bit float in R channel.
96 \value TexureData.R32F The color format is considered as 32-bit float R channel.
97 \value TexureData.BC1 The color format is considred as BC1 compressed format with R, G, B, and alpha channels.
98 \value TexureData.BC2 The color format is considred as BC2 compressed format with R, G, B, and alpha channels.
99 \value TexureData.BC3 The color format is considred as BC3 compressed format with R, G, B, and alpha channels.
100 \value TexureData.BC4 The color format is considred as BC4 compressed format with one color channel.
101 \value TexureData.BC5 The color format is considred as BC5 compressed format with two color channels.
102 \value TexureData.BC6H The color format is considred as BC6H compressed format with three high dynamic range color channels.
103 \value TexureData.BC7 The color format is considred as BC7 compressed format with R, G, B, and alpha channels.
104 \value TexureData.DXT1_RGBA The color format is considered as DXT1 compressed format with R, G, B and alpha channels.
105 \value TexureData.DXT1_RGB The color format is considered as DXT1 compressed format with R, G and B channels.
106 \value TexureData.DXT3_RGBA The color format is considered as DXT3 compressed format with R, G, B and alpha channels.
107 \value TexureData.DXT5_RGBA The color format is considered as DXT5 compressed format with R, G, B and alpha channels.
108 \value TexureData.ETC2_RGB8 The color format is considered as ETC2 compressed format for RGB888 data
109 \value TexureData.ETC2_RGB8A1 The color format is considered as ETC2 compressed format for RGBA data where alpha is 1-bit.
110 \value TexureData.ETC2_RGBA8 The color format is considered as ETC2 compressed format with RGBA8888 data.
111 \value TexureData.ASTC_4x4 The color format is considered as ASTC compressed format with 4x4 block footprint.
112 \value TexureData.ASTC_5x4 The color format is considered as ASTC compressed format with 5x4 block footprint.
113 \value TexureData.ASTC_5x5 The color format is considered as ASTC compressed format with 5x5 block footprint.
114 \value TexureData.ASTC_6x5 The color format is considered as ASTC compressed format with 6x5 block footprint.
115 \value TexureData.ASTC_6x6 The color format is considered as ASTC compressed format with 6x6 block footprint.
116 \value TexureData.ASTC_8x5 The color format is considered as ASTC compressed format with 8x5 block footprint.
117 \value TexureData.ASTC_8x6 The color format is considered as ASTC compressed format with 8x6 block footprint.
118 \value TexureData.ASTC_8x8 The color format is considered as ASTC compressed format with 8x8 block footprint.
119 \value TexureData.ASTC_10x5 The color format is considered as ASTC compressed format with 10x5 block footprint.
120 \value TexureData.ASTC_10x6 The color format is considered as ASTC compressed format with 10x6 block footprint.
121 \value TexureData.ASTC_10x8 The color format is considered as ASTC compressed format with 10x8 block footprint.
122 \value TexureData.ASTC_10x10 The color format is considered as ASTC compressed format with 10x10 block footprint.
123 \value TexureData.ASTC_12x10 The color format is considered as ASTC compressed format with 12x10 block footprint.
124 \value TexureData.ASTC_12x12 The color format is considered as ASTC compressed format with 12x12 block footprint.
125
126 \note With the exception of \c TexureData.RGBA8, not every format is supported at runtime as this
127 depends on which backend is being used as well which hardware is being used.
128
129 \note \c TexureData.RGBE is internally represented as an \c TexureData.RGBA8 but is intepreted as described when used
130 as a lightProbe or skybox texture.
131
132 \note Using the value \c TexureData.None will assume the default value of \c TexureData.RGBA8
133*/
134
135/*!
136 \qmlproperty ArrayBuffer ProceduralTextureData::textureData
137
138 This property holds the texture data.
139*/
140
141QQuick3DTextureDataFrontend::QQuick3DTextureDataFrontend()
142{
143
144}
145
146
147QQuick3DTextureData::Format QQuick3DTextureDataFrontend::format() const
148{
149 return QQuick3DTextureData::format();
150}
151
152void QQuick3DTextureDataFrontend::setFormat(const QQuick3DTextureData::Format &newFormat)
153{
154 if (newFormat == QQuick3DTextureData::format())
155 return;
156
157 QQuick3DTextureData::setFormat(newFormat);
158
159 Q_EMIT formatChanged();
160}
161
162int QQuick3DTextureDataFrontend::depth() const
163{
164 return QQuick3DTextureData::depth();
165}
166
167void QQuick3DTextureDataFrontend::setDepth(int newDepth)
168{
169 if (newDepth == QQuick3DTextureData::depth())
170 return;
171
172
173 QQuick3DTextureData::setDepth(newDepth);
174
175 Q_EMIT depthChanged();
176}
177
178bool QQuick3DTextureDataFrontend::hasTransparency() const
179{
180 return QQuick3DTextureData::hasTransparency();
181}
182
183void QQuick3DTextureDataFrontend::setHasTransparency(bool newHasTransparency)
184{
185 if (newHasTransparency == QQuick3DTextureData::hasTransparency())
186 return;
187
188 QQuick3DTextureData::setHasTransparency(newHasTransparency);
189
190 Q_EMIT hasTransparencyChanged();
191}
192
193QByteArray QQuick3DTextureDataFrontend::textureData() const
194{
195 return QQuick3DTextureData::textureData();
196}
197
198void QQuick3DTextureDataFrontend::setTextureData(const QByteArray &newTextureData)
199{
200 QQuick3DTextureData::setTextureData(newTextureData);
201 Q_EMIT textureDataChanged();
202}
203
204int QQuick3DTextureDataFrontend::width() const
205{
206 return QQuick3DTextureData::size().width();
207}
208
209void QQuick3DTextureDataFrontend::setWidth(int newWidth)
210{
211 const auto size = QQuick3DTextureData::size();
212
213 if (size.width() == newWidth)
214 return;
215
216 QQuick3DTextureData::setSize(QSize(newWidth, size.height()));
217
218 Q_EMIT widthChanged();
219}
220
221int QQuick3DTextureDataFrontend::height() const
222{
223 return QQuick3DTextureData::size().height();
224}
225
226void QQuick3DTextureDataFrontend::setHeight(int newHeight)
227{
228 const auto size = QQuick3DTextureData::size();
229
230 if (size.height() == newHeight)
231 return;
232
233 QQuick3DTextureData::setSize(QSize(size.width(), newHeight));
234
235 Q_EMIT heightChanged();
236}
237
238QT_END_NAMESPACE
239

source code of qtquick3d/src/helpers/qquick3dtexturedatafrontend.cpp