godotengine / godot

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

There is still a transparency issue with setting ALPHA=1.0 in the shader script #92144

Open jianliang-li opened 3 months ago

jianliang-li commented 3 months ago

Tested versions

4.0.4, 4.2, 3.5

System information

macOS 14.4 (23E214) Godot 4.2, 4.0.4

Issue description

Setting ALPHA=1.0 in the shader script still has a transparent effect, just like setting the transparency of StandardMaterial3D to alpha or setting no depth test. But if the rendering mode of the shadow script adds depth_draw-always, setting ALPHA=1.0 can display normally, but it cannot produce shadow effects.

Steps to reproduce

In the project I uploaded: In the scene of node_3d_desk, the shader script has set ALPHA=1.0 and I do not want to see the tripod of the table.

Desk_normal has added StandardMaterial3D to the surfaceMaterialOverlay of Box024 without any changes. image

Desk_alpha added StandardMaterial3D to the SurfaceMaterialOverrides of Box024 and changed transparency to alpha. image

Desk_no_depth_test has been added to StandardMaterial3D and transparency has been changed to no_depth_test in the surface MaterialOverrides of Box 024. image

Desk_shaded_alpha added ShaderMaterial in the surfaceMaterialOverride of Box024, with only ALPHA=1.0 set in the script. image

In the scene of node_3d_sofa, there is also a similar problem. This problem occurs whenever the mesh has an irregular shape. image image image image

How can I set ALPHA=1.0 in the shader without expecting transparency or no_depth_test effects, and still display shadows normally.

When I add render_mode depth_draw_always in the shader script, it can display normally, but it cannot display shadows properly. If the following image is displayed: Shadow display in normal mode: image Display shadows after setting ALPHA=1.0 and adding render_mode depth_draw_always: image

Minimal reproduction project (MRP)

Test0520.zip

Calinou commented 3 months ago

This is expected, as setting ALPHA anywhere in the shader will force the material to go through the transparent pipeline. This is the case even if you only use ALPHA = 1.0.

I don't think we should have a special case for ALPHA = 1.0 not making the material go through the transparent pipeline. Could you describe why you want to write to ALPHA in the first place? Perhaps you can achieve the desired effect another way.

Khasehemwy commented 2 months ago

I had a similar problem, when using depth_draw_always, ALPHA=0.9 or less resulted in depth not being written, but ALPHA=0.99 did. (engine version 4.3.dev)

(a sphere in the scene, screen shows the view depth) image image

clayjohn commented 2 months ago

@Khasehemwy what you are seeing is a different issue from the OP.

In your case you are seeing a few different things:

  1. Because depth_prepass_alpha is being used, the object is rendered in the depth prepass. However, the depth prepass will only write out the depth for objects that have an alpha higher than 0.99. Accordingly, nothing is written in the depth prepass.
  2. When you read from the depth texture, you are actually reading a copy of the depth texture that was copied right after doing the depth prepass. Therefore, only objects caught by the depth prepass are included
  3. Because depth_draw_always is enabled, the depth is written during the transparent pass. You don't see the results in the depth texture, because the depth texture was copied before the transparent pass
Khasehemwy commented 2 months ago

@clayjohn Thank you! I found the changes needed to make the depth pre pass work for transparent objects. But does that mean there is no way to get the depth of a transparent object by depth texture in official version?

clayjohn commented 2 months ago

@clayjohn Thank you! I found the changes needed to make the depth pre pass work for transparent objects. But does that mean there is no way to get the depth of a transparent object by depth texture in official version?

When using hint_depth_texture that is correct. You can however get the depth texture after rendering transparent objects using a CompositorEffect in 4.3