Why do I need a texture in TextureRect to apply a shader?
Asked Answered
L

5

0

In the Screen Space Shaders example, TextureRect nodes are used to apply shaders to the scene. If the TextureRect doesn't have a texture the shader won't work.

I'm new to shaders. I'd like to know why do I need to put a texture in TextureRect and what it does if it adds something to the result. Apart from the fact that the shader doesn't work if the texture doesn't exist.

I'm starting to use shaders applied to 2D scenes and I forget to put the texture and the shader doesn't work until I realize.

Thanks.

Locality answered 6/11, 2022 at 11:50 Comment(0)
A
0

You don't have to use a TextureRect. If you don't need a texture, then you can use a ColorRect.

Agustinaah answered 6/11, 2022 at 12:3 Comment(0)
L
0

Agustinaah

Yes, it works and it's better. Thanks!

It doesn't answer the question though.I still feel like there's a void in my knowledge about how shaders are implemented.

Maybe some node that paints is required so the shader can replace it with their own calculations. If the node doesn't paint there's nothing to replace.

But then I wonder why a TextureRect is usually recommended even when it requires to load some random image.

Locality answered 6/11, 2022 at 12:25 Comment(0)
A
0

In general, shaders can only be applied to things that draw to the screen. I believe any class that inherits from CanvasItem can draw canvas_item shaders.

However, I'm not sure if any class will work, since the need to probably draw something first. For example, Node2D has properties like position, but does not draw by default.

Agustinaah answered 6/11, 2022 at 14:4 Comment(0)
L
0

Thanks for the answers.

Locality answered 6/11, 2022 at 15:5 Comment(0)
C
0

because a texturerect gets it's size from the texture IIRC. Just to answer the title question, as per it's wording. But as cybereality said, you can just as well use colorrect, which is how I'd usually go too.

Catchall answered 6/11, 2022 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.