Check if texture has repeat enabled through code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VitusVeit

Hi, I’ve been making a custom class replicating the perspective warp effect using the polygon 2d’s uv system. The problem is that the code I wrote doesn’t work when the texture has the repeat property enabled.

I need a way to check through the code if the texture has the repeat flag enabled (mirrored too if possible) so that I can disable the uv changing step.

The docs mention the get_flags() function but don’t say how to use it. I’ve tried using it as an enum but it didn’t work:

if tex.get_flags() == tex.FLAG_REPEAT:
    #do stuff...
else:
    #do other stuff...

Does anybody know a way to do this?

:bust_in_silhouette: Reply From: Wakatta

OMG feel your struggle and walked down that same road
Unfortunately its not as simple as you’ve described since the flags are calculated together.

#example

 FLAG_REPEAT = 2
 FLAG_MIRRORED_REPEAT = 32

 var flags = get_flags(): # equals 34 for the above options

Wait it gets even worse as there are flag combinations that aren’t documented

#example 
FLAG_REPEAT + FLAG_MIPMAPS

now that you know how it works it should be easier to get what you want.

:bust_in_silhouette: Reply From: jgodfrey

You can use something like this to check if a given bit is set (or not) in a given value:

func is_bit_set(value, bit):
	return value & (1 << bit) != 0

In your case, the FLAG_REPEAT is documented to have a value of 2. So, that’d be bit position 2. So, to test for that, you can do:

var flags = tex.get_flags()
var is_set = is_bit_set(flags, 2)

Note, you need to pass the bit POSITION as the second argument, not the bit VALUE. So, for example, FLAG_FILTER is value4, so that’d be bit position 3.

And, to Wakatta’s point, it doesn’t really matter if complex bit combinations are set. The above will correctly check if any given bit is set in a specified value.

Dang forgot about bitwise operations.
Fantastic answer.

Wakatta | 2023-01-03 03:44

Yeah, I didn’t even think about using them. Anyway thanks for the answers, they really helped me!

VitusVeit | 2023-01-03 12:57

A few things here after waking up today with a working brain… :frowning:

First, it seems my bit positions are off by one in the above explanation. So, rather than the value of “1” being bit position “1”, it’s bit position “0” (and so on) - at least the way the provided method works.

Also, I realize that you’re really working with a bit VALUE (in the FLAG_* enums) rather than a bit POSITION. With that in mind, this can be simplified to this:

func is_bit_val_set(value, bit_value):
	return value & bit_value != 0

With that, you can simply pass the bit VALUE (rather than manually converting it to a bit POSITION - and running the chance of getting that wrong as I did above):

var flags = tex.get_flags()
var is_set = is_bit_val_set(flags, TEXTURE.FLAG_REPEAT)
 

jgodfrey | 2023-01-03 21:11