Skip to content
Permalink
Browse files
Add transparency support for phong materials
  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed May 13, 2022
1 parent c69fcd7 commit e24f5cbbef615d94dda7cfc2d6dd578601e815b4
@@ -66,6 +66,13 @@ Returns specular color component
float shininess() const;
%Docstring
Returns shininess of the surface
%End

float opacity() const;
%Docstring
Returns the opacity of the surface

.. versionadded:: 3.26
%End

virtual QMap<QString, QString> toExportParameters() const;
@@ -88,6 +95,15 @@ Sets specular color component
Sets shininess of the surface
%End

void setOpacity( float opacity );
%Docstring
Sets shininess of the surface

.. versionadded:: 3.26
%End



virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context );

virtual void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
@@ -20,6 +20,7 @@
#include "qgsimagecache.h"
#include <Qt3DExtras/QDiffuseMapMaterial>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QPhongAlphaMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>
@@ -69,6 +70,7 @@ void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWr
mDiffuse = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
mSpecular = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toFloat();
mOpacity = elem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toFloat();

QgsAbstractMaterialSettings::readXml( elem, context );
}
@@ -79,6 +81,7 @@ void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteCo
elem.setAttribute( QStringLiteral( "diffuse" ), QgsSymbolLayerUtils::encodeColor( mDiffuse ) );
elem.setAttribute( QStringLiteral( "specular" ), QgsSymbolLayerUtils::encodeColor( mSpecular ) );
elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
elem.setAttribute( QStringLiteral( "opacity" ), mOpacity );

QgsAbstractMaterialSettings::writeXml( elem, context );
}
@@ -98,6 +101,24 @@ Qt3DRender::QMaterial *QgsPhongMaterialSettings::toMaterial( QgsMaterialSettings
if ( dataDefinedProperties().hasActiveProperties() )
return dataDefinedMaterial();

if ( mOpacity != 1 )
{
Qt3DExtras::QPhongAlphaMaterial *material = new Qt3DExtras::QPhongAlphaMaterial;
material->setDiffuse( mDiffuse );
material->setAmbient( mAmbient );
material->setSpecular( mSpecular );
material->setShininess( mShininess );
material->setAlpha( mOpacity );

if ( context.isSelected() )
{
// update the material with selection colors
material->setDiffuse( context.selectionColor() );
material->setAmbient( context.selectionColor().darker() );
}
return material;
}

Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
material->setDiffuse( mDiffuse );
material->setAmbient( mAmbient );
@@ -73,6 +73,12 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings
//! Returns shininess of the surface
float shininess() const { return mShininess; }

/**
* Returns the opacity of the surface
* \since QGIS 3.26
*/
float opacity() const { return mOpacity; }

QMap<QString, QString> toExportParameters() const override;

//! Sets ambient color component
@@ -84,6 +90,14 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings
//! Sets shininess of the surface
void setShininess( float shininess ) { mShininess = shininess; }

/**
* Sets shininess of the surface
* \since QGIS 3.26
*/
void setOpacity( float opacity ) { mOpacity = opacity; }



void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const override;

@@ -110,6 +124,7 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings
QColor mDiffuse{ QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) };
QColor mSpecular{ QColor::fromRgbF( 1.0f, 1.0f, 1.0f, 1.0f ) };
float mShininess = 0.0f;
float mOpacity = 1.0f;

//! Constructs a material from shader files
Qt3DRender::QMaterial *dataDefinedMaterial() const;
@@ -25,6 +25,7 @@
#include <Qt3DRender/QSceneLoader>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QPhongAlphaMaterial>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DLogic/QFrameAction>
#include <Qt3DRender/QEffect>
@@ -911,6 +912,19 @@ void Qgs3DMapScene::finalizeNewEntity( Qt3DCore::QEntity *newEntity )

bm->setViewportSize( mCameraController->viewport().size() );
}

