QGIS API Documentation 4.1.0-Master (3fcefe620d1)
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();
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 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
71) const
72{
73 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
74
75 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
76 material->setEnvironmentalLightingEnabled( true );
77 material->setInstancingEnabled( true, flags );
78 material->setInstancingMeshTransform( transform );
79
80 material->setObjectName( u"metalRoughMaterial"_s );
81 applySettingsToMaterial( metalRoughSettings, material, context );
82
83 return material;
84}
85
87{
88 QMap<QString, QString> parameters;
89 return parameters;
90}
91
92bool QgsMetalRoughMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
93{
94 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
95
96 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
97 if ( material->objectName() != "metalRoughMaterial"_L1 )
98 return false;
99
100 applySettingsToMaterial( metalRoughSettings, material, context );
101 return true;
102}
103
105{
106 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
107 Q_ASSERT( metalRoughSettings );
108 const QgsPropertyCollection &dataDefinedProperties = metalRoughSettings->dataDefinedProperties();
109 const QColor base = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::BaseColor, expressionContext, metalRoughSettings->baseColor() ) );
110 const QColor rawEmissionColor = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::EmissionColor, expressionContext, metalRoughSettings->emissionColor() );
111 const QColor emission = rawEmissionColor.isValid() ? Qgs3DUtils::srgbToLinear( rawEmissionColor ) : QColor();
112
113 QByteArray array;
114 array.resize( sizeof( float ) * 6 );
115 float *fptr = reinterpret_cast<float *>( array.data() );
116
117 *fptr++ = base.redF();
118 *fptr++ = base.greenF();
119 *fptr++ = base.blueF();
120
121 if ( emission.isValid() )
122 {
123 *fptr++ = emission.redF();
124 *fptr++ = emission.greenF();
125 *fptr++ = emission.blueF();
126 }
127 else
128 {
129 *fptr++ = 0.0f;
130 *fptr++ = 0.0f;
131 *fptr++ = 0.0f;
132 }
133 return array;
134}
135
136void QgsMetalRoughMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
137{
138 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
139
140 Qt3DCore::QAttribute *baseColorAttribute = new Qt3DCore::QAttribute( geometry );
141 baseColorAttribute->setName( u"dataDefinedBaseColor"_s );
142 baseColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
143 baseColorAttribute->setVertexSize( 3 );
144 baseColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
145 baseColorAttribute->setBuffer( dataBuffer );
146 baseColorAttribute->setByteStride( 6 * sizeof( float ) );
147 baseColorAttribute->setByteOffset( 0 );
148 baseColorAttribute->setCount( vertexCount );
149 geometry->addAttribute( baseColorAttribute );
150
151 Qt3DCore::QAttribute *emissionColorAttribute = new Qt3DCore::QAttribute( geometry );
152 emissionColorAttribute->setName( u"dataDefinedEmissionColor"_s );
153 emissionColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
154 emissionColorAttribute->setVertexSize( 3 );
155 emissionColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
156 emissionColorAttribute->setBuffer( dataBuffer );
157 emissionColorAttribute->setByteStride( 6 * sizeof( float ) );
158 emissionColorAttribute->setByteOffset( 3 * sizeof( float ) );
159 emissionColorAttribute->setCount( vertexCount );
160 geometry->addAttribute( emissionColorAttribute );
161
162 dataBuffer->setData( data );
163}
164
165void QgsMetalRoughMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughMaterialSettings *metalRoughSettings, QgsMetalRoughMaterial *material, const QgsMaterialContext &context )
166{
167 material->setBaseColor( context.isSelected() ? context.selectionColor() : metalRoughSettings->baseColor() );
168 material->setEmissionColor( metalRoughSettings->emissionColor().isValid() ? metalRoughSettings->emissionColor() : QColor( 0, 0, 0 ) );
169 material->setEmissionFactor( static_cast< float>( metalRoughSettings->emissionFactor() ) );
170 material->setClearCoatFactor( static_cast< float >( metalRoughSettings->clearCoatFactor() ) );
171 material->setClearCoatRoughness( static_cast< float >( metalRoughSettings->clearCoatRoughness() ) );
172 material->setMetalness( static_cast< float >( metalRoughSettings->metalness() ) );
173 material->setRoughness( static_cast< float >( metalRoughSettings->roughness() ) );
174 material->setReflectance( static_cast< float >( metalRoughSettings->reflectance() ) );
175 material->setAnisotropy( static_cast< float >( metalRoughSettings->anisotropy() ) );
176 material->setAnisotropyRotation( static_cast< float >( metalRoughSettings->anisotropyRotation() ) );
177 material->setOpacity( static_cast< float >( metalRoughSettings->opacity() ) );
178}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4375
@ Points
Point based rendering, requires point data.
Definition qgis.h:4379
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4376
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4381
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4377
@ Billboards
Flat billboard rendering.
Definition qgis.h:4383
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4382
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4378
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4380
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4398
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 QMatrix4x4 &transform=QMatrix4x4()) 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.
double reflectance() const
Returns the material's reflectance, as a value between 0 and 1.
double anisotropyRotation() const
Returns the rotation of the material's anisotropy, as a angle in degrees.
double clearCoatRoughness() const
Returns the material's clear coat roughness, as a value between 0 and 1.
double clearCoatFactor() const
Returns the material's clear coat factor, as a value between 0 and 1.
QColor emissionColor() const
Returns the material's emissive color.
double anisotropy() const
Returns the material's anisotropy, as a value between 0 and 1.
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.