Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Diffuse Detail Specular Normal Shader

Discussion in 'Shaders' started by pyromanci, May 14, 2014.

  1. pyromanci

    pyromanci

    Joined:
    Mar 21, 2011
    Posts:
    11
    I am in need of a Diffuse + Detail + Specular + Normal Shader. See i have a some material files i am wanting to bring over from a different engine in mtl format. While I don't mind recreating the material I just lack the shader needed.

    I did find one on the forums, but it was rather old (2.x) and unity 4 did not like it.

    Code (csharp):
    1. Shader "Diffuse Detail Specular Normal" {
    2.  
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    6.         _Detail ("Detail (RGB)", 2D) = "gray" {}
    7.         _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    8.         _SpecMap ("Spec map (RGB)", 2D) = "white" {}
    9.         _BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
    10.     }
    11.  
    12.     Category {
    13.         Blend AppSrcAdd AppDstAdd
    14.         Fog { Color [_AddFog] }
    15.  
    16.         // ------------------------------------------------------------------
    17.         // ARB fragment program
    18.  
    19.         SubShader {
    20.              // Ambient pass
    21.              Pass {
    22.                 Name "BASE"
    23.                 Tags {"LightMode" = "PixelOrNone"}
    24.                 Blend AppSrcAdd AppDstAdd
    25.                 Color [_PPLAmbient]
    26.                 SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    27.                 SetTexture [_Detail] {combine previous * texture DOUBLE, previous}
    28.              }
    29.            
    30.              // Vertex lights
    31.              Pass {
    32.                 Name "BASE"
    33.                 Tags {"LightMode" = "Vertex"}
    34.                 Material {
    35.                         Diffuse [_Color]
    36.                         Emission [_PPLAmbient]
    37.                 }
    38.                 Lighting On
    39.                 SetTexture [_MainTex] {constantColor [_PPLAmbent] Combine texture * primary DOUBLE, texture * primary}
    40.                 SetTexture [_Detail] { combine previous * texture DOUBLE, previous }
    41.              }
    42.            
    43.              // Pixel lights
    44.              Pass {
    45.                 Name "PPL" 
    46.                 Tags { "LightMode" = "Pixel" }
    47.                 CGPROGRAM
    48.                 #pragma vertex vert
    49.                 #pragma fragment frag
    50.                 #pragma multi_compile_builtin
    51.                 #pragma fragmentoption ARB_fog_exp2
    52.                 #pragma fragmentoption ARB_precision_hint_fastest
    53.                 #include "UnityCG.cginc"
    54.                 #include "AutoLight.cginc"
    55.  
    56.                 struct v2f {
    57.                     V2F_POS_FOG;
    58.                     LIGHTING_COORDS
    59.                     float3   uvK; // xy = main map, z = specular K
    60.                     float2   uv; // detail map
    61.                     float4   uv2; // bumpmap UV, specmap UV
    62.                     float3   viewDirT;
    63.                     float3   lightDirT;
    64.                 };
    65.  
    66.                 uniform float4 _MainTex_ST,_Detail_ST, _BumpMap_ST, _SpecMap_ST;
    67.                 uniform float _Shininess;
    68.  
    69.                 v2f vert (appdata_tan v)
    70.                 {  
    71.                     v2f o;
    72.                     PositionFog( v.vertex, o.pos, o.fog );
    73.                     o.uvK.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    74.                     o.uvK.z = _Shininess * 128;
    75.                     o.uv.xy =TRANSFORM_TEX(v.texcoord, _Detail);
    76.                     o.uv2.xy = TRANSFORM_TEX(v.texcoord, _BumpMap);
    77.                     o.uv2.zw = TRANSFORM_TEX(v.texcoord, _SpecMap);
    78.                     TANGENT_SPACE_ROTATION;
    79.                     o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );   
    80.                     o.viewDirT = mul( rotation, ObjSpaceViewDir( v.vertex ) ); 
    81.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    82.                     return o;
    83.                 }
    84.  
    85.                 uniform sampler2D _BumpMap;
    86.                 uniform sampler2D _MainTex;
    87.                 uniform sampler2D _Detail;
    88.                 uniform sampler2D _SpecMap;
    89.                 uniform float4 _LightColor0;
    90.  
    91.                 // Calculates Blinn-Phong (specular) lighting model
    92.                 inline half4 SpecularColorLight( half3 lightDir, half3 viewDir, half3 normal, half4 color, half4 specColor, float specK, half atten )
    93.                 {
    94.                     #ifndef USING_DIRECTIONAL_LIGHT
    95.                     lightDir = normalize(lightDir);
    96.                     #endif
    97.                     viewDir = normalize(viewDir);
    98.                     half3 h = normalize( lightDir + viewDir );
    99.  
    100.                     half diffuse = dot( normal, lightDir );
    101.  
    102.                     float nh = saturate( dot( h, normal ) );
    103.                     float spec = pow( nh, specK ) * color.a;
    104.  
    105.                     half4 c;
    106.                     c.rgb = (color.rgb * _ModelLightColor0.rgb * diffuse + _LightColor0.rgb * specColor.rgb * spec) * (atten * 2);
    107.                     c.a = _LightColor0.a * specColor.a * spec * atten; // specular passes by default put highlights to overbright
    108.                     return c;
    109.                 }
    110.  
    111.                 half4 frag (v2f i) : COLOR
    112.                 {        
    113.                     half4 texcol = tex2D( _MainTex, i.uvK.xy );
    114.                     texcol.rgb *= tex2D(_Detail,i.uv).rgb*2;
    115.                     half4 speccol = tex2D( _SpecMap, i.uv2.zw );
    116.                     float3 normal = tex2D(_BumpMap, i.uv2.xy).xyz * 2.0 - 1.0;
    117.  
    118.                     half4 c = SpecularColorLight( i.lightDirT, i.viewDirT, normal, texcol, speccol, i.uvK.z, LIGHT_ATTENUATION(i) );
    119.                     return c;
    120.                 }
    121.                 ENDCG
    122.              }
    123.         }  
    124.     }
    125.  
    126.     FallBack "Diffuse"
    127. }
    any help would great.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    Here is what I came up with. Hope it helps! :)

    Code (csharp):
    1.  
    2. Shader "Custom/Diffuse Detail Bumped Specular" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
    6.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     _Detail ("Detail (RGB)", 2D) = "gray" {}
    9.     _BumpMap ("Normalmap", 2D) = "bump" {}
    10. }
    11.  
    12. SubShader {
    13.     Tags { "RenderType"="Opaque" }
    14.     LOD 400
    15.    
    16. CGPROGRAM
    17. //#pragma target 3.0
    18. #pragma surface surf BlinnPhong
    19. #pragma exclude_renderers flash
    20.  
    21. sampler2D _MainTex;
    22. sampler2D _Detail;
    23. sampler2D _BumpMap;
    24. fixed4 _Color;
    25. half _Shininess;
    26.  
    27. struct Input {
    28.     float2 uv_MainTex;
    29.     float2 uv_Detail;
    30.     float2 uv_BumpMap;
    31. };
    32.  
    33. void surf (Input IN, inout SurfaceOutput o) {
    34.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    35.     fixed4 norm= tex2D(_BumpMap, IN.uv_BumpMap);
    36.     fixed4 detail=tex2D(_Detail,IN.uv_Detail);
    37.    
    38.     fixed4 c = tex * _Color;
    39.     c.rgb *= detail.rgb*2;
    40.     o.Albedo = c.rgb;
    41.     o.Gloss = tex.a;
    42.     o.Alpha = tex.a * _Color.a;
    43.     o.Specular = _Shininess;
    44.     o.Normal = UnpackNormal(norm);
    45. }
    46. ENDCG
    47. }
    48.  
    49. Fallback "Diffuse Detail"
    50. }
    51.