Search Unity

Unlit Shader, find out if r/g/b of texture exist

Discussion in 'Shaders' started by zmatiaki, Apr 10, 2021.

  1. zmatiaki

    zmatiaki

    Joined:
    Nov 21, 2018
    Posts:
    13
    Hi to all,

    I have created an Unlit shader for UI usage, with 2 x Texture2DArrays, 2 x Index for each array and 3 InputColors. Indexes and InputColors are setted from scipt.

    Shader is responsible to replace r,g,b colors with the input Colors that are setted outside editor.

    But I have an issue that I cant figure out how to solve. In my shader I want to have a float Propertie that is changed within the shader, to represents which of R,G,B are available on the texture. For example if texture has red ,float = 1, if has r,g float = 2 etc. In this way I will have a variable that I can access from another script and know for each case which channels are available on texture (r,g,b)

    Is it possible or I am missing the point?How should I edit below shader to make it work?

    Properties
    Code (CSharp):
    1. Properties
    2.     {
    3.         _MainTex ("Texture", 2D) = "white" {}          
    4.      
    5.         //Colors
    6.         _Color("BlueColor", Color) = (1,1,1,1)
    7.         _ColorOverlay("GreenColor", Color) = (1,1,1,1)
    8.         _ColorOverlay2("RedColor", Color) = (1,1,1,1)
    9.  
    10.         //Floats example
    11.             _isToggledB("HasBlue", float) = 1.0    
    12.             _isToggledG("HasGreen", float) = 1.0    
    13.             _isToggledR("HasRed", float) = 1.0
    14.  
    15.           //Texture2DArray v1
    16.         _MainTex0("Textures_Floors", 2DArray) = "" {}
    17.         _TextureIndex2("Index_Floors", float) = 0
    18.  
    19.           //Texture2DArray v2
    20.         _MainTex2("Textures_Walls", 2DArray) = "" {}
    21.         _TextureIndex("Index_Walls", float) = 0
    22.  
    23.  
    24.         _ChooseTexture("IsWall:1", Int) = 0
    25.  
    26.     }
    Shader code (part of it) :
    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2.             {
    3.                 // sample the texture
    4.                 fixed4 c = tex2D(_MainTex, i.uv);
    5.          
    6.                 if (UNITY_ACCESS_INSTANCED_PROP(Props, _ChooseTexture) > 0)
    7.                 {
    8.                     c = UNITY_SAMPLE_TEX2DARRAY(_MainTex2, float3(i.uv, UNITY_ACCESS_INSTANCED_PROP(Props, _TextureIndex)));
    9.  
    10.                 }
    11.                 else
    12.                 {
    13.                     c = UNITY_SAMPLE_TEX2DARRAY(_MainTex0, float3(i.uv, UNITY_ACCESS_INSTANCED_PROP(Props, _TextureIndex2)));
    14.                 }
    15.                 //Change Colors
    16.                 float4 tempBlue = lerp(float4(0, 0, 0, 0), c.b, UNITY_ACCESS_INSTANCED_PROP(Props, _Color));        
    17.                 float4 tempGreen = lerp(float4(0, 0, 0, 0), UNITY_ACCESS_INSTANCED_PROP(Props, _ColorOverlay), c.g);
    18.                 float4 tempRed = lerp(float4(0, 0, 0, 0), c.r, UNITY_ACCESS_INSTANCED_PROP(Props, _ColorOverlay2));
    19.  
    20.                 float4 templerp = lerp(tempRed, float4(1, 1, 1, 0), tempGreen);          
    21.                 float4 finalLerp = lerp(tempBlue, float4(1, 1, 1, 1), templerp);
    22.  
    23.                 c = finalLerp;
    24.                 // apply fog
    25.                 UNITY_APPLY_FOG(i.fogCoord, c);
    26.                 return c;
    27.             }
     
    Last edited: Apr 10, 2021