Logan Brunner
Logan Brunner

Reputation: 11

How do you add unique instances of a shader to multiple nodes in a scene? Godot 4

I have multiple "interactable" objects in my game all extend the InteractableBody class that sets some stuff up so the player can interact with the object. I am trying to add an outline shader to the sprite so I can outline objects that are able to be interacted with. This is my code right now that does this. However, it currently just applies the outline to the first object with the shader, no matter what object. I had a previous version working where all objects had the outline and were all being changed when only one object was being updated.

This is my current code for applying the shader:

class_name InteractableBody
extends CharacterBody2D

@export var string_name : String
@export var icon : Texture2D
@export var interact_range : float
@export var interactable_sprite : Sprite2D
@export var outline_shader : Shader

var player_node : Player = null

func _ready():
    add_to_group("interactable")
    var material_instance = ShaderMaterial.new()
    var new_shader = outline_shader
    material_instance.shader = new_shader
    interactable_sprite.set_material(material_instance)
    hide_outline()

func setup_player(_player_node):
    if _player_node:
        player_node = _player_node

func _process(delta):
    if player_node:
        if player_node.global_position.distance_to(global_position) < interact_range:
            if player_node.interactable_objects.has(self) == false:
                show_outline()
                player_node.add_interactable(self)
        else:
            if player_node.interactable_objects.has(self) == true:
                hide_outline()
                player_node.remove_interactable(self)

func show_outline():
    var shader_mat : ShaderMaterial = interactable_sprite.material
    shader_mat.set_shader_parameter("color", Color(1.0, 1.0, 1.0, 1.0))

func hide_outline():
    var shader_mat : ShaderMaterial = interactable_sprite.material
    shader_mat.set_shader_parameter("color", Color(1.0, 1.0, 1.0, 0.0))

I tried to apply an outline shader to each object in a group and then be able to turn the shader on/off on each node one at a time. What actually is happening is only the first node with the shader has an outline and that outline is being updated no matter what node I am interacting with.

Upvotes: 1

Views: 383

Answers (1)

user2635469
user2635469

Reputation: 77

Here are some solutions, which may work depending on your specific setup:

  1. Instead of setting the shader material, create a uniform within the shader to set the outline's color. Then you can change the shader's uniform. This might be the solution you are looking for because you are trying to change the shader itself instead of the node you are applying it to.
  2. Create a second object whose only function is to hold the outline. Then, hide it or show it depending on your needs.
  3. If you insist on actually changing the shader, you can duplicate your shader in your ready function to make it unique. Then when you change it later this should not affect the other copies of the shader.

Upvotes: 0

Related Questions