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() );
54 mMaterial->addParameter( mColorTextureParameter );
55 mMaterial->addParameter( mDepthTextureParameter );
56 mMaterial->addParameter( mAmbientOcclusionTextureParameter );
58 QList<Qt3DRender::QParameter *> globalShadowParams;
59 mShadowMapParameter =
new Qt3DRender::QParameter( u
"shadowTexture"_s, shadowRenderView.
mapTextureArray() );
60 globalShadowParams << mShadowMapParameter;
66 mLightCameras[i] = shadowRenderView.
lightCamera( i );
72 mCsmMatricesParameter =
new Qt3DRender::QParameter( QString(
"csmMatrices[0]" ), csmMatrices );
73 globalShadowParams << mCsmMatricesParameter;
74 mCsmBoundsMatricesParameter =
new Qt3DRender::QParameter( QString(
"csmBoundsMatrices[0]" ), csmMatrices );
75 globalShadowParams << mCsmBoundsMatricesParameter;
76 mMaxShadowDistanceParameter =
new Qt3DRender::QParameter( u
"maxShadowDistance"_s, QVariant::fromValue( 0.0f ) );
77 globalShadowParams << mMaxShadowDistanceParameter;
79 mFarPlaneParameter =
new Qt3DRender::QParameter( u
"farPlane"_s, mMainCamera->farPlane() );
80 mMaterial->addParameter( mFarPlaneParameter );
81 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&](
float farPlane ) { mFarPlaneParameter->setValue( farPlane ); } );
82 mNearPlaneParameter =
new Qt3DRender::QParameter( u
"nearPlane"_s, mMainCamera->nearPlane() );
83 mMaterial->addParameter( mNearPlaneParameter );
84 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane ) { mNearPlaneParameter->setValue( nearPlane ); } );
86 mMainCameraInvViewMatrixParameter =
new Qt3DRender::QParameter( u
"invertedCameraView"_s, mMainCamera->viewMatrix().inverted() );
87 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
88 mMainCameraInvProjMatrixParameter =
new Qt3DRender::QParameter( u
"invertedCameraProj"_s, mMainCamera->projectionMatrix().inverted() );
89 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
90 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&](
const QMatrix4x4 &projectionMatrix ) {
91 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
93 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]() { mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() ); } );
95 mRenderShadowsParameter =
new Qt3DRender::QParameter( u
"renderShadows"_s, QVariant::fromValue( 0 ) );
96 globalShadowParams << mRenderShadowsParameter;
97 mShadowLightIndexParameter =
new Qt3DRender::QParameter( u
"shadowLightIndex"_s, QVariant::fromValue( 0 ) );
98 globalShadowParams << mShadowLightIndexParameter;
99 mShadowBiasParameter =
new Qt3DRender::QParameter( u
"shadowBias"_s, QVariant::fromValue( 0.00001f ) );
100 globalShadowParams << mShadowBiasParameter;
104 mEyeDomeLightingEnabledParameter =
new Qt3DRender::QParameter( u
"edlEnabled"_s, QVariant::fromValue( 0 ) );
105 mEyeDomeLightingStrengthParameter =
new Qt3DRender::QParameter( u
"edlStrength"_s, QVariant::fromValue( 1000.0f ) );
106 mEyeDomeLightingDistanceParameter =
new Qt3DRender::QParameter( u
"edlDistance"_s, QVariant::fromValue( 2.0f ) );
107 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
108 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
109 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
111 mAmbientOcclusionEnabledParameter =
new Qt3DRender::QParameter( u
"ssaoEnabled"_s, QVariant::fromValue( 0 ) );
112 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
114 mBloomTextureParameter =
new Qt3DRender::QParameter( u
"bloomTexture"_s, bloomRenderView.
bloomTexture() );
115 mMaterial->addParameter( mBloomTextureParameter );
117 mBloomEnabledParameter =
new Qt3DRender::QParameter( u
"bloomEnabled"_s, QVariant::fromValue( 1 ) );
118 mMaterial->addParameter( mBloomEnabledParameter );
120 mBloomFactorParameter =
new Qt3DRender::QParameter( u
"bloomFactor"_s, 0.05 );
121 mMaterial->addParameter( mBloomFactorParameter );
123 mExposureParameter =
new Qt3DRender::QParameter( u
"exposureAdjustment"_s, 0.0f );
124 mMaterial->addParameter( mExposureParameter );
125 mToneMappingParameter =
new Qt3DRender::QParameter( u
"toneMapping"_s, 1 );
126 mMaterial->addParameter( mToneMappingParameter );
128 const QString vertexShaderPath = u
"qrc:/shaders/postprocess.vert"_s;
129 const QString fragmentShaderPath = u
"qrc:/shaders/postprocess.frag"_s;
131 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
133 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) );
135 mShader->setFragmentShaderCode( finalFragmentShaderCode );
153 mShadowMapResolution = size;
158 const QVector3D lightDirection = lightDir.
toVector3D().normalized();
161 const float mainCameraNearPlane = mMainCamera->nearPlane();
163 const float mainCameraFarPlane = std::min( mMainCamera->farPlane(), maximumShadowRenderingDistance );
169 constexpr float PRACTICAL_SPLIT_SCHEME_LAMBDA = 0.9f;
172 const QMatrix4x4 invertedCameraView = mMainCamera->viewMatrix().inverted();
173 const float cameraFov = mMainCamera->fieldOfView();
174 const float cameraAspect = mMainCamera->aspectRatio();
176 const float shadowMapResolution =
static_cast< float >( mShadowMapResolution );
190 const float zNear = cascadeSplits[i];
191 const float zFar = cascadeSplits[i + 1];
194 QVector3D worldFrustumCorners[8];
195 QVector3D worldFrustrumCenter;
199 float rawRadius = 0.0f;
200 for (
int j = 0; j < 8; ++j )
202 rawRadius = std::max( rawRadius, ( worldFrustumCorners[j] - worldFrustrumCenter ).length() );
207 const float stepSize = std::max( std::pow( 2.0f, std::floor( std::log2( rawRadius ) ) - 4.0f ), 0.01f );
208 const float radius = std::ceil( rawRadius / stepSize ) * stepSize;
211 QMatrix4x4 lightRotation;
212 lightRotation.lookAt( QVector3D( 0, 0, 0 ), lightDirection, up );
213 QVector3D centerLightSpace = lightRotation * worldFrustrumCenter;
217 const float worldUnitsPerTexel = ( 2.0f * radius ) / shadowMapResolution;
219 centerLightSpace.setX( std::floor( centerLightSpace.x() / worldUnitsPerTexel ) * worldUnitsPerTexel );
220 centerLightSpace.setY( std::floor( centerLightSpace.y() / worldUnitsPerTexel ) * worldUnitsPerTexel );
221 const QVector3D snappedWorldCenter = lightRotation.inverted() * centerLightSpace;
224 QMatrix4x4 lightView;
225 const QVector3D lightPos = snappedWorldCenter - ( lightDirection * radius );
226 lightView.lookAt( lightPos, snappedWorldCenter, up );
229 mLightCameras[i]->setPosition( lightPos );
230 mLightCameras[i]->setViewCenter( snappedWorldCenter );
231 mLightCameras[i]->setUpVector( up );
233 float lightCameraLeft = -radius;
234 float lightCameraRight = radius;
235 float lightCameraBottom = -radius;
236 float lightCameraTop = radius;
238 float lightCameraNearPlane = -radius;
239 float lightCameraFarPlane = radius * 2.0f;
241 QMatrix4x4 orthoBoundsMatrix;
242 orthoBoundsMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
243 csmBoundsMatrices[i] = QVariant::fromValue( orthoBoundsMatrix * lightView );
248 constexpr float NEAR_PLANE_RETREAT = 5000.0f;
249 lightCameraNearPlane -= NEAR_PLANE_RETREAT;
252 mLightCameras[i]->lens()->setOrthographicProjection( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
255 QMatrix4x4 orthoMatrix;
256 orthoMatrix.ortho( lightCameraLeft, lightCameraRight, lightCameraBottom, lightCameraTop, lightCameraNearPlane, lightCameraFarPlane );
257 csmMatrices[i] = QVariant::fromValue( orthoMatrix * lightView );
260 mCsmMatricesParameter->setValue( csmMatrices );
261 mCsmBoundsMatricesParameter->setValue( csmBoundsMatrices );
262 mMaxShadowDistanceParameter->setValue( mainCameraFarPlane );
void updateShadowSettings(const QgsShadowSettings &shadowSettings, const QgsVector3D &lightDir, int size, int globalLightIndex)
Sets shadow rendering to use a directional light.