QGIS API Documentation 4.1.0-Master (31622b25bb0)
Loading...
Searching...
No Matches
qgsmetalroughmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmetalroughmaterial3dhandler.cpp
3 --------------------------------------
4 Date : December 2023
5 Copyright : (C) 2023 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 "qgs3dutils.h"
22
23#include <QString>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QEntity>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DRender/QParameter>
29
30using namespace Qt::StringLiterals;
31
33{
34 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
35 Q_ASSERT( metalRoughSettings );
36
37 switch ( technique )
38 {
43 {
44 if ( context.isHighlighted() )
45 {
46 return new QgsHighlightMaterial( technique );
47 }
48
49 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial;
50 material->setObjectName( u"metalRoughMaterial"_s );
51 applySettingsToMaterial( metalRoughSettings, material, context );
52 material->setDataDefinedEnabled(
55 );
56 return material;
57 }
58
63 return nullptr;
64 }
65 return nullptr;
66}
67
69{
70 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
71
72 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
73 material->setInstancingEnabled( true, flags );
74
75 material->setObjectName( u"metalRoughMaterial"_s );
76 applySettingsToMaterial( metalRoughSettings, material, context );
77
78 return material;
79}
80
82{
83 QMap<QString, QString> parameters;
84 return parameters;
85}
86
87bool QgsMetalRoughMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
88{
89 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
90
91 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
92 if ( material->objectName() != "metalRoughMaterial"_L1 )
93 return false;
94
95 applySettingsToMaterial( metalRoughSettings, material, context );
96 return true;
97}
98
100{
101 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
102 Q_ASSERT( metalRoughSettings );
103 const QgsPropertyCollection &dataDefinedProperties = metalRoughSettings->dataDefinedProperties();
104 const QColor base = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::BaseColor, expressionContext, metalRoughSettings->baseColor() ) );
105 const QColor rawEmissionColor = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::EmissionColor, expressionContext, metalRoughSettings->emissionColor() );
106 const QColor emission = rawEmissionColor.isValid() ? Qgs3DUtils::srgbToLinear( rawEmissionColor ) : QColor();
107
108 QByteArray array;
109 array.resize( sizeof( float ) * 6 );
110 float *fptr = reinterpret_cast<float *>( array.data() );
111
112 *fptr++ = base.redF();
113 *fptr++ = base.greenF();
114 *fptr++ = base.blueF();
115
116 if ( emission.isValid() )
117 {
118 *fptr++ = emission.redF();
119 *fptr++ = emission.greenF();
120 *fptr++ = emission.blueF();
121 }
122 else
123 {
124 *fptr++ = 0.0f;
125 *fptr++ = 0.0f;
126 *fptr++ = 0.0f;
127 }
128 return array;
129}
130
131void QgsMetalRoughMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
132{
133 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
134
135 Qt3DCore::QAttribute *baseColorAttribute = new Qt3DCore::QAttribute( geometry );
136 baseColorAttribute->setName( u"dataDefinedBaseColor"_s );
137 baseColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
138 baseColorAttribute->setVertexSize( 3 );
139 baseColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
140 baseColorAttribute->setBuffer( dataBuffer );
141 baseColorAttribute->setByteStride( 6 * sizeof( float ) );
142 baseColorAttribute->setByteOffset( 0 );
143 baseColorAttribute->setCount( vertexCount );
144 geometry->addAttribute( baseColorAttribute );
145
146 Qt3DCore::QAttribute *emissionColorAttribute = new Qt3DCore::QAttribute( geometry );
147 emissionColorAttribute->setName( u"dataDefinedEmissionColor"_s );
148 emissionColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
149 emissionColorAttribute->setVertexSize( 3 );
150 emissionColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
151 emissionColorAttribute->setBuffer( dataBuffer );
152 emissionColorAttribute->setByteStride( 6 * sizeof( float ) );
153 emissionColorAttribute->setByteOffset( 3 * sizeof( float ) );
154 emissionColorAttribute->setCount( vertexCount );
155 geometry->addAttribute( emissionColorAttribute );
156
157 dataBuffer->setData( data );
158}
159
160void QgsMetalRoughMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughMaterialSettings *metalRoughSettings, QgsMetalRoughMaterial *material, const QgsMaterialContext &context )
161{
162 material->setBaseColor( context.isSelected() ? context.selectionColor() : metalRoughSettings->baseColor() );
163 material->setEmissionColor( metalRoughSettings->emissionColor().isValid() ? metalRoughSettings->emissionColor() : QColor( 0, 0, 0 ) );
164 material->setEmissionFactor( static_cast< float>( metalRoughSettings->emissionFactor() ) );
165 material->setMetalness( static_cast< float >( metalRoughSettings->metalness() ) );
166 material->setRoughness( static_cast< float >( metalRoughSettings->roughness() ) );
167 material->setOpacity( static_cast< float >( metalRoughSettings->opacity() ) );
168}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4342
@ Points
Point based rendering, requires point data.
Definition qgis.h:4346
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4348
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4344
@ Billboards
Flat billboard rendering.
Definition qgis.h:4350
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4349
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4345
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4347
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4365
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
Abstract base class for material settings.
@ BaseColor
Base color (metal-rough material).
@ EmissionColor
Emission color (metal-rough material).
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
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
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
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.
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...
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags) const override
Creates a QgsMaterial for instanced point rendering.
A PBR metal rough shading material used for rendering.
double opacity() const
Returns the opacity of the surface.
double roughness() const
Returns the material's roughness, as a value between 0 and 1.
double metalness() const
Returns the material's metalness, as a value between 0 and 1.
QColor emissionColor() const
Returns the material's emissive color.
double emissionFactor() const
Returns the emission factor, which dictates the strength of the emission effect.
QColor baseColor() const
Returns the base material color.
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.