Skip to content

Conversation

@x2048
Copy link
Contributor

@x2048 x2048 commented Nov 1, 2021

Fixes a number of problems with how shadows are applied to entities:

  • Bug with mod 'drawers' described in Shadow mapping breaks visuals in minetest-mod-drawers #11326
  • Colored shadows not being applied properly to entities
  • Different shader logic being used for entities and nodes
  • Light and color of entities being calculated and communicated differently from nodes
  • Entity surface not being shaded correctly when facing way from directional light

What I've done so far:

  • I've added code to assign SM texture to its texture layer when setting texture in content_cao.
  • Implemented the same light color contract for entities as for nodes (alpha communicating the balance between natural/artificial). The only difference is that emissiveColor is used as a factor to the vertex color.
  • Copied the SM logic from node shader and object shader.
  • Fixed the way colored shadows are applied at small falling angle (cos(alpha) < 0.035 for nodes and 0.14 for objects)

What's left

  • Fix the way split-normals are loaded, since it causes problems with shading (e.g. MCL2 chests, boats, wolves)
  • Calibrate offset bias to remove self-shadowing on dropped node stacks (vanilla Minetest)
  • Fix phantom shadows from entities (looks like a broken world transformation)
  • (Almost) Fix self-shadowing of banner entities in MCL2 (offsets?)

This PR is Ready for Review

How to test

  • Add 'drawers' mod
  • Place three drawers: one in a shadow, one on the edge and one in the sunlight.
  • Place node of sand into each of the drawers (right-click)
  • Drawers should display sand inventory image, properly shaded together with the drawer node itself.

Another test:

  • Place any entity (e.g. animals, chests and boats in MCL2).
  • Fly/walk over.
  • Observe your shadow rendered on the entity.

@x2048 x2048 marked this pull request as draft November 3, 2021 23:35
@x2048 x2048 changed the title Render shadows on entities. WIP Render shadows on entities. Nov 3, 2021
@x2048
Copy link
Contributor Author

x2048 commented Nov 4, 2021

Colored shadows are now correctly applied and shaded at small angles to the surface:
entity_shadow

@x2048
Copy link
Contributor Author

x2048 commented Nov 4, 2021

Problem with normals in certain b3d models:
image

@x2048
Copy link
Contributor Author

x2048 commented Nov 8, 2021

Normals look good with the latest change and minetest/irrlicht#77:

image

@Andrey2470T
Copy link
Contributor

I get the compile-time error when compiling the PR's branch (I use IrrlichtMt from the latest git commit):
Снимок экрана от 2022-01-01 17-25-31

@rollerozxa
Copy link
Member

rollerozxa commented Jan 1, 2022

That's caused by changes made in IrrlichtMt commit 52e03a that required fixes to the engine, and this PR is based off of an older commit of the engine which doesn't contain the fixes. Checkout dd09fd in IrrlichtMt and it should compile without issues (no difference in behavior anyways, it's just unused code removal).

@x2048 x2048 force-pushed the shadows_entities branch from c2be311 to fbd3cdf Compare January 1, 2022 22:46
@x2048
Copy link
Contributor Author

x2048 commented Jan 1, 2022

@Andrey2470T @rollerozxa I've rebased the branch to the latest master and checked that it compiles with the current tip of IrrlichtMt.

@lhofhansl
Copy link
Contributor

lhofhansl commented Feb 8, 2022

Tested that shadows affect entities and that entities are properly self-shading, including player entities. Pretty cool!

@lhofhansl
Copy link
Contributor

lhofhansl commented Feb 8, 2022

@x2048 What's left for this one? Looks good.
I have not observed "phantom" shadows. And MCL2 banners can be fixed in a different PR.
Skimmed the code, looks fine too.

Based on that, set "One Approval". I realize this is marked as WIP, but it seems to work pretty well.

@x2048
Copy link
Contributor Author

x2048 commented Feb 9, 2022

@lhofhansl It was long ago, and I need to review the code myself :)

