QGIS API Documentation 4.1.0-Master (31622b25bb0)
Loading...
Searching...
No Matches
qgsmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaterial3dhandler.cpp
3 --------------------------------------
4 Date : March 2026
5 Copyright : (C) 2026 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgs3drendercontext.h"
19
20#include <QPropertyAnimation>
21#include <QSequentialAnimationGroup>
22#include <QString>
23#include <Qt3DCore/QEntity>
24#include <Qt3DCore/QTransform>
25#include <Qt3DExtras/QConeMesh>
26#include <Qt3DExtras/QCuboidMesh>
27#include <Qt3DExtras/QSphereMesh>
28#include <Qt3DRender/QParameter>
29#include <Qt3DRender/QPointLight>
30#include <Qt3DRender/QTechnique>
31
32using namespace Qt::StringLiterals;
33
34
36{
38 res.mSelectedColor = context.selectionColor();
39 res.mTextureFilterQuality = context.textureFilterQuality();
40 return res;
41}
42
44{
45 Q_UNUSED( flags )
46 Q_UNUSED( settings )
47 Q_UNUSED( context )
48 return nullptr;
49}
50
52{
53 Q_UNUSED( settings )
54 Q_UNUSED( expressionContext )
55 return QByteArray();
56}
57
58void QgsAbstractMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes ) const
59{
60 Q_UNUSED( settings )
61 Q_UNUSED( geometry )
62 Q_UNUSED( vertexCount )
63 Q_UNUSED( dataDefinedBytes )
64}
65
66QList<QgsAbstractMaterial3DHandler::PreviewMeshType> QgsAbstractMaterial3DHandler::previewMeshTypes() const
67{
68 PreviewMeshType sphere;
69 sphere.type = u"sphere"_s;
70 sphere.displayName = QObject::tr( "Sphere" );
71
72 PreviewMeshType cube;
73 cube.type = u"cube"_s;
74 cube.displayName = QObject::tr( "Cube" );
75
76 PreviewMeshType cone;
77 cone.type = u"cone"_s;
78 cone.displayName = QObject::tr( "Cone" );
79
80 return { sphere, cube, cone };
81}
82
83Qt3DRender::QParameter *QgsAbstractMaterial3DHandler::findParameter( Qt3DRender::QEffect *effect, const QString &name )
84{
85 const QList<Qt3DRender::QParameter *> parameters = effect->parameters();
86 for ( Qt3DRender::QParameter *parameter : parameters )
87 {
88 if ( parameter->name() == name )
89 {
90 return parameter;
91 }
92 }
93
94 const QList< Qt3DRender::QTechnique *> techniques = effect->techniques();
95 for ( Qt3DRender::QTechnique *technique : techniques )
96 {
97 const QList<Qt3DRender::QParameter *> parameters = technique->parameters();
98 for ( Qt3DRender::QParameter *parameter : parameters )
99 {
100 if ( parameter->name() == name )
101 {
102 return parameter;
103 }
104 }
105 }
106
107 return nullptr;
108}
109
110Qt3DCore::QEntity *QgsAbstractMaterial3DHandler::createPreviewMesh( const QString &type, Qt3DCore::QEntity *parent ) const
111{
112 auto *entity = new Qt3DCore::QEntity( parent );
113 if ( type == "sphere"_L1 )
114 {
115 auto *mesh = new Qt3DExtras::QSphereMesh( entity );
116 mesh->setRadius( 1.0f );
117 mesh->setRings( 32 );
118 mesh->setSlices( 32 );
119 mesh->setGenerateTangents( true );
120 entity->addComponent( mesh );
121 }
122 else if ( type == "cube"_L1 )
123 {
124 auto *mesh = new Qt3DExtras::QCuboidMesh( entity );
125 mesh->setXExtent( 1.8f );
126 mesh->setYExtent( 1.8f );
127 mesh->setZExtent( 1.8f );
128
129 auto *transform = new Qt3DCore::QTransform( mesh );
130 transform->setRotation( QQuaternion::fromEulerAngles( 15, 35, 15 ) );
131
132 entity->addComponent( mesh );
133 entity->addComponent( transform );
134 }
135 else if ( type == "cone"_L1 )
136 {
137 auto *mesh = new Qt3DExtras::QConeMesh( entity );
138 mesh->setBottomRadius( 1.2f );
139 mesh->setLength( 1.8f );
140 mesh->setRings( 32 );
141 mesh->setSlices( 32 );
142 auto *transform = new Qt3DCore::QTransform( mesh );
143 transform->setRotation( QQuaternion::fromEulerAngles( 5, 0, 0 ) );
144
145 entity->addComponent( mesh );
146 entity->addComponent( transform );
147 }
148 return entity;
149}
150
152 const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, QWindow *, Qt3DCore::QEntity *parent
153) const
154{
155 auto *root = new Qt3DCore::QEntity( parent );
156
157 Qt3DCore::QEntity *meshEntity = createPreviewMesh( type, root );
158 Q_ASSERT( meshEntity );
159 meshEntity->setObjectName( "mesh" );
160 if ( QgsMaterial *mat = toMaterial( settings, Qgis::MaterialRenderingTechnique::Triangles, context ) )
161 {
162 mat->setParent( meshEntity );
163 meshEntity->addComponent( mat );
164 }
165
166 auto *lightEntity = new Qt3DCore::QEntity( root );
167 auto *light = new Qt3DRender::QPointLight( lightEntity );
168 light->setColor( Qt::white );
169 light->setIntensity( 1.0f );
170 auto *lightTransform = new Qt3DCore::QTransform( lightEntity );
171 lightTransform->setTranslation( QVector3D( 3, 3, 3 ) );
172 lightEntity->addComponent( light );
173 lightEntity->addComponent( lightTransform );
174
175 auto *animGroup = new QSequentialAnimationGroup( lightEntity );
176 animGroup->setLoopCount( -1 );
177
178 auto *swingLeft = new QPropertyAnimation( lightTransform, "translation", animGroup );
179 swingLeft->setDuration( 8000 );
180 swingLeft->setStartValue( QVector3D( 3, 3, 3 ) );
181 swingLeft->setEndValue( QVector3D( -5, 5, 3 ) );
182 swingLeft->setEasingCurve( QEasingCurve::InOutSine );
183
184 auto *swingRight = new QPropertyAnimation( lightTransform, "translation", animGroup );
185 swingRight->setDuration( 8000 );
186 swingRight->setStartValue( QVector3D( -5, 5, 3 ) );
187 swingRight->setEndValue( QVector3D( 3, 3, 3 ) );
188 swingRight->setEasingCurve( QEasingCurve::InOutSine );
189
190 animGroup->addAnimation( swingLeft );
191 animGroup->addAnimation( swingRight );
192 animGroup->start();
193
194 return root;
195}
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4365
Rendering context for preparation of 3D entities.
QColor selectionColor() const
Returns color used for selected features.
Qgis::TextureFilterQuality textureFilterQuality() const
Returns the texture filtering quality.
virtual Qt3DCore::QEntity * createPreviewScene(const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, QWindow *window, Qt3DCore::QEntity *parent) const
Builds a complete self-contained scene for previewing the material, using the specified mesh type.
static Qt3DRender::QParameter * findParameter(Qt3DRender::QEffect *effect, const QString &name)
Finds an existing parameter in an effect by name.
virtual void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes) const
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
virtual Qt3DCore::QEntity * createPreviewMesh(const QString &type, Qt3DCore::QEntity *parent) const
Creates a new entity representing a suitable preview mesh for this material type.
virtual QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const =0
Creates a new QgsMaterial object representing the material settings.
virtual QList< PreviewMeshType > previewMeshTypes() const
Returns a list of available preview mesh types for the material.
virtual QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags) const
Creates a QgsMaterial for instanced point rendering.
virtual QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
Abstract base class for material settings.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Context settings for a material.
static QgsMaterialContext fromRenderContext(const Qgs3DRenderContext &context)
Constructs a material context from the settings in a 3D render context.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
Encapsulates information about available preview meshes.
QString displayName
Translated, user-friendly name.