Hi, I’ve been playing around with Godot to learn some game development skills, but I’ve run into a problem. I’m trying to display some 2d “cards” on the screen, and their texture is being generated by a shader. However, each card looks exactly the same, where I’d like some variation between the cards.

So I passed a seed value into my shader using:

instance uniform float seed;

And in my GDScript code:

func _ready() -> void:
    color_rect.material.set_shader_parameter("seed", randf() )

I’ve also tried:

func _ready() -> void:
    color_rect.set_instance_shader_parameter("seed", randf() )

Nothing breaks, but each of my cards are still identical, as if the seed value is the same for all three cards. From what I’ve read, the ‘instance’ keyword should let me pass a unique seed value for each object using the shader, right?

  • copygirl@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    2 days ago

    color_rect.set_instance_shader_parameter should work, as long as the color_rect is unique to each instance of the card, rather than being reused. If you assigned it in the node editor you could check the “Local To Scene” checkbox. Assuming cards are defined as separate scenes you instantiate. Otherwise you could duplicate() the resource in code.

    The same would be true for color_rect.material.set_shader_parameter for the ShaderMaterial. If you could ensure it’s unique per card, it should work. However, duplicating the material might not be as ideal, especially since the other option is also available.

    A little tip: You could try print debugging with the get functions before and after you use set to see what the value was before (and notice that it might be the same as the previous set call from another card) and that it was correctly set afterwards. (Assuming I’m correct about diagnosing the problem.)

    edit: Actually, my advice is only true if color_rect was a resource, but I think it’s a node, specifically a sub-class of CanvasItem or GeometryInstance3D, so it should already be unique for each instantiated card. So without more information I’m not sure why set_instance_shader_parameter is not working.