Search Unity

Normal + Displacement + Occlusion + Specular + Diffuse Map In One Shader How to ?

Discussion in 'Shaders' started by Lucian-caetano, May 18, 2013.

  1. Lucian-caetano

    Lucian-caetano

    Joined:
    Dec 20, 2012
    Posts:
    2
    Hello everione,

    I need to use the Normal, Displacement, Occlusion, Specular, Diffuse Map in only a shader/texture how i start without a external script or tools like Substance ?

    I can't use Subshaders for compatibility reasons.



    Thanks for any productive reply or tips!
     
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    I think a relief shader with self shadow is the closest one,
    But I dont think that shader is in Unity's build_in shaders.
     
  3. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    I adjusted the Parallax Specular shader;

    Code (csharp):
    1. Shader "Parallax Specular Occlusion" {
    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.01, 1)) = 0.078125
    6.     _Parallax ("Height", Range (0.005, 0.08)) = 0.02
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     _BumpMap ("Normalmap", 2D) = "bump" {}
    9.     _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    10.     _Occlusion ("Occlusion Map", 2D) = "white" {}
    11. }
    12. SubShader {
    13.     Tags { "RenderType"="Opaque" }
    14.     LOD 600
    15.    
    16. CGPROGRAM
    17. #pragma surface surf BlinnPhong
    18. #pragma target 3.0
    19.  
    20. sampler2D _MainTex;
    21. sampler2D _BumpMap;
    22. sampler2D _ParallaxMap;
    23. sampler2D _Occlusion;
    24. fixed4 _Color;
    25. half _Shininess;
    26. float _Parallax;
    27.  
    28. struct Input {
    29.     float2 uv_MainTex;
    30.     float2 uv_BumpMap;
    31.     float3 viewDir;
    32. };
    33.  
    34. void surf (Input IN, inout SurfaceOutput o) {
    35.     half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
    36.     float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    37.     IN.uv_MainTex += offset;
    38.     IN.uv_BumpMap += offset;
    39.    
    40.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    41.     fixed4 Occ = tex2D(_Occlusion, IN.uv_MainTex);
    42.     o.Albedo = tex.rgb * _Color.rgb * Occ.rgb;
    43.     o.Gloss = tex.a;
    44.     o.Alpha = tex.a * _Color.a;
    45.     o.Specular = _Shininess;
    46.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    47. }
    48. ENDCG
    49. }
    50.  
    51. FallBack "Bumped Specular"
    52. }
    53.  
    changes were on lines: 1,10,23,41,42

    Just hit Rightmouse>Create new shader and copy-paste this into the new shader file when you open it in mono ;)
    Let me know if it's what you needed!
     
    pottering likes this.
  4. Lucian-caetano

    Lucian-caetano

    Joined:
    Dec 20, 2012
    Posts:
    2
    Thanks i will do it.

    I believe it is not only me who had this question,.
    Works greak
     
    Last edited: May 22, 2013
  5. EddieChristian

    EddieChristian

    Joined:
    Oct 9, 2012
    Posts:
    725
    Wow is there any way to get this with a slot for an actual specular map. Seems nearly perfect for what I need.
     
  6. Borzi

    Borzi

    Joined:
    Apr 13, 2013
    Posts:
    26
    Yes I'd need this with a specular slot as well. That would look so good!
     
  7. MrNick01

    MrNick01

    Joined:
    Dec 2, 2013
    Posts:
    2
    Acctually, it really doesn't... with all the other maps at play, I barely see a difference. So, I made a shader with everything I could think of... tessellation, diffuse, gloss, ao, specularity, normal. And well, considering this is the first shader I've written in my life... I'd somewhat proud of myself :)
    Here's the code, it should be noted that (a) the glossmap is a separate texture and (b) the glossmap and heightmap require the data to be on the a channel... shouldn't be a big deal:
    Code (csharp):
    1.  
    2. Shader "Tessellation/Diffuse Bumped Specular Gloss AO" {
    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.     _Parallax ("Height", Range (0.0, 1.0)) = 0.5
    8.  
    9.     _MainTex ("Base (RGB)", 2D) = "white" {}
    10.     _GlossMap ("Glossmap (A)", 2D) = "gloss" {}
    11.     _BumpMap ("Normalmap", 2D) = "bump" {}
    12.     _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    13.     _SpecMap ("Specular map", 2D) = "spec" {}
    14.     _Occlusion ("Occlusion Map", 2D) = "white" {}
    15.  
    16.     _EdgeLength ("Edge length", Range(3,50)) = 10
    17. }
    18. SubShader {
    19.     Tags { "RenderType"="Opaque" }
    20.     LOD 800
    21.    
    22. CGPROGRAM
    23. #pragma surface surf BlinnPhong addshadow vertex:disp tessellate:tessEdge
    24. #include "Tessellation.cginc"
    25.  
    26. struct appdata {
    27.     float4 vertex : POSITION;
    28.     float4 tangent : TANGENT;
    29.     float3 normal : NORMAL;
    30.     float2 texcoord : TEXCOORD0;
    31.     float2 texcoord1 : TEXCOORD1;
    32. };
    33.  
    34. float _EdgeLength;
    35. float _Parallax;
    36.  
    37. float4 tessEdge (appdata v0, appdata v1, appdata v2)
    38. {
    39.     return UnityEdgeLengthBasedTessCull (v0.vertex, v1.vertex, v2.vertex, _EdgeLength, _Parallax * 1.5f);
    40. }
    41.  
    42. sampler2D _ParallaxMap;
    43.  
    44. void disp (inout appdata v)
    45. {
    46.     float d = tex2Dlod(_ParallaxMap, float4(v.texcoord.xy,0,0)).a * _Parallax;
    47.     v.vertex.xyz += v.normal * d;
    48. }
    49.  
    50. sampler2D _MainTex;
    51. sampler2D _BumpMap;
    52. sampler2D _GlossMap;
    53. sampler2D _SpecMap;
    54. sampler2D _Occlusion;
    55. fixed4 _Color;
    56. half _Shininess;
    57.  
    58. struct Input {
    59.     float2 uv_MainTex;
    60.     float2 uv_BumpMap;
    61.     float2 uv_GlossMap;
    62.     float2 uv_SpecMap;
    63. };
    64.  
    65. void surf (Input IN, inout SurfaceOutput o) {
    66.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    67.     fixed4 gTex = tex2D(_GlossMap, IN.uv_GlossMap);
    68.     fixed4 Occ = tex2D(_Occlusion, IN.uv_MainTex);
    69.     fixed4 specTex = tex2D(_SpecMap, IN.uv_SpecMap);
    70.     o.Albedo = tex.rgb * _Color.rgb * Occ.a;
    71.     o.Gloss = gTex.a;
    72.     o.Alpha = gTex.a * _Color.a;
    73.     o.Specular = _Shininess * specTex.g;
    74.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    75. }
    76. ENDCG
    77. }
    78.  
    79. FallBack "Bumped Specular"
    80. }
    81.  
    Tell me what you think guys. I'm actually pretty interested to know how I did...

    EDIT: My bad guys, had a typo on lines 69 and 71. Fixed it now though, hope it didn't cause any problems.
     
    Last edited: Dec 4, 2013
    Sungold and pottering like this.
  8. Afassolas

    Afassolas

    Joined:
    Apr 10, 2013
    Posts:
    78
    Nice going MrNick01, but since you made the Specular map work without the need to be in the a channel, how come you didn't do the same for gloss and heightmap? I should think it's an easy deal to do it. by the way put them in the a channel of diffuse map?
     
    Last edited: Jun 20, 2014
  9. Deleted User

    Deleted User

    Guest

    Something to keep in mind. Ambient occlusion is only supposed to be applied to the ambient lighting contribution. In the current version of your shader, you are applying it to both Direct and Ambient.
     
  10. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Is that so? Would you have a reference for that assertion? Knowing what AO does, I fail to see how that adds value only in indirectly-lit areas but none whatsoever in directly-lit areas?
     
  11. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Oh wait, I guess I got it --- this is about per-geometry "AO" (aka cavity?) maps, rather than global-illumination "AO" like for example SSAO amirite?
     
  12. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Thats right, but i believe the theory behind it is bigger than SSAO versus AO maps

    Ambient Occlusion darkens places that are occluded, but why should a directly lit place be darkened? shadows are the absence of light, if there's light it shouldn't have shadows. But to be fair AO is yet another approximation of a phenomenon in light theory. I've yet to see a good SSAO, most of the time the scene gets darker for no good reason. It would be nice to see a new kind of SSAO that only works in shadows and respects the lights in the scene. But i guess we'll have to wait for realtime raytracing.
     
  13. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    The way I first heard per-material cavity maps explained was "marking occluding areas where light is trapped" so I guess you'd apply that "AO" on a surface even when it is supposedly directly lit --- to actually prevent (or lessen) the direct light reaching that "cavity" area, just like a shadow would. So only ambient would reach there I guess..

    But I can see why it makes sense to apply global SSAO only to indirectly lit areas, which of course includes all shadowed areas. Will keep that in mind for my AO later on, in my custom/homegrown "deferred" setup it'll be trivial to restrict AO to only those areas.

    I have a different view, even mediocre/implausible SSAO is a big win over no AO at all. For me it's not primarily about better or more realistic lighting and shadowing per se, but simply improving depth perception, it's a fantastic spatial cue that I find absolutely essential to 3D scenes. As long as the cam is moving, parallax helps to grasp the , but AO adds the ultimate depth hint to the human eye, no matter how accurate.
     
  14. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Don't get me wrong, what i meant from good/bad SSAO is the technical part of the contribution to the light setup, when creating assets for a scene that you know is going to have SSAO you have to account for a darker environment, it's a pain trying to be correct and beautiful at the same time, but i completely agree on the whole depth perception

    Maybe you'll find this interesting it's a simple concept completely dedicated to the depth perception of a scene although lacking any real physical explanation, it's also cheaper than SSAO, i wouldn't trade it for SSAO but could be useful in mobile
     
  15. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Very interesting paper, much appreciated! Yeah looks pretty cool. I think they darken too strongly in their examples, but if used very lightly, should work quite nicely to complement other techniques. Will give it a go (in a week or 3 :p ) and report back :D
     
    kebrus likes this.