20#include <Qt3DRender/QParameter>
25 mDepthTextureParameter =
new Qt3DRender::QParameter( QStringLiteral(
"depthTexture" ), depthTexture );
26 mMaterial->addParameter( mDepthTextureParameter );
28 mFarPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"farPlane" ), camera->farPlane() );
29 mMaterial->addParameter( mFarPlaneParameter );
30 connect( camera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&](
float farPlane )
32 mFarPlaneParameter->setValue( farPlane );
34 mNearPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"nearPlane" ), camera->nearPlane() );
35 mMaterial->addParameter( mNearPlaneParameter );
36 connect( camera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane )
38 mNearPlaneParameter->setValue( nearPlane );
40 mProjMatrixParameter =
new Qt3DRender::QParameter( QStringLiteral(
"origProjMatrix" ), camera->projectionMatrix() );
41 mMaterial->addParameter( mProjMatrixParameter );
42 connect( camera, &Qt3DRender::QCamera::projectionMatrixChanged, mProjMatrixParameter, [&](
const QMatrix4x4 & projectionMatrix )
44 mProjMatrixParameter->setValue( projectionMatrix );
46 mAspectRatioParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uAspectRatio" ), camera->aspectRatio() );
47 mMaterial->addParameter( mAspectRatioParameter );
48 connect( camera, &Qt3DRender::QCamera::aspectRatioChanged, mAspectRatioParameter, [&](
float ratio )
50 mAspectRatioParameter->setValue( ratio );
52 mTanHalfFovParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uTanHalfFov" ), tan( camera->fieldOfView() / 2 * M_PI / 180 ) );
53 mMaterial->addParameter( mTanHalfFovParameter );
54 connect( camera, &Qt3DRender::QCamera::fieldOfViewChanged, mTanHalfFovParameter, [&](
float fov )
56 mTanHalfFovParameter->setValue( tan( fov / 2 * M_PI / 180 ) );
59 QVariantList ssaoKernelValues;
61 std::uniform_real_distribution<float> randomFloats( 0.0, 1.0 );
62 std::default_random_engine generator;
63 unsigned int kernelSize = 64;
64 for (
unsigned int i = 0; i < kernelSize; ++i )
67 randomFloats( generator ) * 2.0 - 1.0,
68 randomFloats( generator ) * 2.0 - 1.0,
69 randomFloats( generator ) * 2.0 - 1.0
72 float scale = i / kernelSize;
73 scale = 0.1 + 0.9 * scale * scale;
75 ssaoKernelValues.push_back( sample );
79 QVariantList ssaoNoise;
80 for (
unsigned int i = 0; i < 16; ++i )
83 randomFloats( generator ),
84 randomFloats( generator ),
87 ssaoNoise.push_back( sample );
89 mAmbientOcclusionKernelParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoKernel[0]" ), ssaoKernelValues );
90 mMaterial->addParameter( mAmbientOcclusionKernelParameter );
92 Qt3DRender::QParameter *noiseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoNoise[0]" ), ssaoNoise );
93 mMaterial->addParameter( noiseParameter );
95 mIntensityParameter =
new Qt3DRender::QParameter( QStringLiteral(
"intensity" ), 0.5f );
96 mMaterial->addParameter( mIntensityParameter );
98 mRadiusParameter =
new Qt3DRender::QParameter( QStringLiteral(
"radius" ), 25.0f );
99 mMaterial->addParameter( mRadiusParameter );
101 mThresholdParameter =
new Qt3DRender::QParameter( QStringLiteral(
"threshold" ), 0.5f );
102 mMaterial->addParameter( mThresholdParameter );
104 const QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.vert" );
105 const QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.frag" );
107 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
108 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
113 mIntensityParameter->setValue( intensity );
118 mRadiusParameter->setValue( radius );
123 mThresholdParameter->setValue( threshold );
void setThreshold(float threshold)
Sets the amount of occlusion when the effects starts to kick in.
QgsAmbientOcclusionRenderEntity(Qt3DRender::QTexture2D *depthTexture, Qt3DRender::QCamera *camera, QNode *parent=nullptr)
Constructor.
void setRadius(float radius)
Sets the radius for the ambient occlusion effect.
void setIntensity(float intensity)
Sets the intensity for the ambient occlusion effect.
Qt3DRender::QShaderProgram * mShader
Qt3DRender::QMaterial * mMaterial