Search Unity

Very Simple Velvet shader!

Discussion in 'Shaders' started by Rond, Feb 5, 2011.

  1. Rond

    Rond

    Joined:
    Oct 21, 2009
    Posts:
    175
    I'm trying this all the day but i have no knowledge with shader programming, i've tried the unify community example but i think it is obsolete, the console returns me a couple of warnings and the material become pink, i switched the codes that the console warned me but still no sucess, it still says that my graphics card can't run subshaders and i have a shader 4.0 graphics card..
     
    Last edited: Feb 5, 2011
  2. Rond

    Rond

    Joined:
    Oct 21, 2009
    Posts:
    175
    I almost say: same here..
    bump!
     
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    same here!
     
  4. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    do you want to work with lightprobes / lightmaps? This is driven by NdotV, of course you can remove the _BumpMap with o.Normal for faster execution, putting Alpha Rim power into _MainTex and remove _RimColor, That would be faster on mobile. Lightmap is not implemented, you can enable it with lightprobes though.

    Code (csharp):
    1.  
    2. Shader "Shan/SurfaceRimLight" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _BumpMap ("Bump Map", 2D) = "bump"{}
    6.         _RimColor("Rim Color(RGB) Power(A)", Color) = (1,1,1,1)
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         #pragma surface surf Lambert
    14.  
    15.  
    16.         struct Input {
    17.             float2 uv_MainTex;
    18.             float2 uv_BumpMap;
    19.             float3 viewDir;
    20.         };
    21.        
    22.        
    23.         sampler2D _MainTex;
    24.         sampler2D _BumpMap;
    25.         float4 _RimColor;
    26.    
    27.         void surf (Input IN, inout SurfaceOutput o) {
    28.        
    29.             half4 c = tex2D (_MainTex, IN.uv_MainTex);         
    30.            
    31.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    32.            
    33.             half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    34.            
    35.            
    36.            
    37.            
    38.             o.Albedo = c.rgb;
    39.            
    40.             // to use with lightprobes.
    41.  //float3 worldNormal = WorldNormalVector(IN,o.Normal);
    42.             //o.Albedo = c.rgb*ShadeSH9(float4(worldNormal,1));
    43.                            
    44.             o.Emission = _RimColor.rgb * pow(rim, _RimColor.a);
    45.            
    46.         }
    47.         ENDCG
    48.     }
    49.     FallBack "Diffuse"
    50. }
    51.  
    52.  
     
    Last edited: Mar 23, 2012
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thats kinda cool! how to optimise it further so it's just rimlight + lightprobes lights? ie removing the normal mapping? I imagine it would be fast on mobile.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    These look great! I'll report back with some findings once I get in the office. One thing I noticed was POW - is that in the pixel or vertex level since it's a tad too expensive otherwise.
     
  7. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    don't know really, surface shader is something unity made, gotta ask them. My first guess is that everything in surface shader is pixel/fragment shader.
     
  8. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    How do I mask the rim with the _ MainTex's alpha?

    I got it working with specular, but I know very very very little about shader scripting, how would I go about making that rim effect be affected by _MainTex's alpha?

    Code (csharp):
    1. Shader "Bumped Specular with rim" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    6.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    7.     _BumpMap ("Normalmap", 2D) = "bump" {}
    8.     _RimColor("Rim Color(RGB) Power(A)", Color) = (1,1,1,1)
    9. }
    10. SubShader {
    11.     Tags { "RenderType"="Opaque" }
    12.     LOD 400
    13.    
    14. CGPROGRAM
    15. #pragma surface surf BlinnPhong
    16.  
    17. struct Input {
    18.     float2 uv_MainTex;
    19.     float2 uv_BumpMap;
    20.     float3 viewDir;
    21. };
    22.  
    23. sampler2D _MainTex;
    24. sampler2D _BumpMap;
    25. float4 _RimColor;
    26. fixed4 _Color;
    27. half _Shininess;
    28.  
    29.  
    30. void surf (Input IN, inout SurfaceOutput o) {
    31.     half4 c = tex2D(_MainTex, IN.uv_MainTex);
    32.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    33.     half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    34.     o.Albedo = c.rgb * _Color.rgb;
    35.     o.Albedo += _RimColor.rgb * pow(rim, _RimColor.a);
    36.     o.Gloss = c.a;
    37.     o.Alpha = c.a * _Color.a;
    38.     o.Specular = _Shininess;
    39.    
    40.    
    41. }
    42. ENDCG
    43. }
    44.  
    45. FallBack "Diffuse"
    46. }
     
    Last edited: Mar 24, 2012
  9. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    You are using alpha for alpha and gloss, so you can't use that again......
     
  10. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    I can't use alpha twice?

    I did this and it seems to work! :-0

    Code (csharp):
    1. Shader "Bumped Specular with rim" {
    2.  
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     _BumpMap ("Normalmap", 2D) = "bump" {}
    9.     _RimColor("Rim Color(RGB) Power(A)", Color) = (1,1,1,1)
    10. }
    11.  
    12. SubShader {
    13.     Tags { "RenderType"="Opaque" }
    14.     LOD 400
    15.  
    16. CGPROGRAM
    17.  
    18. #pragma surface surf BlinnPhong
    19.  
    20. struct Input {
    21.     float2 uv_MainTex;
    22.     float2 uv_BumpMap;
    23.     float3 viewDir;
    24. };
    25.  
    26. sampler2D _MainTex;
    27. sampler2D _BumpMap;
    28. float4 _RimColor;
    29. fixed4 _Color;
    30. half _Shininess;
    31.  
    32. void surf (Input IN, inout SurfaceOutput o) {
    33.     half4 c = tex2D(_MainTex, IN.uv_MainTex);
    34.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    35.     half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    36.     o.Albedo = c.rgb * _Color.rgb;
    37.     o.Albedo +=  _RimColor.rgb * pow(rim, _RimColor.a) * c.a;
    38.     o.Gloss = c.a;
    39.     o.Alpha = c.a * _Color.a;
    40.     o.Specular = _Shininess;  
    41. }
    42. ENDCG
    43. }
    44. FallBack "Diffuse"
    45. }
    46.  
     
    Last edited: Mar 24, 2012
  11. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73

    You can use alpha as many times as you want, you only have one alpha and you want to represent alpha, gloss and rim with it? That's why it "seems" to be working for you
     
    Last edited: Mar 25, 2012