// Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example proceduraltexture \ingroup quick3d-examples \title Qt Quick 3D - Procedural Texture Example \examplecategory {3D} \brief Demonstrates how to provide custom texture data from C++ or QML. \image proceduraltexture-example.jpg This example makes use of QQuick3DTextureData and \l{Texture::textureData}{the textureData property} of Texture to provide texture data generated dynamically at runtime instead of loading it from a static asset. For demonstration purposes this example generates two gradient textures in C++ and QML respectively. First we define a C++ class for our texture data. We make it a subclass of QQuick3DTextureData. This is not strictly necessary, since there are no virtual functions, but it is much more convenient to have everything in one class. We define the properties we are going to use, and add \l QML_NAMED_ELEMENT to make it available from QML: \snippet proceduraltexture/gradienttexture.h class definition \dots We add a function to update the texture. It uses setSize, and setFormat to configure the texture, and setTextureData to set the image data: \snippet proceduraltexture/gradienttexture.cpp updateTexture The function \c generateTexture creates a QByteArray of the correct size, and fills it with image data: \snippet proceduraltexture/gradienttexture.cpp generateTexture We call \c updateTexture each time a property is changed: \snippet proceduraltexture/gradienttexture.cpp property Finally, we can use our new texture from QML: \snippet proceduraltexture/Main.qml cppTexture It is also possible to generate the same texture data in QML. In this case we use the ProceduralTextureData component: \snippet proceduraltexture/Main.qml qmlTexture Just like in C++ we fill a QByteArray with image data that reflects the size and format of the texture. When doing this from QML use the ArrayBuffer type to avoid unnecessary type conversion. */