QGIS API Documentation 4.1.0-Master (ca2ac17535b)
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 return mIsPreview;
46}
47
49{
50 mIsPreview = isPreview;
51}
52
54{
55 Q_UNUSED( flags )
56 Q_UNUSED( settings )
57 Q_UNUSED( context )
58 return nullptr;
59}
60
62{
63 Q_UNUSED( settings )
64 Q_UNUSED( expressionContext )
65 return QByteArray();
66}
67
68void QgsAbstractMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes ) const
69{
70 Q_UNUSED( settings )
71 Q_UNUSED( geometry )
72 Q_UNUSED( vertexCount )
73 Q_UNUSED( dataDefinedBytes )
74}
75
76QList<QgsAbstractMaterial3DHandler::PreviewMeshType> QgsAbstractMaterial3DHandler::previewMeshTypes() const
77{
78 PreviewMeshType sphere;
79 sphere.type = u"sphere"_s;
80 sphere.displayName = QObject::tr( "Sphere" );
81
82 PreviewMeshType cube;
83 cube.type = u"cube"_s;
84 cube.displayName = QObject::tr( "Cube" );
85
86 PreviewMeshType cone;
87 cone.type = u"cone"_s;
88 cone.displayName = QObject::tr( "Cone" );
89
90 return { sphere, cube, cone };
91}
92
93Qt3DRender::QParameter *QgsAbstractMaterial3DHandler::findParameter( Qt3DRender::QEffect *effect, const QString &name )
94{
95 const QList<Qt3DRender::QParameter *> parameters = effect->parameters();
96 for ( Qt3DRender::QParameter *parameter : parameters )
97 {
98 if ( parameter->name() == name )
99 {
100 return parameter;
101 }
102 }
103
104 const QList< Qt3DRender::QTechnique *> techniques = effect->techniques();
105 for ( Qt3DRender::QTechnique *technique : techniques )
106 {
107 const QList<Qt3DRender::QParameter *> parameters = technique->parameters();
108 for ( Qt3DRender::QParameter *parameter : parameters )
109 {
110 if ( parameter->name() == name )
111 {
112 return parameter;
113 }
114 }
115 }
116
117 return nullptr;
118}
119
120Qt3DCore::QEntity *QgsAbstractMaterial3DHandler::createPreviewMesh( const QString &type, Qt3DCore::QEntity *parent ) const
121{
122 auto *entity = new Qt3DCore::QEntity( parent );
123 if ( type == "sphere"_L1 )
124 {
125 auto *mesh = new Qt3DExtras::QSphereMesh( entity );
126 mesh->setRadius( 1.0f );
127 mesh->setRings( 32 );
128 mesh->setSlices( 32 );
129 mesh->setGenerateTangents( true );
130 entity->addComponent( mesh );
131 }
132 else if ( type == "cube"_L1 )
133 {
134 auto *mesh = new Qt3DExtras::QCuboidMesh( entity );
135 mesh->setXExtent( 1.8f );
136 mesh->setYExtent( 1.8f );
137 mesh->setZExtent( 1.8f );
138
139 auto *transform = new Qt3DCore::QTransform( mesh );
140 transform->setRotation( QQuaternion::fromEulerAngles( 15, 35, 15 ) );
141
142 entity->addComponent( mesh );
143 entity->addComponent( transform );
144 }
145 else if ( type == "cone"_L1 )
146 {
147 auto *mesh = new Qt3DExtras::QConeMesh( entity );
148 mesh->setBottomRadius( 1.2f );
149 mesh->setLength( 1.8f );
150 mesh->setRings( 32 );
151 mesh->setSlices( 32 );
152 auto *transform = new Qt3DCore::QTransform( mesh );
153 transform->setRotation( QQuaternion::fromEulerAngles( 5, 0, 0 ) );
154
155 entity->addComponent( mesh );
156 entity->addComponent( transform );
157 }
158 return entity;
159}
160
162 const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, QWindow *, Qt3DCore::QEntity *parent
163) const
164{
165 auto *root = new Qt3DCore::QEntity( parent );
166
167 Qt3DCore::QEntity *meshEntity = createPreviewMesh( type, root );
168 Q_ASSERT( meshEntity );
169 meshEntity->setObjectName( "mesh" );
170 if ( QgsMaterial *mat = toMaterial( settings, Qgis::MaterialRenderingTechnique::Triangles, context ) )
171 {
172 mat->setParent( meshEntity );
173 meshEntity->addComponent( mat );
174 }
175
176 auto *lightEntity = new Qt3DCore::QEntity( root );
177 auto *light = new Qt3DRender::QPointLight( lightEntity );
178 light->setColor( Qt::white );
179 light->setIntensity( 1.0f );
180 auto *lightTransform = new Qt3DCore::QTransform( lightEntity );
181 lightTransform->setTranslation( QVector3D( 3, 3, 3 ) );
182 lightEntity->addComponent( light );
183 lightEntity->addComponent( lightTransform );
184
185 auto *animGroup = new QSequentialAnimationGroup( lightEntity );
186 animGroup->setLoopCount( -1 );
187
188 auto *swingLeft = new QPropertyAnimation( lightTransform, "translation", animGroup );
189 swingLeft->setDuration( 8000 );
190 swingLeft->setStartValue( QVector3D( 3, 3, 3 ) );
191 swingLeft->setEndValue( QVector3D( -5, 5, 3 ) );
192 swingLeft->setEasingCurve( QEasingCurve::InOutSine );
193
194 auto *swingRight = new QPropertyAnimation( lightTransform, "translation", animGroup );
195 swingRight->setDuration( 8000 );
196 swingRight->setStartValue( QVector3D( -5, 5, 3 ) );
197 swingRight->setEndValue( QVector3D( 3, 3, 3 ) );
198 swingRight->setEasingCurve( QEasingCurve::InOutSine );
199
200 animGroup->addAnimation( swingLeft );
201 animGroup->addAnimation( swingRight );
202 animGroup->start();
203
204 return root;
205}
@ 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.
void setIsPreview(bool isPreview)
Sets whether the material is being shown in a preview widget.
bool isPreview() const
Returns true if the material is being shown in a preview widget.
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.