19#include <Qt3DCore/QEntity>
20#include <Qt3DCore/QComponent>
21#include <Qt3DCore/QNode>
23#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
24#include <Qt3DRender/QAttribute>
25#include <Qt3DRender/QBuffer>
26#include <Qt3DRender/QGeometry>
32#include <Qt3DCore/QAttribute>
33#include <Qt3DCore/QBuffer>
34#include <Qt3DCore/QGeometry>
41#include <Qt3DRender/QGeometryRenderer>
42#include <Qt3DExtras/QPlaneGeometry>
43#include <Qt3DCore/QTransform>
44#include <Qt3DExtras/QDiffuseSpecularMaterial>
45#include <Qt3DExtras/QTextureMaterial>
46#include <Qt3DRender/QTextureImage>
47#include <Qt3DRender/QTexture>
48#include <Qt3DRender/QMesh>
49#include <Qt3DRender/QSceneLoader>
50#include <Qt3DRender/QAbstractTexture>
51#include <Qt3DExtras/QCylinderGeometry>
52#include <Qt3DExtras/QConeGeometry>
53#include <Qt3DExtras/QSphereGeometry>
54#include <Qt3DExtras/QCuboidGeometry>
55#include <Qt3DExtras/QTorusGeometry>
56#include <Qt3DExtras/QExtrudedTextMesh>
57#include <Qt3DExtras/QPhongMaterial>
58#include <Qt3DRender/QAbstractTextureImage>
77#include "qgsmeshterraingenerator.h"
78#include "qgsmeshterraintileloader_p.h"
91 const uint bytesOffset = attribute->byteOffset();
92 const uint bytesStride = attribute->byteStride();
93 const uint vertexSize = attribute->vertexSize();
94 const uint dataSize =
static_cast<uint
>( data.size() );
97 if ( bytesStride == 0 )
99 QgsDebugError(
"bytesStride==0, the attribute probably was not set properly" );
103 const char *pData = data.constData();
104 for (
unsigned int i = bytesOffset; i < dataSize; i += bytesStride )
106 for (
unsigned int j = 0; j < vertexSize *
sizeof( T ); j +=
sizeof( T ) )
109 memcpy( &v, pData + i + j,
sizeof( T ) );
110 result.push_back( v );
119 QVector<uint> result;
120 const char *pData = data.constData();
121 for (
int i = 0; i < data.size(); i +=
sizeof( T ) )
124 memcpy( &v, pData + i,
sizeof( T ) );
125 result.push_back( ( uint ) v );
132 switch ( indexAttribute->vertexBaseType() )
134 case Qt3DQAttribute::VertexBaseType::Int:
135 return _getIndexDataImplementation<int>( data );
136 case Qt3DQAttribute::VertexBaseType::UnsignedInt:
137 return _getIndexDataImplementation<uint>( data );
138 case Qt3DQAttribute::VertexBaseType::Short:
139 return _getIndexDataImplementation<short>( data );
140 case Qt3DQAttribute::VertexBaseType::UnsignedShort:
141 return _getIndexDataImplementation<ushort>( data );
142 case Qt3DQAttribute::VertexBaseType::Byte:
143 return _getIndexDataImplementation<char>( data );
144 case Qt3DQAttribute::VertexBaseType::UnsignedByte:
145 return _getIndexDataImplementation<uchar>( data );
147 QgsDebugError(
"Probably trying to get index data using an attribute that has vertex data" );
150 return QVector<uint>();
155 QByteArray bytes = buffer->data();
156 if ( bytes.isNull() )
165 QVector<Qt3DQAttribute *> attributes = geometry->attributes();
168 if ( attribute->attributeType() != type )
continue;
169 if ( name.isEmpty() || attribute->name() == name )
return attribute;
174template<
typename Component>
179 QVector<Qt3DCore::QComponent *> components = entity->components();
180 for ( Qt3DCore::QComponent *component : components )
182 Component *typedComponent = qobject_cast<Component *>( component );
183 if ( typedComponent )
184 return typedComponent;
192 const QString rendererType = abstractRenderer->
type();
194 if ( rendererType ==
"rulebased" )
196 int prevSize = mObjects.size();
198 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
199 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
201 Qt3DCore::QEntity *parentEntity = qobject_cast<Qt3DCore::QEntity *>( renderer->parent() );
204 Qgs3DExportObject *
object = processGeometryRenderer( renderer, layer->
name() + QStringLiteral(
"_" ) );
207 if ( mExportTextures )
208 processEntityMaterial( parentEntity,
object );
209 mObjects.push_back(
object );
211 return mObjects.size() > prevSize;
214 else if ( rendererType ==
"vector" )
217 if ( vectorLayerRenderer )
229 QgsDebugMsgLevel( QStringLiteral(
"Type '%1' of layer '%2' is not exportable." ).arg( layer->
name(), rendererType ), 2 );
236void Qgs3DSceneExporter::processEntityMaterial( Qt3DCore::QEntity *entity,
Qgs3DExportObject *
object )
238 Qt3DExtras::QPhongMaterial *phongMaterial = findTypedComponent<Qt3DExtras::QPhongMaterial>( entity );
242 object->setupMaterial( &material );
245 Qt3DExtras::QDiffuseSpecularMaterial *diffuseMapMaterial = findTypedComponent<Qt3DExtras::QDiffuseSpecularMaterial>( entity );
246 if ( diffuseMapMaterial )
248 const Qt3DRender::QTexture2D *diffuseTexture = diffuseMapMaterial->diffuse().value< Qt3DRender::QTexture2D * >();
249 if ( diffuseTexture )
251 const QVector<Qt3DRender::QAbstractTextureImage *> textureImages = diffuseTexture->textureImages();
252 for (
const Qt3DRender::QAbstractTextureImage *tex : textureImages )
257 const QImage image = imageTexture->
getImage();
258 object->setTextureImage( image );
272 QgsChunkNode *node = terrain->rootNode();
277 QgsTerrainTileEntity *terrainTile =
nullptr;
278 QgsTerrainTextureGenerator *textureGenerator = terrain->textureGenerator();
279 textureGenerator->waitForFinished();
280 const QSize oldResolution = textureGenerator->textureSize();
281 textureGenerator->setTextureSize( QSize( mTerrainTextureResolution, mTerrainTextureResolution ) );
282 switch ( generator->
type() )
285 terrainTile = getDemTerrainEntity( terrain, node );
286 parseDemTile( terrainTile, layerName + QStringLiteral(
"_" ) );
289 terrainTile = getFlatTerrainEntity( terrain, node );
290 parseFlatTile( terrainTile, layerName + QStringLiteral(
"_" ) );
293 terrainTile = getMeshTerrainEntity( terrain, node );
294 parseMeshTile( terrainTile, layerName + QStringLiteral(
"_" ) );
301 textureGenerator->setTextureSize( oldResolution );
304QgsTerrainTileEntity *Qgs3DSceneExporter::getFlatTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node )
307 FlatTerrainChunkLoader *flatTerrainLoader = qobject_cast<FlatTerrainChunkLoader *>( generator->
createChunkLoader( node ) );
308 if ( mExportTextures )
309 terrain->textureGenerator()->waitForFinished();
311 Qt3DCore::QEntity *entity = flatTerrainLoader->createEntity(
this );
312 QgsTerrainTileEntity *tileEntity = qobject_cast<QgsTerrainTileEntity *>( entity );
316QgsTerrainTileEntity *Qgs3DSceneExporter::getDemTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node )
322 QgsDemTerrainTileLoader *loader = qobject_cast<QgsDemTerrainTileLoader *>( generator->
createChunkLoader( node ) );
324 if ( mExportTextures )
325 terrain->textureGenerator()->waitForFinished();
326 QgsTerrainTileEntity *tileEntity = qobject_cast<QgsTerrainTileEntity *>( loader->createEntity(
this ) );
331QgsTerrainTileEntity *Qgs3DSceneExporter::getMeshTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node )
333 QgsMeshTerrainGenerator *generator =
dynamic_cast<QgsMeshTerrainGenerator *
>( terrain->mapSettings()->terrainGenerator() );
334 QgsMeshTerrainTileLoader *loader = qobject_cast<QgsMeshTerrainTileLoader *>( generator->createChunkLoader( node ) );
336 QgsTerrainTileEntity *tileEntity = qobject_cast<QgsTerrainTileEntity *>( loader->createEntity(
this ) );
340void Qgs3DSceneExporter::parseFlatTile( QgsTerrainTileEntity *tileEntity,
const QString &layerName )
342 Qt3DRender::QGeometryRenderer *mesh = findTypedComponent<Qt3DRender::QGeometryRenderer>( tileEntity );
343 Qt3DCore::QTransform *transform = findTypedComponent<Qt3DCore::QTransform>( tileEntity );
346 Qt3DExtras::QPlaneGeometry *tileGeometry = qobject_cast<Qt3DExtras::QPlaneGeometry *>( geometry );
349 QgsDebugError(
"Qt3DExtras::QPlaneGeometry* is expected but something else was given" );
353 const float scale = transform->scale();
354 const QVector3D translation = transform->translation();
357 Qt3DQAttribute *positionAttribute = tileGeometry->positionAttribute();
358 if ( !positionAttribute )
360 QgsDebugError( QString(
"Cannot export '%1' - geometry has no position attribute!" ).arg( layerName ) );
363 const QByteArray verticesBytes =
getData( positionAttribute->buffer() );
364 if ( verticesBytes.isNull() )
366 QgsDebugError( QString(
"Geometry for '%1' has position attribute with empty data!" ).arg( layerName ) );
369 const QVector<float> positionBuffer = getAttributeData<float>( positionAttribute, verticesBytes );
373 if ( !indexAttribute )
375 QgsDebugError( QString(
"Cannot export '%1' - geometry has no index attribute!" ).arg( layerName ) );
378 const QByteArray indexBytes =
getData( indexAttribute->buffer() );
379 if ( indexBytes.isNull() )
381 QgsDebugError( QString(
"Geometry for '%1' has index attribute with empty data!" ).arg( layerName ) );
384 const QVector<uint> indexesBuffer =
getIndexData( indexAttribute, indexBytes );
386 QString objectNamePrefix = layerName;
387 if ( objectNamePrefix != QString() ) objectNamePrefix += QString();
390 mObjects.push_back(
object );
392 object->setSmoothEdges( mSmoothEdges );
393 object->setupPositionCoordinates( positionBuffer,
scale, translation );
394 object->setupFaces( indexesBuffer );
396 if ( mExportNormals )
399 QVector<float> normalsBuffer;
400 for (
int i = 0; i < positionBuffer.size(); i += 3 ) normalsBuffer << 0.0f << 1.0f << 0.0f;
401 object->setupNormalCoordinates( normalsBuffer );
404 Qt3DQAttribute *texCoordsAttribute = tileGeometry->texCoordAttribute();
405 if ( mExportTextures && texCoordsAttribute )
408 const QVector<float> texCoords = getAttributeData<float>( texCoordsAttribute, verticesBytes );
409 object->setupTextureCoordinates( texCoords );
411 QgsTerrainTextureImage *textureImage = tileEntity->textureImage();
412 const QImage img = textureImage->getImage();
413 object->setTextureImage( img );
417void Qgs3DSceneExporter::parseDemTile( QgsTerrainTileEntity *tileEntity,
const QString &layerName )
419 Qt3DRender::QGeometryRenderer *mesh = findTypedComponent<Qt3DRender::QGeometryRenderer>( tileEntity );
420 Qt3DCore::QTransform *transform = findTypedComponent<Qt3DCore::QTransform>( tileEntity );
423 DemTerrainTileGeometry *tileGeometry = qobject_cast<DemTerrainTileGeometry *>( geometry );
426 QgsDebugError(
"DemTerrainTileGeometry* is expected but something else was given" );
430 const float scale = transform->scale();
431 const QVector3D translation = transform->translation();
433 Qt3DQAttribute *positionAttribute = tileGeometry->positionAttribute();
434 const QByteArray positionBytes = positionAttribute->buffer()->data();
435 const QVector<float> positionBuffer = getAttributeData<float>( positionAttribute, positionBytes );
438 const QByteArray indexBytes = indexAttribute->buffer()->data();
439 const QVector<unsigned int> indexBuffer =
getIndexData( indexAttribute, indexBytes );
442 mObjects.push_back(
object );
444 object->setSmoothEdges( mSmoothEdges );
445 object->setupPositionCoordinates( positionBuffer,
scale, translation );
446 object->setupFaces( indexBuffer );
448 Qt3DQAttribute *normalsAttributes = tileGeometry->normalAttribute();
449 if ( mExportNormals && normalsAttributes )
451 const QByteArray normalsBytes = normalsAttributes->buffer()->data();
452 const QVector<float> normalsBuffer = getAttributeData<float>( normalsAttributes, normalsBytes );
453 object->setupNormalCoordinates( normalsBuffer );
456 Qt3DQAttribute *texCoordsAttribute = tileGeometry->texCoordsAttribute();
457 if ( mExportTextures && texCoordsAttribute )
459 const QByteArray texCoordsBytes = texCoordsAttribute->buffer()->data();
460 const QVector<float> texCoordsBuffer = getAttributeData<float>( texCoordsAttribute, texCoordsBytes );
461 object->setupTextureCoordinates( texCoordsBuffer );
463 QgsTerrainTextureImage *textureImage = tileEntity->textureImage();
464 const QImage img = textureImage->getImage();
465 object->setTextureImage( img );
469void Qgs3DSceneExporter::parseMeshTile( QgsTerrainTileEntity *tileEntity,
const QString &layerName )
471 QString objectNamePrefix = layerName;
472 if ( objectNamePrefix != QString() ) objectNamePrefix += QStringLiteral(
"_" );
474 const QList<Qt3DRender::QGeometryRenderer *> renderers = tileEntity->findChildren<Qt3DRender::QGeometryRenderer *>();
475 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
484QVector<Qgs3DExportObject *> Qgs3DSceneExporter::processInstancedPointGeometry( Qt3DCore::QEntity *entity,
const QString &objectNamePrefix )
486 QVector<Qgs3DExportObject *> objects;
487 const QList<Qt3DQGeometry *> geometriesList = entity->findChildren<
Qt3DQGeometry *>();
490 Qt3DQAttribute *positionAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultPositionAttributeName(), Qt3DQAttribute::VertexAttribute );
492 if ( !positionAttribute || !indexAttribute )
494 QgsDebugError( QString(
"Cannot export '%1' - geometry has no position or index attribute!" ).arg( objectNamePrefix ) );
498 const QByteArray vertexBytes = positionAttribute->buffer()->data();
499 const QByteArray indexBytes = indexAttribute->buffer()->data();
500 if ( vertexBytes.isNull() || indexBytes.isNull() )
502 QgsDebugError( QString(
"Geometry for '%1' has position or index attribute with empty data!" ).arg( objectNamePrefix ) );
506 const QVector<float> positionData = getAttributeData<float>( positionAttribute, vertexBytes );
507 const QVector<uint> indexData =
getIndexData( indexAttribute, indexBytes );
510 if ( !instanceDataAttribute )
512 QgsDebugError( QString(
"Cannot export '%1' - geometry has no instanceData attribute!" ).arg( objectNamePrefix ) );
515 const QByteArray instancePositionBytes =
getData( instanceDataAttribute->buffer() );
516 if ( instancePositionBytes.isNull() )
518 QgsDebugError( QString(
"Geometry for '%1' has instanceData attribute with empty data!" ).arg( objectNamePrefix ) );
521 QVector<float> instancePosition = getAttributeData<float>( instanceDataAttribute, instancePositionBytes );
523 for (
int i = 0; i < instancePosition.size(); i += 3 )
526 objects.push_back(
object );
527 object->setupPositionCoordinates( positionData, 1.0f, QVector3D( instancePosition[i], instancePosition[i + 1], instancePosition[i + 2] ) );
528 object->setupFaces( indexData );
530 object->setSmoothEdges( mSmoothEdges );
532 Qt3DQAttribute *normalsAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultNormalAttributeName(), Qt3DQAttribute::VertexAttribute );
533 if ( mExportNormals && normalsAttribute )
536 const QVector<float> normalsData = getAttributeData<float>( normalsAttribute, vertexBytes );
537 object->setupNormalCoordinates( normalsData );
545QVector<Qgs3DExportObject *> Qgs3DSceneExporter::processSceneLoaderGeometries( Qt3DRender::QSceneLoader *sceneLoader,
const QString &objectNamePrefix )
547 QVector<Qgs3DExportObject *> objects;
548 Qt3DCore::QEntity *sceneLoaderParent = qobject_cast<Qt3DCore::QEntity *>( sceneLoader->parent() );
549 Qt3DCore::QTransform *entityTransform = findTypedComponent<Qt3DCore::QTransform>( sceneLoaderParent );
550 float sceneScale = 1.0f;
551 QVector3D sceneTranslation( 0.0f, 0.0f, 0.0f );
552 if ( entityTransform )
554 sceneScale = entityTransform->scale();
555 sceneTranslation = entityTransform->translation();
557 QStringList entityNames = sceneLoader->entityNames();
558 for (
const QString &entityName : entityNames )
560 Qt3DRender::QGeometryRenderer *mesh = qobject_cast<Qt3DRender::QGeometryRenderer *>( sceneLoader->component( entityName, Qt3DRender::QSceneLoader::GeometryRendererComponent ) );
561 Qgs3DExportObject *
object = processGeometryRenderer( mesh, objectNamePrefix, sceneScale, sceneTranslation );
564 objects.push_back(
object );
569Qgs3DExportObject *Qgs3DSceneExporter::processGeometryRenderer( Qt3DRender::QGeometryRenderer *geomRenderer,
const QString &objectNamePrefix,
float sceneScale, QVector3D sceneTranslation )
572 if ( geomRenderer->primitiveType() != Qt3DRender::QGeometryRenderer::Triangles )
587 QVector<std::pair<uint, uint>> triangleIndexStartingIndiceToKeep;
591 QVector<QgsFeatureId> featureIds = tessGeom->
featureIds();
593 QSet<QgsFeatureId> tempFeatToAdd;
596 for (
int idx = 0; idx < featureIds.size(); idx++ )
599 if ( ! mExportedFeatureIds.contains( feat ) )
602 tempFeatToAdd += feat;
605 const uint startIdx = triangleIndex[idx] * 3;
606 const uint endIdx = idx < triangleIndex.size() - 1 ? triangleIndex[idx + 1] * 3 : std::numeric_limits<uint>::max();
608 if ( startIdx < endIdx )
609 triangleIndexStartingIndiceToKeep.append( std::pair<uint, uint>( startIdx, endIdx ) );
612 mExportedFeatureIds += tempFeatToAdd;
614 if ( triangleIndexStartingIndiceToKeep.isEmpty() )
622 QVector3D translation( 0.0f, 0.0f, 0.0f );
623 QObject *parent = geomRenderer->parent();
626 Qt3DCore::QEntity *entity = qobject_cast<Qt3DCore::QEntity *>( parent );
627 Qt3DCore::QTransform *transform = findTypedComponent<Qt3DCore::QTransform>( entity );
630 scale *= transform->scale();
631 translation += transform->translation();
633 parent = parent->parent();
638 QByteArray indexBytes, vertexBytes;
639 QVector<uint> indexDataTmp;
640 QVector<uint> indexData;
641 QVector<float> positionData;
644 positionAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultPositionAttributeName(), Qt3DQAttribute::VertexAttribute );
645 if ( !positionAttribute )
647 QgsDebugError( QString(
"Cannot export '%1' - geometry has no position attribute!" ).arg( objectNamePrefix ) );
651 vertexBytes =
getData( positionAttribute->buffer() );
652 if ( vertexBytes.isNull() )
654 QgsDebugError( QString(
"Will not export '%1' as geometry has empty position data!" ).arg( objectNamePrefix ) );
658 positionData = getAttributeData<float>( positionAttribute, vertexBytes );
661 QVector<Qt3DQAttribute *> attributes = geometry->attributes();
664 if ( attribute->attributeType() == Qt3DQAttribute::IndexAttribute )
666 indexAttribute = attribute;
667 indexBytes =
getData( indexAttribute->buffer() );
668 if ( indexBytes.isNull() )
670 QgsDebugError( QString(
"Geometry for '%1' has index attribute with empty data!" ).arg( objectNamePrefix ) );
674 indexDataTmp =
getIndexData( indexAttribute, indexBytes );
681 if ( !indexAttribute )
683 for ( uint i = 0; i < static_cast<uint>( positionData.size() / 3 ); ++i )
685 indexDataTmp.push_back( i );
690 if ( triangleIndexStartingIndiceToKeep.isEmpty() )
693 indexData.append( indexDataTmp );
699 const int triangleIndexStartingIndiceToKeepSize = triangleIndexStartingIndiceToKeep.size();
700 const uint indexDataTmpSize =
static_cast<uint
>( indexDataTmp.size() );
701 for ( uint i = 0; i < indexDataTmpSize; ++i )
703 uint idx = indexDataTmp[
static_cast<int>( i )];
705 while ( intervalIdx < triangleIndexStartingIndiceToKeepSize
706 && idx > triangleIndexStartingIndiceToKeep[intervalIdx].first
707 && idx >= triangleIndexStartingIndiceToKeep[intervalIdx].second )
713 if ( intervalIdx < triangleIndexStartingIndiceToKeepSize
714 && idx >= triangleIndexStartingIndiceToKeep[intervalIdx].first
715 && idx < triangleIndexStartingIndiceToKeep[intervalIdx].second )
717 indexData.push_back( idx );
724 object->setupPositionCoordinates( positionData,
scale * sceneScale, translation + sceneTranslation );
725 object->setupFaces( indexData );
727 Qt3DQAttribute *normalsAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultNormalAttributeName(), Qt3DQAttribute::VertexAttribute );
728 if ( mExportNormals && normalsAttribute )
731 const QVector<float> normalsData = getAttributeData<float>( normalsAttribute, vertexBytes );
732 object->setupNormalCoordinates( normalsData );
735 Qt3DQAttribute *texCoordsAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultTextureCoordinateAttributeName(), Qt3DQAttribute::VertexAttribute );
736 if ( mExportTextures && texCoordsAttribute )
739 const QVector<float> texCoordsData = getAttributeData<float>( texCoordsAttribute, vertexBytes );
740 object->setupTextureCoordinates( texCoordsData );
746QVector<Qgs3DExportObject *> Qgs3DSceneExporter::processLines( Qt3DCore::QEntity *entity,
const QString &objectNamePrefix )
748 QVector<Qgs3DExportObject *> objs;
749 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
750 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
752 if ( renderer->primitiveType() != Qt3DRender::QGeometryRenderer::LineStripAdjacency )
continue;
754 Qt3DQAttribute *positionAttribute =
findAttribute( geom, Qt3DQAttribute::defaultPositionAttributeName(), Qt3DQAttribute::VertexAttribute );
756 if ( !positionAttribute || !indexAttribute )
758 QgsDebugError( QString(
"Cannot export '%1' - geometry has no position or index attribute!" ).arg( objectNamePrefix ) );
762 const QByteArray vertexBytes =
getData( positionAttribute->buffer() );
763 const QByteArray indexBytes =
getData( indexAttribute->buffer() );
764 if ( vertexBytes.isNull() || indexBytes.isNull() )
766 QgsDebugError( QString(
"Geometry for '%1' has position or index attribute with empty data!" ).arg( objectNamePrefix ) );
769 const QVector<float> positionData = getAttributeData<float>( positionAttribute, vertexBytes );
770 const QVector<uint> indexData =
getIndexData( indexAttribute, indexBytes );
777 objs.push_back( exportObject );
782Qgs3DExportObject *Qgs3DSceneExporter::processPoints( Qt3DCore::QEntity *entity,
const QString &objectNamePrefix )
784 QVector<float> points;
785 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
786 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
788 Qt3DQGeometry *geometry = qobject_cast<QgsBillboardGeometry *>( renderer->geometry() );
791 Qt3DQAttribute *positionAttribute =
findAttribute( geometry, Qt3DQAttribute::defaultPositionAttributeName(), Qt3DQAttribute::VertexAttribute );
792 if ( !positionAttribute )
794 QgsDebugError( QString(
"Cannot export '%1' - geometry has no position attribute!" ).arg( objectNamePrefix ) );
797 const QByteArray positionBytes =
getData( positionAttribute->buffer() );
798 if ( positionBytes.isNull() )
800 QgsDebugError( QString(
"Geometry for '%1' has position attribute with empty data!" ).arg( objectNamePrefix ) );
803 const QVector<float> positions = getAttributeData<float>( positionAttribute, positionBytes );
814 const QString objFilePath = QDir( sceneFolderPath ).filePath( sceneName + QStringLiteral(
".obj" ) );
815 const QString mtlFilePath = QDir( sceneFolderPath ).filePath( sceneName + QStringLiteral(
".mtl" ) );
817 QFile file( objFilePath );
818 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
820 QgsDebugError( QStringLiteral(
"Scene can not be exported to '%1'. File access error." ).arg( objFilePath ) );
823 QFile mtlFile( mtlFilePath );
824 if ( !mtlFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
826 QgsDebugError( QStringLiteral(
"Scene can not be exported to '%1'. File access error." ).arg( mtlFilePath ) );
830 float maxfloat = std::numeric_limits<float>::max(), minFloat = std::numeric_limits<float>::lowest();
831 float minX = maxfloat, minY = maxfloat, minZ = maxfloat, maxX = minFloat, maxY = minFloat, maxZ = minFloat;
834 obj->
objectBounds( minX, minY, minZ, maxX, maxY, maxZ );
837 float diffX = 1.0f, diffY = 1.0f, diffZ = 1.0f;
842 const float centerX = ( minX + maxX ) / 2.0f;
843 const float centerY = ( minY + maxY ) / 2.0f;
844 const float centerZ = ( minZ + maxZ ) / 2.0f;
846 const float scale = std::max( diffX, std::max( diffY, diffZ ) );
848 QTextStream out( &file );
850 const QString mtlLibName = sceneName +
".mtl";
851 out <<
"mtllib " << mtlLibName <<
"\n";
853 QTextStream mtlOut( &mtlFile );
859 const QString material = obj->
saveMaterial( mtlOut, sceneFolderPath );
860 out <<
"o " << obj->
name() <<
"\n";
861 if ( material != QString() )
862 out <<
"usemtl " << material <<
"\n";
866 QgsDebugMsgLevel( QStringLiteral(
"Scene exported to '%1'" ).arg( objFilePath ), 2 );
869QString Qgs3DSceneExporter::getObjectName(
const QString &name )
872 if ( usedObjectNamesCounter.contains( name ) )
874 ret = QStringLiteral(
"%1%2" ).arg( name ).arg( usedObjectNamesCounter[name] );
875 usedObjectNamesCounter[name]++;
878 usedObjectNamesCounter[name] = 2;
Manages the data of each object of the scene (positions, normals, texture coordinates ....
void setType(ObjectType type)
Sets the object type.
void objectBounds(float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ)
Updates the box bounds explained with the current object bounds This expands the bounding box if the ...
void saveTo(QTextStream &out, float scale, const QVector3D ¢er, int precision=6)
Saves the current object to the output stream while scaling the object and centering it to be visible...
void setupPositionCoordinates(const QVector< float > &positionsBuffer, float scale=1.0f, const QVector3D &translation=QVector3D(0, 0, 0))
Sets positions coordinates and does the translation and scaling.
QString name() const
Returns the object name.
QString saveMaterial(QTextStream &mtlOut, const QString &folder)
saves the texture of the object and material information
void setupLine(const QVector< uint > &facesIndexes)
sets line vertex indexes
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
bool terrainRenderingEnabled() const
Returns whether the 2D terrain surface will be rendered.
void save(const QString &sceneName, const QString &sceneFolderPath, int precision=6)
Saves the scene to a .obj file.
void parseTerrain(QgsTerrainEntity *terrain, const QString &layer)
Creates terrain export objects from the terrain entity.
float scale() const
Returns the scale of the exported 3D model.
bool parseVectorLayerEntity(Qt3DCore::QEntity *entity, QgsVectorLayer *layer)
Creates necessary export objects from entity if it represents valid vector layer entity Returns false...
static QgsPhongMaterialSettings phongMaterialFromQt3DComponent(Qt3DExtras::QPhongMaterial *material)
Returns phong material settings object based on the Qt3D material.
Base class for all renderers that may to participate in 3D view.
virtual QString type() const =0
Returns unique identifier of the renderer class (used to identify subclass)
virtual bool exportGeometries(Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix) const
Exports the geometries contained within the hierarchy of entity.
QgsDemHeightMapGenerator * heightMapGenerator()
Returns height map generator object - takes care of extraction of elevations from the layer)
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
void setResolution(int resolution)
Sets resolution of the generator (how many elevation samples on one side of a terrain tile)
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override SIP_FACTORY
Holds an image that can be used as a texture in the 3D view.
QImage getImage() const
Returns the image.
QgsAbstract3DRenderer * renderer3D() const
Returns 3D renderer associated with the layer.
@ QuantizedMesh
Terrain is built from quantized mesh tiles.
@ Dem
Terrain is built from raster layer with digital elevation model.
@ Online
Terrain is built from downloaded tiles with digital elevation model.
@ Mesh
Terrain is built from mesh layer with z value on vertices.
@ Flat
The whole terrain is flat area.
virtual Type type() const =0
What texture generator implementation is this.
QVector< QgsFeatureId > featureIds() const
Returns included feature ids.
QVector< uint > triangleIndexStartingIndices() const
Returns triangle index for features. For a feature featureIds()[i], matching triangles start at trian...
3D renderer that renders all features of a vector layer with the same 3D symbol.
const QgsAbstract3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
Represents a vector layer which manages a vector based data sets.
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
QVector< T > getAttributeData(Qt3DQAttribute *attribute, const QByteArray &data)
Qt3DQAttribute * findAttribute(Qt3DQGeometry *geometry, const QString &name, Qt3DQAttribute::AttributeType type)
Qt3DCore::QAttribute Qt3DQAttribute
QVector< uint > getIndexData(Qt3DQAttribute *indexAttribute, const QByteArray &data)
QByteArray getData(Qt3DQBuffer *buffer)
Qt3DCore::QBuffer Qt3DQBuffer
QVector< uint > _getIndexDataImplementation(const QByteArray &data)
Component * findTypedComponent(Qt3DCore::QEntity *entity)
Qt3DCore::QGeometry Qt3DQGeometry
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)