Function to get or set the emissive color in a fixed material

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bethleem
:warning: Old Version Published before Godot 3 was released.

I have a model a of a bulb light and made it emissive with the editor GUI. It glows green now even when a shadow comes over it. So far, so good. If I want to change it to glows red through code and not just creating another ball whit a red emissive color, how would I do that? I´ve reading the docs but didn´t find a function to get or set the emissive color in a fixed material.

Sorry if it´s a dumb question but I´m completely lost over it.

:bust_in_silhouette: Reply From: rredesigns

You will most probably have to access the shader info. You can create a new material for the same mesh and then just switch between those. There’s a set_material() method, and is described Here

:bust_in_silhouette: Reply From: eons

Get the mesh, then the material resource of the surface you want to modify, once you got the FixedMaterial use set_parameter to modify the parameter you want (check the numeric constants on docs).

http://docs.godotengine.org/en/stable/classes/class_fixedmaterial.html#class-fixedmaterial

Ok, I get it.
So I figured out how to access the mesh using
var ship = get_node(“barco”)
and how to create the new material I need
var new_material = FixedMaterial.new()
new_material.set_parameter(FixedMaterial.PARAM_EMISSION, Color(255,0,0))

Now, how I read the actual material from ship or asing it the new one, not a clue.
Tried with ship.surface_get_material(0) but throws an error"Attempt to call function ‘surface_get_material’ in base ‘null instance’ on a null instance."

bethleem | 2017-05-12 21:40

Mesh is a resource, I guess you have the MeshInstance.

Get the mesh resource from the MeshInstance node (get_mesh)
Then the corresponding FixedMaterial (if is fixed) from the mesh.

There you will have access to the material resource data.


To modify current material, should be:

mesh_instance = get_node("name_of_node")
mesh = mesh_instance.get_mesh()
material_on_surface_zero = mesh.surface_get_material(0)
material_on_surface_zero.set_parameter(FixedMaterial.PARAM_EMISSION, Color(255,0,0)) #if fixed

To set a material on a surface:

mesh.surface_set_material(0, new_material)

eons | 2017-05-12 23:17

:bust_in_silhouette: Reply From: bethleem

Ok, I figured it out. While trying to get to the FixedMaterial, I just get over and over the same error: “Attempt to call function ‘set_parameter’ in base ‘null instance’ on a null instance”. Don´t know if it´s a bug or me knowing nothing xD but I code a workaround to get the job done:

Instead of setting the textures, colors, etc in the regular FixedMaterial, wich I´m unable to access, I put all of them in the material override section of the mesh. Then:

    var material = get_material_override()
    material.set_parameter(material.PARAM_EMISSION, color)
    material.set_texture(material.PARAM_EMISSION,testura)
    material.set_texture(material.PARAM_DIFFUSE,barco)
    set_material_override(material)

And it works like a charm.