| 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 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \qmltype ProceduralTextureData |
| 11 | \inqmlmodule QtQuick3D.Helpers |
| 12 | \inherits TextureData |
| 13 | \brief Allows creation and population 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 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. |
| 60 | \default 0 |
| 61 | |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | \qmlproperty int ProceduralTextureData::height |
| 66 | |
| 67 | This property holds the height of the texture data in pixels. |
| 68 | \default 0 |
| 69 | */ |
| 70 | |
| 71 | /*! |
| 72 | \qmlproperty int ProceduralTextureData::depth |
| 73 | |
| 74 | This property holds the depth of the texture data in pixels. |
| 75 | \default 0 |
| 76 | Setting the depth above 0 means that the texture is handled as a 3D texture. |
| 77 | */ |
| 78 | |
| 79 | /*! |
| 80 | \qmlproperty bool ProceduralTextureData::hasTransparency |
| 81 | |
| 82 | This property holds whether the texture data has transparency. |
| 83 | */ |
| 84 | |
| 85 | /*! |
| 86 | \qmlproperty enumeration ProceduralTextureData::format |
| 87 | |
| 88 | This property holds the format of the texture data. |
| 89 | |
| 90 | \default TextureData.RGBA8 |
| 91 | |
| 92 | \value TexureData.None The color format is not defined |
| 93 | \value TexureData.RGBA8 The color format is considered as 8-bit integer in R, G, B and alpha channels. |
| 94 | \value TexureData.RGBA16F The color format is considered as 16-bit float in R,G,B and alpha channels. |
| 95 | \value TexureData.RGBA32F The color format is considered as 32-bit float in R, G, B and alpha channels. |
| 96 | \value TexureData.RGBE8 The color format is considered as 8-bit mantissa in the R, G, and B channels and 8-bit shared exponent. |
| 97 | \value TexureData.R8 The color format is considered as 8-bit integer in R channel. |
| 98 | \value TexureData.R16 The color format is considered as 16-bit integer in R channel. |
| 99 | \value TexureData.R16F The color format is considered as 16-bit float in R channel. |
| 100 | \value TexureData.R32F The color format is considered as 32-bit float R channel. |
| 101 | \value TexureData.BC1 The color format is considered as BC1 compressed format with R, G, B, and alpha channels. |
| 102 | \value TexureData.BC2 The color format is considered as BC2 compressed format with R, G, B, and alpha channels. |
| 103 | \value TexureData.BC3 The color format is considered as BC3 compressed format with R, G, B, and alpha channels. |
| 104 | \value TexureData.BC4 The color format is considered as BC4 compressed format with one color channel. |
| 105 | \value TexureData.BC5 The color format is considered as BC5 compressed format with two color channels. |
| 106 | \value TexureData.BC6H The color format is considered as BC6H compressed format with three high dynamic range color channels. |
| 107 | \value TexureData.BC7 The color format is considered as BC7 compressed format with R, G, B, and alpha channels. |
| 108 | \value TexureData.DXT1_RGBA The color format is considered as DXT1 compressed format with R, G, B and alpha channels. |
| 109 | \value TexureData.DXT1_RGB The color format is considered as DXT1 compressed format with R, G and B channels. |
| 110 | \value TexureData.DXT3_RGBA The color format is considered as DXT3 compressed format with R, G, B and alpha channels. |
| 111 | \value TexureData.DXT5_RGBA The color format is considered as DXT5 compressed format with R, G, B and alpha channels. |
| 112 | \value TexureData.ETC2_RGB8 The color format is considered as ETC2 compressed format for RGB888 data |
| 113 | \value TexureData.ETC2_RGB8A1 The color format is considered as ETC2 compressed format for RGBA data where alpha is 1-bit. |
| 114 | \value TexureData.ETC2_RGBA8 The color format is considered as ETC2 compressed format with RGBA8888 data. |
| 115 | \value TexureData.ASTC_4x4 The color format is considered as ASTC compressed format with 4x4 block footprint. |
| 116 | \value TexureData.ASTC_5x4 The color format is considered as ASTC compressed format with 5x4 block footprint. |
| 117 | \value TexureData.ASTC_5x5 The color format is considered as ASTC compressed format with 5x5 block footprint. |
| 118 | \value TexureData.ASTC_6x5 The color format is considered as ASTC compressed format with 6x5 block footprint. |
| 119 | \value TexureData.ASTC_6x6 The color format is considered as ASTC compressed format with 6x6 block footprint. |
| 120 | \value TexureData.ASTC_8x5 The color format is considered as ASTC compressed format with 8x5 block footprint. |
| 121 | \value TexureData.ASTC_8x6 The color format is considered as ASTC compressed format with 8x6 block footprint. |
| 122 | \value TexureData.ASTC_8x8 The color format is considered as ASTC compressed format with 8x8 block footprint. |
| 123 | \value TexureData.ASTC_10x5 The color format is considered as ASTC compressed format with 10x5 block footprint. |
| 124 | \value TexureData.ASTC_10x6 The color format is considered as ASTC compressed format with 10x6 block footprint. |
| 125 | \value TexureData.ASTC_10x8 The color format is considered as ASTC compressed format with 10x8 block footprint. |
| 126 | \value TexureData.ASTC_10x10 The color format is considered as ASTC compressed format with 10x10 block footprint. |
| 127 | \value TexureData.ASTC_12x10 The color format is considered as ASTC compressed format with 12x10 block footprint. |
| 128 | \value TexureData.ASTC_12x12 The color format is considered as ASTC compressed format with 12x12 block footprint. |
| 129 | |
| 130 | \note With the exception of \c TexureData.RGBA8, not every format is supported at runtime as this |
| 131 | depends on which backend is being used as well which hardware is being used. |
| 132 | |
| 133 | \note \c TexureData.RGBE is internally represented as an \c TexureData.RGBA8 but is interpreted as described when used |
| 134 | as a lightProbe or skybox texture. |
| 135 | |
| 136 | \note Using the value \c TexureData.None will assume the default value of \c TexureData.RGBA8 |
| 137 | */ |
| 138 | |
| 139 | /*! |
| 140 | \qmlproperty ArrayBuffer ProceduralTextureData::textureData |
| 141 | |
| 142 | This property holds the texture data. |
| 143 | */ |
| 144 | |
| 145 | QQuick3DTextureDataFrontend::QQuick3DTextureDataFrontend() |
| 146 | { |
| 147 | |
| 148 | } |
| 149 | |
| 150 | |
| 151 | QQuick3DTextureData::Format QQuick3DTextureDataFrontend::format() const |
| 152 | { |
| 153 | return QQuick3DTextureData::format(); |
| 154 | } |
| 155 | |
| 156 | void QQuick3DTextureDataFrontend::setFormat(const QQuick3DTextureData::Format &newFormat) |
| 157 | { |
| 158 | if (newFormat == QQuick3DTextureData::format()) |
| 159 | return; |
| 160 | |
| 161 | QQuick3DTextureData::setFormat(newFormat); |
| 162 | |
| 163 | Q_EMIT formatChanged(); |
| 164 | } |
| 165 | |
| 166 | int QQuick3DTextureDataFrontend::depth() const |
| 167 | { |
| 168 | return QQuick3DTextureData::depth(); |
| 169 | } |
| 170 | |
| 171 | void QQuick3DTextureDataFrontend::setDepth(int newDepth) |
| 172 | { |
| 173 | if (newDepth == QQuick3DTextureData::depth()) |
| 174 | return; |
| 175 | |
| 176 | |
| 177 | QQuick3DTextureData::setDepth(newDepth); |
| 178 | |
| 179 | Q_EMIT depthChanged(); |
| 180 | } |
| 181 | |
| 182 | bool QQuick3DTextureDataFrontend::hasTransparency() const |
| 183 | { |
| 184 | return QQuick3DTextureData::hasTransparency(); |
| 185 | } |
| 186 | |
| 187 | void QQuick3DTextureDataFrontend::setHasTransparency(bool newHasTransparency) |
| 188 | { |
| 189 | if (newHasTransparency == QQuick3DTextureData::hasTransparency()) |
| 190 | return; |
| 191 | |
| 192 | QQuick3DTextureData::setHasTransparency(newHasTransparency); |
| 193 | |
| 194 | Q_EMIT hasTransparencyChanged(); |
| 195 | } |
| 196 | |
| 197 | QByteArray QQuick3DTextureDataFrontend::textureData() const |
| 198 | { |
| 199 | return QQuick3DTextureData::textureData(); |
| 200 | } |
| 201 | |
| 202 | void QQuick3DTextureDataFrontend::setTextureData(const QByteArray &newTextureData) |
| 203 | { |
| 204 | QQuick3DTextureData::setTextureData(newTextureData); |
| 205 | Q_EMIT textureDataChanged(); |
| 206 | } |
| 207 | |
| 208 | int QQuick3DTextureDataFrontend::width() const |
| 209 | { |
| 210 | return QQuick3DTextureData::size().width(); |
| 211 | } |
| 212 | |
| 213 | void QQuick3DTextureDataFrontend::setWidth(int newWidth) |
| 214 | { |
| 215 | const auto size = QQuick3DTextureData::size(); |
| 216 | |
| 217 | if (size.width() == newWidth) |
| 218 | return; |
| 219 | |
| 220 | QQuick3DTextureData::setSize(QSize(newWidth, size.height())); |
| 221 | |
| 222 | Q_EMIT widthChanged(); |
| 223 | } |
| 224 | |
| 225 | int QQuick3DTextureDataFrontend::height() const |
| 226 | { |
| 227 | return QQuick3DTextureData::size().height(); |
| 228 | } |
| 229 | |
| 230 | void QQuick3DTextureDataFrontend::setHeight(int newHeight) |
| 231 | { |
| 232 | const auto size = QQuick3DTextureData::size(); |
| 233 | |
| 234 | if (size.height() == newHeight) |
| 235 | return; |
| 236 | |
| 237 | QQuick3DTextureData::setSize(QSize(size.width(), newHeight)); |
| 238 | |
| 239 | Q_EMIT heightChanged(); |
| 240 | } |
| 241 | |
| 242 | QT_END_NAMESPACE |
| 243 | |