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

Using externally baked lightmaps in Unity 3.5

Discussion in 'Shaders' started by payneb25827, Aug 2, 2012.

  1. payneb25827

    payneb25827

    Joined:
    Jun 6, 2012
    Posts:
    4
    We are developing a project in Unity 3.5 and we want to maintain control over lightmaps by baking them in 3D Studio Max instead of using Beast. We are currently using the Legacy Lightmap Shader and the lightmaps are very faint (almost non-existent).

    Is there any way to import custom lightmaps into Beast (while preserving the existing UV layout) without having to bake lightmaps in Unity? If not, is there any way to adjust the Legacy shader (or custom shader) to better handle the lightmaps from 3D Studio Max?

    Thanks!
     
  2. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    You should be able to do this one of two ways:



    1) Use the Max lightmaps in place of Beast's within Unity's workflow.

    I may be missing a step but I think this is relatively straightforward. Lightmaps are defined per-scene, and referenced per-object. The relevant classes are:

    http://docs.unity3d.com/Documentation/ScriptReference/LightmapSettings.html < Enable lightmaps, pass the LightMapData instances with your lightmaps

    http://docs.unity3d.com/Documentation/ScriptReference/LightmapData.html < Wrapper class for the EXR files (please read http://docs.unity3d.com/Documentation/Manual/LightmappingInDepth.html for near/far explanation)

    http://docs.unity3d.com/Documentation/ScriptReference/Renderer-lightmapIndex.html < Tell your objects which map to look up.

    It's easy to assign lightmaps dynamically during level construction, and this is exactly the same except you're pointing to a self-made EXR. This only works if your EXR has lighting stored the way Unity expects, of course, but that should be easy enough to test.



    2) Disable internal lightmap via shader profile options, and just write your own support for your EXRs. This is also pretty simple, it's basically
    finalColor = textureColor * materialColor * lightmap.rgb * lightmap.alpha * someConstant;
    where someConstant varies by platform, and defaults to 8 on a PC (different for mobile).



    If MentalRay/VRay/whatever are writing EXRs such that they don't work for (1), try (2) and adjust the expansion until it looks right. You can just use that, or if you absolutely need to, you can always write a script to rebuild the EXRs with data in the correct format for Unity, probably after experiementing with (2).

    Good luck,
    -abm
     
  3. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    Renderer.lightmapTilingOffset < Tell your object which part of the lightmap to sample from.

    The only thing that's specific to those EXR files is that we store (-1.0f, -1.0f, -1.0f) in texels that don't have valid lighting information (are the background). That allows us to distinguish valid texels from the background when creating smaller mip levels (so that the background doesn't bleed into smaller mip levels) and also fill the whole background when going up the mip chain (so that when using bilinear filtering the object will never sample the background).

    Not quite. See UnityCG.cginc:

    Code (csharp):
    1. // Decodes lightmaps:
    2. // - doubleLDR encoded on GLES
    3. // - RGBM encoded with range [0;8] on other platforms using surface shaders
    4. inline fixed3 DecodeLightmap( fixed4 color )
    5. {
    6. #if defined(SHADER_API_GLES)  defined(SHADER_API_MOBILE)
    7.     return 2.0 * color.rgb;
    8. #else
    9.     // potentially faster to do the scalar multiplication
    10.     // in parenthesis for scalar GPUs
    11.     return (8.0 * color.a) * color.rgb;
    12. #endif
    13. }
    That said -- please use any of the lit builtin shaders (diffuse, bump spec, etc.) as they properly handle lightmap decoding. Please don't use the legacy shaders, as they are... legacy and should not be used anymore.
     
  4. Diego-de-Paula

    Diego-de-Paula

    Joined:
    Oct 10, 2012
    Posts:
    2
    Hi There!

    I'm facing the same situation as the author. I've created my lightmaps in Maya using V-ray. They were baked as ".exr" in separate render elements to provide for the Near and Far lightmaps automatically.

    Through some experimentation, I was able achieve my goal, by first baking the lightmaps inside of Unity and then, looking for the created textures and replacing them with the ones from Vray. Though it was all very clunky and cumbbersome, since I have to first bake unnecessary lightmaps on the objects I wanted to be lightmapped, just so I could indirectly "Activate" the lightmapping functionality for those objects within Unity's inner workings.

    Advice number (1) seems to be the answer I'm looking for. Since Unity now handles lightmaps in a "sort of" lower-level state, as oppose to previous to Beast. Therefore, being able to take control of the inner workings of this behaviour would provide the ideal solution for using external lightmaps in a seamless Unity workflow.

    The thing is... I really didn't make much out of the info provided. I have no idea how to create this script that will enforce my custom lightmaps on the objects I specify. I read all the info on the links provided but I simply don't know how to put it all together and how to go from here.

    PS: Can anyone save me?
     
    Last edited: May 28, 2013
  5. cadviz

    cadviz

    Joined:
    Jan 17, 2012
    Posts:
    28
    Had you read this..!? I hope it may help... :)
    http://forum.unity3d.com/threads/97877-Baking-lightmaps-with-Vray-and-3D-Studio-Max-a-tentative-guide
     
    Last edited: Dec 23, 2013
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The only problem I actually had with those legacy shaders is that they don't exclude ambient light. So I made my own set that excluded the ambient light. My light maps basically have the ambient term in them, so the added ambient in unity washes out the light map. (And I can't set the ambient in unity to zero, because I do want it to light up objects that don't have a light map.)

    If I don't use the built in light map system, why would I worry about improper light map decoding? Since I supply the light maps, I determine how they are encoded and how they should be decoded in the shader. Note that I don't mark the light map textures as being a light map. They are just textures.