Search Unity

Shadergraph gradient alpha on y-axis

Discussion in 'Shaders' started by Sparkmasterflex, Jan 19, 2020.

  1. Sparkmasterflex

    Sparkmasterflex

    Joined:
    May 6, 2019
    Posts:
    1
    Hello all! I'm just learning how to use the shadergraph and I'm having a heck of a time trying to sort out what I thought would be relatively uncomplicated.

    I'm trying to have a shader/material to add to a cube that has a color and a gradient (solid-ish to transparent) on the Y-axis.

    I'm guessing that I need to have something set to world space so the the y-axis is consistent on all the side, but that I really am not that familiar with all this yet.

    I currently have a Texture2D of a gray scale gradient and uv into a SampleTexture2d with RGBA going into the Albedo and A(1) going into Alpha and my cube looks like there's a gradient going left to right on each side of my cube and absolutely no transparency at all. (I do have the PBR surface set to "transparent")

    I really don't know where to go from here or even if this is the correct start. If anyone knows how to accomplish this I'd greatly appreciate it.

    Thanks
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Does your gradient texture actually have a Gradient alpha channel as well?

    But regardless, you don't even need to sample a texture for that. You can use the Position node, grab its .y value and plug it into the Alpha. Also multiply the .y value by the color you want your gradient and plug it into Albedo.

    This approach means you can still have proper base UVs for other texturing, but... it has its issues to workaround:
    • Object space positions of the mesh are both positive and negative relative to the object's pivot point, anything <=0 would just result in black for the gradient...
    • The positions can be greater or less than the -1 to 1 range depending on how its modeled, meaning there's no consistent value to scale the y-position by into a 0-1 gradient range.
    • The positive or the negative side may have a greater range than the other if the object doesn't have a centered pivot, further complicating the issue.
    • Requires unique material per differently modeled (scaling doesn't matter) dimension mesh.
    To solve those issues would be to make a Vector2 property on the shader that you can set the vertical bounds and bottom (absolute) extents of the mesh to, then you can add this extents value to the position.y in the shader and divide by the vertical bounds to get a 0-1 gradient going from bottom to top of the mesh.

    You can simplify all this if all you care about is having a vertical gradient starting from say, world height 0 and grading up to a certain height as a global gradient for all using the shader, no values need to be set per-mesh for the material then since it would just be dividing worldPos.y of vertex by that world space height.

    Another route would be to make sure your UVs for your mesh are set up so that your texture gradient does actually run vertically up the mesh, this is something you'd do in your modeling program's UV editor.
    Though once you do this, you don't actually need to sample a texture, you'd be able to just make a UV node and grab the .y value of that and do the plugging into Alpha, and plugging multiplied by color into Albedo.
    Also, with this method you can simply have a secondary UV channel that is laid out this way if you still want to use your primary UV channel for other textures.

    A third option would be to simply write your gradient to the Vertex Colors of your mesh, which you should be able to do in your modeling program, though some don't have the option. This would be one of the most efficient and simplest methods and not require unique materials per mesh. And you can grab the color from the Vertex Color node in ShaderGraph. Your colors will naturally interpolate from one vertex to another, giving you a gradient between them.
     
    Last edited: Jan 19, 2020
  3. Jointy87

    Jointy87

    Joined:
    Dec 19, 2019
    Posts:
    21
    A bit late with replying, but for anybody else looking for how to do this, this worked for me:

    Follow the steps in this video (very well explained):


    Swap out the 2 color values for 2 alpha Vector1's (top alpha, bottom alpha). Set the clamp max before the lerp to 2 (for some reason) and plug it all into the Alpha slot of the PBR Master node. Make sure your surface is set to 'Transparent' (click the cogwheel on the PBR Master node to do that). Now play around with the values. For me top alpha of 0.5 and bottom alpha of 0 works great. Not sure if this is the most efficient way to do it but works.

    Cheers!