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> 63 class QgsInstancedPoint3DSymbolHandler :
public QgsFeature3DHandler
67 : mSymbol( symbol ), mSelectedIds( selectedIds ) {}
69 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
70 void processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
71 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
76 static Qt3DRender::QGeometryRenderer *renderer(
const QgsPoint3DSymbol &symbol,
const QVector<QVector3D> &positions );
77 static Qt3DRender::QGeometry *symbolGeometry(
QgsPoint3DSymbol::Shape shape,
const QVariantMap &shapeProperties );
82 QVector<QVector3D> positions;
85 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
94 PointData outSelected;
98 bool QgsInstancedPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
101 Q_UNUSED( attributeNames )
105 void QgsInstancedPoint3DSymbolHandler::processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
107 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
115 void QgsInstancedPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
117 makeEntity( parent, context, outNormal,
false );
118 makeEntity( parent, context, outSelected,
true );
121 void QgsInstancedPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
124 Qt3DRender::QMaterial *mat = material( mSymbol );
129 for ( Qt3DRender::QParameter *param : mat->effect()->parameters() )
131 if ( param->name() == QLatin1String(
"kd" ) )
132 param->setValue( context.map().selectionColor() );
133 else if ( param->name() == QLatin1String(
"ka" ) )
134 param->setValue( context.map().selectionColor().darker() );
139 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
140 entity->addComponent( renderer( mSymbol, out.positions ) );
141 entity->addComponent( mat );
142 entity->setParent( parent );
147 Qt3DRender::QMaterial *QgsInstancedPoint3DSymbolHandler::material(
const QgsPoint3DSymbol &symbol )
149 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey;
150 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
151 filterKey->setValue(
"forward" );
156 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram;
157 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.vert" ) ) ) );
158 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/instanced.frag" ) ) ) );
160 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
161 renderPass->setShaderProgram( shaderProgram );
163 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
164 technique->addFilterKey( filterKey );
165 technique->addRenderPass( renderPass );
166 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
167 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
168 technique->graphicsApiFilter()->setMajorVersion( 3 );
169 technique->graphicsApiFilter()->setMinorVersion( 2 );
171 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ka" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) );
172 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"kd" ), QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) );
173 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ks" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) );
174 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ), 150.0f );
181 QMatrix4x4 transformMatrix = symbol.
transform();
182 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix();
185 float *n = normalMatrix.data();
186 QMatrix4x4 normalMatrix4(
192 Qt3DRender::QParameter *paramInst =
new Qt3DRender::QParameter;
193 paramInst->setName( QStringLiteral(
"inst" ) );
194 paramInst->setValue( transformMatrix );
196 Qt3DRender::QParameter *paramInstNormal =
new Qt3DRender::QParameter;
197 paramInstNormal->setName( QStringLiteral(
"instNormal" ) );
198 paramInstNormal->setValue( normalMatrix4 );
200 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
201 effect->addTechnique( technique );
202 effect->addParameter( paramInst );
203 effect->addParameter( paramInstNormal );
205 effect->addParameter( ambientParameter );
206 effect->addParameter( diffuseParameter );
207 effect->addParameter( specularParameter );
208 effect->addParameter( shininessParameter );
210 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
211 material->setEffect( effect );
216 Qt3DRender::QGeometryRenderer *QgsInstancedPoint3DSymbolHandler::renderer(
const QgsPoint3DSymbol &symbol,
const QVector<QVector3D> &positions )
218 int count = positions.count();
219 int byteCount = positions.count() *
sizeof( QVector3D );
221 ba.resize( byteCount );
222 memcpy( ba.data(), positions.constData(), byteCount );
224 Qt3DRender::QBuffer *instanceBuffer =
new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer );
225 instanceBuffer->setData( ba );
227 Qt3DRender::QAttribute *instanceDataAttribute =
new Qt3DRender::QAttribute;
228 instanceDataAttribute->setName( QStringLiteral(
"pos" ) );
229 instanceDataAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
230 instanceDataAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
231 instanceDataAttribute->setVertexSize( 3 );
232 instanceDataAttribute->setDivisor( 1 );
233 instanceDataAttribute->setBuffer( instanceBuffer );
234 instanceDataAttribute->setCount( count );
235 instanceDataAttribute->setByteStride( 3 *
sizeof(
float ) );
238 geometry->addAttribute( instanceDataAttribute );
239 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
241 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
242 renderer->setGeometry( geometry );
243 renderer->setInstanceCount( count );
248 Qt3DRender::QGeometry *QgsInstancedPoint3DSymbolHandler::symbolGeometry(
QgsPoint3DSymbol::Shape shape,
const QVariantMap &shapeProperties )
254 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
255 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
256 Qt3DExtras::QCylinderGeometry *g =
new Qt3DExtras::QCylinderGeometry;
259 g->setRadius( radius ? radius : 10 );
260 g->setLength( length ? length : 10 );
266 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
267 Qt3DExtras::QSphereGeometry *g =
new Qt3DExtras::QSphereGeometry;
268 g->setRadius( radius ? radius : 10 );
274 float length = shapeProperties[QStringLiteral(
"length" )].toFloat();
275 float bottomRadius = shapeProperties[QStringLiteral(
"bottomRadius" )].toFloat();
276 float topRadius = shapeProperties[QStringLiteral(
"topRadius" )].toFloat();
277 Qt3DExtras::QConeGeometry *g =
new Qt3DExtras::QConeGeometry;
278 g->setLength( length ? length : 10 );
279 g->setBottomRadius( bottomRadius );
280 g->setTopRadius( topRadius );
288 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
289 Qt3DExtras::QCuboidGeometry *g =
new Qt3DExtras::QCuboidGeometry;
290 g->setXExtent( size ? size : 10 );
291 g->setYExtent( size ? size : 10 );
292 g->setZExtent( size ? size : 10 );
298 float radius = shapeProperties[QStringLiteral(
"radius" )].toFloat();
299 float minorRadius = shapeProperties[QStringLiteral(
"minorRadius" )].toFloat();
300 Qt3DExtras::QTorusGeometry *g =
new Qt3DExtras::QTorusGeometry;
301 g->setRadius( radius ? radius : 10 );
302 g->setMinorRadius( minorRadius ? minorRadius : 5 );
308 float size = shapeProperties[QStringLiteral(
"size" )].toFloat();
309 Qt3DExtras::QPlaneGeometry *g =
new Qt3DExtras::QPlaneGeometry;
310 g->setWidth( size ? size : 10 );
311 g->setHeight( size ? size : 10 );
315 #if QT_VERSION >= 0x050900 318 float depth = shapeProperties[QStringLiteral(
"depth" )].toFloat();
319 QString text = shapeProperties[QStringLiteral(
"text" )].toString();
320 Qt3DExtras::QExtrudedTextGeometry *g =
new Qt3DExtras::QExtrudedTextGeometry;
321 g->setDepth( depth ? depth : 1 );
336 class QgsModelPoint3DSymbolHandler :
public QgsFeature3DHandler
340 : mSymbol( symbol ), mSelectedIds( selectedIds ) {}
342 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
343 void processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
344 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
348 static void addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol &symbol, Qt3DCore::QEntity *parent );
349 static void addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol &symbol, Qt3DCore::QEntity *parent,
bool are_selected );
350 static Qt3DCore::QTransform *transform( QVector3D position,
const QgsPoint3DSymbol &symbol );
355 QVector<QVector3D> positions;
358 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
367 PointData outSelected;
370 bool QgsModelPoint3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
373 Q_UNUSED( attributeNames )
377 void QgsModelPoint3DSymbolHandler::processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
379 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
387 void QgsModelPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
389 makeEntity( parent, context, outNormal,
false );
390 makeEntity( parent, context, outSelected,
true );
393 void QgsModelPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
397 addMeshEntities( context.map(), out.positions, mSymbol, parent, true );
401 if ( mSymbol.shapeProperties()[QStringLiteral(
"overwriteMaterial" )].toBool() )
403 addMeshEntities( context.map(), out.positions, mSymbol, parent, false );
407 addSceneEntities( context.map(), out.positions, mSymbol, parent );
414 void QgsModelPoint3DSymbolHandler::addSceneEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol &symbol, Qt3DCore::QEntity *parent )
417 for (
const QVector3D &position : positions )
420 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
422 QUrl url = QUrl::fromLocalFile( symbol.
shapeProperties()[QStringLiteral(
"model" )].toString() );
423 Qt3DRender::QSceneLoader *modelLoader =
new Qt3DRender::QSceneLoader;
424 modelLoader->setSource( url );
426 entity->addComponent( modelLoader );
427 entity->addComponent( transform( position, symbol ) );
428 entity->setParent( parent );
432 void QgsModelPoint3DSymbolHandler::addMeshEntities(
const Qgs3DMapSettings &map,
const QVector<QVector3D> &positions,
const QgsPoint3DSymbol &symbol, Qt3DCore::QEntity *parent,
bool are_selected )
444 for (
const QVector3D &position : positions )
447 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
449 QUrl url = QUrl::fromLocalFile( symbol.
shapeProperties()[QStringLiteral(
"model" )].toString() );
450 Qt3DRender::QMesh *mesh =
new Qt3DRender::QMesh;
451 mesh->setSource( url );
453 entity->addComponent( mesh );
454 entity->addComponent( mat );
455 entity->addComponent( transform( position, symbol ) );
456 entity->setParent( parent );
460 Qt3DCore::QTransform *QgsModelPoint3DSymbolHandler::transform( QVector3D position,
const QgsPoint3DSymbol &symbol )
462 Qt3DCore::QTransform *tr =
new Qt3DCore::QTransform;
464 tr->setTranslation( position + tr->translation() );
472 class QgsPoint3DBillboardSymbolHandler :
public QgsFeature3DHandler
476 : mSymbol( symbol ), mSelectedIds( selectedIds ) {}
478 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
479 void processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
480 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
487 QVector<QVector3D> positions;
490 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected );
499 PointData outSelected;
502 bool QgsPoint3DBillboardSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
505 Q_UNUSED( attributeNames )
509 void QgsPoint3DBillboardSymbolHandler::processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
511 PointData &out = mSelectedIds.contains( feature.
id() ) ? outSelected : outNormal;
519 void QgsPoint3DBillboardSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
521 makeEntity( parent, context, outNormal,
false );
522 makeEntity( parent, context, outSelected,
true );
525 void QgsPoint3DBillboardSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PointData &out,
bool selected )
529 billboardGeometry->
setPoints( out.positions );
532 Qt3DRender::QGeometryRenderer *billboardGeometryRenderer =
new Qt3DRender::QGeometryRenderer;
533 billboardGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
534 billboardGeometryRenderer->setGeometry( billboardGeometry );
535 billboardGeometryRenderer->setVertexCount( billboardGeometry->
count() );
551 Qt3DCore::QTransform *billboardTransform =
new Qt3DCore::QTransform();
552 billboardTransform->setMatrix( mSymbol.billboardTransform() );
555 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
557 entity->addComponent( billboardMaterial );
558 entity->addComponent( billboardTransform );
559 entity->addComponent( billboardGeometryRenderer );
560 entity->setParent( parent );
564 namespace Qgs3DSymbolImpl
580 QgsFeature3DHandler *handler = handlerForPoint3DSymbol( layer, symbol );
581 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
3 Material of the billboard rendering for points in 3D map view.
float shininess() const
Returns shininess of the surface.
void useDefaultSymbol(const Qgs3DMapSettings &map, bool selected=false)
Set default symbol for the texture with map and selected parameter for rendering. ...
QSet< QgsFeatureId > QgsFeatureIds
QColor selectionColor() const
Returns color used for selected features.
QColor specular() const
Returns specular color component.
static Qt3DExtras::QPhongMaterial * phongMaterial(const QgsPhongMaterialSettings &settings)
Returns phong material object based on the material settings.
Shape shape() const
Returns 3D shape for points.
3 Geometry of the billboard rendering for points in 3D map view.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
3 Definition of the world
A marker symbol type, for rendering Point and MultiPoint geometries.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
void setTexture2DFromSymbol(QgsMarkerSymbol *markerSymbol, const Qgs3DMapSettings &map, bool selected=false)
Set markerSymbol for the texture with map and selected parameter for rendering.
static void extractPointPositions(QgsFeature &f, const Qgs3DMapSettings &map, Qgs3DTypes::AltitudeClamping altClamp, QVector< QVector3D > &positions)
Calculates (x,y,z) positions of (multi)point from the given feature.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes...
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
Shape
3D shape types supported by the symbol
void setPoints(const QVector< QVector3D > &vertices)
Set the points for the billboard with vertices.
QColor ambient() const
Returns ambient color component.
QColor diffuse() const
Returns diffuse color component.
Represents a vector layer which manages a vector based data sets.