50 mColorTextureParameter =
new Qt3DRender::QParameter( u
"colorTexture"_s, forwardRenderView.
colorTexture() );
51 mDepthTextureParameter =
new Qt3DRender::QParameter( u
"depthTexture"_s, forwardRenderView.
depthTexture() );
52 mAmbientOcclusionTextureParameter =
new Qt3DRender::QParameter( u
"ssaoTexture"_s, aoRenderView.
blurredFactorMapTexture() );
53 mMaterial->addParameter( mColorTextureParameter );
54 mMaterial->addParameter( mDepthTextureParameter );
55 mMaterial->addParameter( mAmbientOcclusionTextureParameter );
57 QList<Qt3DRender::QParameter *> globalShadowParams;
58 mShadowMapParameter =
new Qt3DRender::QParameter( u
"shadowTexture"_s, shadowRenderView.
mapTextureArray() );
59 globalShadowParams << mShadowMapParameter;
65 mLightCameras[i] = shadowRenderView.
lightCamera( i );
71 mCsmMatricesParameter =
new Qt3DRender::QParameter( QString(
"csmMatrices[0]" ), csmMatrices );
72 globalShadowParams << mCsmMatricesParameter;
73 mCsmBoundsMatricesParameter =
new Qt3DRender::QParameter( QString(
"csmBoundsMatrices[0]" ), csmMatrices );
74 globalShadowParams << mCsmBoundsMatricesParameter;
75 mMaxShadowDistanceParameter =
new Qt3DRender::QParameter( u
"maxShadowDistance"_s, QVariant::fromValue( 0.0f ) );
76 globalShadowParams << mMaxShadowDistanceParameter;
78 mFarPlaneParameter =
new Qt3DRender::QParameter( u
"farPlane"_s, mMainCamera->farPlane() );
79 mMaterial->addParameter( mFarPlaneParameter );
80 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&](
float farPlane ) { mFarPlaneParameter->setValue( farPlane ); } );
81 mNearPlaneParameter =
new Qt3DRender::QParameter( u
"nearPlane"_s, mMainCamera->nearPlane() );
82 mMaterial->addParameter( mNearPlaneParameter );
83 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane ) { mNearPlaneParameter->setValue( nearPlane ); } );
85 mMainCameraInvViewMatrixParameter =
new Qt3DRender::QParameter( u
"invertedCameraView"_s, mMainCamera->viewMatrix().inverted() );
86 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
87 mMainCameraInvProjMatrixParameter =
new Qt3DRender::QParameter( u
"invertedCameraProj"_s, mMainCamera->projectionMatrix().inverted() );
88 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
89 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&](
const QMatrix4x4 &projectionMatrix ) {
90 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
92 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]() { mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() ); } );
94 mRenderShadowsParameter =
new Qt3DRender::QParameter( u
"renderShadows"_s, QVariant::fromValue( 0 ) );
95 globalShadowParams << mRenderShadowsParameter;
96 mShadowLightIndexParameter =
new Qt3DRender::QParameter( u
"shadowLightIndex"_s, QVariant::fromValue( 0 ) );
97 globalShadowParams << mShadowLightIndexParameter;
98 mShadowBiasParameter =
new Qt3DRender::QParameter( u
"shadowBias"_s, QVariant::fromValue( 0.00001f ) );
99 globalShadowParams << mShadowBiasParameter;
103 mEyeDomeLightingEnabledParameter =
new Qt3DRender::QParameter( u
"edlEnabled"_s, QVariant::fromValue( 0 ) );
104 mEyeDomeLightingStrengthParameter =
new Qt3DRender::QParameter( u
"edlStrength"_s, QVariant::fromValue( 1000.0f ) );
105 mEyeDomeLightingDistanceParameter =
new Qt3DRender::QParameter( u
"edlDistance"_s, QVariant::fromValue( 2.0f ) );
106 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
107 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
108 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
110 mAmbientOcclusionEnabledParameter =
new Qt3DRender::QParameter( u
"ssaoEnabled"_s, QVariant::fromValue( 0 ) );
111 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
113 mBloomTextureParameter =
new Qt3DRender::QParameter( u
"bloomTexture"_s, bloomRenderView.
bloomTexture() );
114 mMaterial->addParameter( mBloomTextureParameter );
116 mBloomEnabledParameter =
new Qt3DRender::QParameter( u
"bloomEnabled"_s, QVariant::fromValue( 1 ) );
117 mMaterial->addParameter( mBloomEnabledParameter );
119 mBloomFactorParameter =
new Qt3DRender::QParameter( u
"bloomFactor"_s, 0.05 );
120 mMaterial->addParameter( mBloomFactorParameter );
122 mExposureParameter =
new Qt3DRender::QParameter( u
"exposureAdjustment"_s, 0.0f );
123 mMaterial->addParameter( mExposureParameter );
124 mToneMappingParameter =
new Qt3DRender::QParameter( u
"toneMapping"_s, 1 );
125 mMaterial->addParameter( mToneMappingParameter );
127 const QString vertexShaderPath = u
"qrc:/shaders/postprocess.vert"_s;
128 const QString fragmentShaderPath = u
"qrc:/shaders/postprocess.frag"_s;
130 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
132 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) );
134 mShader->setFragmentShaderCode( finalFragmentShaderCode );
147 const QVector3D lightDirection = lightDir.
toVector3D().normalized();
150 const float mainCameraNearPlane = mMainCamera->nearPlane();
152 const float mainCameraFarPlane = std::min( mMainCamera->farPlane(), maximumShadowRenderingDistance );
158 constexpr float PRACTICAL_SPLIT_SCHEME_LAMBDA = 0.9f;
161 const QMatrix4x4 invertedCameraView = mMainCamera->viewMatrix().inverted();
162 const float cameraFov = mMainCamera->fieldOfView();
163 const float cameraAspect = mMainCamera->aspectRatio();
165 const float shadowMapResolution =
static_cast< float >( mShadowMapResolution );
179 const float zNear = cascadeSplits[i];
180 const float zFar = cascadeSplits[i + 1];
183 QVector3D worldFrustumCorners[8];
184 QVector3D worldFrustrumCenter;
188 float rawRadius = 0.0f;
189 for (
int j = 0; j < 8; ++j )
191 rawRadius = std::max( rawRadius, ( worldFrustumCorners[j] - worldFrustrumCenter ).length() );
196 const float stepSize = std::max( std::pow( 2.0f, std::floor( std::log2( rawRadius ) ) - 4.0f ), 0.01f );
197 const float radius = std::ceil( rawRadius / stepSize ) * stepSize;
200 QMatrix4x4 lightRotation;
201 lightRotation.lookAt( QVector3D( 0, 0, 0 ), lightDirection, up );
202 QVector3D centerLightSpace = lightRotation * worldFrustrumCenter;
206 const float worldUnitsPerTexel = ( 2.0f * radius ) / shadowMapResolution;
208 centerLightSpace.setX( std::floor( centerLightSpace.x() / worldUnitsPerTexel ) * worldUnitsPerTexel );
209 centerLightSpace.setY( std::floor( centerLightSpace.y() / worldUnitsPerTexel ) * worldUnitsPerTexel );
210 const QVector3D snappedWorldCenter = lightRotation.inverted() * centerLightSpace;
213 QMatrix4x4 lightView;
214 const QVector3D lightPos = snappedWorldCenter - ( lightDirection * radius );
215 lightView.lookAt( lightPos, snappedWorldCenter, up );
218 mLightCameras[i]->setPosition( lightPos );
219 mLightCameras[i]->setViewCenter( snappedWorldCenter );
220 mLightCameras[i]->setUpVector( up );
222 float lightCameraLeft = -radius;
223 float lightCameraRight = radius;
224 float lightCameraBottom = -radius;
225 float lightCameraTop = radius;
227 float lightCameraNearPlane = -radius;
228 float lightCameraFarPlane = radius * 2.0f;
230 QMatrix4x4 orthoBoundsMatrix;
231 orthoBoundsMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
232 csmBoundsMatrices[i] = QVariant::fromValue( orthoBoundsMatrix * lightView );
237 constexpr float NEAR_PLANE_RETREAT = 5000.0f;
238 lightCameraNearPlane -= NEAR_PLANE_RETREAT;
241 mLightCameras[i]->lens()->setOrthographicProjection( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
244 QMatrix4x4 orthoMatrix;
245 orthoMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
246 csmMatrices[i] = QVariant::fromValue( orthoMatrix * lightView );
249 mCsmMatricesParameter->setValue( csmMatrices );
250 mCsmBoundsMatricesParameter->setValue( csmBoundsMatrices );
251 mMaxShadowDistanceParameter->setValue( mainCameraFarPlane );
void updateShadowSettings(const QgsVector3D &lightDirection, float maximumShadowRenderingDistance)
Sets shadow rendering to use a directional light.