QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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 "qgsapplication.h"
20#include "qgsimagecache.h"
21#include "qgsimagetexture.h"
24
25#include <QString>
26#include <Qt3DCore/QEntity>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QPaintedTextureImage>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32#include <Qt3DRender/QTexture>
33
34using namespace Qt::StringLiterals;
35
37{
38 const QgsMetalRoughTexturedMaterialSettings *texturedSettings = dynamic_cast< const QgsMetalRoughTexturedMaterialSettings * >( settings );
39 Q_ASSERT( texturedSettings );
40
41 switch ( technique )
42 {
45 {
46 if ( context.isHighlighted() )
47 {
48 return new QgsHighlightMaterial( technique );
49 }
50
51 QgsMetalRoughMaterial *material = new QgsMetalRoughMaterial();
52 material->setObjectName( u"metalRoughTexturedMaterial"_s );
53 applySettingsToMaterial( texturedSettings, material );
54
55 return material;
56 }
57
58 default:
59 return nullptr;
60 }
61}
62
64{
65 QMap<QString, QString> parameters;
66 return parameters;
67}
68
71
72bool QgsMetalRoughTexturedMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
73{
74 const QgsMetalRoughTexturedMaterialSettings *metalRoughTexturedSettings = qgis::down_cast< const QgsMetalRoughTexturedMaterialSettings * >( settings );
75
76 QgsMetalRoughMaterial *material = sceneRoot->findChild<QgsMetalRoughMaterial *>();
77 if ( !material || material->objectName() != "metalRoughTexturedMaterial"_L1 )
78 return false;
79
80 applySettingsToMaterial( metalRoughTexturedSettings, material );
81 return true;
82}
83
84Qt3DRender::QTexture2D *QgsMetalRoughTexturedMaterial3DHandler::loadTexture( const QString &path )
85{
86 if ( path.isEmpty() )
87 return nullptr;
88
89 bool fitsInCache = false;
90 QImage image = QgsApplication::imageCache()->pathAsImage( path, QSize(), true, 1.0, fitsInCache );
91 if ( image.isNull() )
92 return nullptr;
93
94 QgsImageTexture *textureImage = new QgsImageTexture( image );
95 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
96 // texture takes ownership of textureImage
97 texture->addTextureImage( textureImage );
98
99 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
100 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
101 texture->setGenerateMipMaps( true );
102 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
103 texture->setMinificationFilter( Qt3DRender::QTexture2D::LinearMipMapLinear );
104
105 return texture;
106}
107
108void QgsMetalRoughTexturedMaterial3DHandler::applySettingsToMaterial( const QgsMetalRoughTexturedMaterialSettings *texturedSettings, QgsMetalRoughMaterial *material )
109{
110 material->setTextureScale( static_cast<float>( texturedSettings->textureScale() ) );
111
112 // base color
113 if ( Qt3DRender::QTexture2D *baseTex = loadTexture( texturedSettings->baseColorTexturePath() ) )
114 {
115 // takes ownership of texture
116 material->setBaseColor( QVariant::fromValue( baseTex ) );
117 }
118 else
119 {
120 // fallback to default solid color if no texture specified
121 material->setBaseColor( QColor( "grey" ) );
122 }
123
124 // metalness
125 if ( Qt3DRender::QTexture2D *metalTex = loadTexture( texturedSettings->metalnessTexturePath() ) )
126 {
127 // takes ownership of texture
128 material->setMetalness( QVariant::fromValue( metalTex ) );
129 }
130 else
131 {
132 // fallback to constant default value if no texture specified
133 material->setMetalness( 0.0 );
134 }
135
136 // roughness
137 if ( Qt3DRender::QTexture2D *roughTex = loadTexture( texturedSettings->roughnessTexturePath() ) )
138 {
139 // takes ownership of texture
140 material->setRoughness( QVariant::fromValue( roughTex ) );
141 }
142 else
143 {
144 // fallback to constant default value if no texture specified
145 material->setRoughness( 0.5 );
146 }
147
148 // ambient occlusion
149 if ( Qt3DRender::QTexture2D *aoTex = loadTexture( texturedSettings->ambientOcclusionTexturePath() ) )
150 {
151 // takes ownership of texture
152 material->setAmbientOcclusion( QVariant::fromValue( aoTex ) );
153 }
154 else
155 {
156 // default to none
157 material->setAmbientOcclusion( QVariant() );
158 }
159};
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4327
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4328
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4334
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.
QString roughnessTexturePath() const
Returns the path to the roughness texture map.
double textureScale() const
Returns the texture scale.
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.