QGIS API Documentation 4.1.0-Master (01362494303)
Loading...
Searching...
No Matches
qgsmetalroughtexturedmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmetalroughtexturedmaterial3dhandler.cpp
3 --------------------------------------
4 Date : April 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 "qgs3dutils.h"
19#include "qgsapplication.h"
21#include "qgsimagecache.h"
22#include "qgsimagetexture.h"
25
26#include <QString>
27#include <Qt3DCore/QEntity>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QPaintedTextureImage>
31#include <Qt3DRender/QParameter>
32#include <Qt3DRender/QTechnique>
33#include <Qt3DRender/QTexture>
34
35using namespace Qt::StringLiterals;
36
38{
39 const QgsMetalRoughTexturedMaterialSettings *texturedSettings = dynamic_cast< const QgsMetalRoughTexturedMaterialSettings * >( settings );
40 Q_ASSERT( texturedSettings );
41
42 switch ( technique )
43 {
46 {
47 if ( context.isHighlighted() )
48 {
49 return new QgsHighlightMaterial( technique );
50 }
51
52 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
53 material->setObjectName( u"metalRoughTexturedMaterial"_s );
54 applySettingsToMaterial( texturedSettings, material );
55
56 return material;
57 }
58
59 default:
60 return nullptr;
61 }
62}
63
65{
66 QMap<QString, QString> parameters;
67 return parameters;
68}
69
72
73bool QgsMetalRoughTexturedMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
74{
75 const QgsMetalRoughTexturedMaterialSettings *metalRoughTexturedSettings = qgis::down_cast< const QgsMetalRoughTexturedMaterialSettings * >( settings );
76
77 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
78 if ( !material || material->objectName() != "metalRoughTexturedMaterial"_L1 )
79 return false;
80
81 applySettingsToMaterial( metalRoughTexturedSettings, material );
82 return true;
83}
84
85Qt3DRender::QTexture2D *QgsMetalRoughTexturedMaterial3DHandler::loadTexture( const QString &path, bool isSrgb )
86{
87 if ( path.isEmpty() )
88 return nullptr;
89
90 bool fitsInCache = false;
91 QImage image = QgsApplication::imageCache()->pathAsImage( path, QSize(), true, 1.0, fitsInCache );
92 if ( image.isNull() )
93 return nullptr;
94
95 QgsImageTexture *textureImage = new QgsImageTexture( image );
96 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
97 // texture takes ownership of textureImage
98 texture->addTextureImage( textureImage );
99
100 if ( isSrgb )
101 {
102 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
103 }
104 else
105 {
106 texture->setFormat( Qt3DRender::QAbstractTexture::RGBA8_UNorm );
107 }
108
109 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
110 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
111 texture->setGenerateMipMaps( true );
112 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
113 texture->setMinificationFilter( Qt3DRender::QTexture2D::LinearMipMapLinear );
114
115 return texture;
116}
117
118void QgsMetalRoughTexturedMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughTexturedMaterialSettings *texturedSettings, QgsMetalRoughMaterial *material )
119{
120 material->setTextureScale( static_cast<float>( texturedSettings->textureScale() ) );
121 material->setTextureRotation( static_cast<float>( texturedSettings->textureRotation() ) );
122
123 // base color
124 if ( Qt3DRender::QTexture2D *baseTex = loadTexture( texturedSettings->baseColorTexturePath(), true ) )
125 {
126 // takes ownership of texture
127 material->setBaseColorTexture( baseTex );
128 }
129 else
130 {
131 // fallback to default solid color if no texture specified
132 material->setBaseColor( QColor( "grey" ) );
133 }
134
135 // metalness
136 if ( Qt3DRender::QTexture2D *metalTex = loadTexture( texturedSettings->metalnessTexturePath(), false ) )
137 {
138 // takes ownership of texture
139 material->setMetalnessTexture( metalTex );
140 }
141 else
142 {
143 // fallback to constant default value if no texture specified
144 material->setMetalness( 0.0 );
145 }
146
147 // roughness
148 if ( Qt3DRender::QTexture2D *roughTex = loadTexture( texturedSettings->roughnessTexturePath(), false ) )
149 {
150 // takes ownership of texture
151 material->setRoughnessTexture( roughTex );
152 }
153 else
154 {
155 // fallback to constant default value if no texture specified
156 material->setRoughness( 0.5 );
157 }
158
159 if ( Qt3DRender::QTexture2D *normalTex = loadTexture( texturedSettings->normalTexturePath(), false ) )
160 {
161 // takes ownership of texture
162 material->setNormalTexture( normalTex );
163 }
164 else
165 {
166 // default to none
167 material->setNormalTexture( nullptr );
168 }
169
170 // ambient occlusion
171 if ( Qt3DRender::QTexture2D *aoTex = loadTexture( texturedSettings->ambientOcclusionTexturePath(), false ) )
172 {
173 // takes ownership of texture
174 material->setAmbientOcclusionTexture( aoTex );
175 }
176 else
177 {
178 // default to none
179 material->setAmbientOcclusionTexture( nullptr );
180 }
181
182 if ( Qt3DRender::QTexture2D *emissionTex = loadTexture( texturedSettings->emissionTexturePath(), true ) )
183 {
184 material->setEmissionTexture( emissionTex );
185 }
186 else
187 {
188 // default to none
189 material->setEmissionTexture( nullptr );
190 }
191
192 material->setEmissionFactor( texturedSettings->emissionFactor() );
193};
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4342
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4349
Abstract base class for material settings.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Holds an image that can be used as a texture in the 3D view.
Context settings for a material.
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.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
A PBR metal rough shading material used for rendering with support for image texture maps.
double emissionFactor() const
Returns the emission factor, which dictates the strength of the emission effect.
QString roughnessTexturePath() const
Returns the path to the roughness texture map.
double textureRotation() const
Returns the texture rotation, in degrees.
double textureScale() const
Returns the texture scale.
QString normalTexturePath() const
Returns the path to the normal texture map.
QString ambientOcclusionTexturePath() const
Returns the path to the ambient occlusion texture map.
QString metalnessTexturePath() const
Returns the path to the metalness texture map.
QString baseColorTexturePath() const
Returns the path to the base color texture map.
QString emissionTexturePath() const
Returns the path to the emission/luminosity texture map.