Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object specific screenspace overlay

Discussion in 'Shaders' started by primaerfunktion, Sep 19, 2014.

  1. primaerfunktion

    primaerfunktion

    Joined:
    Jan 16, 2012
    Posts:
    98
    Hey!

    I want to get rid of some of the UI and thought it would be a good idea to let the players just fill up with a color according to their damage. I assumed I would be able to achieve this by modifying the Detail Texture in Screen Space shader (http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html) and after a day of fiddling around with this I didn't even get close. So obviously I know next to nothing about shaders.

    Could anyone give me a hint on how I could achieve this or something similar, I'd be very grateful!

    Here's a mock up of what I want to do:
    healthbarMock.jpg
     
    Last edited: Sep 19, 2014
  2. primaerfunktion

    primaerfunktion

    Joined:
    Jan 16, 2012
    Posts:
    98
    I did manage to get something similar. But It's not in screen space, so it's not as readable. Help would be still greatly appreciated.

    shader.gif

    Here's the code:
    Code (CSharp):
    1. Shader "SpecularOverlayBar"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,0)
    6.         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    7.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    8.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    9.      
    10.         _GradientMap ("Gradient Map (A)", 2D)  = "white" {}
    11.         _TextureScale ("Texture Scale",float) = 1
    12.         _DamageColor ("Damage Color", Color) = (1,0,0,0)
    13.         _Range ("Range", Range(0,1)) = 0.5
    14.     }
    15.     SubShader
    16.     {
    17.         Tags {
    18.         "Queue"="Geometry"
    19.         "IgnoreProjector"="False"
    20.         "RenderType"="Opaque"
    21.     }
    22.         CGPROGRAM
    23.         #pragma surface surf BlinnPhong vertex:vert
    24.      
    25.         sampler2D _MainTex;
    26.         half _Shininess;
    27.         float4 _Color;
    28.         sampler2D _GradientMap;
    29.         float _TextureScale;
    30.         float4 _DamageColor;
    31.         float _Range;
    32.        
    33.         struct Input
    34.         {
    35.             float2 uv_MainTex;
    36.             float3 worldPos;
    37.             float3 localPos;
    38.         };
    39.      
    40.         void vert (inout appdata_full v, out Input o) {
    41.             UNITY_INITIALIZE_OUTPUT(Input,o);
    42.             o.localPos = v.vertex.xyz;
    43.         }
    44.         void surf (Input IN, inout SurfaceOutput o)
    45.         {
    46.             half2 yUV = IN.localPos.xz / _TextureScale;
    47.             half2 xUV = IN.localPos.zy / _TextureScale;
    48.             half2 zUV = IN.localPos.xy / _TextureScale;
    49.             half4 yDiff = tex2D (_GradientMap, yUV);
    50.             half4 xDiff = tex2D (_GradientMap, xUV);
    51.             half4 zDiff = tex2D (_GradientMap, zUV);
    52.          
    53.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    54.          
    55.             if(xDiff.a < _Range){
    56.                  tex.rgb *= _DamageColor * 1.5;
    57.                  tex.rgb *= _DamageColor * 1.5;
    58.                  tex.rgb *= _DamageColor * 1.5;
    59.             }
    60.             o.Albedo = tex * _Color;
    61.             o.Gloss = tex.a;
    62.             o.Alpha = tex.a * _Color.a;
    63.             o.Specular = _Shininess;
    64.         }
    65.         ENDCG
    66.     }
    67.     Fallback "Diffuse"
    68. }