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. Dismiss Notice

Bug Color based on height using a gradient in shader graph.

Discussion in 'Shader Graph' started by JonteErik, Jul 3, 2023.

  1. JonteErik

    JonteErik

    Joined:
    Jan 29, 2022
    Posts:
    5
    So Im making a shader that changes color of an object based on height. To do this im using a gradient, since i can use many colors. It works just fine until i set the color furthest to the right of the gradient, that color takes up about half of the object regardless of how much i set it to on the gradient. Any tips on how I can fix that? The picture shown is the shader graph that has the issue.
     
  2. MixxusStudio

    MixxusStudio

    Joined:
    May 9, 2014
    Posts:
    44
    Can't see the picture but what comes to my mind is some 0-1 issue. Try checking your values. Are you saying that you sometimes sets the color to a color outside the range of the gradient and then that color takes up half the object? Other problems that can arise is conversion linear, gamma but that's not my first guess here.
     
  3. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    702
    I cant see the pic, but keep in mind height is usually only 1 unit high. Everything after that is 1. So if your height is higher than 1, you need to normalize it. (Divide by the total height or maximum height)

    The gradient also only maps from 0 - 1. So anything below zero you would have to adjust the shader for.

    For example, a primitive cube usually has its origin in the center. If you use object space, the lower half of the cube would be below zero, and the upper half 0 - 1 on its y axis. If you use world space, then scale comes into play as well, so if the cube is scaled by 5, resting at world origin (0, 0,0), only the first 1/5th after the center will map to gradient, after that, the rest will only show the last value in the gradient. If you move the cube in game, you will see the color moves around.

    To get an idea of how it all works, make a simple unlit shader graph with position node, space set to object, and plug it in to base color, apply it to a cube, note the mixture of colors, with everything below zero on the y axis being black, and everything above zero on y axis having green in it.

    Note how red associates to positive x axis, and blue to positive z. Then play around with the space settings to get an idea.
     
    FredMoreau likes this.
  4. RudyRG3D

    RudyRG3D

    Joined:
    Mar 3, 2022
    Posts:
    15
    Just a couple of ideas to add what the others are mentioning that might help get you in the right direction, and I could be wrong here as I cannot see the picture either...

    I guess it depends when you mean 'height,' as do you mean how tall the object is, or it's distance relative to World Origin. Take traditional DCCs like Maya, etc. as a starting point. 0.5 (neutral gray) no distance takes place - it's flat, anything going towards the (0) direction goes negative (downwards) and anything going towards (1) goes positive (upwards) - this is just a way to think about your setup, and allows for values to go downwards, or below, World Origin if you set World Origin to ((0.5) - neutral)

    If you have an object that is quite "tall" (object height) or "far" (distance-wise from Origin), I would look into the Remap node, as the above people might be mentioning. Take your input values of your object and Remap that to a 0-to-1 range. Then, let the Gradient use that new 0-to-1 range to set the color. Just an idea.
     
    FredMoreau likes this.