Phantom shadows appeared as tiny spots at certain angles, when there were nearby entities. I'll look if I can reproduce it.

@lhofhansl
Copy link
Contributor

:)

I think this is needed to "complete" shadow functionality.
With blocks and entities working we can then focus on the API and perhaps on LSPSM.

@x2048 x2048 changed the title WIP Render shadows on entities. Render shadows on entities. Feb 12, 2022
@x2048 x2048 requested a review from lhofhansl February 12, 2022 07:13
@x2048 x2048 marked this pull request as ready for review February 12, 2022 07:13
@x2048
Copy link
Contributor Author

x2048 commented Feb 12, 2022

@lhofhansl I've narrowed the phantom shadows to a problem with materials in upright_sprite entities. The most noticeable change is that player has a shadow in devtest now.

I've checked that entities in devtest cast and accept shadows correctly, but extra testing would always help.

I've removed One Approval and requested a review for the new code when you have time.

Add compatibility with colored shadows.
Pass correct natural & artificial light to the shaders
Use natural/artificial light ratio for correct rendering of shadows
Detect non-zero normals which point in the opposite direction from the
face plane normal.
Fixes shadows of animated sprite entities
Avoid using read only materials in mesh scene node, as
it confuses shadow renderer.

if (PCFBOUND == 0.0) return 0.0;
// Return fast if sharp shadows are requested
if (PCFBOUND == 0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Looks like this a duplicate.

@lhofhansl
Copy link
Contributor

The soft-shadow stuff is still weird, and leads to strange artifacts. Also for a different PR - or maybe we just remove that code and/or replace it with something simpler (not depth dependent).

Then we can rethink #11944 and control both strength a softness via an API.

@lhofhansl
Copy link
Contributor

Hot air balloons are still weird:
screenshot_20220214_211853

And I still think that we do not have to hold this up for this visual glitch.

@lhofhansl
Copy link
Contributor

I do not think we have to wait with this one. If there are bugs, we can file additional PRs. Without this shadows are not visually complete.
Let's merge this one as is. @x2048 you have my +1, so feel free to merge (yours is the second implied +1).

@x2048
Copy link
Contributor Author

x2048 commented Feb 19, 2022

@lhofhansl There's one last change I have in the works while I am on it, and I'll move to merging.

One thing I noticed is that bias0 of 0.9 is very aggressive, causing significant undersampling of all shadows farther than rougly 10% of the shadow distance. Value of 0.8 gives a smoother curve and contributes to removing shadow acne.

@lhofhansl
Copy link
Contributor

Sounds good. Do you know what these artifacts are? (see the circle line in the the picture)

screenshot_20220219_104125

Remove use of magic constants.
Apply cameraOffset
Calculate distance projected on SM plane
@x2048
Copy link
Contributor Author

x2048 commented Feb 20, 2022

For the record, the texelSize expression in the shaders is the solution of the following equation for d:

where r is texture resolution, and x is distance between player and vertex projected on the shadow map.

@lhofhansl I have not seen the artifact before, but I guess it is related to the precision of perspective factor transformation, and the center of this circle is the center of the SM texture.

@lhofhansl
Copy link
Contributor

One thing I noticed since the last bias change are strange patterns on the player model:
screenshot_20220221_102930

Those stripes are not part of texture.

@x2048
Copy link
Contributor Author

x2048 commented Feb 21, 2022

This is because the normal bias is quite small right now, and PCF filter leaks shadow. I want to adjust perspective bias first, then normal bias, and then PCF filter radius, but not in this PR (and I'm well aware this looks like a regression).

@lhofhansl
Copy link
Contributor

@x2048 Cool. Let's merge this one and get it out of the way. :)

@x2048 x2048 merged commit b651bbf into luanti-org:master Mar 7, 2022
@x2048 x2048 deleted the shadows_entities branch March 7, 2022 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature ✨ PRs that add or enhance a feature Roadmap The change matches an item on the current roadmap Shaders Testing needed >= Two approvals ✅ ✅

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants