Search Unity

Shader Experiments

Discussion in 'Shaders' started by 747823, May 8, 2013.

  1. 747823

    747823

    Joined:
    Apr 10, 2013
    Posts:
    43
    Here I'm going to upload demos and downloads of various shaders I create for practice (and to use!), which will all be free to download and use. I was thinking about putting them on the asset store but no, I think I'd rather make them free. Another reason I'm posting here is because I would like suggestions for how to improve the code etc. Please post suggestions if you have them.

    I'll first upload this splatmapping shader for any mesh (not just terrain) which blends textures based on their height, stored in the diffuse alpha channel.
    Demo:
    https://dl.dropboxusercontent.com/u/27273749/SplatDemo/SplatDemo.html
    Shader:
    https://dl.dropboxusercontent.com/u/27273749/SplatDemo/Diffuse_Specular_Normal_Splatmapped.shader

    More to come I think...
     
  2. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,665
    It is possible to put them on the asset store and make them free. Just set the price to "Free" in the AST-window :) It would be a lot better to have them there instead to have them on Dropbox.
     
  3. 747823

    747823

    Joined:
    Apr 10, 2013
    Posts:
    43
    The reason I have not uploaded to the asset store is that I don't really feel like I have time or motivation to prepare a demo and documentation on how to use it and package everything up neatly. Plus, like I said, they're still just experiments so they are not necessarily optimized or complete.

    Well I'm still experimenting with the shader, here's a test terrain made in sculptris with the shader applied.
    http://i.imgur.com/kI3sGBZ.jpg

    I added an overlay "color map" for ao and color variation, but it's just a random texture currently, there is no ao baked for that model.

    Also fixed the normals, I had accidentally clamped th em between 0 and 1 instead of -1 and 1. Now they look a lot more as they should.

    I think I've decided that sculpting a terrain in another program and importing it is far superior to unity's terrain system... Unity, get your S*** together and make a terrain system with sculpting in all directions + better mapping..

    Updated code:
    Code (csharp):
    1.  
    2. Shader "747823/Diffuse_Specular_Normal_Splatmapped"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Main Color", Color)                                = (0.8, 0.8, 0.8, 0)
    7.         _Spec ("Specular Power", Float )                            = 0.2
    8.         _Shininess ("Glossiness", Float )                           = 0.15
    9.         _BlendSoft ("Texture Blend Softness", Range(0, 1))          = 0.1
    10.         _NScale ("Normals Scale", Range(1, 4))                      = 2
    11.         _Tex_Color ("Color map (RGB)", 2D)                          = "white" {}
    12.         _Tex_Splat ("Splat map (RGBA)", 2D)                         = "white" {}
    13.         _Tex_DiffuseR ("Splat Diffuse R (RGB), Height (A)", 2D)     = "white" {}
    14.         _Tex_DiffuseG ("Splat Diffuse G (RGB), Height (A)", 2D)     = "white" {}
    15.         _Tex_DiffuseB ("Splat Diffuse B (RGB), Height (A)", 2D)     = "white" {}
    16.         _Tex_DiffuseA ("Splat Diffuse A (RGB), Height (A)", 2D)     = "white" {}
    17.         _Tex_NormalR ("Splat Normal R", 2D)                         = "bump" {}
    18.         _Tex_NormalG ("Splat Normal G", 2D)                         = "bump" {}
    19.         _Tex_NormalB ("Splat Normal B", 2D)                         = "bump" {}
    20.         _Tex_NormalA ("Splat Normal A", 2D)                         = "bump" {}
    21.     }
    22.    
    23.     SubShader
    24.     {
    25.         Tags { "RenderType"="Opaque" }
    26.        
    27.         CGPROGRAM
    28.  
    29.             #pragma surface surf BlinnPhongSpecMap
    30.             #pragma target 3.0
    31.            
    32.             sampler2D _Tex_Splat;
    33.             sampler2D _Tex_Color;
    34.             sampler2D _Tex_DiffuseR;
    35.             sampler2D _Tex_DiffuseG;
    36.             sampler2D _Tex_DiffuseB;
    37.             sampler2D _Tex_DiffuseA;
    38.             sampler2D _Tex_NormalR;
    39.             sampler2D _Tex_NormalG;
    40.             sampler2D _Tex_NormalB;
    41.             sampler2D _Tex_NormalA;
    42.  
    43.             fixed4 _Color;
    44.             half _Spec;
    45.             half _Shininess;
    46.             half _DetailTileX;
    47.             half _DetailTileY;
    48.             half _BlendSoft;
    49.             half _NScale;
    50.  
    51.             static const float _PI = 3.14159265359f;
    52.            
    53.             struct Input
    54.             {
    55.                 float2 uv_Tex_Splat;
    56.                 float2 uv_Tex_Color;
    57.                 float2 uv_Tex_DiffuseR;
    58.                 float2 uv_Tex_DiffuseG;
    59.                 float2 uv_Tex_DiffuseB;
    60.                 float2 uv_Tex_DiffuseA;
    61.                 // float2 uv_BumpMap;
    62.             };
    63.            
    64.             struct SurfaceOut
    65.             {
    66.                 fixed3 Albedo;
    67.                 fixed3 Normal;
    68.                 fixed3 Emission;
    69.                 half Specular;
    70.                 fixed3 Gloss;
    71.                 fixed Alpha;
    72.             };
    73.  
    74.             //compares input against compares. returns 1 if input is greater than ALL compares, else 0
    75.             float cutoff( float input, float compare1, float compare2, float compare3 )
    76.             {
    77.                 return ( input > compare1  input > compare2  input > compare3 ) ? 1 : 0;
    78.             }
    79.  
    80.             float3 blend_overlay( float3 base, float3 blend )
    81.             {
    82.                     return lerp((base*blend*2),(1.0-(2.0*(1.0-base)*(1.0-blend))),round(base));
    83.             }
    84.  
    85.             void surf (Input IN, inout SurfaceOut o)
    86.             {
    87.                 o.Albedo = 0;
    88.                 float3 c = {0, 0, 0};
    89.  
    90.                 //Diffuse
    91.                 float4 splat = tex2D( _Tex_Splat, IN.uv_Tex_Splat );
    92.                 float4 colormap = tex2D( _Tex_Color, IN.uv_Tex_Color );
    93.                 float4 dr = tex2D( _Tex_DiffuseR, IN.uv_Tex_DiffuseR );
    94.                 float4 dg = tex2D( _Tex_DiffuseG, IN.uv_Tex_DiffuseG );
    95.                 float4 db = tex2D( _Tex_DiffuseB, IN.uv_Tex_DiffuseB );
    96.                 float4 da = tex2D( _Tex_DiffuseA, IN.uv_Tex_DiffuseA );
    97.                 dr.a *= splat.r;
    98.                 dg.a *= splat.g;
    99.                 db.a *= splat.b;
    100.                 da.a *= splat.a;
    101.  
    102.                 //Combine all alphas to equal 1
    103.                 float sum = dr.a + dg.a + db.a + da.a;
    104.                 if ( sum > 0 )
    105.                 {
    106.                     dr.a /= sum;
    107.                     dg.a /= sum;
    108.                     db.a /= sum;
    109.                     da.a /= sum;
    110.                 }
    111.                 else
    112.                 {
    113.                     dr.a = 1;
    114.                 }
    115.  
    116.                 //Cutoff each alpha, comparing against each other
    117.                 float dr_cutoff = cutoff( dr.a, dg.a, db.a, da.a );
    118.                 float dg_cutoff = cutoff( dg.a, dr.a, db.a, da.a );
    119.                 float db_cutoff = cutoff( db.a, dr.a, dg.a, da.a );
    120.                 float da_cutoff = cutoff( da.a, dr.a, dg.a, db.a );
    121.  
    122.                 //Lerp between the smooth alpha and cutoff alpha by the softness value
    123.                 //Amount to lerp should increase nonlinearly as the difference between the cutoff and smooth increases
    124.                 //That way we "round the corners" as we lerp.
    125.                 dr.a = lerp( dr_cutoff, dr.a, saturate( _BlendSoft ) );
    126.                 dg.a = lerp( dg_cutoff, dg.a, saturate( _BlendSoft ) );
    127.                 db.a = lerp( db_cutoff, db.a, saturate( _BlendSoft ) );
    128.                 da.a = lerp( da_cutoff, da.a, saturate( _BlendSoft ) );
    129.  
    130.                 c = saturate( (dr.rgb*dr.a + dg.rgb*dg.a + db.rgb*db.a + da.rgb*da.a) * _Color );
    131.                 c = blend_overlay( c.rgb, colormap.rgb );
    132.  
    133.                 o.Albedo = c;
    134.  
    135.  
    136.                 //--------------------------------------------
    137.                 //Specular
    138.                 o.Gloss = _Spec;
    139.                 o.Specular = _Shininess;
    140.  
    141.  
    142.                 //--------------------------------------------
    143.                 //Normal
    144.                 float3 nr = UnpackNormal( tex2D( _Tex_NormalR, IN.uv_Tex_DiffuseR ) );
    145.                 float3 ng = UnpackNormal( tex2D( _Tex_NormalG, IN.uv_Tex_DiffuseG ) );
    146.                 float3 nb = UnpackNormal( tex2D( _Tex_NormalB, IN.uv_Tex_DiffuseB ) );
    147.                 float3 na = UnpackNormal( tex2D( _Tex_NormalA, IN.uv_Tex_DiffuseA ) );
    148.                 o.Normal = clamp( (nr.rgb*dr.a + ng.rgb*dg.a + nb.rgb*db.a + na.rgb*da.a)*_NScale, -1, 1 );
    149.             }
    150.            
    151.             inline fixed4 LightingBlinnPhongSpecMap (SurfaceOut s, fixed3 lightDir, half3 viewDir, fixed atten)
    152.             {
    153.                 half3 h = normalize (lightDir + viewDir);
    154.                 fixed diff = max (0, dot (s.Normal, lightDir));
    155.                 float nh = max (0, dot (s.Normal, h));
    156.                 float spec = pow (nh, s.Specular*128.0);
    157.                 fixed4 c;
    158.                 c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Gloss) * (atten * 2);
    159.                 c.a = s.Alpha + _LightColor0.a * (0.2989f * s.Gloss.r + 0.5870f * s.Gloss.g + 0.1140f * s.Gloss.b) * spec * atten;
    160.                 return c;
    161.             }
    162.  
    163.         ENDCG
    164.     }
    165.    
    166. }
    167.  
     
    Last edited: May 11, 2013
  4. cician

    cician

    Joined:
    Dec 10, 2012
    Posts:
    233
    In that case I recommend something like github or bitbucket, with additional advantage of a proper versioning system. Also the community wiki seems a good place for such purposes. Oh, and provide an explicit license and copyright notice.

    As for the terrain, yeah heightmaps are very limiting.
     
  5. muheydari

    muheydari

    Joined:
    Jun 7, 2012
    Posts:
    6
    Man were where you?
    I found your shader nice.
    but after lightmap normalmap doesn't work anymore.
    how to fix that?
     
  6. IPnose

    IPnose

    Joined:
    Jun 6, 2014
    Posts:
    20
    If, from the time, you managed to solve this, I'm interested.