QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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 <QString>
19#include <Qt3DCore/QEntity>
20#include <Qt3DCore/QTransform>
21#include <Qt3DExtras/QConeMesh>
22#include <Qt3DExtras/QCuboidMesh>
23#include <Qt3DExtras/QSphereMesh>
24#include <Qt3DRender/QParameter>
25#include <Qt3DRender/QPointLight>
26#include <Qt3DRender/QTechnique>
27
28using namespace Qt::StringLiterals;
29
30
32{
33 Q_UNUSED( settings )
34 Q_UNUSED( expressionContext )
35 return QByteArray();
36}
37
38void QgsAbstractMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes ) const
39{
40 Q_UNUSED( settings )
41 Q_UNUSED( geometry )
42 Q_UNUSED( vertexCount )
43 Q_UNUSED( dataDefinedBytes )
44}
45
50
51QList<QgsAbstractMaterial3DHandler::PreviewMeshType> QgsAbstractMaterial3DHandler::previewMeshTypes() const
52{
53 PreviewMeshType sphere;
54 sphere.type = u"sphere"_s;
55 sphere.displayName = QObject::tr( "Sphere" );
56
57 PreviewMeshType cube;
58 cube.type = u"cube"_s;
59 cube.displayName = QObject::tr( "Cube" );
60
61 PreviewMeshType cone;
62 cone.type = u"cone"_s;
63 cone.displayName = QObject::tr( "Cone" );
64
65 return { sphere, cube, cone };
66}
67
68Qt3DRender::QParameter *QgsAbstractMaterial3DHandler::findParameter( Qt3DRender::QEffect *effect, const QString &name )
69{
70 const QList<Qt3DRender::QParameter *> parameters = effect->parameters();
71 for ( Qt3DRender::QParameter *parameter : parameters )
72 {
73 if ( parameter->name() == name )
74 {
75 return parameter;
76 }
77 }
78
79 const QList< Qt3DRender::QTechnique *> techniques = effect->techniques();
80 for ( Qt3DRender::QTechnique *technique : techniques )
81 {
82 const QList<Qt3DRender::QParameter *> parameters = technique->parameters();
83 for ( Qt3DRender::QParameter *parameter : parameters )
84 {
85 if ( parameter->name() == name )
86 {
87 return parameter;
88 }
89 }
90 }
91
92 return nullptr;
93}
94
95Qt3DCore::QEntity *QgsAbstractMaterial3DHandler::createPreviewMesh( const QString &type, Qt3DCore::QEntity *parent ) const
96{
97 auto *entity = new Qt3DCore::QEntity( parent );
98 if ( type == "sphere"_L1 )
99 {
100 auto *mesh = new Qt3DExtras::QSphereMesh( entity );
101 mesh->setRadius( 1.0f );
102 mesh->setRings( 32 );
103 mesh->setSlices( 32 );
104 entity->addComponent( mesh );
105 }
106 else if ( type == "cube"_L1 )
107 {
108 auto *mesh = new Qt3DExtras::QCuboidMesh( entity );
109 mesh->setXExtent( 1.8f );
110 mesh->setYExtent( 1.8f );
111 mesh->setZExtent( 1.8f );
112
113 auto *transform = new Qt3DCore::QTransform( mesh );
114 transform->setRotation( QQuaternion::fromEulerAngles( 15, 35, 15 ) );
115
116 entity->addComponent( mesh );
117 entity->addComponent( transform );
118 }
119 else if ( type == "cone"_L1 )
120 {
121 auto *mesh = new Qt3DExtras::QConeMesh( entity );
122 mesh->setBottomRadius( 1.2f );
123 mesh->setLength( 1.8f );
124 mesh->setRings( 32 );
125 mesh->setSlices( 32 );
126 auto *transform = new Qt3DCore::QTransform( mesh );
127 transform->setRotation( QQuaternion::fromEulerAngles( 5, 0, 0 ) );
128
129 entity->addComponent( mesh );
130 entity->addComponent( transform );
131 }
132 return entity;
133}
134
136 const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, Qt3DExtras::Qt3DWindow *, Qt3DCore::QEntity *parent
137) const
138{
139 auto *root = new Qt3DCore::QEntity( parent );
140
141 Qt3DCore::QEntity *meshEntity = createPreviewMesh( type, root );
142 Q_ASSERT( meshEntity );
143 meshEntity->setObjectName( "mesh" );
144 if ( QgsMaterial *mat = toMaterial( settings, Qgis::MaterialRenderingTechnique::Triangles, context ) )
145 {
146 mat->setParent( meshEntity );
147 meshEntity->addComponent( mat );
148 }
149
150 auto *lightEntity = new Qt3DCore::QEntity( root );
151 auto *light = new Qt3DRender::QPointLight( lightEntity );
152 light->setColor( Qt::white );
153 light->setIntensity( 1.0f );
154 auto *lightTransform = new Qt3DCore::QTransform( lightEntity );
155 lightTransform->setTranslation( QVector3D( 3, 3, 3 ) );
156 lightEntity->addComponent( light );
157 lightEntity->addComponent( lightTransform );
158
159 return root;
160}
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4328
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 Qt3DCore::QEntity * createPreviewScene(const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, Qt3DExtras::Qt3DWindow *window, Qt3DCore::QEntity *parent) const
Builds a complete self-contained scene for previewing the material, using the specified mesh type.
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.
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.