18#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19#include <Qt3DRender/QAttribute>
20#include <Qt3DRender/QBuffer>
21#include <Qt3DRender/QGeometry>
22#include <Qt3DCore/QTransform>
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QGeometry>
37#include <Qt3DRender/QEffect>
38#include <Qt3DRender/QGraphicsApiFilter>
39#include <Qt3DRender/QParameter>
40#include <Qt3DRender/QTechnique>
42#include <Qt3DExtras/QCylinderGeometry>
43#include <Qt3DExtras/QConeGeometry>
44#include <Qt3DExtras/QCuboidGeometry>
45#include <Qt3DExtras/QPlaneGeometry>
46#include <Qt3DExtras/QSphereGeometry>
47#include <Qt3DExtras/QTorusGeometry>
48#include <Qt3DExtras/QPhongMaterial>
49#include <Qt3DRender/QSceneLoader>
50#include <Qt3DRender/QPaintedTextureImage>
52#include <Qt3DRender/QMesh>
54#include <Qt3DExtras/QExtrudedTextGeometry>
75class QgsInstancedPoint3DSymbolHandler :
public QgsFeature3DHandler
80 , mSelectedIds( selectedIds ) {}
82 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
83 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
84 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
89 static Qt3DRender::QGeometryRenderer *renderer(
const QgsPoint3DSymbol *symbol,
const QVector<QVector3D> &positions );
95 QVector<QVector3D> positions;
98 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
101 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
107 PointData outSelected;
111bool QgsInstancedPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
114 Q_UNUSED( attributeNames )
118void QgsInstancedPoint3DSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
120 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
129void QgsInstancedPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
131 makeEntity( parent, context, outNormal,
false );
132 makeEntity( parent, context, outSelected,
true );
134 updateZRangeFromPositions( outNormal.positions );
135 updateZRangeFromPositions( outSelected.positions );
138 const float symbolOffset = mSymbol->transform().data()[13];
142 switch ( mSymbol->shape() )
146 const float length = mSymbol->shapeProperty( QStringLiteral(
"length" ) ).toFloat();
147 mZMin -= length * 0.5f;
148 mZMax += length * 0.5f;
154 const float radius = mSymbol->shapeProperty( QStringLiteral(
"radius" ) ).toFloat();
162 const float length = mSymbol->shapeProperty( QStringLiteral(
"length" ) ).toFloat();
163 mZMin -= length * 0.5f;
164 mZMax += length * 0.5f;
170 const float size = mSymbol->shapeProperty( QStringLiteral(
"size" ) ).toFloat();
171 mZMin -= size * 0.5f;
172 mZMax += size * 0.5f;
178 const float radius = mSymbol->shapeProperty( QStringLiteral(
"radius" ) ).toFloat();
188 const float size = mSymbol->shapeProperty( QStringLiteral(
"size" ) ).toFloat();
189 mZMin -= size * 0.5f;
190 mZMax += size * 0.5f;
200 mZMin += symbolOffset;
201 mZMax += symbolOffset;
204void QgsInstancedPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
207 Qt3DRender::QMaterial *mat = material( mSymbol.get() );
212 for ( Qt3DRender::QParameter *param : mat->effect()->parameters() )
214 if ( param->name() == QLatin1String(
"diffuseColor" ) )
215 param->setValue( context.map().selectionColor() );
216 else if ( param->name() == QLatin1String(
"ambientColor" ) )
217 param->setValue( context.map().selectionColor().darker() );
222 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
223 entity->addComponent( renderer( mSymbol.get(), out.positions ) );
224 entity->addComponent( mat );
225 entity->setParent( parent );
233Qt3DRender::QMaterial *QgsInstancedPoint3DSymbolHandler::material(
const QgsPoint3DSymbol *symbol )
235 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey;
236 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
237 filterKey->setValue(
"forward" );
239 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram;
240 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.vert" ) ) ) );
241 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/phongConstant.frag" ) ) ) );
243 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
244 renderPass->setShaderProgram( shaderProgram );
246 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
247 technique->addFilterKey( filterKey );
248 technique->addRenderPass( renderPass );
249 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
250 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
251 technique->graphicsApiFilter()->setMajorVersion( 3 );
252 technique->graphicsApiFilter()->setMinorVersion( 2 );
254 const QMatrix4x4 transformMatrix = symbol->
transform();
255 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix();
258 float *n = normalMatrix.data();
259 const QMatrix4x4 normalMatrix4(
265 Qt3DRender::QParameter *paramInst =
new Qt3DRender::QParameter;
266 paramInst->setName( QStringLiteral(
"inst" ) );
267 paramInst->setValue( transformMatrix );
269 Qt3DRender::QParameter *paramInstNormal =
new Qt3DRender::QParameter;
270 paramInstNormal->setName( QStringLiteral(
"instNormal" ) );
271 paramInstNormal->setValue( normalMatrix4 );
273 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
274 effect->addTechnique( technique );
275 effect->addParameter( paramInst );
276 effect->addParameter( paramInstNormal );
280 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
281 material->setEffect( effect );
286Qt3DRender::QGeometryRenderer *QgsInstancedPoint3DSymbolHandler::renderer(
const QgsPoint3DSymbol *symbol,
const QVector<QVector3D> &positions )
288 const int count = positions.count();
289 const int byteCount = positions.count() *
sizeof( QVector3D );
291 ba.resize( byteCount );
292 memcpy( ba.data(), positions.constData(), byteCount );
295 instanceBuffer->setData( ba );
298 instanceDataAttribute->setName( QStringLiteral(
"pos" ) );
299 instanceDataAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
300 instanceDataAttribute->setVertexBaseType( Qt3DQAttribute::Float );
301 instanceDataAttribute->setVertexSize( 3 );
302 instanceDataAttribute->setByteOffset( 0 );
303 instanceDataAttribute->setDivisor( 1 );
304 instanceDataAttribute->setBuffer( instanceBuffer );
305 instanceDataAttribute->setCount( count );
306 instanceDataAttribute->setByteStride( 3 *
sizeof(
float ) );
309 geometry->addAttribute( instanceDataAttribute );
310 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
312 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
313 renderer->setGeometry( geometry );
314 renderer->setInstanceCount( count );
321 switch ( symbol->
shape() )
325 const float radius = symbol->
shapeProperty( QStringLiteral(
"radius" ) ).toFloat();
326 const float length = symbol->
shapeProperty( QStringLiteral(
"length" ) ).toFloat();
327 Qt3DExtras::QCylinderGeometry *g =
new Qt3DExtras::QCylinderGeometry;
330 g->setRadius( radius );
331 g->setLength( length );
337 const float radius = symbol->
shapeProperty( QStringLiteral(
"radius" ) ).toFloat();
338 Qt3DExtras::QSphereGeometry *g =
new Qt3DExtras::QSphereGeometry;
339 g->setRadius( radius );
345 const float length = symbol->
shapeProperty( QStringLiteral(
"length" ) ).toFloat();
346 const float bottomRadius = symbol->
shapeProperty( QStringLiteral(
"bottomRadius" ) ).toFloat();
347 const float topRadius = symbol->
shapeProperty( QStringLiteral(
"topRadius" ) ).toFloat();
349 Qt3DExtras::QConeGeometry *g =
new Qt3DExtras::QConeGeometry;
350 g->setLength( length );
351 g->setBottomRadius( bottomRadius );
352 g->setTopRadius( topRadius );
360 const float size = symbol->
shapeProperty( QStringLiteral(
"size" ) ).toFloat();
361 Qt3DExtras::QCuboidGeometry *g =
new Qt3DExtras::QCuboidGeometry;
362 g->setXExtent( size );
363 g->setYExtent( size );
364 g->setZExtent( size );
370 const float radius = symbol->
shapeProperty( QStringLiteral(
"radius" ) ).toFloat();
371 const float minorRadius = symbol->
shapeProperty( QStringLiteral(
"minorRadius" ) ).toFloat();
372 Qt3DExtras::QTorusGeometry *g =
new Qt3DExtras::QTorusGeometry;
373 g->setRadius( radius );
374 g->setMinorRadius( minorRadius );
380 const float size = symbol->
shapeProperty( QStringLiteral(
"size" ) ).toFloat();
381 Qt3DExtras::QPlaneGeometry *g =
new Qt3DExtras::QPlaneGeometry;
383 g->setHeight( size );
389 const float depth = symbol->
shapeProperty( QStringLiteral(
"depth" ) ).toFloat();
390 const QString text = symbol->
shapeProperty( QStringLiteral(
"text" ) ).toString();
391 Qt3DExtras::QExtrudedTextGeometry *g =
new Qt3DExtras::QExtrudedTextGeometry;
392 g->setDepth( depth );
408class QgsModelPoint3DSymbolHandler :
public QgsFeature3DHandler
413 , mSelectedIds( selectedIds ) {}
415 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
416 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
417 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
421 static void addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent );
422 static void addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent,
bool are_selected );
423 static Qt3DCore::QTransform *transform( QVector3D position,
const QgsPoint3DSymbol *symbol );
428 QVector<QVector3D> positions;
431 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
434 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
440 PointData outSelected;
443bool QgsModelPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
446 Q_UNUSED( attributeNames )
450void QgsModelPoint3DSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
452 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
461void QgsModelPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
463 makeEntity( parent, context, outNormal,
false );
464 makeEntity( parent, context, outSelected,
true );
466 updateZRangeFromPositions( outNormal.positions );
467 updateZRangeFromPositions( outSelected.positions );
470 const float symbolHeight = mSymbol->transform().data()[13];
471 mZMin += symbolHeight;
472 mZMax += symbolHeight;
475void QgsModelPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
479 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent,
true );
484 if ( mSymbol->shapeProperty( QStringLiteral(
"overwriteMaterial" ) ).toBool()
485 || ( mSymbol->materialSettings() && mSymbol->materialSettings()->type() != QLatin1String(
"null" ) ) )
487 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent,
false );
491 addSceneEntities( context.map(), out.positions, mSymbol.get(), parent );
498void QgsModelPoint3DSymbolHandler::addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent )
501 for (
const QVector3D &position : positions )
505 if ( !source.isEmpty() )
508 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
510 const QUrl url = QUrl::fromLocalFile( source );
511 Qt3DRender::QSceneLoader *modelLoader =
new Qt3DRender::QSceneLoader;
512 modelLoader->setSource( url );
514 entity->addComponent( modelLoader );
515 entity->addComponent( transform( position, symbol ) );
516 entity->setParent( parent );
524void QgsModelPoint3DSymbolHandler::addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent,
bool are_selected )
526 if ( positions.empty() )
536 for (
const QVector3D &position : positions )
539 if ( !source.isEmpty() )
542 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
544 const QUrl url = QUrl::fromLocalFile( source );
545 Qt3DRender::QMesh *mesh =
new Qt3DRender::QMesh;
546 mesh->setSource( url );
548 entity->addComponent( mesh );
549 entity->addComponent( mat );
550 entity->addComponent( transform( position, symbol ) );
551 entity->setParent( parent );
559Qt3DCore::QTransform *QgsModelPoint3DSymbolHandler::transform( QVector3D position,
const QgsPoint3DSymbol *symbol )
561 Qt3DCore::QTransform *tr =
new Qt3DCore::QTransform;
563 tr->setTranslation( position + tr->translation() );
571class QgsPoint3DBillboardSymbolHandler :
public QgsFeature3DHandler
576 , mSelectedIds( selectedIds ) {}
578 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
579 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
580 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
587 QVector<QVector3D> positions;
590 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
593 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
599 PointData outSelected;
602bool QgsPoint3DBillboardSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
605 Q_UNUSED( attributeNames )
609void QgsPoint3DBillboardSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
611 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
620void QgsPoint3DBillboardSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
622 makeEntity( parent, context, outNormal,
false );
623 makeEntity( parent, context, outSelected,
true );
625 updateZRangeFromPositions( outNormal.positions );
626 updateZRangeFromPositions( outSelected.positions );
629 const float billboardHeight = mSymbol->transform().data()[13];
630 mZMin += billboardHeight;
631 mZMax += billboardHeight;
634void QgsPoint3DBillboardSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
638 billboardGeometry->
setPoints( out.positions );
641 Qt3DRender::QGeometryRenderer *billboardGeometryRenderer =
new Qt3DRender::QGeometryRenderer;
642 billboardGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
643 billboardGeometryRenderer->setGeometry( billboardGeometry );
644 billboardGeometryRenderer->setVertexCount( billboardGeometry->
count() );
660 Qt3DCore::QTransform *billboardTransform =
new Qt3DCore::QTransform();
661 billboardTransform->setMatrix( mSymbol->billboardTransform() );
664 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
666 entity->addComponent( billboardMaterial );
667 entity->addComponent( billboardTransform );
668 entity->addComponent( billboardGeometryRenderer );
669 entity->setParent( parent );
676namespace Qgs3DSymbolImpl
689 return new QgsPoint3DBillboardSymbolHandler( pointSymbol, layer->
selectedFeatureIds() );
691 return new QgsInstancedPoint3DSymbolHandler( pointSymbol, layer->
selectedFeatureIds() );
696 QgsFeature3DHandler *handler = handlerForPoint3DSymbol( layer, &symbol );
697 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
@ ExtrudedText
Extruded text.
QColor selectionColor() const
Returns color used for selected features.
static void extractPointPositions(const QgsFeature &f, const Qgs3DMapSettings &map, Qgis::AltitudeClamping altClamp, QVector< QVector3D > &positions)
Calculates (x,y,z) positions of (multi)point from the given feature.
virtual Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const =0
Creates a new QMaterial object representing the material settings.
virtual void addParametersToEffect(Qt3DRender::QEffect *effect) const =0
Adds parameters from the material to a destination effect.
static QgsSourceCache * sourceCache()
Returns the application's source cache, used for caching embedded and remote source strings as local ...
void setPoints(const QVector< QVector3D > &vertices)
Set the points for the billboard with vertices.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
A marker symbol type, for rendering Point and MultiPoint geometries.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
void setSelectionColor(const QColor &color)
Sets the color for representing materials in a selected state.
void useDefaultSymbol(const Qgs3DMapSettings &map, bool selected=false)
Set default symbol for the texture with map and selected parameter for rendering.
void setTexture2DFromSymbol(QgsMarkerSymbol *markerSymbol, const Qgs3DMapSettings &map, bool selected=false)
Set markerSymbol for the texture with map and selected parameter for rendering.
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
Qgis::Point3DShape shape() const
Returns 3D shape for points.
QgsAbstract3DSymbol * clone() const override
Returns a new instance of the symbol with the same settings.
QVariant shapeProperty(const QString &property) const
Returns the value for a specific shape property.
QString localFilePath(const QString &path, bool blocking=false)
Returns a local file path reflecting the content of a specified source path.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
@ Triangles
Triangle based rendering (default)
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
QSet< QgsFeatureId > QgsFeatureIds
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry