26#include <Qt3DCore/QAttribute>
27#include <Qt3DCore/QBuffer>
28#include <Qt3DCore/QGeometry>
29#include <Qt3DExtras/Qt3DWindow>
30#include <Qt3DRender/QEffect>
31#include <Qt3DRender/QParameter>
32#include <Qt3DRender/QTexture>
34using namespace Qt::StringLiterals;
40 Q_ASSERT( lineSettings );
61 QgsLineMaterial *mat =
new QgsLineMaterial;
64 mat->setLineColor( lineSettings->
ambient() );
71 mat->setUseVertexColors(
false );
82 Q_ASSERT( lineSettings );
84 QMap<QString, QString> parameters;
85 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( lineSettings->
ambient().redF() ).arg( lineSettings->
ambient().greenF() ).arg( lineSettings->
ambient().blueF() );
92 Q_ASSERT( lineSettings );
97 array.resize(
sizeof(
unsigned char ) * 3 );
98 unsigned char *fptr =
reinterpret_cast<unsigned char *
>( array.data() );
100 *fptr++ =
static_cast<unsigned char>( ambient.red() );
101 *fptr++ =
static_cast<unsigned char>( ambient.green() );
102 *fptr++ =
static_cast<unsigned char>( ambient.blue() );
108 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
110 Qt3DCore::QAttribute *colorAttribute =
new Qt3DCore::QAttribute( geometry );
111 colorAttribute->setName( u
"dataDefinedColor"_s );
112 colorAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
113 colorAttribute->setVertexSize( 3 );
114 colorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
115 colorAttribute->setBuffer( dataBuffer );
116 colorAttribute->setByteStride( 3 *
sizeof(
unsigned char ) );
117 colorAttribute->setByteOffset( 0 );
118 colorAttribute->setCount( instanceCount );
119 colorAttribute->setDivisor( 1 );
120 geometry->addAttribute( colorAttribute );
122 dataBuffer->setData( data );
128 lines.
type = u
"lines"_s;
135 QgsLineVertexData lineVertexData;
136 constexpr double s = 1.0;
138 lineVertexData.addLineString(
QgsLineString( { -s, s, s, -s, -s }, { -s, -s, s, s, -s }, { 0, 0, 0, 0, 0 } ) );
140 Qt3DCore::QEntity *entity = lineVertexData.createSegmentEntity(
nullptr );
141 entity->setParent( parent );
143 if ( Qt3DCore::QEntity *joinEntity = lineVertexData.createJoinEntity(
nullptr ) )
144 joinEntity->setParent( entity );
153 Qt3DCore::QEntity *root =
new Qt3DCore::QEntity( parent );
157 QgsLineMaterial *lineMaterial = qobject_cast<QgsLineMaterial *>( mat );
158 Q_ASSERT( lineMaterial );
159 lineMaterial->setLineWidth( 2 );
161 mat->setParent( mesh );
162 mesh->addComponent( mat );
164 QgsLineMaterial *joinMaterial =
nullptr;
165 const QList<QgsLineMaterial *> childLineMaterials = mesh->findChildren<QgsLineMaterial *>();
166 for ( QgsLineMaterial *candidate : childLineMaterials )
168 if ( candidate->part() == QgsLineMaterial::LinePart::Join )
170 joinMaterial = candidate;
175 lineMaterial->copyLineParametersTo( joinMaterial );
180 auto updateViewport = [lineMaterial, joinMaterial, window]() {
181 const QSizeF size( window->width(), window->height() );
182 lineMaterial->setViewportSize( size );
184 joinMaterial->setViewportSize( size );
186 QObject::connect( window, &Qt3DExtras::Qt3DWindow::widthChanged, lineMaterial, updateViewport );
187 QObject::connect( window, &Qt3DExtras::Qt3DWindow::heightChanged, lineMaterial, updateViewport );
198 const QList<QgsLineMaterial *> materials = sceneRoot->findChildren<QgsLineMaterial *>();
199 for ( QgsLineMaterial *material : materials )
200 material->setLineColor( lineSettings->
ambient() );
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.
@ 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...
Line string geometry type, with support for z-dimension and m-values.
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.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
QList< PreviewMeshType > previewMeshTypes() const override
Returns a list of available preview mesh types for the material.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Qt3DCore::QEntity * createPreviewScene(const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, QWindow *window, Qt3DCore::QEntity *parent) const override
Builds a complete self-contained scene for previewing the material, using the specified mesh type.
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...
Qt3DCore::QEntity * createPreviewMesh(const QString &type, Qt3DCore::QEntity *parent) const override
Creates a new entity representing a suitable preview mesh for this material type.
Basic shading material used for rendering simple lines as solid line components.
QColor ambient() const
Returns the ambient color component.
Encapsulates information about available preview meshes.
QString type
Identifier string.
QString displayName
Translated, user-friendly name.