// Finalize adding the 3D transparent objects by adding the layer components to the entities
for ( Qt3DExtras::QPhongAlphaMaterial *ph : newEntity->findChildren<Qt3DExtras::QPhongAlphaMaterial *>() )
{
Qt3DCore::QEntity *entity = qobject_cast<Qt3DCore::QEntity *>( ph->parent() );
if ( !entity )
continue;
QgsShadowRenderingFrameGraph *frameGraph = mEngine->frameGraph();
Qt3DRender::QLayer *layer = frameGraph->transparentObjectLayer();
if ( entity->components().contains( layer ) )
continue;
entity->addComponent( layer );
}
}

int Qgs3DMapScene::maximumTextureSize() const
@@ -26,7 +26,8 @@
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QTechnique>
#include <Qt3DRender/QGraphicsApiFilter>

#include <Qt3DRender/QBlendEquation>
#include <Qt3DRender/QSortPolicy>

Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructTexturesPreviewPass()
{
@@ -46,6 +47,14 @@ Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructTexturesPrev

Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructForwardRenderPass()
{
// The branch structure:
// mMainCameraSelector
// mForwardRenderLayerFilter
// mForwardRenderTargetSelector
// opaqueObjectsFilter | transparentObjectsLayerFilter
// forwaredRenderStateSet | sortPolicy
// mFrustumCulling | transparentObjectsRenderStateSet
// mForwardClearBuffers |
mMainCameraSelector = new Qt3DRender::QCameraSelector;
mMainCameraSelector->setCamera( mMainCamera );

@@ -85,12 +94,11 @@ Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructForwardRende
mForwardRenderTargetSelector = new Qt3DRender::QRenderTargetSelector( mForwardRenderLayerFilter );
mForwardRenderTargetSelector->setTarget( forwardRenderTarget );

mForwardClearBuffers = new Qt3DRender::QClearBuffers( mForwardRenderTargetSelector );
mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
mForwardClearBuffers->setClearDepthValue( 1.0f );
Qt3DRender::QLayerFilter *opaqueObjectsFilter = new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
opaqueObjectsFilter->addLayer( mTransparentObjectsPassLayer );
opaqueObjectsFilter->setFilterMode( Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers );

Qt3DRender::QRenderStateSet *forwaredRenderStateSet = new Qt3DRender::QRenderStateSet( mForwardClearBuffers );
Qt3DRender::QRenderStateSet *forwaredRenderStateSet = new Qt3DRender::QRenderStateSet( opaqueObjectsFilter );

Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
@@ -102,6 +110,38 @@ Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructForwardRende

mFrustumCulling = new Qt3DRender::QFrustumCulling( forwaredRenderStateSet );

mForwardClearBuffers = new Qt3DRender::QClearBuffers( mFrustumCulling );
mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
mForwardClearBuffers->setClearDepthValue( 1.0f );

Qt3DRender::QLayerFilter *transparentObjectsLayerFilter = new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
transparentObjectsLayerFilter->addLayer( mTransparentObjectsPassLayer );
transparentObjectsLayerFilter->setFilterMode( Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers );

Qt3DRender::QSortPolicy *sortPolicy = new Qt3DRender::QSortPolicy( transparentObjectsLayerFilter );
QVector<Qt3DRender::QSortPolicy::SortType> sortTypes;
sortTypes.push_back( Qt3DRender::QSortPolicy::BackToFront );
sortTypes.push_back( Qt3DRender::QSortPolicy::Material );
sortTypes.push_back( Qt3DRender::QSortPolicy::StateChangeCost );
sortTypes.push_back( Qt3DRender::QSortPolicy::FrontToBack );
sortPolicy->setSortTypes( sortTypes );

Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSet = new Qt3DRender::QRenderStateSet( sortPolicy );
{
Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
transparentObjectsRenderStateSet->addRenderState( depthTest );

Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
transparentObjectsRenderStateSet->addRenderState( cullFace );

Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation;
blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Subtract );
transparentObjectsRenderStateSet->addRenderState( blendEquation );
}

return mMainCameraSelector;
}

