Search Unity

Fur shader

Discussion in 'Shaders' started by Deleted User, Jul 14, 2010.

  1. Deleted User

    Deleted User

    Guest

    Just finished porting Fur shader to shaderlab.
    It was an easy one, and I'm pretty sure that I've done it 100% right, although I might have made a mistake somewhere so you might want to read through it.

    I'm not quite sure how to use it properly lol, I tried attaching it to a sphere, and even though the shader works fine and has some decent effect, I can't seem to understand why it's called "Fur shader" because it doesn't do anything fur-ish :D

    Maybe I just don't know how to use it properly, tell me if you manage to get anything interesting out of it :)


    Code (csharp):
    1. Shader "Edvinas/FurShader" {
    2.     Properties {
    3.         s_diffuse_texture("g_diffuse_texture", 2D) = "white" {}
    4.         s_detail_texture("g_detail_texture", 2D) = "white" {}
    5.         g_layer("g_layer", Float) = 1.0
    6.         g_fur_current_length("g_fur_current_length", Float) = 1.0
    7.         light_colour("light_color", Color) = (1,1,1)
    8.            
    9.         g_wind_direction("g_wind_direction", Float) = 1.0
    10.         g_wind_magnitutde("g_wind_magnitutde", Float) = 1.0
    11.         g_wind_speed("g_wind_speed", Float) = 1.0
    12.         g_wind_fizzling("g_wind_fizzling", Float) = 1.0
    13.            
    14.         _Cutoff ("Alpha cutoff", Range (0,1)) = 0.5
    15.        
    16.     }
    17.    
    18.     SubShader {
    19. Pass {
    20.  
    21. Cull Off
    22. Blend SrcAlpha OneMinusSrcAlpha
    23. AlphaTest Greater [_Cutoff]
    24.    
    25. CGPROGRAM
    26.  
    27.  
    28. #pragma target 3.0
    29. #pragma vertex vert
    30. #pragma fragment frag
    31. #include "UnityCG.cginc"
    32.  
    33.  
    34.     sampler2D s_detail_texture;
    35.     sampler2D s_diffuse_texture;
    36.  
    37.    
    38.     float g_layer;
    39.     float g_fur_current_length;
    40.    
    41.     float2 g_wind_direction;
    42.     float g_wind_magnitutde;
    43.     float2 g_wind_speed;
    44.     float g_wind_fizzling;
    45.    
    46.     float3 light_colour;
    47.    
    48.     float3 directional_lighting_colour(float3 normal, float3 light_dir)
    49. {
    50.     return light_colour * saturate(dot(light_dir, normal));    
    51.  
    52. }
    53.  
    54. struct v2f
    55. {
    56.     float4  position    : POSITION;
    57.     float3  normal      : TEXCOORD0;
    58.     float2  texcoord_uv : TEXCOORD1;   
    59. };
    60.  
    61.  
    62.  
    63.  
    64. v2f vert ( appdata_base v) {
    65. appdata_tan n;
    66. v2f r;
    67.  
    68.   //r.position = mul(glstate.matrix.mvp, v.vertex);
    69.   float4 pos = v.vertex;
    70.    
    71.     float fur_length        = float(float(g_layer + 1) / 30.0f);
    72.     float fur_length_scalar = 0.8;
    73.     pos.xyz                 += (v.normal * fur_length);
    74.    
    75. // now apply the wind effect
    76.     //g_dir                 = mul(g_dir, g_world);
    77.     float k                 = pow(fur_length , g_wind_fizzling);
    78.     //float k                   = pow(fur_length , 3);
    79.     pos.xz                  += k * g_wind_direction;
    80.     r.position              = mul(glstate.matrix.mvp, pos);
    81.     // pass the normal
    82.     r.normal                = v.normal;
    83.    
    84.     // pass the texture coordinates as is
    85.     r.texcoord_uv           = v.texcoord * 20;
    86.    
    87.   return r;
    88.  
    89. }
    90.  
    91. float4 frag (v2f v) : COLOR {
    92.    
    93.     // read the pixels from the samplers
    94.     float4 diffuse_pixel    = tex2D(s_diffuse_texture, v.texcoord_uv);
    95.     float4 detail_pixel     = tex2D(s_detail_texture, v.texcoord_uv);
    96.    
    97.     // Detail Mapping
    98.     diffuse_pixel.rgb       = lerp(diffuse_pixel.rgb, detail_pixel.rgb, detail_pixel.a);
    99.  
    100.     // calculate diffuse color
    101.     detail_pixel.rgb        *= directional_lighting_colour(v.normal.rgb, light_colour);
    102.    
    103.     float4 finalcolor       = float4(detail_pixel.rgb,diffuse_pixel.a);
    104.     //finalcolor            = float4(diffuse_pixel.rgb + glstate.lightmodel.ambient,diffuse_pixel.a);
    105.     //finalcolor            = float4(diffuse_pixel.rgb, diffuse_pixel.a);
    106.     //finalcolor            = float4(normal_pixel.rgb, diffuse_pixel.a);
    107.     //finalcolor            = float4(v.normal.rgb, diffuse_pixel.a);
    108.    
    109.     return finalcolor;
    110.  
    111. }
    112.  
    113. ENDCG
    114.  
    115. }
    116.  
    117.     }
    118.  
    119. }
    120.  
    121.  
    Thanks
     
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,765
    Where did you port this from?
     
  3. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Hmm this is an Old thread, but honestly i haven't read this yet. Is anyone tried it already?