Search Unity

Question Changing Sprite Colour Palette

Discussion in 'Shaders' started by SPG-Vulpine, Aug 2, 2022.

  1. SPG-Vulpine

    SPG-Vulpine

    Joined:
    May 27, 2019
    Posts:
    1
    Hello All,

    I'm trying to create a shader which will change the colours on a sprite based on the palette that I pass in, for example;
    198277-examples.png
    But instead, what I am getting is this;
    198278-results.png

    This is my first time working on a shader, if anyone could point me in the right direction, I would be extremely grateful

    Thanks

    Code (CSharp):
    1. Shader "Custom/bitty_shader"
    2. {
    3.      Properties
    4.      {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _DefaultPaletteTex ("Default Palette (RGB)", 2D) = "white"
    8.         _CustomPaletteTex ("Custom Palette (RGB)", 2D) = "white"
    9.      }
    10.      SubShader
    11.      {
    12.         Lighting Off
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert
    17.  
    18.         fixed4 _Color;
    19.         sampler2D _MainTex;
    20.         sampler2D _DefaultPaletteTex;
    21.         sampler2D _CustomPaletteTex;
    22.      
    23.         float4 _DefaultPaletteTex_TexelSize;
    24.         float4 _CustomPaletteTex_TexelSize;
    25.      
    26.         struct Input {
    27.           float2 uv_MainTex;
    28.         };
    29.  
    30.         void surf (Input IN, inout SurfaceOutput o) {
    31.             o.Alpha = 0;
    32.          
    33.             for(int _DefaultPalette_x = 0; _DefaultPalette_x < _DefaultPaletteTex_TexelSize.z; _DefaultPalette_x++)
    34.             {
    35.                 for(int _DefaultPalette_y = 0; _DefaultPalette_y < _DefaultPaletteTex_TexelSize.w; _DefaultPalette_y++)
    36.                 {
    37.                     fixed4 main = tex2D(_MainTex, IN.uv_MainTex);
    38.                     fixed4 palette = tex2D(_DefaultPaletteTex, float2(_DefaultPalette_x, _DefaultPalette_y));
    39.                     if(main.r == palette.r && main.g == palette.g && main.b == palette.b)
    40.                     {
    41.                         fixed4 c = tex2D(_CustomPaletteTex, float2(_DefaultPalette_x, _DefaultPalette_y));
    42.                         o.Albedo = c.rgb;
    43.                         o.Alpha = 255;
    44.                     }
    45.                 }
    46.             }
    47.         }
    48.         ENDCG
    49.      }
    50.   FallBack "Diffuse"
    51. }
     
    Last edited: Aug 2, 2022