28#include <Qt3DRender/QTexture>
29#include <Qt3DRender/QTechnique>
30#include <Qt3DRender/QCullFace>
32#include <Qt3DExtras/QTextureMaterial>
33#include <Qt3DExtras/QDiffuseSpecularMaterial>
34#include <Qt3DExtras/QPhongMaterial>
38QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
39 : QgsChunkLoader( node )
43 const QgsChunkNodeId nodeId = node->tileId();
46 mTileDebugText = nodeId.text();
49void QgsTerrainTileLoader::loadTexture()
51 connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady,
this, &QgsTerrainTileLoader::onImageReady );
52 mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
55void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity,
bool isShadingEnabled,
const QgsPhongMaterialSettings &shadingMaterial,
bool useTexture )
57 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
59 Qt3DRender::QMaterial *material =
nullptr;
62 if ( isShadingEnabled )
64 Qt3DExtras::QDiffuseSpecularMaterial *diffuseMapMaterial =
new Qt3DExtras::QDiffuseSpecularMaterial;
65 diffuseMapMaterial->setDiffuse( QVariant::fromValue( texture ) );
66 diffuseMapMaterial->setAmbient( shadingMaterial.
ambient() );
67 diffuseMapMaterial->setSpecular( shadingMaterial.
specular() );
68 diffuseMapMaterial->setShininess( shadingMaterial.
shininess() );
69 material = diffuseMapMaterial;
73 Qt3DExtras::QTextureMaterial *textureMaterial =
new Qt3DExtras::QTextureMaterial;
74 textureMaterial->setTexture( texture );
75 material = textureMaterial;
80 Qt3DExtras::QPhongMaterial *phongMaterial =
new Qt3DExtras::QPhongMaterial;
81 phongMaterial->setDiffuse( shadingMaterial.
diffuse() );
82 phongMaterial->setAmbient( shadingMaterial.
ambient() );
83 phongMaterial->setSpecular( shadingMaterial.
specular() );
84 phongMaterial->setShininess( shadingMaterial.
shininess() );
85 material = phongMaterial;
89 const QVector<Qt3DRender::QTechnique *> techniques = material->effect()->techniques();
90 for ( Qt3DRender::QTechnique *techique : techniques )
92 const QVector<Qt3DRender::QRenderPass *> passes = techique->renderPasses();
93 for ( Qt3DRender::QRenderPass *pass : passes )
95 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
96 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
97 pass->addRenderState( cullFace );
101 entity->addComponent( material );
104Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
106 Qt3DRender::QTexture2D *texture =
new Qt3DRender::QTexture2D;
107 QgsTerrainTextureImage *textureImage =
new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
108 texture->addTextureImage( textureImage );
109 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
110 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
112 entity->setTextureImage( textureImage );
117void QgsTerrainTileLoader::onImageReady(
int jobId,
const QImage &image )
119 if ( mTextureJobId == jobId )
121 mTextureImage = image;
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
QColor diffuse() const
Returns diffuse color component.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
float shininess() const
Returns shininess of the surface.
A rectangle specified with double values.
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
const QgsTilingScheme & tilingScheme() const
Returns tiling scheme of the terrain.
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.