Search Unity

Question Shader for Slider Effect on World-Space Objects

Discussion in 'Shaders' started by 13E12K, Jan 17, 2023.

  1. 13E12K

    13E12K

    Joined:
    May 4, 2014
    Posts:
    20
    Hi everyone,

    I am kind of new in writing shaders and I am trying to achieve a slider effect on my world-space objects. So, to achieve this, at the end of the shader I am checking whether the alpha value of a given reference texture is above the fillrate variable. If it is, consequently, setting the alpha value to zero.

    As a result, I have achieved what I wanted as in attached gif, but I am not sure, whether this is the most performant and best way to do it.



    Is there any other way to achieve similar behaviour, without using if statements? Or is this a valid and proper approach to get this kind of slider effect?

    Code (CSharp):
    1. Shader "Ocean Surface Alpha"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _FillRateTex("Texture", 2D) = "white" {}
    7.         _FillRate("Fill Rate", Range(0.0, 1.0)) = 1.0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    13.  
    14.         Pass
    15.         {
    16.             Blend [_BlendModeSrc] [_BlendModeTgt]
    17.  
    18.             ZWrite Off
    19.            
    20.             CGPROGRAM
    21.             #pragma vertex Vert
    22.             #pragma fragment Frag
    23.             #pragma multi_compile_fog
    24.  
    25.             #include "UnityCG.cginc"
    26.  
    27.             sampler2D _MainTex;
    28.             sampler2D _FillRateTex;
    29.             half _Alpha;
    30.             half _FillRate;
    31.            
    32.             //
    33.             // Shader doing projection stuff
    34.             //
    35.             {
    36.                 col.a *= _Alpha;
    37.                 if (tex2D(_FillRateTex, input.uv).a > _FillRate)
    38.                 {
    39.                     col.a = 0;
    40.                 }
    41.  
    42.                 return col;
    43.             }
    44.             ENDCG
    45.         }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Yes, but there's no reason to use them as what you're doing is already the fastest option.