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

A sphere with X% green in it and under it is red (dynamic material?)

Discussion in 'General Graphics' started by Firlefanz73, Feb 14, 2016.

  1. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,294
    Hello,

    I have a 3d sphere I want to use for displaying energy.It is a prefab with a primitive 3d mesh sphere.
    I could add a script in it.

    If possible, I want to have it this way:

    If energy is 100%, sphere is green.
    If energy is 50%, the half top is green, the bottom red.
    If energy is 0 completely red.

    Is it possible? I have no idea how to do this, any advice? Thanks a lot and have a nice sunday!

    Firlefanz
     
  2. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    You would want to have the vertex shader output the world Y position (i.e. height). If you use a surface shader, you will have IN.worldPos provided to you automatically.

    Then, in the frag (or surface) shader just use a step function with the height you want the color to change. You will need to add a shader property for the height cutoff, and then set that property from code depending on where and how big your sphere is. Just find the bounding box for your sphere, calculate the percentage of its height you want, and pass that into a material that is using your shader with "SetFloat".

    It would look something like this for a surface shader:
    Code (csharp):
    1.  
    2. o.Emission = lerp(float4(1,0,0,1), float4(0,1,0,1), step(_heightCutoff, IN.worldPos.y));
    3.  
    Though I just did it in my head so I may have got some of the terms flipped--just read through the docs (or experiment) to make sure the terms are all in the right order.
     
  3. StevenGerrard

    StevenGerrard

    Joined:
    Jun 1, 2015
    Posts:
    97
    Well, one way is use some custom shader to implement such feature.
    However I would say how about change the texture of sphere, maybe this is the most simplest way.
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Or even this, if you want it a smooth edge rather than a hard edge:
    Code (csharp):
    1.  
    2. o.Emission = lerp(float4(1,0,0,1), float4(0,1,0,1), IN.worldPos.y-(_heightCutoff*2-1));
    3.  
     
  5. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,294
    Thanks a lot. I made a Standard material with an Albedo Color.
    And let it fade from green to red, depending from energy and maxenegery.