Search Unity

Resolved Perspective-Correct Texture Mapping

Discussion in 'General Graphics' started by creepercrack, Nov 25, 2020.

  1. creepercrack

    creepercrack

    Joined:
    Sep 6, 2018
    Posts:
    5
    I'm making a series of procedurals shapes with uv and texture, but unity doesnt correctly correct for the perspective. This is what it looks like:
    Completely wrong affine mapping:

    Half correct half wrong on the same object:
     
  2. Tartiflette

    Tartiflette

    Joined:
    Apr 10, 2015
    Posts:
    84
    The perspective is correct. What you are seeing is a very similar problem but Unity isn't doing anything wrong.
    Basically, if your quad is a rectangle in UV space and take the middle of your diagonal edge, it should be exactly in the middle of your UV rectangle. But in the 3D space you can see that it isn't quite in the middle of your trapeze. Maybe a solution would be to have an extra vertex in the middle (so your trapeze would be made of 4 triangles joined at the centre). You will most likely have to choose where that centre vertex lies in UV space, I don't know if it's necessarily the centre of your UV quad. But with a bit of trial and error, it might be enough to fix the UV/3D centre discrepancy.
     
  3. creepercrack

    creepercrack

    Joined:
    Sep 6, 2018
    Posts:
    5
    I've tried doing what you say in Blender, but it didn't. This is how it looks like:
    upload_2020-11-26_22-50-17.png
    The solution was to map the objects shape like this and create the texture accordingly (rough estimation):
     

    Attached Files:

    Last edited: Nov 26, 2020
  4. Tartiflette

    Tartiflette

    Joined:
    Apr 10, 2015
    Posts:
    84
    Yep, I guess I was wrong. I tried moving the centre vertex in 3d and UV space a bit, and you can never really get rid of the distortion completely.
    Of course the real solution is to have the UVs match the overall 3D shape like in your second example, and then create the texture accordingly, but this isn't always practical. Assuming your example is a road, you have to use a special texture just for that ring shape as opposed to using the same tileable road texture you could use on your straight road segments.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    What you're looking for is quadrilateral interpolation of the texture UVs. The problem is GPUs use triangle interpolation.

    It's possible with some work to correct the triangle interpolation back to quad interpolation, but it requires manually encoding data into the mesh vertices based on the original quads, it can't be done with a shader alone. And there aren't any existing tools to help with this.

    The easier solution is to add more tesselation to your mesh, minimizing the amount of distortion that can happen by making the triangles smaller.
     
  6. creepercrack

    creepercrack

    Joined:
    Sep 6, 2018
    Posts:
    5
    Thank you all for your replies