Santarh / MToon

Toon Shader with Unity Global Illumination

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

法線が反転する場合がある

Santarh opened this issue · comments

Projection Matrix が flip しているときに法線を反転するコードが、カメラに対して面が寝ているときに誤って反転させている場合がある

前に対策したこちらでうまくいかない場合がある
#16

再現
TransparentZWrite 両面 の Plane の裏面側を寝かせて見ると、Shade Lit どちらも見える

https://github.com/Santarh/MToon/blob/master/MToon/Resources/Shaders/MToonCore.cginc#L158

閾値が -0.1 になってるから。
0 でいいはずだけどなにか理由があった?

4cbf54f

おそらくこのときは viewDir が正規化されていないせいで変な閾値を設定してしまったのではないか

When using normal map, This issue continues to occur.

Problem

  • When camera rendering matrix is flipped, VFACE semantics of vertex shader input is flipped.
commented

Hi, @Santarh - I just had a user of my Godot shader port report this issue. I was able to verify that the issue also happen on Unity, and I found this issue.

After looking into the cause, I determined that this will happen if custom mesh normals are used, for example on the Face.

As an experiment, I used Abnormalize to customize the face normals on Alicia, and tweaked material settings to make this issue more obvious.
If I view Alicia from below, it shows this artifact on the face:
V shaped shadow artifact on top of the face when viewed from below

This bug can be fixed by commenting out the line on MToonCore.cginc line 165:

    worldNormal *= step(0, dot(worldView, worldNormal)) * 2 - 1; // flip if projection matrix is flipped

However, that creates a new problem: it would then be necessary to account for VFACE or SV_IsFrontFace to handle meshes set to "Cull Off".

@Santarh Regarding the problem you describe with VFACE and inverted camera rendering matrix, a fix for that issue exists on Unity 2017 and newer:

Until 18 June 2019, this was needed as a workaround in VRChat for a bug in Unity 2017.4.15f1 and below, causing normals to be flipped in mirrors.

The change to Unity's handling of VFACE is best described by VRChat here: https://docs.vrchat.com/docs/vrchat-201924

Attention shader authors: The Unity 2017.4.28f1 upgrade fixed a bug with Unity where SV_IsFrontFace/VFACE was wrong when viewed in cameras or mirrors. If you were compensating for this bug (by flipping face normals when in a mirror, for example), things will now be broken and lighting will not behave properly. You'll need to update your shaders!

  • For users, this means you might have to update your shaders on your avatar if things look off in mirrors.

Today, it is two years later. I think it would be nice to remove the Unity <=2017.4.15f1 hack from the MToon code and instead use VFACE.

Side note: I tested Hidden/VRM10/vrmc_materials_mtoon with Double Sided = On, and it shows that a bug is present, since "MToon 1.0" does not use this hack, and it also does not account for VFACE. I should probably file an issue on MToon 1.0 / UniVRM:
MToon 1.0 - Cull Off has wrong normals

Thanks your report.

I could not find this fix in the official release note of Unity... 😢
But I will try this.