Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Free - Normalmap AND lightmap?

Discussion in 'Shaders' started by Tiles, Feb 25, 2012.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I am stil at searching for a way to have both, lightmaps and normalmaps in Unity Free 3.42 (or also 3.5 in case the patch for the more drawcalls issues is available). By default and using the default shaders this doesn`t work.

    It seems that you need your own selfmade shader for that. As stated in the manual:

    So i grabbed Strumpy`s shader editor, modified the bumped diffuse shader and enabled the dualforward surface shader directive there. Which can be done by one checkbox in the Settings / Color and Lighting Settings, called Forward Dual Lightmaps.

    Then i grabbed my level, have redone the lightmapping, using dual lightmaps and use dual in forward rendering, and have applied the shader. As a result the whole geometry turned darker. Lightmaps are hardly to see now, and bump effect is invisible. Useless result.

    I reduced the problem to one object. The near lightmap does look odd. Pure grey. This is not what i want. I search for a way to display bump and lightmap at the same time. When the near lightmap would be the same than the far lightmap then we would talking. But it doesn`t really help me when i can have either the lightmapping or the normalmapping.

    I`ve attached the reduced example, made in 3.42. Could somebody point me into the right direction here, please? Have i overlooked some settings? Is there a way to use lightmaps together with normalmaps in Unity free at all? HOW?
     
  2. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    One step closer, but still not happy. I have copied the far lightmap and have replaced the near lightmap by that. Now i have lightmapping and normalmapping, which shows that the shader works. But the whole thing is much too bright. See screenshot.

    I cannot edit the lightmap because i haven`t found a software to do that. What software is able to open exr please? Blender was the only one i have that at least opens the exr. But Blender saves the exr file in blue then. No clue if it`s a bug or wrong settings. Exr is a odd format.

    Somebody an idea how to get this to work? What software do i need to modify the exr file? Or do i need to modify the shader?

    Halp :(
     
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Okay, a few experiments later i have to say the method is at least working. Just the brightness is still an issue. I have to retweak the whole light setting of the scene for that. And to replace the Near lightmaps with the Far lightmaps after baking is also a bit cumbersome. The working bump mapping is an advantage though. So it`s worth the hassle :)

    What was funny is that in my level i have added a second point light. So i have two of them to light the scene now. One at Auto, one at Realtime Only. Which gives a good mix for the normalmapping versus brightness because the Realtime Only light doesn`t longer light the lightmapped geometry. Gives of course a fewer bump effect. In my experimental scene here though this doesn`t work. Dunno why. Here the brightness is the same for Point Lights, no matter how the light is set up.

    Anyways, Problem solved :)

    Now let`s have a look to create a diffuse bumped detail shader that works this way here ...
     
  4. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Bad news. I´ve made this example with 3.42. The by Strumpy created shader does not work in Unity 3.5 though. And i am planning to upgrade my project at one point. Which means this method here is no go with the current shader.

    What i get is a pink mesh and a bunch of warnings after conversion. See shot. The funny thing is that the shader code ends at line 116, and the debugger tells me about an error at line 190.

    And when i close and reopen the project then i get just this warning here:

    Code (csharp):
    1. Material doesn't have a texture property '_Normal'
    2. UnityEditor.DockArea:OnGUI()
    I know that the lighting engine has changed in 3.5. The question is, what especially has changed? I am lost with writing shader code, that`s why i use Strumpy`s shader editor. Has somebody an idea how to fix this issue? Or do i need to wait for a Strumpy Shader Editor version for 3.5?
     
  5. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    It is possible to use beast lightmaps in combination with realtime lights, so there is no need for this quirky workaround with dual lightmaps.

    The following shader uses the lightmap directly and deactivates the Unity lightmap handling:
    Code (csharp):
    1. Shader "Custom\BumpedLightmapped" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Dummy ("Dummy", 2D) = "white" {}
    5.         _BumpMap ("Normalmap", 2D) = "bump" {}
    6.        
    7.     }
    8.    
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert nolightmap
    15.  
    16.         sampler2D _MainTex;
    17.         sampler2D unity_Lightmap;
    18.         float4 unity_LightmapST;
    19.         sampler2D _Dummy;
    20.         sampler2D _BumpMap;    
    21.        
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.             float2 uv2_Dummy;
    25.             float2 uv_BumpMap;
    26.         };
    27.  
    28.         void surf (Input IN, inout SurfaceOutput o) {
    29.        
    30.             fixed4 tex  = tex2D (_MainTex, IN.uv_MainTex);
    31.             float2 lmuv = IN.uv2_Dummy.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    32.             o.Albedo    = tex.rgb * DecodeLightmap( tex2D (unity_Lightmap, lmuv) );
    33.             o.Alpha     = tex.a;
    34.             o.Normal    = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));    
    35.         }
    36.         ENDCG
    37.     }
    38.     FallBack "Diffuse"
    39. }
    I don't know if there is any way to get a surface shader to supply lightmap UVs, therefore this workaround with a dummy texture.

    However, you still have to fiddle with your lighting setup as normal mapping requires some amount of realtime lighting.
     
    Last edited: Mar 1, 2012
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Awesome. Now that would be nice to have. Unfortunately i do something wrong here it seems. No matter what i do, with or without generate lightmap UV´s, i get a result with not fitting lightmap. Dummy needs to stay clear, right? Could you please help me out here? :)
     
  7. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    Hi Tiles,

    I have updated the shader to support lightmap atlas, see above.
    P.S. Yeah dummy has to stay clear with Tiling 1 and Offset 0

    If you don't want to use realtime lights, you could use a cubemap to create a bumped effect.
    To create a cubemap for lighting purposes, use an inverted sphere with all the lighting applied and render your cubemap from inside(center) of this sphere.
     
    Last edited: Mar 1, 2012
  8. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Perfect. Now it works as thougth. Many thanks for your help and tips :)

    Regarding the cubemap idea, that would be another shader then, right? I think i am settled with the current method for now. But the cubemap idea is definitely stored.The only thing i may reinvestigate is if the quirky dual lightmap method may be better for performance. But that`s a thing to investigate when the by Strumpy created shader works in 3.5 :)
     
  9. ilya_ca

    ilya_ca

    Joined:
    Nov 19, 2011
    Posts:
    274
    Thank you man, you saved my life! :)
     
  10. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    This shader works great! Thanks BIG BUG! The problem is that there is no way to use it for terrain :"(
     
  11. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Why not use a two light setup? Bake the scene with a baked only light, then user a realtime only light that picks up the normal maps. Simple.
     
  12. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    O... M... G..., that's so simple, why didn't I think of that... *Head Explodes*
     
  13. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Well, I tried it and doesn't work.
     
    Last edited: Dec 1, 2012
  14. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Bringing this back up- it does work, and it works just fine for me.
     
  15. zhaoqingMal

    zhaoqingMal

    Joined:
    Dec 24, 2013
    Posts:
    5
    Is there any chance you could give some step-by-step instructions on making this work, please?

    Cheers,
    Malcolm