0 votes

I have a PackedScene that extends Node2D and I want to apply a shader over it. I tried using another Node2D and then I put a material shader on it, but it did not work.

in Engine by (682 points)

1 Answer

0 votes
Best answer

You can't apply a shader to a PackedScene, instead Godot allows you to change the shader of every instance of that scene.

In other words, change the shader just before the object is added to the game:

extends Spatial

export (PackedScene) var TheScene = null
export (ShaderMaterial) var TheNewShader = null

func _ready():

    if TheScene != null :
        var CloneOfScene  = TheScene.instance()
        #The scene I am using is Spatial with a MeshInstance as a child
        var TheMesh = CloneOfScene.get_node("MeshInstance") as MeshInstance

        #Now that I have a pointer to the mesh, I can change it's material
        if TheNewShader != null :
            #Set the new material to slot 0
            TheMesh.set_surface_material(0, TheNewShader)

        #Lastly add the now modified instance to the game
        self.add_child(CloneOfScene)
by (1,488 points)
selected by

I think it would work if I been working with 3D, but I'm currently working with 2D.

2D is easier. Because sprites can only have one material, they don't need any fancy checks.
You just use material = newMaterial

extends Node2D

export (PackedScene) var TheScene = null
export (ShaderMaterial) var TheNewShader = null

func _ready():
    if TheScene != null :
        var CloneOfScene  = TheScene.instance()
        #As a 2D scene I decided just to use a sprite, 
        #it has nothing else in the scene, if your scene
        # has some other things, you will need to find a path to it.
        if TheNewShader != null :
            #Sprites only have a one material so it is done like this
            CloneOfScene.material = TheNewShader
        #Lastly add the now modified instance to the game
        self.add_child(CloneOfScene)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.