QGIS API Documentation 4.3.0-Master (bf28115e945)
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 "qgs3d.h"
19#include "qgs3dutils.h"
22#include "qgsunlitmaterial.h"
23
24#include <QString>
25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QEntity>
28#include <Qt3DCore/QGeometry>
29#include <Qt3DRender/QParameter>
30
31using namespace Qt::StringLiterals;
32
34{
35 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
36 Q_ASSERT( metalRoughSettings );
37
38 switch ( technique )
39 {
44 {
45 if ( context.isHighlighted() )
46 {
48 }
49
50 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial( nullptr );
51 material->setEnvironmentalLightingEnabled( !context.isPreview() );
52 material->setObjectName( u"metalRoughMaterial"_s );
53 applySettingsToMaterial( metalRoughSettings, material, context );
54 material->setDataDefinedEnabled(
57 );
58 return material;
59 }
60
65 return nullptr;
66 }
67 return nullptr;
68}
69
71 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
72) const
73{
74 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
75
76 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
77 material->setEnvironmentalLightingEnabled( true );
78 material->setInstancingEnabled( true, flags );
79 material->setInstancingMeshTransform( transform );
80
81 material->setObjectName( u"metalRoughMaterial"_s );
82 applySettingsToMaterial( metalRoughSettings, material, context );
83
84 return material;
85}
86
88{
89 QMap<QString, QString> parameters;
90 return parameters;
91}
92
93bool QgsMetalRoughMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
94{
95 const QgsMetalRoughMaterialSettings *metalRoughSettings = qgis::down_cast< const QgsMetalRoughMaterialSettings * >( settings );
96
97 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
98 if ( material->objectName() != "metalRoughMaterial"_L1 )
99 return false;
100
101 applySettingsToMaterial( metalRoughSettings, material, context );
102 return true;
103}
104
106{
107 const QgsMetalRoughMaterialSettings *metalRoughSettings = dynamic_cast< const QgsMetalRoughMaterialSettings * >( settings );
108 Q_ASSERT( metalRoughSettings );
109 const QgsPropertyCollection &dataDefinedProperties = metalRoughSettings->dataDefinedProperties();
110 const QColor base = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::BaseColor, expressionContext, metalRoughSettings->baseColor() ) );
111 const QColor rawEmissionColor = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::EmissionColor, expressionContext, metalRoughSettings->emissionColor() );
112 const QColor emission = rawEmissionColor.isValid() ? Qgs3DUtils::srgbToLinear( rawEmissionColor ) : QColor();
113
114 QByteArray array;
115 array.resize( sizeof( float ) * 6 );
116 float *fptr = reinterpret_cast<float *>( array.data() );
117
118 *fptr++ = base.redF();
119 *fptr++ = base.greenF();
120 *fptr++ = base.blueF();
121
122 if ( emission.isValid() )
123 {
124 *fptr++ = emission.redF();
125 *fptr++ = emission.greenF();
126 *fptr++ = emission.blueF();
127 }
128 else
129 {
130 *fptr++ = 0.0f;
131 *fptr++ = 0.0f;
132 *fptr++ = 0.0f;
133 }
134 return array;
135}
136
137void QgsMetalRoughMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
138{
139 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
140
141 Qt3DCore::QAttribute *baseColorAttribute = new Qt3DCore::QAttribute( geometry );
142 baseColorAttribute->setName( u"dataDefinedBaseColor"_s );
143 baseColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
144 baseColorAttribute->setVertexSize( 3 );
145 baseColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
146 baseColorAttribute->setBuffer( dataBuffer );
147 baseColorAttribute->setByteStride( 6 * sizeof( float ) );
148 baseColorAttribute->setByteOffset( 0 );
149 baseColorAttribute->setCount( vertexCount );
150 geometry->addAttribute( baseColorAttribute );
151
152 Qt3DCore::QAttribute *emissionColorAttribute = new Qt3DCore::QAttribute( geometry );
153 emissionColorAttribute->setName( u"dataDefinedEmissionColor"_s );
154 emissionColorAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
155 emissionColorAttribute->setVertexSize( 3 );
156 emissionColorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
157 emissionColorAttribute->setBuffer( dataBuffer );
158 emissionColorAttribute->setByteStride( 6 * sizeof( float ) );
159 emissionColorAttribute->setByteOffset( 3 * sizeof( float ) );
160 emissionColorAttribute->setCount( vertexCount );
161 geometry->addAttribute( emissionColorAttribute );
162
163 dataBuffer->setData( data );
164}
165
166void QgsMetalRoughMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughMaterialSettings *metalRoughSettings, QgsMetalRoughMaterial *material, const QgsMaterialContext &context )
167{
168 material->setBaseColor( context.isSelected() ? context.selectionColor() : metalRoughSettings->baseColor() );
169 material->setEmissionColor( metalRoughSettings->emissionColor().isValid() ? metalRoughSettings->emissionColor() : QColor( 0, 0, 0 ) );
170 material->setEmissionFactor( static_cast< float>( metalRoughSettings->emissionFactor() ) );
171 material->setClearCoatFactor( static_cast< float >( metalRoughSettings->clearCoatFactor() ) );
172 material->setClearCoatRoughness( static_cast< float >( metalRoughSettings->clearCoatRoughness() ) );
173 material->setMetalness( static_cast< float >( metalRoughSettings->metalness() ) );
174 material->setRoughness( static_cast< float >( metalRoughSettings->roughness() ) );
175 material->setReflectance( static_cast< float >( metalRoughSettings->reflectance() ) );
176 material->setAnisotropy( static_cast< float >( metalRoughSettings->anisotropy() ) );
177 material->setAnisotropyRotation( static_cast< float >( metalRoughSettings->anisotropyRotation() ) );
178 material->setOpacity( static_cast< float >( metalRoughSettings->opacity() ) );
179}
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:163
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.