Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Gradient Shading

Discussion in 'Shaders' started by Frostbite23, Jun 28, 2014.

  1. Frostbite23


    Mar 8, 2013
    Hi guys, for my game I would like to have gradient shading on my props and characters in my game. Ive been stuck on this for a while but how can I do this? basically i want to lerp 2 colors to make a gradient, and i want the gradient to be facing in the direction of the sun.
    heres an example, so from the look of the gradient it looks like the sun is at a 90 degree angle. 2014-06-28 13.31.40.png
    so right now the gradient is just a texture but i want a gradient made out of 2 colors and i want that gradient to face to the direction of the sun. Is this possible?

    EDIT: Ok managed to get a lerped gradient so now i need to figure out how to rotate the gradient to the direction of the light.

    P.S No surface shaders, this is all done in vertex/pixel shader code (modifying unitys sprite shader)

    EDIT: I got the shading to work but it only sticks to the texture so if i rotate it the shading is unaffected
    how can i fix this?
    Code (CSharp):
    1.             fixed4 frag(v2f v) : COLOR
    2.             {
    3.                 float3 light = normalize( );
    4.                 float4 tex = tex2D(_MainTex, v.texcoord);
    6.                 float3 col = lerp(LC2, LC1, dot(v.texcoord.xy, light));
    8.                 return half4((tex.rgb * (col) * Fallof) * v.color, tex.a);
    9.             }
    Last edited: Jun 28, 2014
  2. kebrus


    Oct 10, 2011
    You find the the color using the light position and the uvs, when you rotate it none of them change, thats why the shading stays

    since you want to effect created by the light that means you can't change the second term of the dot product so something has to change in the first. maybe you can use the object position and the uvs as an offset, or even better project the uvs to screenspace, or the vertices... i guess there are lots of different ways to solve it

    this in the vertex function will get you their positions in screenspace
    Code (csharp):
    1. o.screenPos = ComputeScreenPos(o.vertex);