QGIS API Documentation 4.1.0-Master (64dc32379c2)
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, context );
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 &context ) 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, context );
82 return true;
83}
84
85Qt3DRender::QTexture2D *QgsMetalRoughTexturedMaterial3DHandler::loadTexture( const QString &path, bool isSrgb, const QgsMaterialContext &context )
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 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
96
97 if ( isSrgb )
98 {
99 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
100 }
101 else
102 {
103 texture->setFormat( Qt3DRender::QAbstractTexture::RGBA8_UNorm );
104 }
105
106 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
107 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
108 Qgs3DUtils::setTextureFiltering( texture, context );
109
110 // texture takes ownership of textureImage
111 texture->addTextureImage( new QgsImageTexture( image ) );
112
113 return texture;
114}
115
116void QgsMetalRoughTexturedMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughTexturedMaterialSettings *texturedSettings, QgsMetalRoughMaterial *material, const QgsMaterialContext &context )
117{
118 material->setTextureScale( static_cast<float>( texturedSettings->textureScale() ) );
119 material->setTextureRotation( static_cast<float>( texturedSettings->textureRotation() ) );
120
121 // base color
122 if ( Qt3DRender::QTexture2D *baseTex = loadTexture( texturedSettings->baseColorTexturePath(), true, context ) )
123 {
124 // takes ownership of texture
125 material->setBaseColorTexture( baseTex );
126 }
127 else
128 {
129 // fallback to default solid color if no texture specified
130 material->setBaseColor( QColor( "grey" ) );
131 }
132
133 // metalness
134 if ( Qt3DRender::QTexture2D *metalTex = loadTexture( texturedSettings->metalnessTexturePath(), false, context ) )
135 {
136 // takes ownership of texture
137 material->setMetalnessTexture( metalTex );
138 }
139 else
140 {
141 // fallback to constant default value if no texture specified
142 material->setMetalness( 0.0 );
143 }
144
145 // roughness
146 if ( Qt3DRender::QTexture2D *roughTex = loadTexture( texturedSettings->roughnessTexturePath(), false, context ) )
147 {
148 // takes ownership of texture
149 material->setRoughnessTexture( roughTex );
150 }
151 else
152 {
153 // fallback to constant default value if no texture specified
154 material->setRoughness( 0.5 );
155 }
156
157 if ( Qt3DRender::QTexture2D *normalTex = loadTexture( texturedSettings->normalTexturePath(), false, context ) )
158 {
159 // takes ownership of texture
160 material->setNormalTexture( normalTex );
161 }
162 else
163 {
164 // default to none
165 material->setNormalTexture( nullptr );
166 }
167
168 if ( Qt3DRender::QTexture2D *heightTex = loadTexture( texturedSettings->heightTexturePath(), false, context ) )
169 {
170 material->setHeightTexture( heightTex );
171 }
172 else
173 {
174 // default to none
175 material->setHeightTexture( nullptr );
176 }
177 material->setParallaxScale( texturedSettings->parallaxScale() );
178
179 // ambient occlusion
180 if ( Qt3DRender::QTexture2D *aoTex = loadTexture( texturedSettings->ambientOcclusionTexturePath(), false, context ) )
181 {
182 // takes ownership of texture
183 material->setAmbientOcclusionTexture( aoTex );
184 }
185 else
186 {
187 // default to none
188 material->setAmbientOcclusionTexture( nullptr );
189 }
190
191 if ( Qt3DRender::QTexture2D *emissionTex = loadTexture( texturedSettings->emissionTexturePath(), true, context ) )
192 {
193 material->setEmissionTexture( emissionTex );
194 }
195 else
196 {
197 // default to none
198 material->setEmissionTexture( nullptr );
199 }
200
201 material->setEmissionFactor( texturedSettings->emissionFactor() );
202 material->setOpacity( static_cast< float >( texturedSettings->opacity() ) );
203};
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
static void setTextureFiltering(Qt3DRender::QAbstractTexture *texture, const QgsMaterialContext &context)
Sets the default filtering options for a texture.
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.
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.
double opacity() const
Returns the opacity of the surface.
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 heightTexturePath() const
Returns the path to the height texture map.
QString emissionTexturePath() const
Returns the path to the emission/luminosity texture map.
double parallaxScale() const
Returns the parallax scale, which dictates the strength of the height displacement effect.