Search Unity

Color specular shader

Discussion in 'Shaders' started by janpec, Dec 13, 2010.

  1. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hello Unity community...heh that rimes well:D

    I am wondering if there is any colored specular shader out there for Unity (specular map as colored specular texture in RGB mode not grayscale). If so i would be happy if someone could give me link for it. Also if anyone is using it i would like to know if this shader is optimised and it does not ruin performance too much, considering that it will be used on every model.

    Also is Strumpy Shader Editor able to use colored specular maps? Is this shader editor optimised comparing to original Unity shaders? If not of what performance drop are we talking about?

    Thank you for all answers in advance.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Colored specular for Unity 3.x, here. Works both in forward deferred rendering:

    Code (csharp):
    1.  
    2. Shader "Surface/Colored Specular" {
    3. Properties {
    4.   _MainTex ("Texture", 2D) = "white" {}
    5.   _SpecMap ("SpecMap", 2D) = "white" {}
    6. }
    7. SubShader {
    8.     Tags { "RenderType" = "Opaque" }
    9. CGPROGRAM
    10. #pragma surface surf ColoredSpecular
    11.  
    12. struct MySurfaceOutput {
    13.     half3 Albedo;
    14.     half3 Normal;
    15.     half3 Emission;
    16.     half Specular;
    17.     half3 GlossColor;
    18.     half Alpha;
    19. };
    20.  
    21.  
    22. inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    23. {
    24.   half3 h = normalize (lightDir + viewDir);
    25.  
    26.   half diff = max (0, dot (s.Normal, lightDir));
    27.  
    28.   float nh = max (0, dot (s.Normal, h));
    29.   float spec = pow (nh, 32.0);
    30.   half3 specCol = spec * s.GlossColor;
    31.  
    32.   half4 c;
    33.   c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
    34.   c.a = s.Alpha;
    35.   return c;
    36. }
    37.  
    38. inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light)
    39. {
    40.     half3 spec = light.a * s.GlossColor;
    41.    
    42.     half4 c;
    43.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    44.     c.a = s.Alpha + spec * _SpecColor.a;
    45.     return c;
    46. }
    47.  
    48.  
    49. struct Input {
    50.   float2 uv_MainTex;
    51.   float2 uv_SpecMap;
    52. };
    53. sampler2D _MainTex;
    54. sampler2D _SpecMap;
    55. void surf (Input IN, inout MySurfaceOutput o)
    56. {
    57.   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.3;
    58.   half4 spec = tex2D (_SpecMap, IN.uv_SpecMap);
    59.   o.GlossColor = spec.rgb;
    60.   o.Specular = 32.0/128.0;
    61. }
    62. ENDCG
    63. }
    64. Fallback "Diffuse"
    65. }
    66.  
    This has some things simplified (i.e. specular power is hardcoded). Should be easy to add full capabilities of built-in specular shader, just with a colored gloss map.
     
    shinatoX1 likes this.
  3. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Thanks for help Arash, however i am not a programmer and i have no idea where do i need to put shader code to get it work or how to create shader in Unity.
     
  4. tokyob

    tokyob

    Joined:
    May 17, 2010
    Posts:
    37
    Copy / paste into a new text file,
    then save as "ColoredSpecular.shader"
    then drag and drop the file into your project explorer.(unity)
    then create newMaterial and , into the inspector select your shader (./Surface/Colored Specular)

    here you are... enjoy.
     
    shinatoX1 likes this.
  5. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Isn't it simpler to take a built-in specular surface shader and override _SpecColor for each pixel?
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The built-in BlinnPhong lighting model declares _SpecColor outside the SurfaceOutput structure, as a uniform value. This means that you have to re-write the lighting model as Aras did in order to use a texture.
     
    Romaleks360 likes this.
  7. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    This works pretty well. Am I still incorrect? :)
    Code (csharp):
    1. Shader "Bumped ColoredSpecular" {
    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.    
    9.     _SpecTex ("Spec (RGB)", 2D) = "white" {}
    10. }
    11. SubShader {
    12.     Tags { "RenderType"="Opaque" }
    13.     LOD 400
    14.    
    15. CGPROGRAM
    16. #pragma surface surf BlinnPhong
    17.  
    18. sampler2D _MainTex;
    19. sampler2D _BumpMap;
    20. sampler2D _SpecTex;
    21. float4 _Color;
    22. //float4 _SpecColor;
    23. float _Shininess;
    24.  
    25. struct Input {
    26.     float2 uv_MainTex;
    27.     float2 uv_BumpMap;
    28. };
    29.  
    30. void surf (Input IN, inout SurfaceOutput o) {
    31.     half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    32.     half4 spectex = tex2D(_SpecTex, IN.uv_MainTex);
    33.     _SpecColor = spectex;
    34.    
    35.     o.Albedo = tex.rgb * _Color.rgb;
    36.     o.Gloss = tex.a;
    37.     o.Alpha = tex.a * _Color.a;
    38.     o.Specular = _Shininess;
    39.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    40. }
    41. ENDCG
    42. }
    43.  
    44. FallBack "Specular"
    45. }
    46.  
     
  8. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Nobody has not answered if this given shader from Aras is optimised ,and also does it supports normal maps?
     
  9. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
  10. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Ah ok thanks. No normals maps is not option for me then.
     
  11. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Could someone writte or extend shader code that Aras wrotte for colored specular shader for/with normal too?
     
  12. Deleted User

    Deleted User

    Guest

    This should work:
    Code (csharp):
    1.  
    2. Shader "Surface/Colored Specular Bumped" {
    3. Properties {
    4.   _MainTex ("Texture", 2D) = "white" {}
    5.   _SpecMap ("SpecMap", 2D) = "white" {}
    6.   _BumpMap ("Normalmap", 2D) = "bump" {}
    7. }
    8. SubShader {
    9.     Tags { "RenderType" = "Opaque" }
    10. CGPROGRAM
    11. #pragma surface surf ColoredSpecular
    12.  
    13. struct MySurfaceOutput {
    14.     half3 Albedo;
    15.     half3 Normal;
    16.     half3 Emission;
    17.     half Specular;
    18.     half3 GlossColor;
    19.     half Alpha;
    20. };
    21.  
    22.  
    23. inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    24. {
    25.   half3 h = normalize (lightDir + viewDir);
    26.  
    27.   half diff = max (0, dot (s.Normal, lightDir));
    28.  
    29.   float nh = max (0, dot (s.Normal, h));
    30.   float spec = pow (nh, 32.0);
    31.   half3 specCol = spec * s.GlossColor;
    32.  
    33.   half4 c;
    34.   c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
    35.   c.a = s.Alpha;
    36.   return c;
    37. }
    38.  
    39. inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light)
    40. {
    41.     half3 spec = light.a * s.GlossColor;
    42.    
    43.     half4 c;
    44.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    45.     c.a = s.Alpha + spec * _SpecColor.a;
    46.     return c;
    47. }
    48.  
    49.  
    50. struct Input {
    51.   float2 uv_MainTex;
    52.   float2 uv_SpecMap;
    53.   float2 uv_BumpMap;
    54. };
    55. sampler2D _MainTex;
    56. sampler2D _SpecMap;
    57. sampler2D _BumpMap;
    58.  
    59. void surf (Input IN, inout MySurfaceOutput o)
    60. {
    61.   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.3;
    62.   half4 spec = tex2D (_SpecMap, IN.uv_SpecMap);
    63.   o.GlossColor = spec.rgb;
    64.   o.Specular = 32.0/128.0;
    65.   o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    66. }
    67. ENDCG
    68. }
    69. Fallback "Diffuse"
    70. }
    71.  
     
  13. jasperstocker

    jasperstocker

    Joined:
    Jul 7, 2010
    Posts:
    429
    gaustwick - you rock.

    That is all.
     
  14. Deleted User

    Deleted User

    Guest

  15. b2c

    b2c

    Joined:
    Jan 11, 2012
    Posts:
    1
    thanx Aras and gaustwick for this shader! and thx janpec for bring up this topic! im working on a game with alot of sci fi materials, and the default unity bump specular doesnt show the normal mapped effects well at all! this one is much better! its very similar to the Maya Blinn shader than i am used to, awesome job guys! xD
     
  16. Deleted User

    Deleted User

    Guest

    I was asked for a version of this shader that supported illumination maps, so I had a go, and added shininess and global colour parameters whilst I was there :) The illumination map needs to be stuck in the specular map's alpha channel.
    Code (csharp):
    1.  
    2. Shader "Surface/Colored Specular Bumped with Illumination" {
    3. Properties {
    4.   _MainTex ("Texture", 2D) = "white" {}
    5.   _SpecMap ("SpecMap(RGB) Illum(A)", 2D) = "white" {}
    6.   _BumpMap ("Normalmap", 2D) = "bump" {}
    7.   _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    8.   _Color ("Main Color", Color) = (1,1,1,1)
    9. }
    10. SubShader {
    11.     Tags { "RenderType" = "Opaque" }
    12. CGPROGRAM
    13. #pragma surface surf ColoredSpecular
    14.  
    15. struct MySurfaceOutput {
    16.     half3 Albedo;
    17.     half3 Normal;
    18.     half3 Emission;
    19.     half Specular;
    20.     half3 GlossColor;
    21.     half Alpha;
    22. };
    23.  
    24.  
    25. inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    26. {
    27.   half3 h = normalize (lightDir + viewDir);
    28.  
    29.   half diff = max (0, dot (s.Normal, lightDir));
    30.  
    31.   float nh = max (0, dot (s.Normal, h));
    32.   float spec = pow (nh, 32.0 * s.Specular);
    33.   half3 specCol = spec * s.GlossColor;
    34.  
    35.   half4 c;
    36.   c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
    37.   c.a = s.Alpha;
    38.   return c;
    39. }
    40.  
    41. inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light)
    42. {
    43.     half3 spec = light.a * s.GlossColor;
    44.    
    45.     half4 c;
    46.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    47.     c.a = s.Alpha + spec * _SpecColor.a;
    48.     return c;
    49. }
    50.  
    51.  
    52. struct Input {
    53.   float2 uv_MainTex;
    54.   float2 uv_SpecMap;
    55.   float2 uv_BumpMap;
    56. };
    57.  
    58. sampler2D _MainTex;
    59. sampler2D _SpecMap;
    60. sampler2D _BumpMap;
    61. half4 _Color;
    62. half _Shininess;
    63.  
    64. void surf (Input IN, inout MySurfaceOutput o)
    65. {
    66.   half3 c = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color.rgb;
    67.   o.Albedo = c;
    68.   half4 spec = tex2D (_SpecMap, IN.uv_SpecMap);
    69.   o.GlossColor = spec.rgb;
    70.   o.Emission = c * spec.a;
    71.   o.Specular = _Shininess;
    72.   o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    73. }
    74. ENDCG
    75. }
    76. Fallback "Diffuse"
    77. }
    78.  
    And the same shader is in the attachments.
     

    Attached Files:

  17. pooter3001

    pooter3001

    Joined:
    Apr 5, 2012
    Posts:
    1
    The posted shaders are all very helpful for a school project that I am currently working on, thank you for the hard work all of you! ^^

    I have one thing that is missing from these shaders that would make it perfect for me, I have another regular shader that forces two-sided rendering(turns off culling). I've made attempts at combining the two shaders however they just don't seem compatable in any way(the double sided shader uses passes, while this shader doesn't. not sure on the specifics behind each, but neither seemed to work in the other >.>).

    The character I'm attempting to use this on has flat geometry that can be seen from both sides(cloak/ect.) and I would love to be able to use the colored specular shader with the double sided geometry if possible.
     
  18. krotana

    krotana

    Joined:
    May 9, 2012
    Posts:
    3
    I have been trying for the last 2-3 hours with no success, how can I add Cutout with Cull Off to this Shader that you guys have come up with? BTW this is great and I am exited to be able to use!

    This is what I am using, no Normals or Specularity do.

    Code (csharp):
    1. Shader "Transparent/Cutout/Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    10.     LOD 200
    11.     Cull Off
    12.  
    13. CGPROGRAM
    14. #pragma surface surf Lambert alphatest:_Cutoff
    15.  
    16. sampler2D _MainTex;
    17. fixed4 _Color;
    18.  
    19. struct Input {
    20.     float2 uv_MainTex;
    21. };
    22.  
    23. void surf (Input IN, inout SurfaceOutput o) {
    24.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    25.     o.Albedo = c.rgb;
    26.     o.Alpha = c.a;
    27. }
    28. ENDCG
    29. }
    30.  
    31. Fallback "Transparent/Cutout/VertexLit"
    32. }
     
  19. RickF

    RickF

    Joined:
    Sep 12, 2012
    Posts:
    33
    Attempting to use gaustwick's Colored Specular Bumped shader that i pulled from this thread, but the object seems to be rendering much darker than when using other shaders. Is this normal?

    A 90% white object is rendering a very dark grey color, other than that it seems to work great. When using the shader with added illumination included, the objects renders much more like it should. Any ideas as to what is causing this, or how to fix it? Should I just edit the shader w/ illumination and remove that property?
     
  20. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    The dark effect comes from the albedo line: o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.3;

    Remove the "*0.3", it doesn't belong there.
     
  21. RickF

    RickF

    Joined:
    Sep 12, 2012
    Posts:
    33
    Awesome thank you! Now if only it had a 'shininess' slider or preferably another texture input for a cosine/ glossiness map, then, perfection.
     
  22. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    This should do the trick (not tested though, but all you have to do is to add a float parameter and replace both "32" value in the shader by the parameter name. Also don't forget to declare the value as half) You can make it as a Range if you want, but that's not as good as a Float parameter since it is less precise (and equally efficient):

    Code (csharp):
    1. Shader "Surface/Colored Specular Bumped" {
    2.  
    3. Properties {
    4.  
    5.   _MainTex ("Texture", 2D) = "white" {}
    6.  
    7.   _SpecMap ("SpecMap", 2D) = "white" {}
    8.  
    9.   _BumpMap ("Normalmap", 2D) = "bump" {}
    10.  
    11.  _Shininess ("Specular Shininess", Float) = 32
    12.  
    13. }
    14.  
    15. SubShader {
    16.  
    17.     Tags { "RenderType" = "Opaque" }
    18.  
    19. CGPROGRAM
    20.  
    21. #pragma surface surf ColoredSpecular
    22.  
    23.  half _Shininess;
    24.  
    25. struct MySurfaceOutput {
    26.  
    27.     half3 Albedo;
    28.  
    29.     half3 Normal;
    30.  
    31.     half3 Emission;
    32.  
    33.     half Specular;
    34.  
    35.     half3 GlossColor;
    36.  
    37.     half Alpha;
    38.  
    39. };
    40.  
    41.  
    42.  
    43.  
    44.  
    45. inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    46.  
    47. {
    48.  
    49.   half3 h = normalize (lightDir + viewDir);
    50.  
    51.  
    52.  
    53.   half diff = max (0, dot (s.Normal, lightDir));
    54.  
    55.  
    56.  
    57.   float nh = max (0, dot (s.Normal, h));
    58.  
    59.   float spec = pow (nh, _Shininess);
    60.  
    61.   half3 specCol = spec * s.GlossColor;
    62.  
    63.  
    64.  
    65.   half4 c;
    66.  
    67.   c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
    68.  
    69.   c.a = s.Alpha;
    70.  
    71.   return c;
    72.  
    73. }
    74.  
    75.  
    76.  
    77. inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light)
    78.  
    79. {
    80.  
    81.     half3 spec = light.a * s.GlossColor;
    82.  
    83.    
    84.  
    85.     half4 c;
    86.  
    87.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    88.  
    89.     c.a = s.Alpha + spec * _SpecColor.a;
    90.  
    91.     return c;
    92.  
    93. }
    94.  
    95.  
    96.  
    97.  
    98.  
    99. struct Input {
    100.  
    101.   float2 uv_MainTex;
    102.  
    103.   float2 uv_SpecMap;
    104.  
    105.   float2 uv_BumpMap;
    106.  
    107. };
    108.  
    109. sampler2D _MainTex;
    110.  
    111. sampler2D _SpecMap;
    112.  
    113. sampler2D _BumpMap;
    114.  
    115.  
    116.  
    117. void surf (Input IN, inout MySurfaceOutput o)
    118.  
    119. {
    120.  
    121.   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    122.  
    123.   half4 spec = tex2D (_SpecMap, IN.uv_SpecMap);
    124.  
    125.   o.GlossColor = spec.rgb;
    126.  
    127.   o.Specular = _Shininess/128.0;
    128.  
    129.   o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    130.  
    131. }
    132.  
    133. ENDCG
    134.  
    135. }
    136.  
    137. Fallback "Diffuse"
    138.  
    139. }
     
    Last edited: Oct 18, 2012
  23. primat

    primat

    Joined:
    Jan 4, 2013
    Posts:
    14
    I am using the shader from aras. Thank you for that! It's exactly what I was looking for.

    But in one scene it works great and in another scene the whole model is black. In both scenes i'm using one directional light. Maybe the shader doesn't get the light in the second scene? How could i debug this?

    Are there any prerequisites to fulfil to use this shader?

    Edit: In the second scene the built-in diffuse shader is working.
     
    Last edited: Jan 4, 2013
  24. nipoco

    nipoco

    Joined:
    Sep 1, 2011
    Posts:
    2,008
    That is great!
    Is there any chance to get this colored specular bumb shader with a gloss slot to control the specularity?
     
  25. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    Just modify the shader by adding the gloss sections of a normal specular shader and that should be solved ;)
     
  26. Deleted User

    Deleted User

    Guest

    Sorry it took so long, as always, I have been ill. This shader should cover most eventualities:
    Code (csharp):
    1.  
    2. Shader "Surface/Colored Specular Bumped wGloss" {
    3. Properties {
    4.   _MainTex ("Texture(RGB) Gloss(A)", 2D) = "white" {}
    5.   _SpecMap ("SpecMap", 2D) = "white" {}
    6.   _BumpMap ("Normalmap", 2D) = "bump" {}
    7.   _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    8. }
    9.  
    10. SubShader {
    11.     Tags { "RenderType" = "Opaque" }
    12.  
    13. CGPROGRAM
    14.  
    15. #pragma surface surf ColoredSpecular
    16.  
    17. struct MySurfaceOutput {
    18.     half3 Albedo;
    19.     half3 Normal;
    20.     half3 Emission;
    21.     half Specular;
    22.     half3 GlossColor;
    23.     half Gloss;
    24.     half Alpha;
    25. };
    26.  
    27. inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    28. {
    29.   half3 h = normalize (lightDir + viewDir);
    30.  
    31.   half diff = max (0, dot (s.Normal, lightDir));
    32.  
    33.   float nh = max (0, dot (s.Normal, h));
    34.   float spec = pow (nh, s.Specular * 128.0f) * s.Gloss;
    35.   half3 specCol = spec * s.GlossColor;
    36.  
    37.   half4 c;
    38.   c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
    39.   c.a = s.Alpha;
    40.  
    41.   return c;
    42. }
    43.  
    44. inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light)
    45. {
    46.     half3 spec = light.a * s.GlossColor * s.Gloss;
    47.  
    48.     half4 c;
    49.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    50.     c.a = s.Alpha + spec * _SpecColor.a;
    51.  
    52.     return c;
    53. }
    54.  
    55. struct Input {
    56.   float2 uv_MainTex;
    57.   float2 uv_SpecMap;
    58.   float2 uv_BumpMap;
    59. };
    60.  
    61. sampler2D _MainTex;
    62. sampler2D _SpecMap;
    63. sampler2D _BumpMap;
    64. half _Shininess;
    65.  
    66.  
    67. void surf (Input IN, inout MySurfaceOutput o)
    68. {
    69.   float4 diff = tex2D (_MainTex, IN.uv_MainTex);
    70.   o.Albedo = diff.rgb;
    71.  
    72.   half4 spec = tex2D (_SpecMap, IN.uv_SpecMap);
    73.   o.GlossColor = spec.rgb;
    74.   o.Gloss = diff.a;
    75.   o.Specular = _Shininess;
    76.   o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    77. }
    78.  
    79. ENDCG
    80. }
    81.  
    82. Fallback "Diffuse"
    83. }
    84.  
     
  27. nipoco

    nipoco

    Joined:
    Sep 1, 2011
    Posts:
    2,008
    No problem bud!

    I really appreciate that :)

    Thanks!
     
  28. avoorhees

    avoorhees

    Joined:
    Aug 23, 2012
    Posts:
    5
    Hey All, great thread with a lot of good info.

    I am trying to create a shader which combines the colored spec from this thread with the TeamFortress2Shader toon shader found here http://wiki.unity3d.com/index.php/TeamFortress2Shader

    I've mostly got it working, but the one last thing I can't figure out is that I want to be able to control the gloss/polish/tightness of the specular highlight using a texture (namely the alpha of the specular texture) instead of a slider as it is now. Since I'm not an engineer, I don't really understand how to create/alter custom lighting models as found in these shaders, making it hard to do this!

    If anyone could help me out with this, I'd be hugely thankful!

    Code (csharp):
    1.  
    2. Shader "Toon_Colored_Spec" {
    3.     Properties {
    4.         _RimColor ("Fresnel Color", Color) = (0.26,0.19,0.16,0.0)
    5.         _RimPower ("Fresnel Power", Range(0.5,8.0)) = 3.0
    6.         _Shininess ("Polish", Range (.01, 5)) = 0.5
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.         _SpecMap ("Specular Color (RGB), Specular Polish (A)", 2D) = "white" {}
    9.         _BumpMap ("Normalmap", 2D) = "bump" {}
    10.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    11.     }
    12. SubShader {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 200
    15.  
    16. CGPROGRAM
    17. #pragma surface surf Toon
    18. #pragma target 3.0
    19.  
    20. sampler2D _SpecMap;
    21. sampler2D _MainTex;
    22. sampler2D _BumpMap;
    23. sampler2D _Ramp;
    24. float4 _Color;
    25. float4 _RimColor;
    26. float _RimPower;
    27. half _Shininess;
    28.  
    29. struct ToonSurfaceOutput {
    30.     half3 Albedo;
    31.     half3 Normal;
    32.     half3 Emission;
    33.     half Specular;
    34.     half3 GlossColor;
    35.     half Alpha;
    36. };
    37.  
    38. inline half4 LightingToon (ToonSurfaceOutput s, half3 lightDir, half3 viewDir, half atten){
    39.     fixed3 h = normalize (lightDir + viewDir);
    40.     fixed NdotL = dot(s.Normal, lightDir) * 0.5 + 0.5;
    41.     fixed3 ramp = tex2D(_Ramp, float2(NdotL * atten)).rgb;
    42.     float nh = max (0, dot (s.Normal, h));
    43.     float spec = pow (nh, s.GlossColor * _Shininess * 2048);
    44.     half3 specCol = spec * s.GlossColor;
    45.     fixed4 c;
    46.     c.rgb = ((s.Albedo * ramp * _LightColor0.rgb + _LightColor0.rgb * specCol) * (atten * 2));
    47.     return c;
    48. }
    49.  
    50. struct Input {
    51.     float2 uv_MainTex;
    52.     float2 uv_SpecMap;
    53.     float2 uv_BumpMap;
    54.     float3 viewDir;
    55.     INTERNAL_DATA
    56. };
    57.  
    58. void surf (Input IN, inout ToonSurfaceOutput o){
    59.     half4 specTex = tex2D (_SpecMap, IN.uv_SpecMap);
    60.     o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    61.     o.GlossColor = specTex;
    62.     o.Specular = _Shininess;
    63.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    64.     half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
    65.     o.Emission = _RimColor.rgb * pow (rim, _RimPower);
    66. }
    67.  
    68. ENDCG
    69.  
    70. }
    71. Fallback "Diffuse"
    72. }
    73.  
     
  29. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi Friends

    I am new in shader programming, I copied that shader and tried to my game, I assumes LightingColoredSpecular_PrePass method call by default at some time, I have checked with commenting this method and it;s will not gives any difference in device or editor mode.

    can any one describe how that method are utilised in shading language....?

    When I trying this shader, I also note one more thing, what ever surface i used this shader, those all surface are not able receive shadow in editor mode, but it receive shadow in Device (I am using iPad 3rd Gen with Unity 4.x)


    here my shader
    Code (csharp):
    1.  
    2. Shader "Custom/Vertex Sepcular with Specular Map" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _SpecColor ("Specular Color", Color) = (0.5859375, 0.5859375, 0.5859375, 1)
    6.         _Shininess ("Shininess", float) = 32
    7.  
    8.         _MainTex ("Texture (RGB)", 2D) = "white" {}
    9.         _BlendTex ("Detail Map (RGB), Gloss (A)" , 2D) = "white" {}
    10.         _BlendDetail("Detail Tiling",vector) = (1,1,1,1)
    11.         _Blend ("Blend Range",float) = 1
    12.     }
    13.     SubShader {
    14.         Tags { "Queue"="Geometry" "IgnoreProjector"="False" "RenderType"="Opaque"}
    15.         LOD 200
    16.         CGPROGRAM
    17.         #pragma surface surf ColoredSpecular
    18.         //vertex:vert
    19.         //finalcolor:mycolor
    20.         #include "UnityCG.cginc"
    21.  
    22.         fixed4 _Color;
    23.         sampler2D _MainTex;
    24.         sampler2D _BlendTex;
    25.         float _Blend;
    26.         float _Shininess;
    27.         float4 _BlendDetail;
    28.  
    29.         struct MySurfaceOutput {
    30.             half3 Albedo;
    31.             half3 Normal;
    32.             half3 Emission;
    33.             half Specular;
    34.             half3 Gloss;
    35.             half Alpha;
    36.         };
    37.  
    38.         struct Input {
    39.             float2 uv_MainTex;
    40.             float2 uv_BlendTex;
    41.         };
    42.         //void vert (inout appdata_full v, out Input o) {
    43.         //}
    44.         inline half4 LightingColoredSpecular_PrePass (MySurfaceOutput s, half4 light) {
    45.             half3 spec = light.a * s.Gloss;
    46.  
    47.             half4 c;
    48.             c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    49.             c.a = s.Alpha + spec * _SpecColor.a;
    50.             return c;
    51.         }
    52.        
    53.         inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)    {
    54.             half3 h = normalize (lightDir + viewDir);
    55.  
    56.             half diff = max (0, dot (s.Normal, lightDir));
    57.             float nh = max (0, dot (s.Normal, h));
    58.             float spec = pow (nh, s.Specular * 128);
    59.             half3 specCol = spec * s.Gloss;
    60.  
    61.             half4 c;
    62.             c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2) * _SpecColor;
    63.             c.a = s.Alpha;
    64.             return c;
    65.         }
    66.  
    67.         void surf (Input IN, inout MySurfaceOutput o) {
    68.             fixed4 mt = tex2D(_MainTex, IN.uv_MainTex);
    69.             float2 swapUVs = IN.uv_BlendTex ;
    70.             swapUVs *= _BlendDetail.xy;
    71.             fixed4 bt = tex2D(_BlendTex, swapUVs);
    72.  
    73.             o.Albedo = mt.rgb * (bt.rgb * _Blend) * _Color.rgb;
    74.             o.Gloss = bt.a;
    75.             o.Specular = _Shininess/128;
    76.         }
    77.         ENDCG
    78.     }
    79. }
    80.  
     
  30. neshius108

    neshius108

    Joined:
    Nov 19, 2015
    Posts:
    110
    Heya! Played around with the shaders here and they work pretty well!
    I was wondering if it was possible to pack Diffuse, Normals AND colored Spec sprites together, without having to have 1 material per Colored Bumped Spec Sprite.

    I know it's possible to use the Alpha channel of the Diffuse (thus dropping any semi-transparency) as the spec values but that is only for mono-chromatic specs. How about RGB specs? How could that be done?