godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.54k stars 20.69k forks source link

DirectionalLight3D shadows don't use the Shadow Max Distance property when the camera is in orthogonal mode (it uses the camera's Far distance instead) #58332

Open megalobyte opened 2 years ago

megalobyte commented 2 years ago

Godot version

499eec13a3

System information

Arch, Vukan, X11, NVIDIA GeForce GTX 1070/PCIe/SSE2

Issue description

Shadows don't appear to be rendered correctly when using an orthogonal perspective.

Orthogonal: Screenshot from 2022-02-19 19-05-44

Perspective: Screenshot from 2022-02-19 19-06-02

Scene uses a directional light.

Steps to reproduce

Any scene with a directional light and an orthogonal camera perspective should recreate the issue, but I've attached a project for convenience.

Minimal reproduction project

[Uploading minimal_project.zip…]()

Calinou commented 2 years ago

Judging by the screenshot, the shadows in orthogonal mode look correct to me. You should probably reduce Shadow Max Distance in the DirectionalLight3D node to increase their effective resolution – the default value is tuned for fairly large open worlds, not small-scale games.

Can you try following the same steps in 3.4.2 and see if shadows look better out of the box, with the same Shadow Max Distance value in both projects?

Also, the minimal reproduction project didn't upload (likely because you clicked away from the text field while it was still uploading). Try editing your post and upload it again.

megalobyte commented 2 years ago

You are right, except it was the camera's far property that I had to update. Is it too early to update the documentation? I know a lot of things are in flux with 4.0

Calinou commented 2 years ago

You are right, except it was the camera's far property that I had to update.

The Far property defaults to 4000 in master to support large open worlds out of the box.

The issue here is that DirectionalLight3D does not use the Shadow Max Distance property when the camera is in orthogonal mode, and uses the camera Far value as the maximum distance instead. Can you check whether this happens in 3.4.2 too?

Is it too early to update the documentation? I know a lot of things are in flux with 4.0

The documentation could be updated, but I feel like the core behavior mentioned above should be changed for 4.0.

megalobyte commented 2 years ago

In 3.4.2, I set the far value to 4000 and this is what I got: Screenshot from 2022-02-19 21-43-26

I should mention that the Max Shadow distance was the same value for 3.4.2 and 4.0

Calinou commented 2 years ago

In 3.4.2, I set the far value to 4000 and this is what I got: Screenshot from 2022-02-19 21-43-26

I should mention that the Max Shadow distance was the same value for 3.4.2 and 4.0

That roughly looks like the shadow resolution you'd get in master. You can compare it more easily by setting the directional shadow filter quality to Hard in the Project Settings.

megalobyte commented 2 years ago

About the same resolution, but doesn't have the same blend effect as 3.4.2 Screenshot from 2022-02-20 12-30-58

I'm guessing that's probably just a difference with how the rendering engine works?

Calinou commented 2 years ago

I'm guessing that's probably just a difference with how the rendering engine works?

3.4 uses PCF5 shadow filtering by default, while master uses an entirely different approach to shadow map filtering. To compare its shadow resolution with master, you have to set the shadow filter mode to Disabled in the Project Settings (in 3.4.2).

megalobyte commented 2 years ago

You are right again :)

Thank you for the help! I've learned a lot

Calinou commented 2 years ago

I can confirm this on master f4b0c7a1e and 3.x 3ba980379 (both GLES3 and GLES2).

master

The DirectionalLight3D's Shadow Max Distance is set to 100 on all screenshots.

Far = 4000 Far = 500 Far = 100
image image image

3.x

The DirectionalLight's Shadow Max Distance is set to 100 on all screenshots.

Far = 4000 Far = 500 Far = 100
image image image
clayjohn commented 1 year ago

It looks like shadow_max_distance has been ignored for a long time (well over 5 years https://github.com/godotengine/godot/commit/eedb39091aaa1dc0b8f204844bb1eb270b2349f6), so there must be something else causing the issue

Calinou commented 1 year ago

This appears to be done on purpose here: https://github.com/godotengine/godot/blob/251fb83d536a96110790bbf85b3fa50e5a8e2b7c/servers/rendering/renderer_scene_cull.cpp#L2084

If you remove the check, shadows will disappear unless you greatly increase Shadow Max Distance in the DirectionalLight3D properties (it must be at least 3,200 or so if the editor camera's Far property is 4,000).

golddotasksquestions commented 12 months ago

Many people are reporting Shadow Mesh LOD not working correctly when they try the "Your first 3D game" official tutorial:

https://www.reddit.com/r/godot/comments/170wqon/weird_shadows_in_godot_3d_tutorial/

There seems to be a relation to orthogonal camera setting

image

Calinou commented 12 months ago

@golddotasksquestions This is a different issue, which is tracked in https://github.com/godotengine/godot/issues/78422. You can work around the issue by disabling LOD generation on 3D scenes that have problematic shadows in the Import dock.