godotgltfgodot4

Godot - Change GLTF Mesh Opacity


I'm relatively new to Godot. I've created an atlas texture in Krita, made some models in Blender, exported as gltf, and imported them into Godot.

Now, in Godot, how do I tweak the opacity of each separate instance of a model?

I'm trying to get some kind of animation of the opacity. I'd be fine doing it in code – it doesn't need to be through the editor.

I had originally hoped to animate the opacity in Blender with an animation, but it didn't seem like animated materials will import, since only the basic Principled BSDF shader is used.. (maybe that's not true?) But even if I can just tweak an "opacity" variable in code, that would fully suffice.

TIA!


Solution

  • I was finally able to figure it out. Here's what I did:

    var material:StandardMaterial3D = null
    
    
    func _ready() -> void:
        # 1. get the material off of the mesh, COPY it, and assign it back
        material = mesh.get_active_material(0).duplicate() as StandardMaterial3D
        # we can use override material to write it back:
        mesh.set_surface_override_material(0, material)
    
        # 2. turn TRANSPARENCY mode on
        material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
    
        # 3. now we can SET the opacity for this instance only via albedo_color.a
        material.albedo_color.a = 0.5 # from 0.0 (invisible) to 1.0 (opaque)
    

    Note, if you're able to enable resource_local_to_scene on the material resource, you can turn it on and then skip the copying in step 1. Because I'm importing a GLTF, these settings are all disabled.