QGIS API Documentation 4.3.0-Master (bf28115e945)
Loading...
Searching...
No Matches
qgsphongtexturedmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongtexturedmaterial3dhandler.cpp
3 --------------------------------------
4 Date : August 2020
5 Copyright : (C) 2020 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"
20#include "qgsapplication.h"
21#include "qgsimagecache.h"
22#include "qgsimagetexture.h"
27#include "qgsunlitmaterial.h"
28
29#include <QMap>
30#include <QString>
31#include <Qt3DCore/QEntity>
32#include <Qt3DRender/QEffect>
33#include <Qt3DRender/QGraphicsApiFilter>
34#include <Qt3DRender/QPaintedTextureImage>
35#include <Qt3DRender/QParameter>
36#include <Qt3DRender/QTechnique>
37#include <Qt3DRender/QTexture>
38
39using namespace Qt::StringLiterals;
40
42{
43 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
44 Q_ASSERT( phongSettings );
45
46 switch ( technique )
47 {
54 {
55 if ( context.isHighlighted() )
56 {
58 }
59
60 bool fitsInCache = false;
61 QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache );
62 ( void ) fitsInCache;
63
64 // No texture image was provided.
65 // Fallback to QgsPhongMaterialSettings.
66 if ( textureSourceImage.isNull() )
67 {
69 phongSettings.setAmbient( phongSettings.ambient() );
70 phongSettings.setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) ); // default diffuse color from QDiffuseSpecularMaterial
71 phongSettings.setOpacity( phongSettings.opacity() );
72 phongSettings.setShininess( phongSettings.shininess() );
73 phongSettings.setSpecular( phongSettings.specular() );
74 QgsMaterial *material = QgsPhongMaterial3DHandler().toMaterial( &phongSettings, technique, context );
75 return material;
76 }
77
78 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
79 material->setObjectName( u"phongTexturedMaterial"_s );
80
81 const float opacity = static_cast<float>( phongSettings->opacity() );
82 QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
83 material->setAmbient( QColor::fromRgbF( ambient.redF(), ambient.greenF(), ambient.blueF(), opacity ) );
84 const QColor specular = phongSettings->specular();
85 material->setSpecular( QColor::fromRgbF( specular.redF(), specular.greenF(), specular.blueF(), opacity ) );
86 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
87 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
88
89 // TODO : if ( context.isSelected() ) dampen the color of diffuse texture
90 // with context.map().selectionColor()
91 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
92
93 bool requiresConversionToRgb = false;
94 Qt3DRender::QAbstractTexture::TextureFormat textureFormat = Qgs3DUtils::determineTextureFormat( textureSourceImage.format(), true, requiresConversionToRgb );
95 if ( requiresConversionToRgb )
96 {
97 textureSourceImage.convertTo( QImage::Format::Format_ARGB32_Premultiplied );
98 }
99 texture->setFormat( textureFormat );
100 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
101 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
102 Qgs3DUtils::setTextureFiltering( texture, context );
103
104 texture->addTextureImage( new QgsImageTexture( textureSourceImage ) );
105
106 material->setDiffuseTexture( texture );
107 material->setDiffuseTextureScale( static_cast<float>( phongSettings->textureScale() ) );
108 material->setDiffuseTextureRotation( static_cast<float>( phongSettings->textureRotation() ) );
109 material->setDiffuseTextureOffset( static_cast<float>( phongSettings->textureOffset().x() ), static_cast<float>( phongSettings->textureOffset().y() ) );
110
111 const QgsPropertyCollection ddProps = phongSettings->dataDefinedProperties();
112 const bool hasDDTextureTransform = ddProps.isActive( QgsAbstractMaterialSettings::Property::TextureOffset )
115 material->setDataDefinedTextureTransformEnabled( hasDDTextureTransform );
116
117 return material;
118 }
119
122 return nullptr;
123 }
124 return nullptr;
125}
126
128 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
129) const
130{
131 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
132 Q_ASSERT( phongSettings );
133
134 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
135 material->setObjectName( u"phongTexturedMaterial"_s );
136 material->setInstancingEnabled( true, flags );
137 material->setInstancingMeshTransform( transform );
138
139 const float opacity = static_cast<float>( phongSettings->opacity() );
140 QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
141 material->setAmbient( QColor::fromRgbF( ambient.redF(), ambient.greenF(), ambient.blueF(), opacity ) );
142 const QColor specular = phongSettings->specular();
143 material->setSpecular( QColor::fromRgbF( specular.redF(), specular.greenF(), specular.blueF(), opacity ) );
144 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
145 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
146
147 bool fitsInCache = false;
148 QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache );
149 ( void ) fitsInCache;
150
151 if ( !textureSourceImage.isNull() )
152 {
153 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
154 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
155 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
156
157 bool requiresConversionToRgb = false;
158 Qt3DRender::QAbstractTexture::TextureFormat textureFormat = Qgs3DUtils::determineTextureFormat( textureSourceImage.format(), true, requiresConversionToRgb );
159 if ( requiresConversionToRgb )
160 {
161 textureSourceImage.convertTo( QImage::Format::Format_ARGB32_Premultiplied );
162 }
163 texture->setFormat( textureFormat );
164
165 Qgs3DUtils::setTextureFiltering( texture, context );
166 texture->addTextureImage( new QgsImageTexture( textureSourceImage ) );
167 material->setDiffuseTexture( texture );
168 material->setDiffuseTextureScale( static_cast<float>( phongSettings->textureScale() ) );
169 material->setDiffuseTextureRotation( static_cast<float>( phongSettings->textureRotation() ) );
170 }
171
172 return material;
173}
174
176{
177 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
178 Q_ASSERT( phongSettings );
179
180 QMap<QString, QString> parameters;
181 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
182 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
183 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
184 return parameters;
185}
186
187bool QgsPhongTexturedMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
188{
189 const QgsPhongTexturedMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongTexturedMaterialSettings * >( settings );
190
191 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
192 if ( material->objectName() != "phongTexturedMaterial"_L1 )
193 return false;
194
195 Qt3DRender::QEffect *effect = material->effect();
196
197 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ambientColor"_s ) )
198 p->setValue( Qgs3DUtils::srgbToLinear( phongSettings->ambient() ) );
199
200 Qt3DRender::QTexture2D *texture = material->findChild<Qt3DRender::QTexture2D *>();
201 bool fitsInCache;
202 texture->addTextureImage( new QgsImageTexture( QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache ) ) );
203 texture->removeTextureImage( texture->textureImages().at( 0 ) );
204
205 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordScale"_s ) )
206 p->setValue( phongSettings->textureScale() );
207 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordRotation"_s ) )
208 p->setValue( phongSettings->textureRotation() );
209 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordOffset"_s ) )
210 p->setValue( QVariant::fromValue( QVector2D( static_cast< float>( phongSettings->textureOffset().x() ), static_cast< float >( phongSettings->textureOffset().y() ) ) ) );
211 if ( Qt3DRender::QParameter *p = findParameter( effect, u"specularColor"_s ) )
212 p->setValue( Qgs3DUtils::srgbToLinear( phongSettings->specular() ) );
213 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
214 p->setValue( phongSettings->shininess() );
215 if ( Qt3DRender::QParameter *p = findParameter( effect, u"opacity"_s ) )
216 p->setValue( phongSettings->opacity() );
217 return true;
218}
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 void setTextureFiltering(Qt3DRender::QAbstractTexture *texture, const QgsMaterialContext &context)
Sets the default filtering options for a texture.
static Qt3DRender::QAbstractTexture::TextureFormat determineTextureFormat(QImage::Format format, bool isSrgb, bool &requiresConversionToRgb)
Given a QImage format, returns the most appropriate corresponding texture format.
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
static Qt3DRender::QParameter * findParameter(Qt3DRender::QEffect *effect, const QString &name)
Finds an existing parameter in an effect by name.
Abstract base class for material settings.
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
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.
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.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
3D handler for the Phong shading material.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering based on the Phong shading model with three color component...
void setOpacity(double opacity)
Sets opacity of the surface.
void setDiffuse(const QColor &diffuse)
Sets diffuse color component.
void setShininess(double shininess)
Sets shininess of the surface.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
void setAmbient(const QColor &ambient)
Sets ambient color component.
double shininess() const
Returns shininess of the surface.
void setSpecular(const QColor &specular)
Sets specular color component.
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform=QMatrix4x4()) const override
Creates a QgsMaterial for instanced point rendering.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
A Phong shading model with diffuse texture map.
QString diffuseTexturePath() const
Returns the diffuse texture path.
QColor specular() const
Returns specular color component.
double textureRotation() const
Returns the texture rotation, in degrees.
QPointF textureOffset() const
Returns the texture offset.
double shininess() const
Returns shininess of the surface.
double opacity() const
Returns the opacity of the surface.
double textureScale() const
Returns the texture scale.
QColor ambient() const
Returns ambient color component.
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.