blob: f552498983ca93d3bbbc4becd9c43fa2b821536c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
// 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.
*/
|