QGIS API Documentation 4.3.0-Master (18b5e825726)
Loading...
Searching...
No Matches
qgsunlitmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsunlitmaterial3dhandler.cpp
3 --------------------------------------
4 Date : June 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 "qgs3d.h"
19#include "qgs3dutils.h"
20#include "qgsunlitmaterial.h"
22
23#include <QMap>
24#include <QString>
25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32
33using namespace Qt::StringLiterals;
34
35
37{
38 switch ( technique )
39 {
41 {
42 Q_ASSERT( false );
43 return nullptr;
44 }
50 {
51 if ( context.isHighlighted() )
52 {
54 }
55
56 const QgsUnlitMaterialSettings *unlitSettings = dynamic_cast< const QgsUnlitMaterialSettings * >( settings );
57 Q_ASSERT( unlitSettings );
58
59 auto material = new QgsUnlitMaterial();
60 material->setObjectName( u"unlitMaterial"_s );
61
62 const QgsPropertyCollection &dataDefinedProperties = unlitSettings->dataDefinedProperties();
63 const bool dataDefined = dataDefinedProperties.isActive( QgsAbstractMaterialSettings::Property::BaseColor );
64 if ( !dataDefined )
65 {
66 const QColor color = context.isSelected() ? context.selectionColor() : unlitSettings->color();
67 material->setColor( color );
68 }
69 material->setDataDefinedEnabled( dataDefined );
70 return material;
71 }
72
75 return nullptr;
76 }
77 return nullptr;
78}
79
81{
82 return {};
83}
84
86{
87 auto unlitSettings = dynamic_cast< const QgsUnlitMaterialSettings * >( settings );
88 Q_ASSERT( unlitSettings );
89
90 const QColor color = Qgs3DUtils::srgbToLinear( unlitSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::BaseColor, expressionContext, unlitSettings->color() ) );
91
92 QByteArray array;
93 array.resize( sizeof( float ) * 3 );
94 float *fptr = reinterpret_cast<float *>( array.data() );
95
96 *fptr++ = color.redF();
97 *fptr++ = color.greenF();
98 *fptr++ = color.blueF();
99
100 return array;
101}
102
103void QgsUnlitMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
104{
105 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
106
107 Qt3DCore::QAttribute *baseColorAttribute = new Qt3DCore::QAttribute( geometry );
108 baseColorAttribute->setName( u"dataDefinedBaseColor"_s );
109 baseColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
110 baseColorAttribute->setVertexSize( 3 );
111 baseColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
112 baseColorAttribute->setBuffer( dataBuffer );
113 baseColorAttribute->setByteStride( 3 * sizeof( float ) );
114 baseColorAttribute->setByteOffset( 0 );
115 baseColorAttribute->setCount( vertexCount );
116 geometry->addAttribute( baseColorAttribute );
117
118 dataBuffer->setData( data );
119}
120
121bool QgsUnlitMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
122{
123 auto unlitSettings = qgis::down_cast< const QgsUnlitMaterialSettings * >( settings );
124
125 QgsUnlitMaterial *material = sceneRoot->findChild<QgsUnlitMaterial *>();
126 if ( !material || material->objectName() != "unlitMaterial"_L1 )
127 return false;
128
129 material->setColor( unlitSettings->color() );
130
131 return true;
132}
133
135 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
136) const
137{
138 auto unlitSettings = qgis::down_cast< const QgsUnlitMaterialSettings * >( settings );
139
140 auto material = new QgsUnlitMaterial();
141 material->setInstancingEnabled( true, flags );
142 material->setInstancingMeshTransform( transform );
143 material->setObjectName( u"unlitMaterial"_s );
144
145 const QColor color = context.isSelected() ? context.selectionColor() : unlitSettings->color();
146 material->setColor( color );
147
148 return material;
149}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4388
@ Points
Point based rendering, requires point data.
Definition qgis.h:4392
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4389
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4394
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4390
@ Billboards
Flat billboard rendering.
Definition qgis.h:4396
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4395
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4391
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4393
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4411
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
static QgsUnlitMaterial * createHighlightMaterial()
Creates a new highlight material, consisting of an unlit material respecting the user's map highlight...
Definition qgs3d.cpp:168
Abstract base class for material settings.
@ BaseColor
Base color (metal-rough material).
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Context settings for a material.
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform=QMatrix4x4()) const override
Creates a QgsMaterial for instanced point rendering.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering solid color, unlit objects.
QColor color() const
Returns the material color.