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