@@ -352,13 +392,14 @@ QgsShadowRenderingFrameGraph::QgsShadowRenderingFrameGraph( QSurface *surface, Q
mCastShadowsLayer = new Qt3DRender::QLayer;
mForwardRenderLayer = new Qt3DRender::QLayer;
mDepthRenderPassLayer = new Qt3DRender::QLayer;

mTransparentObjectsPassLayer = new Qt3DRender::QLayer;

mPostprocessPassLayer->setRecursive( true );
mPreviewLayer->setRecursive( true );
mCastShadowsLayer->setRecursive( true );
mForwardRenderLayer->setRecursive( true );
mDepthRenderPassLayer->setRecursive( true );
mTransparentObjectsPassLayer->setRecursive( true );

mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;

@@ -78,6 +78,12 @@ class QgsShadowRenderingFrameGraph : public Qt3DCore::QEntity
//! Returns a layer object used to indicate that an entity will be rendered during the forward rendering pass
Qt3DRender::QLayer *forwardRenderLayer() { return mForwardRenderLayer; }

/**
* Returns a layer object used to indicate that the object is transparent
* \since QGIS 3.26
*/
Qt3DRender::QLayer *transparentObjectLayer() { return mTransparentObjectsPassLayer; }

//! Returns the main camera
Qt3DRender::QCamera *mainCamera() { return mMainCamera; }
//! Returns the light camera
@@ -220,6 +226,7 @@ class QgsShadowRenderingFrameGraph : public Qt3DCore::QEntity
Qt3DRender::QLayer *mForwardRenderLayer = nullptr;
Qt3DRender::QLayer *mCastShadowsLayer = nullptr;
Qt3DRender::QLayer *mDepthRenderPassLayer = nullptr;
Qt3DRender::QLayer *mTransparentObjectsPassLayer = nullptr;

QgsPostprocessingEntity *mPostprocessingEntity = nullptr;

@@ -33,6 +33,11 @@ QgsPhongMaterialWidget::QgsPhongMaterialWidget( QWidget *parent )
connect( mAmbientDataDefinedButton, &QgsPropertyOverrideButton::changed, this, &QgsPhongMaterialWidget::changed );
connect( mDiffuseDataDefinedButton, &QgsPropertyOverrideButton::changed, this, &QgsPhongMaterialWidget::changed );
connect( mSpecularDataDefinedButton, &QgsPropertyOverrideButton::changed, this, &QgsPhongMaterialWidget::changed );
connect( mOpacitySlider, &QSlider::valueChanged, this, &QgsPhongMaterialWidget::changed );
connect( mOpacitySlider, &QSlider::valueChanged, this, [&]( int value )
{
mOpacityPercentageLabel->setText( QStringLiteral( "%1%" ).arg( value ) );
} );
}

QgsMaterialSettingsWidget *QgsPhongMaterialWidget::create()
@@ -100,6 +105,7 @@ void QgsPhongMaterialWidget::setSettings( const QgsAbstractMaterialSettings *set
btnAmbient->setColor( phongMaterial->ambient() );
btnSpecular->setColor( phongMaterial->specular() );
spinShininess->setValue( phongMaterial->shininess() );
mOpacitySlider->setValue( phongMaterial->opacity() * 100.0 );

mPropertyCollection = settings->dataDefinedProperties();

@@ -115,6 +121,7 @@ QgsAbstractMaterialSettings *QgsPhongMaterialWidget::settings()
m->setAmbient( btnAmbient->color() );
m->setSpecular( btnSpecular->color() );
m->setShininess( spinShininess->value() );
m->setOpacity( mOpacitySlider->value() / 100.0 );

mPropertyCollection.setProperty( QgsAbstractMaterialSettings::Diffuse, mDiffuseDataDefinedButton->toProperty() );
mPropertyCollection.setProperty( QgsAbstractMaterialSettings::Ambient, mAmbientDataDefinedButton->toProperty() );

0 comments on commit e24f5cb

Please sign in to comment.