Search Unity

using a 9 color texture for masking 9 texture elements

Discussion in 'Shaders' started by mmwizard, Jun 4, 2013.

  1. mmwizard

    mmwizard

    Joined:
    Mar 27, 2009
    Posts:
    65
    Hi,

    Im creating a Configurator where the user can change the colors of the sticker layout. I do have a solution but i dont know if this is a good idea. see the shader code below. my questions are

    1 can i use this shader in android or ios
    2 wil the code below perform well since i use so many if statements
    3 does anybody have another suggestion to solve this because i need to mask 8 parts of the sticker layout
    4 i also get jaggies at the edges between different colors any suggestions?

    Code (csharp):
    1. Shader "MMWizard/multicolor" {
    2.     Properties {
    3.         _MainTex ("Mask (A)", 2D) = "white" {}
    4.         _Color1 ("r 64", Color) = (1,1,1,1)
    5.         _Color2 ("r 192", Color) = (1,1,1,1)
    6.         _Color3 ("g 64", Color) = (1,1,1,1)
    7.         _Color4 ("g 192", Color) = (1,1,1,1)
    8.         _Color5 ("b 64", Color) = (1,1,1,1)
    9.         _Color6 ("b 192", Color) = (1,1,1,1)
    10.         _Color7 ("white", Color) = (1,1,1,1)
    11.         _Color8 ("black", Color) = (1,1,1,1)
    12.     }
    13.     SubShader {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 200
    16.        
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert
    19.  
    20.         sampler2D _MainTex;
    21.         fixed4 _Color1;
    22.         fixed4 _Color2;
    23.         fixed4 _Color3;
    24.         fixed4 _Color4;
    25.         fixed4 _Color5;
    26.         fixed4 _Color6;
    27.         fixed4 _Color7;
    28.         fixed4 _Color8;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         void surf (Input IN, inout SurfaceOutput o) {
    35.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    36.             //o.Albedo = 0;
    37.            
    38.             o.Albedo = _Color8.rgb;
    39.             if(c.r > 0  c.r < 0.3){ o.Albedo = _Color1.rgb; }
    40.             if(c.r > 0.6){ o.Albedo = _Color2.rgb; }
    41.             if(c.g > 0  c.g < 0.3){ o.Albedo = _Color3.rgb; }
    42.             if(c.g > 0.6){ o.Albedo = _Color4.rgb; }
    43.             if(c.b > 0  c.b < 0.3){ o.Albedo = _Color5.rgb; }
    44.             if(c.b > 0.6){ o.Albedo = _Color6.rgb; }
    45.             if(c.r == 1){ o.Albedo = _Color7.rgb; }
    46.            
    47.         }
    48.         ENDCG
    49.     }
    50.     FallBack "Diffuse"
    51. }
    thanx in advance
     
    Last edited: Jun 5, 2013