From 6d7ee48cee5c68b233fe74be388f2604254e4174 Mon Sep 17 00:00:00 2001 From: Evgeny Prikazchikov Date: Sat, 2 Dec 2023 08:31:33 +0300 Subject: [PATCH] Editor: Preview renders shadows with artifacts #621 --- engine/src/components/camera.cpp | 19 +++++++------------ engine/src/pipelinetasks/shadowmap.cpp | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/engine/src/components/camera.cpp b/engine/src/components/camera.cpp index 1290c6b6f..d78a26b1e 100644 --- a/engine/src/components/camera.cpp +++ b/engine/src/components/camera.cpp @@ -53,12 +53,9 @@ Matrix4 Camera::projectionMatrix() const { Returns result of transformation. */ Vector3 Camera::project(const Vector3 &worldSpace, const Matrix4 &modelView, const Matrix4 &projection) { - Vector4 in; - Vector4 out; - - in = Vector4(worldSpace.x, worldSpace.y, worldSpace.z, 1.0f); - out = modelView * in; - in = projection * out; + Vector4 in(worldSpace.x, worldSpace.y, worldSpace.z, 1.0f); + Vector4 out(modelView * in); + in = projection * out; if(in.w == 0.0f) { return Vector3(); // false; @@ -75,17 +72,15 @@ Vector3 Camera::project(const Vector3 &worldSpace, const Matrix4 &modelView, con Returns result of transformation. */ Vector3 Camera::unproject(const Vector3 &screenSpace, const Matrix4 &modelView, const Matrix4 &projection) { - Matrix4 final; - Vector4 in; - Vector4 out; - - final = (projection * modelView).inverse(); + Matrix4 final((projection * modelView).inverse()); + Vector4 in; in.x = (screenSpace.x) * 2.0f - 1.0f; in.y = (screenSpace.y) * 2.0f - 1.0f; in.z = 2.0f * screenSpace.z - 1.0f; in.w = 1.0f; - out = final * in; + + Vector4 out(final * in); if(out.w == 0.0f) { return Vector3(); // false diff --git a/engine/src/pipelinetasks/shadowmap.cpp b/engine/src/pipelinetasks/shadowmap.cpp index 97d6f7d12..3bff33dbd 100644 --- a/engine/src/pipelinetasks/shadowmap.cpp +++ b/engine/src/pipelinetasks/shadowmap.cpp @@ -96,9 +96,9 @@ void ShadowMap::areaLightUpdate(PipelineContext *context, AreaLight *light, list float zNear = 0.1f; float zFar = light->radius(); - Matrix4 crop = Matrix4::perspective(90.0f, 1.0f, zNear, zFar); + Matrix4 crop(Matrix4::perspective(90.0f, 1.0f, zNear, zFar)); - Matrix4 wt = t->worldTransform(); + Matrix4 wt(t->worldTransform()); Vector3 position(wt[12], wt[13], wt[14]); Matrix4 wp; @@ -149,16 +149,16 @@ void ShadowMap::areaLightUpdate(PipelineContext *context, AreaLight *light, list void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light, list &components, const Camera &camera) { CommandBuffer *buffer = context->buffer(); - Vector4 distance; float nearPlane = camera.nearPlane(); - Matrix4 p = buffer->projection(); + Matrix4 p(camera.projectionMatrix()); float split = SPLIT_WEIGHT; float farPlane = camera.farPlane(); float ratio = farPlane / nearPlane; + Vector4 distance; Vector4 planeDistance; for(int i = 0; i < MAX_LODS; i++) { float f = (i + 1) / static_cast(MAX_LODS); @@ -171,12 +171,12 @@ void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light, } Transform *lightTransform = light->transform(); - Quaternion lightRot = lightTransform->worldQuaternion(); - Matrix4 rot = Matrix4(lightRot.toMatrix()).inverse(); + Quaternion lightRot(lightTransform->worldQuaternion()); + Matrix4 rot(Matrix4(lightRot.toMatrix()).inverse()); Transform *cameraTransform = camera.transform(); - Vector3 cameraPos = cameraTransform->worldPosition(); - Quaternion cameraRot = cameraTransform->worldQuaternion(); + Vector3 cameraPos(cameraTransform->worldPosition()); + Quaternion cameraRot(cameraTransform->worldQuaternion()); bool orthographic = camera.orthographic(); float sigma = (camera.orthographic()) ? camera.orthoSize() : camera.fov(); @@ -263,7 +263,7 @@ void ShadowMap::pointLightUpdate(PipelineContext *context, PointLight *light, li float zNear = 0.1f; float zFar = light->attenuationRadius(); - Matrix4 crop = Matrix4::perspective(90.0f, 1.0f, zNear, zFar); + Matrix4 crop(Matrix4::perspective(90.0f, 1.0f, zNear, zFar)); Matrix4 wt(t->worldTransform()); Vector3 position(wt[12], wt[13], wt[14]); @@ -328,7 +328,7 @@ void ShadowMap::spotLightUpdate(PipelineContext *context, SpotLight *light, list float zNear = 0.1f; float zFar = light->attenuationDistance(); - Matrix4 crop = Matrix4::perspective(light->outerAngle() * 2.0f, 1.0f, zNear, zFar); + Matrix4 crop(Matrix4::perspective(light->outerAngle() * 2.0f, 1.0f, zNear, zFar)); int32_t x = 0; int32_t y = 0;