27 #include <Qt3DRender/QTexture>
28 #include <Qt3DRender/QTechnique>
29 #include <Qt3DRender/QCullFace>
31 #include <Qt3DExtras/QTextureMaterial>
32 #include <Qt3DExtras/QDiffuseSpecularMaterial>
33 #include <Qt3DExtras/QPhongMaterial>
39 QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
40 : QgsChunkLoader( node )
54 const QgsChunkNodeId nodeId = node->tileId();
59 mTileDebugText = nodeId.text();
62 void QgsTerrainTileLoader::loadTexture()
64 connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady,
this, &QgsTerrainTileLoader::onImageReady );
65 mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
68 void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity,
bool isShadingEnabled,
const QgsPhongMaterialSettings &shadingMaterial,
bool useTexture )
70 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
72 Qt3DRender::QMaterial *material =
nullptr;
75 if ( isShadingEnabled )
77 Qt3DExtras::QDiffuseSpecularMaterial *diffuseMapMaterial =
new Qt3DExtras::QDiffuseSpecularMaterial;
78 diffuseMapMaterial->setDiffuse( QVariant::fromValue( texture ) );
79 diffuseMapMaterial->setAmbient( shadingMaterial.
ambient() );
80 diffuseMapMaterial->setSpecular( shadingMaterial.
specular() );
81 diffuseMapMaterial->setShininess( shadingMaterial.
shininess() );
82 material = diffuseMapMaterial;
86 Qt3DExtras::QTextureMaterial *textureMaterial =
new Qt3DExtras::QTextureMaterial;
87 textureMaterial->setTexture( texture );
88 material = textureMaterial;
93 Qt3DExtras::QPhongMaterial *phongMaterial =
new Qt3DExtras::QPhongMaterial;
94 phongMaterial->setDiffuse( shadingMaterial.
diffuse() );
95 phongMaterial->setAmbient( shadingMaterial.
ambient() );
96 phongMaterial->setSpecular( shadingMaterial.
specular() );
97 phongMaterial->setShininess( shadingMaterial.
shininess() );
98 material = phongMaterial;
102 const QVector<Qt3DRender::QTechnique *> techniques = material->effect()->techniques();
103 for ( Qt3DRender::QTechnique *techique : techniques )
105 const QVector<Qt3DRender::QRenderPass *> passes = techique->renderPasses();
106 for ( Qt3DRender::QRenderPass *pass : passes )
108 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
109 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
110 pass->addRenderState( cullFace );
114 entity->addComponent( material );
117 Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
119 Qt3DRender::QTexture2D *texture =
new Qt3DRender::QTexture2D;
120 QgsTerrainTextureImage *textureImage =
new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
121 texture->addTextureImage( textureImage );
122 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
123 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
125 entity->setTextureImage( textureImage );
130 void QgsTerrainTileLoader::onImageReady(
int jobId,
const QImage &image )
132 if ( mTextureJobId == jobId )
134 mTextureImage = image;