Home Technical Talk

UV Coordinates in Shaders

Hangs
null
Offline / Send Message
Hangs null
Hi everyone !

I have some problems understaning how custom UVs inside of a shader work. For example if i want to have a planar Mapping i do this in Unreal:


and i get this:

What is correct. But my point is that i dont understand why its working like this, and so i cant do more complex things with this method. It really would help me to know or understand the principle of UVs, i am coming more from the artist side and know about "normal" UV mapping and Textures.

Can someone explain this to me, or give me a link where it is explained so an artist can understand :D

Replies

  • Farfarer
    It should be fairly obvious by the naming...

    It takes the world position of the vertices - where they are relative to (0,0,0) in the scene. World position is useful here because it means it's consistent no matter how the mesh is moved, rotated or scaled.

    Then divides it by 500 (to scale the result up a bit - so that in the end, every in-game metre contains 5 repeats of the texture - that feels a bit unintuitive but remember that smaller UV coordinates means less of the texture, so it gets spread further).

    It then strips out only the G and B channels of that (which are equivalent to the Y and Z of the world position - RGB and XYZ are effectively the same thing for shaders).

    The G and B channels are then used as the UV coordinates.

    In effect, the vertex position on the Y and Z axes become the U and V coordinates for the texture (i.e. it's mapped as if you were looking straight down the scene's X axis).
  • Obscura
    Offline / Send Message
    Obscura grand marshal polycounter
    There is a function called world aligned texture. That would give you world space mapping.
  • Hangs
    Offline / Send Message
    Hangs null
    @Farfarer: Thank you for the explaination. So what i have to do if i would like to rotate the texture 45° around the projection axis or 45° around a would axis ?
  • Farfarer
    That gets trickier... I think once you've gotten the world position you need to rotate it's X and Y components and feed it back in to a new vector...

    newX = X * cos(45) - Y * sin(45)
    newY = Y * cos(45) + X * sin(45)
  • Hangs
    Offline / Send Message
    Hangs null
    @Farfarer : Thank you, that worked for the on axis rotation (see below for graph). Do you know any reference where this kind of math is explained for shader usage ?


    The Rotation values have to be between 0 and 2 to have one roatation.



  • Farfarer
    I just googled 2D rotation, heh. Google's your friend for this kind of stuff :D
  • Hangs
    Offline / Send Message
    Hangs null
    nah, what i mean is more like a collection whats possible and what kind of function/math you need for it and an explenation of it.
  • chronic
    Offline / Send Message
    chronic polycounter lvl 10
    your rotation math is in radians. you need to convert a user exposed value in deg to rad.
Sign In or Register to comment.