Search Unity

Binary Shader

Discussion in 'Made With Unity' started by Lab013, Jun 3, 2009.

  1. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    I have been working on a "binary" shader (see http://forum.unity3d.com/viewtopic.php?p=159501&highlight=#159501 ), and I finally got everything working pretty good, and thought I would give yall the code, and youtube link.

    (I will add it to the wiki as well, and will put some more documentation there)

    http://www.youtube.com/watch?v=YQ5UrL8klhs&feature=channel_page

    Code (csharp):
    1.  
    2. Shader "LimitedColors/Binary" {
    3.     Properties {
    4.         _Color ("Pre-Calculative Color", Color) = (1,1,1,1) // this color gets applied before texture calculations take place
    5.         _Col1 ("Color One", Color) = (.5,.5,.5,1) // one of the colors that the end fragment can become
    6.         _Col2 ("Color Two", Color) = (0,0,0,1) // the second color that the end fragment can become
    7.         _Ambient("Ambient Color", Color) = (1, 1, 1, 1) // the ambient light level.
    8.         _Final("Final Color Multiplication", Color) = (1, 1, 1, 1) // this color is applied after all calculations (including colors are applied) have been completed
    9.         _MainTex ("Base (RGB)", 2D) = "white" {} // main texture
    10.         _Equa ("Equality Number ", float) = 2 // helps to determain what color range _Col1  _Col2 should be applied to
    11.         _Modulus ("Modulus Number", float) = 4 // helps to determain what color range _Col1  _Col2 should be applied to
    12.         _Multi ("Multiplication Number ", float) = 10 // determains how many "rings" should be made
    13.         /* Note:
    14.         *   The last 3 'floats' are converted to ints
    15.         */
    16.     }
    17.     SubShader {
    18.        
    19.         Pass {
    20.             Name "Lighting On"
    21.             Tags { "LightMode" = "Pixel" } // do all of these calculations when a pixel light is using it
    22.             CGPROGRAM
    23.             // go through all the pragma and file includes
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma multi_compile_builtin
    27.             #pragma fragmentoption ARB_fog_exp2
    28.             #pragma fragmentoption ARB_precision_hint_fastest
    29.             #include "UnityCG.cginc"
    30.             #include "AutoLight.cginc"
    31.            
    32.             // get our v2f struct ready
    33.             struct v2f {
    34.                 V2F_POS_FOG;
    35.                 LIGHTING_COORDS
    36.                 float4 color : COLOR0;
    37.                 float2  uv : TEXCOORD0;
    38.             };
    39.            
    40.             // all of our uniform var's
    41.             sampler2D _MainTex;
    42.             float4 _Col1;
    43.             float4 _Col2;
    44.             float4 _Color;
    45.             float4 _Ambient;
    46.             float4 _Final;
    47.             float _Equa;
    48.             float _Modulus;
    49.             float _Multi;
    50.            
    51.             // our actual texture calculation function
    52.             float4 TextureCalculations (float4 tex, float4 lighting) {
    53.                 // get int version of floats
    54.                 int equa = _Equa;
    55.                 int mod = _Modulus;
    56.                 int multi = _Multi;
    57.                 // preform 'core' calculations
    58.                 tex *= _Color;
    59.                 tex[3] = 1;
    60.                 lighting[3] = 1;
    61.                 lighting += _Ambient;
    62.                 tex *= lighting;
    63.                 float avrg = tex[0] + tex[1] + tex[2];
    64.                 avrg = avrg / 3;
    65.                 avrg *= multi;
    66.                 int tmp = avrg;
    67.                 tmp += tmp % 2;
    68.                 int asdf = avrg;
    69.                 if (asdf % mod == equa)
    70.                     tex = _Col1;
    71.                 else
    72.                     tex = _Col2;
    73.  
    74.                 return tex * _Final;
    75.             }
    76.            
    77.            
    78.            
    79.             v2f vert (appdata_base v) {
    80.                 // do basic lighting calculations, and UV calculations
    81.                 v2f o;
    82.                 PositionFog( v.vertex, o.pos, o.fog );
    83.                 float3 ldir = normalize( ObjSpaceLightDir( v.vertex ) );
    84.                 float diffuse = dot( v.normal, ldir );
    85.                 o.color = diffuse * _ModelLightColor0;
    86.                 o.uv = TRANSFORM_UV(0);
    87.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    88.                 return o;
    89.             }
    90.  
    91.             float4 frag (v2f i) : COLOR {
    92.                 float4 texcol = tex2D( _MainTex, i.uv ); // get a texture
    93.                 i.color = i.color * LIGHT_ATTENUATION(i) * 2; // get lighting
    94.                 texcol = TextureCalculations(texcol, i.color); // preform 'core' calculations
    95.                 return texcol; // return results
    96.             }
    97.             ENDCG
    98.         }
    99.        
    100.        
    101.        
    102.         Pass {
    103.             Name "Lighting Off"
    104.             // this one is quite similar to everything above, just more simple since it doesn't need to preform lighting calculations
    105.             Tags { "LightMode" = "VertexOrNone" }
    106.             CGPROGRAM
    107.             #pragma vertex vert
    108.             #pragma fragment frag
    109.             #pragma fragmentoption ARB_fog_exp2
    110.             #include "UnityCG.cginc"
    111.            
    112.    
    113.             struct v2f {
    114.                 V2F_POS_FOG;
    115.                 float4 color : COLOR0;
    116.                 float2  uv : TEXCOORD0;
    117.             };
    118.             sampler2D _MainTex;
    119.             float4 _Col1;
    120.             float4 _Col2;
    121.             float4 _Color;
    122.             float4 _Ambient;
    123.             float4 _Final;
    124.             float _Equa;
    125.             float _Modulus;
    126.             float _Multi;
    127.            
    128.             //Calculate texture
    129.             float4 TextureCalculations (float4 tex, float4 lighting) {
    130.                 int equa = _Equa;
    131.                 int mod = _Modulus;
    132.                 int multi = _Multi;
    133.                
    134.                 tex *= _Color;
    135.                 tex[3] = 1;
    136.                 lighting[3] = 1;
    137.                 lighting += _Ambient;
    138.                 tex *= lighting;
    139.                 float avrg = tex[0] + tex[1] + tex[2];
    140.                 avrg = avrg / 3;
    141.                 avrg *= multi;
    142.                 int tmp = avrg;
    143.                 tmp += tmp % 2;
    144.                 int asdf = avrg;
    145.                 if (asdf % mod == equa)
    146.                     tex = _Col1;
    147.                 else
    148.                     tex = _Col2;
    149.  
    150.                 return tex * _Final;
    151.             }
    152.            
    153.            
    154.            
    155.             v2f vert (appdata_base v) {
    156.                 v2f o;
    157.                 PositionFog( v.vertex, o.pos, o.fog );
    158.                 o.uv = TRANSFORM_UV(0);
    159.                 return o;
    160.             }
    161.  
    162.             float4 frag (v2f i) : COLOR {
    163.                 float4 texcol = tex2D( _MainTex, i.uv );
    164.                 i.color = 0;
    165.                 texcol = TextureCalculations(texcol, i.color);
    166.                 return texcol;
    167.             }
    168.             ENDCG
    169.         }
    170.        
    171.     }
    172.     // get our fall back...
    173.     FallBack "Diffuse", 1
    174. }
    175.  
     
  2. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Very interesting! :)
     
  4. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    Thank you!
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Cool indeed, keep up the awesome work!
     
  6. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
  7. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    Thanks to both of you! Glad you liked it, I'm hoping to start working on some volumetric shaders soon.
     
  8. Sakar

    Sakar

    Joined:
    Feb 12, 2009
    Posts:
    141
    Very nice! This will come in handy for one of my future projects. Thanks!
     
  9. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    Thanks, and no problem! Glad you will be able to use it in some of your projects.