SpatialMaterial.flags_use_shadow_to_opacity does not work on a mesh generated by SurfaceTool

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jean dupond

Hello everyone,

It’s my first time asking a question, so my apologies if it would be irrelevant.

I am creating triangles given an array of points (Vector3). I can properly visualize the resulting mesh. However, if I create a material and I activate flags_use_shadow_to_opacity to apply some transparency to the mesh, the result is that the whole mesh gets invisible.

this is how i am creating the material:

var material = SpatialMaterial.new()
material.params_cull_mode = SpatialMaterial.CULL_DISABLED
material.flags_use_shadow_to_opacity = true
material.albedo_color = Color.darkgray

Any ideas why?

Thanks!!

Does the mesh have valid normals and tangents? This may be required for shadow to opacity to work (I haven’t checked).

Calinou | 2023-03-02 17:55

Hello,

This is how i am generating the mesh:

func _draw_walls() -> void:
    var normal = Vector3(0, 0, -1)
    var material = SpatialMaterial.new()
    material.params_cull_mode = SpatialMaterial.CULL_DISABLED
    material.flags_use_shadow_to_opacity = true
    material.albedo_color = Color.darkgray

    for i in range(len(_fence_walls_points_) - 1):
        var pair = _fence_walls_points_[i]
        var pair_next = _fence_walls_points_[i+1]
        
        var wall = MeshInstance.new()
        var st = SurfaceTool.new()
        
        st.begin(Mesh.PRIMITIVE_TRIANGLE_FAN)
        
        # top left
        st.add_normal(normal)
        st.add_vertex(pair[1])
        
        # bottom left
        st.add_normal(normal)
        st.add_vertex(pair[0])
        
        # bottom right
        st.add_normal(normal)
        st.add_vertex(pair_next[0])

        # top right
        st.add_normal(normal)
        st.add_vertex(pair_next[1])
        
        # 1st triangle
        st.add_index(0)
        st.add_index(1)
        st.add_index(2)
        
        # 2nd triangle
        st.add_index(0)
        st.add_index(2)
        st.add_index(3)
        
        wall.material_override = material
        wall.mesh = st.commit()
        
        st.generate_normals()
        st.generate_tangents()

        FenceWallsNd.add_child(wall)

Basically i am looping over a few points, and for each 4 points i create a wall (made by 2 triangles). Maybe the order i do it is not the proper one, thats why i do not see the mesh when i apply the “flags_use_shadow_to_opacity”… With this code, if i do not apply that flag, i can see the meshes, but with full opacity.

jean dupond | 2023-03-03 07:47

AHA! I found out this: 3D - Spatial Material - 《Godot v3.3 Documentation》 - 书栈网 · BookStack

They explain that

Lighting modifies the alpha so shadowed areas are opaque and
non-shadowed areas are transparent.

And i realized i had a DirectionalLight in my scene. After removing it, the transparency is properly applied! At least i know where the problem is coming from.

What do you think?

jean dupond | 2023-03-03 08:12

Finally solved it by setting the walls meshes to another layer, so the DirectionalLight does not influence them. Thank you so much.

jean dupond | 2023-03-03 09:20