Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Wiggling texture on procedural mesh

Discussion in 'Shaders' started by MikeyMyk, Nov 27, 2012.

  1. MikeyMyk

    MikeyMyk

    Joined:
    Nov 20, 2012
    Posts:
    9
    Hi,

    I'm procedurally generating a mesh which is a long tri strip. As part of this I am generating UV's for a texture that is applied. The UVs run from 0.0 to 1.0 in both directions. The applied texture has a straight line along the centre of it. The results however have a clearly defined 'wiggle'.

    $wiggling2.jpg


    This is on PC and (I believe) I am setting the Shader Model to 3.0. I'm using a really simple shader to render the texture :-

    Code (csharp):
    1.  
    2. Shader "Custom/Diffuse Texture" {
    3.     Properties {
    4.       _MainTex ("Texture", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.       Tags { "RenderType" = "Opaque" }
    8.       CGPROGRAM
    9.       #pragma surface surf Lambert
    10.       #pragma target 3.0
    11.       struct Input {
    12.           float2 uv_MainTex;
    13.       };
    14.       sampler2D _MainTex;
    15.       void surf (Input IN, inout SurfaceOutput o) {
    16.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    17.       }
    18.       ENDCG
    19.     }
    20.     Fallback "Diffuse"
    21.   }
    22.  
    Does anyone know how I can change the texture interpolation to avoid the wiggle.:confused: I don't really want to subdivide the mesh any further. Grateful for any thoughts!

    Cheers!
     

    Attached Files:

    Last edited: Nov 28, 2012
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,865
    I dont think its a shader issue, check your uv mapping code again for funny float numbers.
     
  3. MikeyMyk

    MikeyMyk

    Joined:
    Nov 20, 2012
    Posts:
    9
    Thanks for the feedback aubergine. Unfortunately the UV's look solid.

    In the x axis, they are either 0.0 or 1.0. Along the y axis they linearly scale perfectly from 0.0 to 1.0. being exactly the same value either side of the strip.

    I.e.
    0: 0.0, 0.0
    1: 1.0, 0.0
    2: 0.0, 0.0625
    3: 1.0, 0.0625
    4: 0.0, 0.125
    5: 1.0, 0.125

    and so on...
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's hard to tell from your screen shot, but does each pair of vertices share the same Y value? It doesn't look like it, in which case they shouldn't be sharing V values.
     
  5. MikeyMyk

    MikeyMyk

    Joined:
    Nov 20, 2012
    Posts:
    9
    Yes, each pair of vertices does share the same Y value and hence does share the same V value. In the screen shot, you're correct, they do vary a little as the mesh is tracing the line of a spline but the issue is still present when everything is lined up and Y values are the same:(

    I'm pretty sure it's a texture interpolation issue which is exaggerated by the thin triangles:( If you look at a single "quad" then the centre of the texture is not at the centre of the "quad". There must be a way of correcting for it. This is a similar issue to non perspective correct texturing isn't it?:-|

    Thanks again!
     
  6. MikeyMyk

    MikeyMyk

    Joined:
    Nov 20, 2012
    Posts:
    9
    Here's another picture that might show the problem a little better:-

    $wiggling2.jpg

    Thanks!
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You're right. I think the fix here is actually to offset your vertices so they aren't in pairs, but instead alternate along each edge. Then your triangles will be symmetrical, and everything should match up.
     
  8. MikeyMyk

    MikeyMyk

    Joined:
    Nov 20, 2012
    Posts:
    9
    Yeah, that would probably avoid it and may indeed be the short term solution that I need to go for, so thanks for the idea.

    There must be a way to correct it in the shader though otherwise all meshes would exhibit this effect unless carefully constructed. It'd be like PlayStation 1 graphics all over again :D

    I'm really rusty on my graphics programming side :( Could it be due to the w component being written out incorrectly in the vertex shader (note, I'm not providing a vertex shader myself, I assuming a simple pass through one with correct w will be supplied) ?
     
  9. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    test it with a different shader, test it with the different texture to see how the extremities lineup, all the zeros should be on the left and all the ones should be on the right.
     
  10. g013

    g013

    Joined:
    Feb 15, 2014
    Posts:
    12
    I have the exact same problem and would be interested in a real solution to this as a generic case, probably involving tex2Dproj I imagine. I'm actually pretty amazed a simple obvious case like this fails on modern rasterizers.
     
  11. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    Might this not just be a driver or hardware issue? Never had such an issue, except for complex meshes which is normal. Looks like affine texture mapping but I have no much experience with this type of stuff.
     
    Last edited: Jan 1, 2017
  12. g013

    g013

    Joined:
    Feb 15, 2014
    Posts:
    12
    I don't think it's related to hardware or driver. IMO, the perspective correction is active and does its job - what we're seeing here makes sense if you consider each triangle individually. The problem here I think, is that the GPU is unaware that it's actually a quad, not just two triangles, which would result in a different (and correct in that case) UV interpolation. So we should make it aware of it, since there's no real quad nor polygon primitive.
     
  13. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    I would agree with @g013 here. This looks like what happens with any strip of non-uniform geometry with "straight" UVs. I suggest you look into affine texture correction. Perspective texture correction works as it should to correct these kinds of artifacts when it comes to perspective warped geometry, but there's no easy way to handle this case generically for arbitrarily warped geometry.
     
  14. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    You guys are right, I assumed that the shape was a perfect line but it doesn't seem to be.