UV multiplication - why/how does it work for tiling?

Working thru “Godot 4 Shaders”, Common Techniques → Scrolling Background.

I don’t get why/how UV = UV * vec2(5.0, 5.0); tiles 5-times ?

As I understands UV is (just) vec2, representing XY coordinates into a texture.
So by multiplying a UV by 5.0 would we not fall out of the texture?

Does the UV wrap (i.e. 1.1 == 0.1) by only using the fraction part of float?

The .gd script example of what I’m wondering about:

var vTile = Vector2(5.0, 5.0)
print( Vector2(0.0, 0.0) * vTile )  # (0.0, 0)
print( Vector2(0.1, 0.0) * vTile )  # (0.5, 0)
print( Vector2(0.11, 0.0) * vTile ) # (0.55, 0)
print( Vector2(0.12, 0.0) * vTile ) # (0.6, 0)
print( Vector2(0.2, 0.0) * vTile )  # (1.0, 0)
print( Vector2(0.3, 0.0) * vTile )  # (1.5, 0) -> (0.5, 0) ?
print( Vector2(0.8, 0.0) * vTile )  # (4.0, 0) -> (1.0, 0) ?
print( Vector2(0.9, 0.0) * vTile )  # (4.5, 0) -> (0.5, 0) ?
print( Vector2(1.0, 0.0) * vTile )  # (5.0, 0) -> (1.0, 0) ?

This would explain it, but can’t find any confirmation …

Hi,

yes, your correct on that one with regards to the sampling UV wrapping as far as my understanding goes.

since we set the UV of the texture to be something like double. we end up with the visual square UV boundaries being from 0,0 to 2,2.

then its sampling the UV coordinates of the original texture ( which is from 0,0, to 1,1)

but, the magic of how this works is that we have repeat enabled, which does wrap the sampled UV of the original texture.

so on our larger UV numbers going to say 2.0, 2.0, it would sample along each axis of this and say and at point 1.1,1.1 since we have the repeat enabled on the texture it would map the originals 0,1,0.1 to the 1.1,1.1 UV of our visible texture on the screeen.

without the texture wrap enabled, after the original texture UV boundaries of 1,1 are reached, it would just display the last edge color.

ill apologise now, that sounded better in my head before i typed it, but your spot on thinking that the original UV sampling wraps, and that its down to the texture having its repeat mode set to enabled.

hope that helps a little,

Darren

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms