30#include <Qt3DCore/QEntity>
31#include <Qt3DRender/QEffect>
32#include <Qt3DRender/QGraphicsApiFilter>
33#include <Qt3DRender/QPaintedTextureImage>
34#include <Qt3DRender/QParameter>
35#include <Qt3DRender/QTechnique>
36#include <Qt3DRender/QTexture>
38using namespace Qt::StringLiterals;
43 Q_ASSERT( phongSettings );
56 return new QgsHighlightMaterial();
59 bool fitsInCache =
false;
65 if ( textureSourceImage.isNull() )
69 phongSettings.
setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) );
77 QgsPhongTexturedMaterial *material =
new QgsPhongTexturedMaterial();
78 material->setObjectName( u
"phongTexturedMaterial"_s );
80 const float opacity =
static_cast<float>( phongSettings->
opacity() );
82 material->setAmbient( QColor::fromRgbF( ambient.redF(), ambient.greenF(), ambient.blueF(), opacity ) );
83 const QColor specular = phongSettings->
specular();
84 material->setSpecular( QColor::fromRgbF( specular.redF(), specular.greenF(), specular.blueF(), opacity ) );
85 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
86 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
90 Qt3DRender::QTexture2D *texture =
new Qt3DRender::QTexture2D();
91 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
92 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
93 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
98 material->setDiffuseTexture( texture );
99 material->setDiffuseTextureScale(
static_cast<float>( phongSettings->
textureScale() ) );
100 material->setDiffuseTextureRotation(
static_cast<float>( phongSettings->
textureRotation() ) );
101 material->setDiffuseTextureOffset(
static_cast<float>( phongSettings->
textureOffset().x() ),
static_cast<float>( phongSettings->
textureOffset().y() ) );
107 material->setDataDefinedTextureTransformEnabled( hasDDTextureTransform );
124 Q_ASSERT( phongSettings );
126 QgsPhongTexturedMaterial *material =
new QgsPhongTexturedMaterial();
127 material->setObjectName( u
"phongTexturedMaterial"_s );
128 material->setInstancingEnabled(
true, flags );
129 material->setInstancingMeshTransform( transform );
131 const float opacity =
static_cast<float>( phongSettings->
opacity() );
133 material->setAmbient( QColor::fromRgbF( ambient.redF(), ambient.greenF(), ambient.blueF(), opacity ) );
134 const QColor specular = phongSettings->
specular();
135 material->setSpecular( QColor::fromRgbF( specular.redF(), specular.greenF(), specular.blueF(), opacity ) );
136 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
137 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
139 bool fitsInCache =
false;
141 ( void ) fitsInCache;
143 if ( !textureSourceImage.isNull() )
145 Qt3DRender::QTexture2D *texture =
new Qt3DRender::QTexture2D();
146 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
147 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
148 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
151 material->setDiffuseTexture( texture );
152 material->setDiffuseTextureScale(
static_cast<float>( phongSettings->
textureScale() ) );
153 material->setDiffuseTextureRotation(
static_cast<float>( phongSettings->
textureRotation() ) );
162 Q_ASSERT( phongSettings );
164 QMap<QString, QString> parameters;
165 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
ambient().redF() ).arg( phongSettings->
ambient().greenF() ).arg( phongSettings->
ambient().blueF() );
166 parameters[u
"Ks"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
specular().redF() ).arg( phongSettings->
specular().greenF() ).arg( phongSettings->
specular().blueF() );
167 parameters[u
"Ns"_s] = QString::number( phongSettings->
shininess() );
176 if ( material->objectName() !=
"phongTexturedMaterial"_L1 )
179 Qt3DRender::QEffect *effect = material->effect();
181 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"ambientColor"_s ) )
184 Qt3DRender::QTexture2D *texture = material->findChild<Qt3DRender::QTexture2D *>();
187 texture->removeTextureImage( texture->textureImages().at( 0 ) );
189 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"texCoordScale"_s ) )
191 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"texCoordRotation"_s ) )
193 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"texCoordOffset"_s ) )
194 p->setValue( QVariant::fromValue( QVector2D(
static_cast< float>( phongSettings->
textureOffset().x() ),
static_cast< float >( phongSettings->
textureOffset().y() ) ) ) );
195 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"specularColor"_s ) )
197 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"shininess"_s ) )
198 p->setValue( phongSettings->
shininess() );
199 if ( Qt3DRender::QParameter *p =
findParameter( effect, u
"opacity"_s ) )
200 p->setValue( phongSettings->
opacity() );
MaterialRenderingTechnique
Material rendering techniques.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default).
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ Billboards
Flat billboard rendering.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
static void setTextureFiltering(Qt3DRender::QAbstractTexture *texture, const QgsMaterialContext &context)
Sets the default filtering options for a texture.
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
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.
@ TextureOffset
Texture offset.
@ TextureRotation
Texture rotation.
@ TextureScale
Texture scale.
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.
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.