Search Unity

Unlit Color Blend with Texture (Simple)

Discussion in 'Shaders' started by gabearts, Sep 14, 2019.

  1. gabearts

    gabearts

    Joined:
    Jun 30, 2014
    Posts:
    10
    Just got this working today and it's super sweet for simple, unlit, textured, colored, prototypes. For anyone who wants a simple, unlit, color, with blending modes, here it is! I honestly wish that Unity had this as a default, I hate having to try a bunch of shaders for something simple like this. So for anyone sharing my pain, here it is! Enjoy!! (side note: divide is pointless but once you look at the code, it's easy).

    ADD COLOR

    MULTIPLY COLOR

    SUBTRACT COLOR

    Code (CSharp):
    1. Shader "Unlit/ColorBlend"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         [Enum(None,0,Add,1,Multiply,2, Subtract,3)] _Blend ("Blend mode subset", Int) = 0
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.  
    9.     }
    10.     SubShader
    11.     {
    12.  
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 100
    15.         Color [_Color]
    16.        
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             // make fog work
    23.             #pragma multi_compile_fog
    24.             #include "UnityCG.cginc"
    25.    
    26.          
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 UNITY_FOG_COORDS(1)
    37.                 float4 vertex : SV_POSITION;
    38.             };
    39.  
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.             float4 _Color;
    43.             float _Blend;
    44.  
    45.  
    46.             v2f vert (appdata v)
    47.             {
    48.                 v2f o;
    49.                 o.vertex = UnityObjectToClipPos(v.vertex);
    50.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    51.                 UNITY_TRANSFER_FOG(o,o.vertex);
    52.                 return o;
    53.             }
    54.  
    55.             fixed4 frag (v2f i) : SV_Target
    56.             {
    57.                 // sample the texture
    58.                 fixed4 col = tex2D(_MainTex, i.uv);
    59.                
    60.                 switch(_Blend){
    61.                     case 1:
    62.                         col = tex2D(_MainTex, i.uv) + _Color;
    63.                         return col;
    64.                        
    65.                     case 2:
    66.                         col = tex2D(_MainTex, i.uv) * _Color;
    67.                         return col;
    68.                                                            
    69.                     case 3:
    70.                         col = tex2D(_MainTex, i.uv) - _Color;
    71.                         return col;
    72.                 }
    73.  
    74.                
    75.                 // apply fog
    76.                 UNITY_APPLY_FOG(i.fogCoord, col);
    77.                 return col;
    78.             }
    79.          
    80.            
    81.             ENDCG
    82.         }
    83.     }
    84. }
    85.  
     
  2. Mike_17

    Mike_17

    Joined:
    Jan 30, 2017
    Posts:
    10
    This is EXACTLY what I was looking for !
    Thanks bro
     
  3. Krazed_Brony

    Krazed_Brony

    Joined:
    Sep 8, 2019
    Posts:
    1
    This shader is exactly what I needed, thank you so much!
     
  4. Darkziss

    Darkziss

    Joined:
    Jan 24, 2022
    Posts:
    1
    Thank you