25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
33using namespace Qt::StringLiterals;
49 return new QgsHighlightMaterial( technique );
52 return buildMaterial( settings, context );
65 Q_ASSERT( phongSettings );
67 QMap<QString, QString> parameters;
68 parameters[u
"Kd"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
diffuse().redF() ).arg( phongSettings->
diffuse().greenF() ).arg( phongSettings->
diffuse().blueF() );
69 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
ambient().redF() ).arg( phongSettings->
ambient().greenF() ).arg( phongSettings->
ambient().blueF() );
70 parameters[u
"Ks"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
specular().redF() ).arg( phongSettings->
specular().greenF() ).arg( phongSettings->
specular().blueF() );
71 parameters[u
"Ns"_s] = QString::number( phongSettings->
shininess() );
78 Q_ASSERT( phongSettings );
83 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter(
91 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter(
101 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter(
109 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( u
"shininess"_s,
static_cast<float>( phongSettings->
shininess() ) );
110 Qt3DRender::QParameter *opacityParameter =
new Qt3DRender::QParameter( u
"opacity"_s,
static_cast<float>( phongSettings->
opacity() ) );
112 effect->addParameter( ambientParameter );
113 effect->addParameter( diffuseParameter );
114 effect->addParameter( specularParameter );
115 effect->addParameter( shininessParameter );
116 effect->addParameter( opacityParameter );
122 Q_ASSERT( phongSettings );
133 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
137 array.resize(
sizeof(
float ) * 9 );
138 float *fptr =
reinterpret_cast<float *
>( array.data() );
140 *fptr++ =
static_cast<float>( diffuse.redF() * diffuseCoefficient );
141 *fptr++ =
static_cast<float>( diffuse.greenF() * diffuseCoefficient );
142 *fptr++ =
static_cast<float>( diffuse.blueF() * diffuseCoefficient );
144 *fptr++ =
static_cast<float>( ambient.redF() * ambientCoefficient );
145 *fptr++ =
static_cast<float>( ambient.greenF() * ambientCoefficient );
146 *fptr++ =
static_cast<float>( ambient.blueF() * ambientCoefficient );
148 *fptr++ =
static_cast<float>( specular.redF() * specularCoefficient );
149 *fptr++ =
static_cast<float>( specular.greenF() * specularCoefficient );
150 *fptr++ =
static_cast<float>( specular.blueF() * specularCoefficient );
154 array.resize(
sizeof(
unsigned char ) * 9 );
155 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
157 *ptr++ =
static_cast<unsigned char>( diffuse.red() );
158 *ptr++ =
static_cast<unsigned char>( diffuse.green() );
159 *ptr++ =
static_cast<unsigned char>( diffuse.blue() );
161 *ptr++ =
static_cast<unsigned char>( ambient.red() );
162 *ptr++ =
static_cast<unsigned char>( ambient.green() );
163 *ptr++ =
static_cast<unsigned char>( ambient.blue() );
165 *ptr++ =
static_cast<unsigned char>( specular.red() );
166 *ptr++ =
static_cast<unsigned char>( specular.green() );
167 *ptr++ =
static_cast<unsigned char>( specular.blue() );
175 return 9 *
sizeof(
unsigned char );
181 Q_ASSERT( phongSettings );
183 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
189 Qt3DCore::QAttribute *diffuseAttribute =
new Qt3DCore::QAttribute( geometry );
190 diffuseAttribute->setName( u
"dataDefinedDiffuseColor"_s );
191 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
192 diffuseAttribute->setVertexSize( 3 );
193 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
194 diffuseAttribute->setBuffer( dataBuffer );
195 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
196 diffuseAttribute->setByteOffset( 0 );
197 diffuseAttribute->setCount( vertexCount );
198 geometry->addAttribute( diffuseAttribute );
200 Qt3DCore::QAttribute *ambientAttribute =
new Qt3DCore::QAttribute( geometry );
201 ambientAttribute->setName( u
"dataDefinedAmbiantColor"_s );
202 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
203 ambientAttribute->setVertexSize( 3 );
204 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
205 ambientAttribute->setBuffer( dataBuffer );
206 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
207 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
208 ambientAttribute->setCount( vertexCount );
209 geometry->addAttribute( ambientAttribute );
211 Qt3DCore::QAttribute *specularAttribute =
new Qt3DCore::QAttribute( geometry );
212 specularAttribute->setName( u
"dataDefinedSpecularColor"_s );
213 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
214 specularAttribute->setVertexSize( 3 );
215 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
216 specularAttribute->setBuffer( dataBuffer );
217 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
218 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
219 specularAttribute->setCount( vertexCount );
220 geometry->addAttribute( specularAttribute );
222 dataBuffer->setData( data );
229 QgsPhongMaterial *material = sceneRoot->findChild<QgsPhongMaterial *>();
230 if ( !material || material->objectName() !=
"phongMaterial"_L1 )
236 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
237 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
245 Q_ASSERT( phongSettings );
247 QgsPhongMaterial *material =
new QgsPhongMaterial;
248 material->setObjectName( u
"phongMaterial"_s );
256 const QColor specular = phongSettings->
specular();
258 material->setAmbient( ambient,
static_cast<float>( phongSettings->
ambientCoefficient() ) );
259 material->setDiffuse( diffuse,
static_cast<float>( phongSettings->
diffuseCoefficient() ) );
260 material->setSpecular( specular,
static_cast<float>( phongSettings->
specularCoefficient() ) );
263 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
264 material->setOpacity(
static_cast<float>( 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).
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
Abstract base class for material settings.
@ Specular
Specular color.
@ Ambient
Ambient color (phong material).
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
int dataDefinedByteStride(const QgsAbstractMaterialSettings *settings) const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
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...
double ambientCoefficient() const
Returns the coefficient for the ambient color contribution (ie strength factor of the ambient color).
QColor diffuse() const
Returns diffuse color component.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double specularCoefficient() const
Returns the coefficient for the specular color contribution (ie strength factor of the specular color...
double shininess() const
Returns shininess of the surface.
double diffuseCoefficient() const
Returns the coefficient for the diffuse color contribution (ie strength factor of the diffuse color).
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...