I understand that containers are responsible for positioning child nodes, but does that extend to scaling? Whenever I add my TextureRect with a custom scale value as a child to a container, it is just ignored. I can manage to get it to scale if I play an animation, but after the animation is done, anything that triggers the container to refresh makes the TextureRect instances revert their size. If this is intended, is there an established workflow to allow custom scaling?
Scale a TextureRect inside a Container
- Edited
Yes, its a container, it contains. Though texture rect is the one node I have had very mixed results with, so I just try to avoid it altogether.
What do you use instead of TextureRect? Is there a good way to say "treat this texture as if it were a certain size, even though it's not really - specifically when including it in a container?
- Edited
You could try using a color rect with a canvas shader on it.
shader_type canvas_item;
render_mode blend_mix;
uniform sampler2D texture_resource;
void fragment(){
COLOR = texture(texture_resource, UV);
}
Interesting, thanks I'll look into that
The thing I find confusing about the question is that you want to scale your node but you want to also have it in a container which will manage it's scale, since it is a container. What exactly are you trying to achieve? Or is it simply that you misunderstood the purpose of containers?
Might it be you are looking to have a container that only constrains the scale of your node in cases where the node might be scaling too large?
When you create a TextureRect, it has some inherent size which is based on the underlying size of the actual texture. But imagine you want the image displayed in-game to be larger or smaller... if you place the TextureRect inside a container, your only option seems to be to create a new texture (or revert to work-arounds using shaders, as you suggested, though I haven't verified if that works). This is potentially wasteful and/or time consuming. Obviously you could run into artifacts when scaling your textures up or down, but it seems that should be left up to the game dev to decide on thresholds that are acceptable. And FWIW my goal isn't to have "absolute control" over the size of the image or anything, I still want to benefit from the auto-management provided by containers, I simply want to be able to say "make that image a bit bigger/smaller overall" without having to actually resize the raw texture.
Does that make sense?
@mwag said:
Does that make sense?
So you are looking to use the texture rect because it can read the image dimensions and sets it's size accordingly, or the fact that it does that is what came as a surprise and you don't want that behavior? If the latter then my recommendation in regards to the ColorRect and the shader above should cover you well.
If however the TextureRect behaviour is desired then you are better off using a panel node or similar and coding your own container logic, but do not use the TextureRect inside of the pre-built containers. That is a world of hurt. At least that is my experience and recommendation thereof.
You can also :
- set the expand
property to true
- set the stretch_mode
to STRETCH_SCALE_ON_EXPAND
- set its min_value
to whatever you want it to be