QGIS API Documentation 4.1.0-Master (ca2ac17535b)
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( nullptr );
50 material->setEnvironmentalLightingEnabled( !context.isPreview() );
51 material->setObjectName( u"metalRoughMaterial"_s );
52 applySettingsToMaterial( metalRoughSettings, material, context );
53 material->setDataDefinedEnabled(
56 );
57 return material;
58 }
59
64 return nullptr;
65 }
66 return nullptr;
67}
68
70{
71 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
72
73 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
74 material->setEnvironmentalLightingEnabled( true );
75 material->setInstancingEnabled( true, flags );
76
77 material->setObjectName( u"metalRoughMaterial"_s );
78 applySettingsToMaterial( metalRoughSettings, material, context );
79
80 return material;
81}
82
84{
85 QMap<QString, QString> parameters;
86 return parameters;
87}
88
89bool QgsMetalRoughMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
90{
91 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
92
93 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
94 if ( material->objectName() != "metalRoughMaterial"_L1 )
95 return false;
96
97 applySettingsToMaterial( metalRoughSettings, material, context );
98 return true;
99}
100
102{
103 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
104 Q_ASSERT( metalRoughSettings );
105 const QgsPropertyCollection &dataDefinedProperties = metalRoughSettings->dataDefinedProperties();
106 const QColor base = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::BaseColor, expressionContext, metalRoughSettings->baseColor() ) );
107 const QColor rawEmissionColor = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::EmissionColor, expressionContext, metalRoughSettings->emissionColor() );
108 const QColor emission = rawEmissionColor.isValid() ? Qgs3DUtils::srgbToLinear( rawEmissionColor ) : QColor();
109
110 QByteArray array;
111 array.resize( sizeof( float ) * 6 );
112 float *fptr = reinterpret_cast<float *>( array.data() );
113
114 *fptr++ = base.redF();
115 *fptr++ = base.greenF();
116 *fptr++ = base.blueF();
117
118 if ( emission.isValid() )
119 {
120 *fptr++ = emission.redF();
121 *fptr++ = emission.greenF();
122 *fptr++ = emission.blueF();
123 }
124 else
125 {
126 *fptr++ = 0.0f;
127 *fptr++ = 0.0f;
128 *fptr++ = 0.0f;
129 }
130 return array;
131}
132
133void QgsMetalRoughMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
134{
135 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
136
137 Qt3DCore::QAttribute *baseColorAttribute = new Qt3DCore::QAttribute( geometry );
138 baseColorAttribute->setName( u"dataDefinedBaseColor"_s );
139 baseColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
140 baseColorAttribute->setVertexSize( 3 );
141 baseColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
142 baseColorAttribute->setBuffer( dataBuffer );
143 baseColorAttribute->setByteStride( 6 * sizeof( float ) );
144 baseColorAttribute->setByteOffset( 0 );
145 baseColorAttribute->setCount( vertexCount );
146 geometry->addAttribute( baseColorAttribute );
147
148 Qt3DCore::QAttribute *emissionColorAttribute = new Qt3DCore::QAttribute( geometry );
149 emissionColorAttribute->setName( u"dataDefinedEmissionColor"_s );
150 emissionColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
151 emissionColorAttribute->setVertexSize( 3 );
152 emissionColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
153 emissionColorAttribute->setBuffer( dataBuffer );
154 emissionColorAttribute->setByteStride( 6 * sizeof( float ) );
155 emissionColorAttribute->setByteOffset( 3 * sizeof( float ) );
156 emissionColorAttribute->setCount( vertexCount );
157 geometry->addAttribute( emissionColorAttribute );
158
159 dataBuffer->setData( data );
160}
161
162void QgsMetalRoughMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughMaterialSettings *metalRoughSettings, QgsMetalRoughMaterial *material, const QgsMaterialContext &context )
163{
164 material->setBaseColor( context.isSelected() ? context.selectionColor() : metalRoughSettings->baseColor() );
165 material->setEmissionColor( metalRoughSettings->emissionColor().isValid() ? metalRoughSettings->emissionColor() : QColor( 0, 0, 0 ) );
166 material->setEmissionFactor( static_cast< float>( metalRoughSettings->emissionFactor() ) );
167 material->setMetalness( static_cast< float >( metalRoughSettings->metalness() ) );
168 material->setRoughness( static_cast< float >( metalRoughSettings->roughness() ) );
169 material->setOpacity( static_cast< float >( metalRoughSettings->opacity() ) );
170}
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.
bool isPreview() const
Returns true if the material is being shown in a preview widget.
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.