0 votes

I did draw a toon outline but I wanted to it change its color to white when the ray cast collides, so it shows that the object can be selected

in Engine by (25 points)

2 Answers

+1 vote
Best answer

You didn't mention if you're working in 2D or 3D, so I'll assume 2D:

if $RayCast2D.is_colliding():
    var collider = $RayCast2D.get_collider()
    collider.material.set_shader_param("outline_color", Color(1., 1., 1., 1.))

That assumes your shader has a parameter outline_color defined:

uniform vec4 outline_color

that you're using to set the outlines color.

by (10,483 points)
selected by

I am using 3d.

I am using 3d.

Alright, then it's RayCast instead of RayCast2D - everything else stays the same.

i am getting this error:
Invalid get index 'material' (on base: 'StaticBody').

if ray.is_colliding():
    var interact = ray.get_collider()
    interact.material.set_shader_param("albedo", Color(255, 0, 0, 0))

i tried outline_color (created the uniform and changed ALBEDO = albedo.rgb) but also get the same error
my outline is in the next pass as a shader. just wanted to change its color of black to white in the runtime

i am getting this error: Invalid get index 'material' (on base: 'StaticBody').

That means the node your RayCast is colliding with (the collider you save into the variable interact) is a StaticBody. Which (unlike it's 2D-counterpart StaticBody2D) does not have the material-property you're trying to access.

So you need to adapt the path to your scene tree and the location of your shader material. If it's on a direct MeshInstance-child of interact it would be:

interact.get_node("MeshInstance").mesh.material
+1 vote

you should use a uniform variable.
For example:

uniform vec4 toon_outline : hint_color

remember to use hint_colour, because it will make setting shader params VERY EASY

by (23 points)
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 webmaster@godotengine.org with your username.