18 #include <Qt3DRender/QAttribute>
19 #include <Qt3DRender/QBuffer>
20 #include <Qt3DRender/QEffect>
21 #include <Qt3DRender/QGraphicsApiFilter>
22 #include <Qt3DRender/QParameter>
23 #include <Qt3DRender/QTechnique>
25 #include <Qt3DExtras/QCylinderGeometry>
26 #include <Qt3DExtras/QConeGeometry>
27 #include <Qt3DExtras/QCuboidGeometry>
28 #include <Qt3DExtras/QPlaneGeometry>
29 #include <Qt3DExtras/QSphereGeometry>
30 #include <Qt3DExtras/QTorusGeometry>
31 #include <Qt3DExtras/QPhongMaterial>
32 #include <Qt3DRender/QSceneLoader>
33 #include <Qt3DRender/QPaintedTextureImage>
35 #include <Qt3DRender/QMesh>
37 #if QT_VERSION >= 0x050900
38 #include <Qt3DExtras/QExtrudedTextGeometry>
65 class QgsInstancedPoint3DSymbolHandler :
public QgsFeature3DHandler
70 , mSelectedIds( selectedIds ) {}
72 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
73 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
74 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
79 static Qt3DRender::QGeometryRenderer *renderer(
const QgsPoint3DSymbol *symbol,
const QVector<QVector3D> &positions );
80 static Qt3DRender::QGeometry *symbolGeometry(
QgsPoint3DSymbol::Shape shape,
const QVariantMap &shapeProperties );
85 QVector<QVector3D> positions;
88 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
91 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
97 PointData outSelected;
101 bool QgsInstancedPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
104 Q_UNUSED( attributeNames )
108 void QgsInstancedPoint3DSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
110 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
118 void QgsInstancedPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
120 makeEntity( parent, context, outNormal,
false );
121 makeEntity( parent, context, outSelected,
true );
123 updateZRangeFromPositions( outNormal.positions );
124 updateZRangeFromPositions( outSelected.positions );
127 float symbolHeight = mSymbol->transform().data()[13];
128 mZMin += symbolHeight;
129 mZMax += symbolHeight;
132 void QgsInstancedPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
135 Qt3DRender::QMaterial *mat = material( mSymbol.get() );
140 for ( Qt3DRender::QParameter *param : mat->effect()->parameters() )
142 if ( param->name() == QLatin1String(
"kd" ) )
143 param->setValue( context.map().selectionColor() );
144 else if ( param->name() == QLatin1String(
"ka" ) )
145 param->setValue( context.map().selectionColor().darker() );
150 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
151 entity->addComponent( renderer( mSymbol.get(), out.positions ) );
152 entity->addComponent( mat );
153 entity->setParent( parent );
161 Qt3DRender::QMaterial *QgsInstancedPoint3DSymbolHandler::material(
const QgsPoint3DSymbol *symbol )
163 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey;
164 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
165 filterKey->setValue(
"forward" );
170 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram;
171 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.vert" ) ) ) );
172 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.frag" ) ) ) );
174 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
175 renderPass->setShaderProgram( shaderProgram );
177 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
178 technique->addFilterKey( filterKey );
179 technique->addRenderPass( renderPass );
180 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
181 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
182 technique->graphicsApiFilter()->setMajorVersion( 3 );
183 technique->graphicsApiFilter()->setMinorVersion( 2 );
185 QMatrix4x4 transformMatrix = symbol->
transform();
186 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix();
189 float *n = normalMatrix.data();
190 QMatrix4x4 normalMatrix4(
196 Qt3DRender::QParameter *paramInst =
new Qt3DRender::QParameter;
197 paramInst->setName( QStringLiteral(
"inst" ) );
198 paramInst->setValue( transformMatrix );
200 Qt3DRender::QParameter *paramInstNormal =
new Qt3DRender::QParameter;
201 paramInstNormal->setName( QStringLiteral(
"instNormal" ) );
202 paramInstNormal->setValue( normalMatrix4 );
204 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
205 effect->addTechnique( technique );
206 effect->addParameter( paramInst );
207 effect->addParameter( paramInstNormal );
211 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
212 material->setEffect( effect );
217 Qt3DRender::QGeometryRenderer *QgsInstancedPoint3DSymbolHandler::renderer(
const QgsPoint3DSymbol *symbol,
const QVector<QVector3D> &positions )
219 int count = positions.count();
220 int byteCount = positions.count() *
sizeof( QVector3D );
222 ba.resize( byteCount );
223 memcpy( ba.data(), positions.constData(), byteCount );
225 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
226 Qt3DRender::QBuffer *instanceBuffer =
new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer );
228 Qt3DRender::QBuffer *instanceBuffer =
new Qt3DRender::QBuffer();
230 instanceBuffer->setData( ba );
232 Qt3DRender::QAttribute *instanceDataAttribute =
new Qt3DRender::QAttribute;
233 instanceDataAttribute->setName( QStringLiteral(
"pos" ) );
234 instanceDataAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
235 instanceDataAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
236 instanceDataAttribute->setVertexSize( 3 );
237 instanceDataAttribute->setByteOffset( 0 );
238 instanceDataAttribute->setDivisor( 1 );
239 instanceDataAttribute->setBuffer( instanceBuffer );
240 instanceDataAttribute->setCount( count );
241 instanceDataAttribute->setByteStride( 3 *
sizeof(
float ) );
244 geometry->addAttribute( instanceDataAttribute );
245 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
247 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
248 renderer->setGeometry( geometry );
249 renderer->setInstanceCount( count );
254 Qt3DRender::QGeometry *QgsInstancedPoint3DSymbolHandler::symbolGeometry(
QgsPoint3DSymbol::Shape shape,
const QVariantMap &shapeProperties )
260 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
261 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
262 Qt3DExtras::QCylinderGeometry *g =
new Qt3DExtras::QCylinderGeometry;
265 g->setRadius( radius ? radius : 10 );
266 g->setLength( length ? length : 10 );
272 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
273 Qt3DExtras::QSphereGeometry *g =
new Qt3DExtras::QSphereGeometry;
274 g->setRadius( radius ? radius : 10 );
280 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
281 float bottomRadius = shapeProperties[QStringLiteral(
"bottomRadius" )].toFloat();
282 float topRadius = shapeProperties[QStringLiteral(
"topRadius" )].toFloat();
283 Qt3DExtras::QConeGeometry *g =
new Qt3DExtras::QConeGeometry;
284 g->setLength( length ? length : 10 );
285 g->setBottomRadius( bottomRadius );
286 g->setTopRadius( topRadius );
294 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
295 Qt3DExtras::QCuboidGeometry *g =
new Qt3DExtras::QCuboidGeometry;
296 g->setXExtent( size ? size : 10 );
297 g->setYExtent( size ? size : 10 );
298 g->setZExtent( size ? size : 10 );
304 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
305 float minorRadius = shapeProperties[QStringLiteral(
"minorRadius" )].toFloat();
306 Qt3DExtras::QTorusGeometry *g =
new Qt3DExtras::QTorusGeometry;
307 g->setRadius( radius ? radius : 10 );
308 g->setMinorRadius( minorRadius ? minorRadius : 5 );
314 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
315 Qt3DExtras::QPlaneGeometry *g =
new Qt3DExtras::QPlaneGeometry;
316 g->setWidth( size ? size : 10 );
317 g->setHeight( size ? size : 10 );
321 #if QT_VERSION >= 0x050900
324 float depth = shapeProperties[QStringLiteral(
"depth" )].toFloat();
325 QString text = shapeProperties[QStringLiteral(
"text" )].toString();
326 Qt3DExtras::QExtrudedTextGeometry *g =
new Qt3DExtras::QExtrudedTextGeometry;
327 g->setDepth( depth ? depth : 1 );
342 class QgsModelPoint3DSymbolHandler :
public QgsFeature3DHandler
347 , mSelectedIds( selectedIds ) {}
349 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
350 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
351 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
355 static void addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent );
356 static void addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent,
bool are_selected );
357 static Qt3DCore::QTransform *transform( QVector3D position,
const QgsPoint3DSymbol *symbol );
362 QVector<QVector3D> positions;
365 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
368 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
374 PointData outSelected;
377 bool QgsModelPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
380 Q_UNUSED( attributeNames )
384 void QgsModelPoint3DSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
386 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
394 void QgsModelPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
396 makeEntity( parent, context, outNormal,
false );
397 makeEntity( parent, context, outSelected,
true );
399 updateZRangeFromPositions( outNormal.positions );
400 updateZRangeFromPositions( outSelected.positions );
403 float symbolHeight = mSymbol->transform().data()[13];
404 mZMin += symbolHeight;
405 mZMax += symbolHeight;
408 void QgsModelPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
412 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent,
true );
417 if ( mSymbol->shapeProperties()[QStringLiteral(
"overwriteMaterial" )].toBool()
418 || ( mSymbol->material() && mSymbol->material()->type() != QLatin1String(
"null" ) ) )
420 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent,
false );
424 addSceneEntities( context.map(), out.positions, mSymbol.get(), parent );
431 void QgsModelPoint3DSymbolHandler::addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent )
434 for (
const QVector3D &position : positions )
438 if ( !source.isEmpty() )
441 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
443 QUrl url = QUrl::fromLocalFile( source );
444 Qt3DRender::QSceneLoader *modelLoader =
new Qt3DRender::QSceneLoader;
445 modelLoader->setSource( url );
447 entity->addComponent( modelLoader );
448 entity->addComponent( transform( position, symbol ) );
449 entity->setParent( parent );
457 void QgsModelPoint3DSymbolHandler::addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent,
bool are_selected )
459 if ( positions.empty() )
469 for (
const QVector3D &position : positions )
472 if ( !source.isEmpty() )
475 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
477 QUrl url = QUrl::fromLocalFile( source );
478 Qt3DRender::QMesh *mesh =
new Qt3DRender::QMesh;
479 mesh->setSource( url );
481 entity->addComponent( mesh );
482 entity->addComponent( mat );
483 entity->addComponent( transform( position, symbol ) );
484 entity->setParent( parent );
492 Qt3DCore::QTransform *QgsModelPoint3DSymbolHandler::transform( QVector3D position,
const QgsPoint3DSymbol *symbol )
494 Qt3DCore::QTransform *tr =
new Qt3DCore::QTransform;
496 tr->setTranslation( position + tr->translation() );
504 class QgsPoint3DBillboardSymbolHandler :
public QgsFeature3DHandler
509 , mSelectedIds( selectedIds ) {}
511 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
512 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
513 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
520 QVector<QVector3D> positions;
523 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
526 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
532 PointData outSelected;
535 bool QgsPoint3DBillboardSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
538 Q_UNUSED( attributeNames )
542 void QgsPoint3DBillboardSymbolHandler::processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
544 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
552 void QgsPoint3DBillboardSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
554 makeEntity( parent, context, outNormal,
false );
555 makeEntity( parent, context, outSelected,
true );
557 updateZRangeFromPositions( outNormal.positions );
558 updateZRangeFromPositions( outSelected.positions );
561 float billboardHeight = mSymbol->transform().data()[13];
562 mZMin += billboardHeight;
563 mZMax += billboardHeight;
566 void QgsPoint3DBillboardSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
570 billboardGeometry->
setPoints( out.positions );
573 Qt3DRender::QGeometryRenderer *billboardGeometryRenderer =
new Qt3DRender::QGeometryRenderer;
574 billboardGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
575 billboardGeometryRenderer->setGeometry( billboardGeometry );
576 billboardGeometryRenderer->setVertexCount( billboardGeometry->
count() );
592 Qt3DCore::QTransform *billboardTransform =
new Qt3DCore::QTransform();
593 billboardTransform->setMatrix( mSymbol->billboardTransform() );
596 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
598 entity->addComponent( billboardMaterial );
599 entity->addComponent( billboardTransform );
600 entity->addComponent( billboardGeometryRenderer );
601 entity->setParent( parent );
608 namespace Qgs3DSymbolImpl
621 return new QgsPoint3DBillboardSymbolHandler( pointSymbol, layer->
selectedFeatureIds() );
623 return new QgsInstancedPoint3DSymbolHandler( pointSymbol, layer->
selectedFeatureIds() );
628 QgsFeature3DHandler *handler = handlerForPoint3DSymbol( layer, &symbol );
629 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
QColor selectionColor() const
Returns color used for selected features.
static void extractPointPositions(const QgsFeature &f, const Qgs3DMapSettings &map, Qgs3DTypes::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 QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
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 id, geometry and a list of field/values...
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.
Shape
3D shape types supported by the symbol
@ ExtrudedText
Supported in Qt 5.9+.
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
Shape shape() const
Returns 3D shape for points.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
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)
QSet< QgsFeatureId > QgsFeatureIds