Search Unity

Lerp color between two vertices

Discussion in 'Shaders' started by Koru, Aug 23, 2016.

  1. Koru

    Koru

    Joined:
    Feb 14, 2012
    Posts:
    42
    Hi,
    I am fairly new to shaders so and started to work with some basics and even here I find first issues :D. I did a simple vertex-fragment shader that uses vertex colors. The problem is how color lerps between vertices in corners. As you can see on images below, longer edges doesn't look good. How to fix that?
    Screen Shot 2016-08-23 at 11.35.42.png Screen Shot 2016-08-23 at 11.35.08.png
     
  2. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    You can try flipping the triangle edges at the corners of your vertex colored patch.
    This will make it look more rounded or beveled. But that is the nature of vertex colors - they will only bing you so far as your mesh is tesselated.
    To break up the edge you could use modulated blending with a mask texture that further influences the shape.
     
  3. Koru

    Koru

    Joined:
    Feb 14, 2012
    Posts:
    42
    Hi Marco-Sperling, thanks for your answer. What do you exactly mean by flipping edges? Changing order of vertices?

    Plane's vertices create grid where I snap cubic objects. I change vertices' colors in runtime to create fake shadows under those cubics. As you can see on the screens, I put 3 objects creating 'L' shape. Do you think is it possible to use mask texture in my case?

    I find it odd, that edge triangles with 1 black and 2 white verts are more blackish and 1 white and 2 black verts are more whiteish. I would say it should be opposite?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I'm not sure what you're looking to get, all of those corners are exactly the same; any difference / brightening / darkening between the outer and inner corner is an optical illusion. Same thing happens with a hard edge corner gradient from photoshop or anything else for that matter.
    gradientcorner.png
     
  5. Koru

    Koru

    Joined:
    Feb 14, 2012
    Posts:
    42
    Hi bgolus,
    I didn't check color values before, you are right that those gradients are same. Is there any chance to change the way how those gradients are calculated between vertices? I am aiming more or less to this effect:
    shadow.png
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Not really, no. Gradients between vertices are linear gradients, always.

    You can add more vertices to make that rounded shape with, or do it with a texture.

    There are some complicated setups you can do using two channels of the vertex color to blend them together to make rounded corners, but it significantly complicates things. Basically you store a red and green as separate gradients that only overlap on the corners and multiply the two channels together. The result is a nice rounded gradient corner, but it's prone to breaking.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352