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 const QString vertexShaderPath = u
"qrc:/shaders/postprocess.vert"_s;
123 const QString fragmentShaderPath = u
"qrc:/shaders/postprocess.frag"_s;
125 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
127 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) );
129 mShader->setFragmentShaderCode( finalFragmentShaderCode );
145 const float mainCameraNearPlane = mMainCamera->nearPlane();
147 const float mainCameraFarPlane = std::min( mMainCamera->farPlane(), maximumShadowRenderingDistance );
153 constexpr float PRACTICAL_SPLIT_SCHEME_LAMBDA = 0.9f;
156 const QMatrix4x4 invertedCameraView = mMainCamera->viewMatrix().inverted();
157 const float cameraFov = mMainCamera->fieldOfView();
158 const float cameraAspect = mMainCamera->aspectRatio();
160 const float shadowMapResolution =
static_cast< float >( mShadowMapResolution );
174 const float zNear = cascadeSplits[i];
175 const float zFar = cascadeSplits[i + 1];
178 QVector3D worldFrustumCorners[8];
179 QVector3D worldFrustrumCenter;
183 float rawRadius = 0.0f;
184 for (
int j = 0; j < 8; ++j )
186 rawRadius = std::max( rawRadius, ( worldFrustumCorners[j] - worldFrustrumCenter ).length() );
191 const float stepSize = std::max( std::pow( 2.0f, std::floor( std::log2( rawRadius ) ) - 4.0f ), 0.01f );
192 const float radius = std::ceil( rawRadius / stepSize ) * stepSize;
195 QMatrix4x4 lightRotation;
196 lightRotation.lookAt( QVector3D( 0, 0, 0 ), lightDirection, up );
197 QVector3D centerLightSpace = lightRotation * worldFrustrumCenter;
201 const float worldUnitsPerTexel = ( 2.0f * radius ) / shadowMapResolution;
203 centerLightSpace.setX( std::floor( centerLightSpace.x() / worldUnitsPerTexel ) * worldUnitsPerTexel );
204 centerLightSpace.setY( std::floor( centerLightSpace.y() / worldUnitsPerTexel ) * worldUnitsPerTexel );
205 const QVector3D snappedWorldCenter = lightRotation.inverted() * centerLightSpace;
208 QMatrix4x4 lightView;
209 const QVector3D lightPos = snappedWorldCenter - ( lightDirection * radius );
210 lightView.lookAt( lightPos, snappedWorldCenter, up );
213 mLightCameras[i]->setPosition( lightPos );
214 mLightCameras[i]->setViewCenter( snappedWorldCenter );
215 mLightCameras[i]->setUpVector( up );
217 float lightCameraLeft = -radius;
218 float lightCameraRight = radius;
219 float lightCameraBottom = -radius;
220 float lightCameraTop = radius;
222 float lightCameraNearPlane = -radius;
223 float lightCameraFarPlane = radius * 2.0f;
225 QMatrix4x4 orthoBoundsMatrix;
226 orthoBoundsMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
227 csmBoundsMatrices[i] = QVariant::fromValue( orthoBoundsMatrix * lightView );
232 constexpr float NEAR_PLANE_RETREAT = 5000.0f;
233 lightCameraNearPlane -= NEAR_PLANE_RETREAT;
236 mLightCameras[i]->lens()->setOrthographicProjection( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
239 QMatrix4x4 orthoMatrix;
240 orthoMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
241 csmMatrices[i] = QVariant::fromValue( orthoMatrix * lightView );
244 mCsmMatricesParameter->setValue( csmMatrices );
245 mCsmBoundsMatricesParameter->setValue( csmBoundsMatrices );
246 mMaxShadowDistanceParameter->setValue( mainCameraFarPlane );
void updateShadowSettings(const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance)
Sets shadow rendering to use a directional light.