Search Unity

Bump color spec shader not working in Unity3.0

Discussion in 'Shaders' started by sgv3dmax, Oct 5, 2010.

  1. sgv3dmax

    sgv3dmax

    Joined:
    Oct 20, 2009
    Posts:
    5
    I tried creating the shader from here:
    http://www.unifycommunity.com/wiki/index.php?title=BumpColorSpec
    and it gives an error - Upgrade NOTE: SubShader commented out; uses 2.x per-pixel lighting. You should rewrite shader into a Surface Shader at line 19
    I am an artist, without any scripting knowledge. If somebody can help, it would be awesome!
    I wonder why unity 3 has no built-in shader with option for specular maps. Every 3d application / most game engines has it. :-(
     
  2. oblivionfeet

    oblivionfeet

    Joined:
    Jul 24, 2010
    Posts:
    481
    Err.. have you even looked at the shaders that are built into Unity? They pretty much all have specular+color+amount+gloss.
     
  3. sgv3dmax

    sgv3dmax

    Joined:
    Oct 20, 2009
    Posts:
    5
    There is specular color, but I need to plug-in a texture for the specularity. This will allow me to fine-tune specular channel. I wonder why Unity does not bundle this shader. Almost every 3d application has it. Specular maps are very common in gaming...:-(
     
  4. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    funny,i posted a thread about this like 10 minutes ago.
    but yeah,its an odd decision as uniform spec color pretty much forces you to give every different surface its own material and drawcall and they will still look mediocre at best
     
  5. sgv3dmax

    sgv3dmax

    Joined:
    Oct 20, 2009
    Posts:
    5
    So, is it possible to write a shader for this? Any answers? I know how to create a shader (noob), but does not have scripting knowledge. So, any help would be appreciated.
     
  6. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    yeah,you can write a shader. my original approach was still correct,but the test shader was apparently too complex for the compiler. the builtin shaders work fine,here's one now:

    Code (csharp):
    1.  
    2. Shader "Basic Surface" {
    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.01, 1)) = 0.078125
    7.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.     _Parallax ("Height", Range (0.005, 0.08)) = 0.02
    9.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" { }
    10.     _SpecTex ("Spec(RGB)", 2D) = "white" { }
    11.     _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    12.     _BumpMap ("Normalmap", 2D) = "bump" { }
    13.     _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    14. }
    15. SubShader {
    16.     Tags { "RenderType"="Opaque" }
    17.     LOD 600
    18.    
    19. CGPROGRAM
    20. #pragma surface surf Custom
    21. #pragma target 3.0
    22.  
    23. sampler2D _MainTex;
    24. sampler2D _SpecTex;
    25.  
    26. sampler2D _BumpMap;
    27. samplerCUBE _Cube;
    28. sampler2D _ParallaxMap;
    29.  
    30. float4 _Color;
    31. float4 _ReflectColor;
    32. float _Shininess;
    33. float _Parallax;
    34.  
    35.  
    36. struct SurfaceOutputCustom {
    37.     half3 Albedo;
    38.     half3 Normal;
    39.     half3 Emission;
    40.     half3 Reflection;
    41.     half Specular;
    42.     half3 Gloss;
    43.     half Alpha;
    44. };
    45.  
    46. inline half4 LightingCustom_PrePass (SurfaceOutputCustom s, half4 light)
    47. {
    48.     half3 spec =(s.Gloss)*light.a ;
    49.     //float spec = pow (nh, s.Specular*128.0) * s.Gloss;
    50.  
    51.     half4 c;
    52.     c.rgb = (s.Albedo * light.rgb + light.rgb * _SpecColor.rgb * spec);
    53.     c.a = s.Alpha + spec * _SpecColor.a;
    54.     return c;
    55. }
    56.  
    57. struct Input {
    58.     float2 uv_MainTex;
    59.     float2 uv_SpecTex;
    60.     float2 uv_BumpMap;
    61.     float3 worldRefl;
    62.     float3 viewDir;
    63.     INTERNAL_DATA
    64. };
    65.  
    66. void surf (Input IN, inout SurfaceOutputCustom o) {
    67.     half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
    68.     float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    69.     IN.uv_MainTex += offset;
    70.     IN.uv_BumpMap += offset;
    71.     half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    72.  
    73.     half3 spec = tex2D(_SpecTex, IN.uv_SpecTex).rgb;
    74.     o.Albedo = tex.rgb * _Color.rgb;
    75.     o.Gloss = spec ;
    76.     o.Specular = _Shininess;
    77.    
    78.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    79.    
    80.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    81.     half4 reflcol = texCUBE (_Cube, worldRefl);
    82.     //reflcol.rgb *= o.Gloss;
    83.     o.Emission = reflcol.rgb * _ReflectColor.rgb*_ReflectColor.a*o.Specular*o.Gloss;
    84.     o.Alpha = reflcol.a * _ReflectColor.a;
    85. }
    86. ENDCG
    87. }
    88.  
    89. FallBack "Reflective/Bumped Specular"
    90. }
    91.  
    92.  
    93.  
     
  7. sgv3dmax

    sgv3dmax

    Joined:
    Oct 20, 2009
    Posts:
    5
    Thanks, multivac. i tried the shader, but it does not seem to have any effect on the specular map. It appears to be the same before and after the spec map is applied. I am using unity indie version. Does it have any effect on this?
     
  8. sgv3dmax

    sgv3dmax

    Joined:
    Oct 20, 2009
    Posts:
    5
    I was expecting some answer in this forum, but got no reply yet. Reading the documentation I found out that if the specular map is used in the alpha channel of the diffuse texture (TGA works) Unity uses the information to define which areas of the object are more reflective than others, thus producing a similar result of a specular map. Thought I would share this info. Thanks.
     
  9. SQR

    SQR

    Joined:
    Jun 29, 2010
    Posts:
    10
    The shader multivac did work for me (thanks mulitvac !) And thank you sgv3dmax for the note about how Unity reads the specularity from the alpha of the diffuse map, I think I read that somewhere but how does that work if I need to use a cutout map since it takes the alpha from the diffuse too?
     
  10. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    In built-in bumped specular shader gloss is taken from alpha channel in _MainTex, as far as I remember.
    If you want colored specular, then you can (i think) edit the code in shader to take into account .rgb in a separate texture instead of taking into account .a from the _MainTex
     
  11. Revoluzifer

    Revoluzifer

    Joined:
    Jun 29, 2010
    Posts:
    10
    I really dunno, what is the basic sense of a specular map...
    But it seems that I may have written something like that :D
    Needed it for a university project.
    What it does:
    You've got 3 texture inputs. The base texture, one normal(bump)map and the specular map. Besides there is a range-selector, which controls emission in specular areas.
    So the specular map controls, which areas might be specular (!= black means it does reflect) and the reflective color is controlled by the specular map's rgb.
    May this fit your needs? If you want to, I can post it this evening or tomorrow...
    Hope I did explain it clearly ;)