28using namespace Qt::StringLiterals;
32 return u
"phongtextured"_s;
70 return *
this == *otherPhong;
80 return !mDiffuseTexturePath.isEmpty();
85 const double avgDiffuseFactor = 0.3;
86 const double avgSpecularFactor = 0.2;
88 const double kAmbient = 0.2;
89 const double kDiffuse = 0.6;
90 const double kSpecular = 0.2;
92 const QColor diffuse = textureAverageColor();
94 double red = kAmbient * mAmbient.redF() + kDiffuse * avgDiffuseFactor * diffuse.redF() + kSpecular * avgSpecularFactor * mSpecular.redF();
96 double green = kAmbient * mAmbient.greenF() + kDiffuse * avgDiffuseFactor * diffuse.greenF() + kSpecular * avgSpecularFactor * mSpecular.greenF();
98 double blue = kAmbient * mAmbient.blueF() + kDiffuse * avgDiffuseFactor * diffuse.blueF() + kSpecular * avgSpecularFactor * mSpecular.blueF();
100 red = std::clamp( red, 0.0, 1.0 );
101 green = std::clamp( green, 0.0, 1.0 );
102 blue = std::clamp( blue, 0.0, 1.0 );
104 return QColor::fromRgbF(
static_cast<float>( red ),
static_cast<float>( green ),
static_cast<float>( blue ),
static_cast<float>( mOpacity ) );
108QColor QgsPhongTexturedMaterialSettings::textureAverageColor()
const
110 if ( mTextureAverageColor.has_value() )
112 return *mTextureAverageColor;
115 bool fitsInCache =
false;
117 if ( texture.isNull() )
119 mTextureAverageColor = QColor( 127, 127, 127 );
120 return *mTextureAverageColor;
123 if ( texture.format() != QImage::Format_ARGB32 )
125 texture = texture.convertToFormat( QImage::Format_ARGB32 );
128 unsigned long long red = 0;
129 unsigned long long green = 0;
130 unsigned long long blue = 0;
131 unsigned long long pixelCount = 0;
134 const int sampleStep = std::min( texture.width() / 5, texture.height() / 5 );
135 const int width = texture.width();
136 const int height = texture.height();
137 for (
int y = 0; y < height; y += sampleStep )
139 const QRgb *line =
reinterpret_cast< const QRgb *
>( texture.constScanLine( y ) );
140 for (
int x = 0; x < width; x += sampleStep )
142 const QRgb pixel = line[x];
143 red += qRed( pixel );
144 green += qGreen( pixel );
145 blue += qBlue( pixel );
150 mTextureAverageColor = QColor(
static_cast<int>( red / pixelCount ),
static_cast<int>( green / pixelCount ),
static_cast<int>( blue / pixelCount ) );
151 return *mTextureAverageColor;
156 metallic = std::clamp( metallic, 0.0f, 1.0f );
158 const float baseR = baseColor.redF();
159 const float baseG = baseColor.greenF();
160 const float baseB = baseColor.blueF();
163 constexpr float AMBIENT_FACTOR = 0.2f;
164 mAmbient = QColor::fromRgbF( baseR * AMBIENT_FACTOR, baseG * AMBIENT_FACTOR, baseB * AMBIENT_FACTOR );
167 constexpr float F0_DIELECTRIC = 0.04f;
174 mSpecular = QColor::fromRgbF( ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseR, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseG, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseB );
176 constexpr float MIN_SHININESS = 32.0f;
177 constexpr float MAX_SHININESS = 200.0f;
178 mShininess = MIN_SHININESS + metallic * ( MAX_SHININESS - MIN_SHININESS );
190 mShininess = elem.attribute( u
"shininess"_s ).toDouble();
191 mOpacity = elem.attribute( u
"opacity"_s, u
"1.0"_s ).toDouble();
192 mDiffuseTexturePath = elem.attribute( u
"diffuse_texture_path"_s, QString() );
193 mTextureScale = elem.attribute( u
"texture_scale"_s, QString(
"1.0" ) ).toDouble();
194 mTextureRotation = elem.attribute( u
"texture-rotation"_s, QString(
"0.0" ) ).toDouble();
204 elem.setAttribute( u
"shininess"_s, mShininess );
205 elem.setAttribute( u
"opacity"_s, mOpacity );
206 elem.setAttribute( u
"diffuse_texture_path"_s, mDiffuseTexturePath );
207 elem.setAttribute( u
"texture_scale"_s, mTextureScale );
208 elem.setAttribute( u
"texture-rotation"_s, mTextureRotation );
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).
Abstract base class for material settings.
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &) const
Writes settings to a DOM element.
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &)
Reads settings from a DOM element.
@ TextureOffset
Texture offset.
@ TextureRotation
Texture rotation.
@ TextureScale
Texture scale.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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.
QSet< QgsAbstractMaterialSettings::Property > supportedProperties() const override
Returns the set of data-defined properties supported by this material.
bool requiresTextureCoordinates() const override
Returns true if the material requires texture coordinates to be generated during triangulation.
QgsPhongTexturedMaterialSettings()=default
static bool supportsTechnique(Qgis::MaterialRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
QColor averageColor() const override
Returns an approximate color representing the blended material color.
void setColorsFromBase(const QColor &baseColor, float metallic)
Decompose an average color into Phong material components, and sets the material's colors accordingly...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QString type() const override
Returns the unique type name for the material.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
QgsPhongTexturedMaterialSettings * clone() const override
Clones the material settings.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongTexturedMaterialSettings.
A container for the context for various read/write operations on objects.
static QString encodePoint(QPointF point)
Encodes a QPointF to a string.
static QPointF decodePoint(const QString &string)
Decodes a QSizeF from a string.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).