Code for change emissive color?

bethleembethleem Posts: 6Member

I´m more an artist than a coder, but I have managed to use tools as AGK quite well. Godot is giving me a terrible headache for the lack of examples to learn from.

Now, 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.


  • MegalomaniakMegalomaniak Posts: 749Admin
    edited May 2017 Answer ✓

    What you are looking for are likely get_parameter and set_parameter in FixedMaterial.

    And the same basically applies for ShaderMaterial as well(edit: set_shader_param), IIRC I might have posted an example file for ShaderMaterial back when there was a previous forum software installed here so it might be gone now.

    Also I don't know what AGK is so that doesn't tell me anything, after a quick google I'm guessing you meant AppGameKit though.

  • bethleembethleem Posts: 6Member

    Yep, AGK stands for AppGameKit.
    Thanks, those are the functions I need. :)
    Now I only have to figure out how to reach the material in the mesh. So easy if the docs would have any example, and so criptic the way it is. I think it´s one of the reasons why people don´t get to use godot.

  • MegalomaniakMegalomaniak Posts: 749Admin
    edited May 2017 Answer ✓

    script attached to a MeshInstance node:

    extends MeshInstance
    export(Color) var new_color = Color(1, 1, 1, 1)
    func _ready():
        self.get_mesh().surface_get_material(0).set_parameter(3, new_color)

    And the material is attached to the mesh referenced, not the MeshInstance node.

  • bethleembethleem Posts: 6Member

    I tried your code and it only returns "Attempt to call function 'set_parameter' in base 'null instance' on a null instance.", so the material its not really accessed.
    It´s the same message I got on every attempt I made, and I have tried a few.
    I´m about to throw godot away for good. :_(

  • MegalomaniakMegalomaniak Posts: 749Admin
    edited May 2017

    Can you share your scene/example file?

    Sounds like you aren't dealing with a mesh instance, so need to get the correct node to access/set the parameter.

  • bethleembethleem Posts: 6Member
    Answer ✓

    Ok, I gave it a last try, out of rage. xD

    I still get the same error, but I figured out a workaround to get the work 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)

    And it works like a charm.

  • MegalomaniakMegalomaniak Posts: 749Admin

    Right, it can be kind of confusing that there is 2 ways to hook up a material, however I'm still curious what your scene might have been like, eg. what node the script is attached to and what was parented to it, etc.

  • SchusterSchuster Posts: 260Member

    Thanks..this is what I needed too...but is there the possibility of slowly crossover(fade), change from color1 to color2?

  • SchusterSchuster Posts: 260Member

    I got it!...Here's the script for changing colors...what I put together for those who could need it
    ....i don't know if it's all OK but it works :)

    extends MeshInstance
    var hue_timer = 0
    var speed = 60
    var color_count = 3.0
    export (Color) var color1
    export (Color) var color2 
    export (Color) var color3
    var mesh 
    const COLOR_TIME = 10.0
    const PARAM_EMISSION = 3
    #const PARAM_GLOW = 5
    func _ready():
        mesh = get_node(".")
        self.get_mesh().surface_get_material(0).set_parameter(3, color1)
    func _process(delta):
        color_count += delta
        hue_timer = fmod(hue_timer + delta * speed, 360)
        var h = hue_timer / 360
        if color_count > COLOR_TIME:
            self.get_mesh().surface_get_material(0).set_parameter(3, color2)
            color_count = 0
            color2.h = h
            self.get_mesh().surface_get_material(0).set_parameter(3, color3)
            color_count = 3
            color3.h = h

  • MegalomaniakMegalomaniak Posts: 749Admin
    edited June 2017

    vec_type mix ( vec_type a,vec_type b, float c ) Linear Interpolate
    float c is what you want to control with a uniform value of type float if using shader material.

    otherwise perhaps create multiple color variables and try lerping over time between them in script and feed the result per frame to the parameter you want to control? edit: @Bishop yeah, didn't look past your first post. :blush:

Sign In or Register to comment.