Search Unity

Using my own lightmap

Discussion in 'Global Illumination' started by Mentaner, Jun 14, 2018.

  1. Mentaner

    Mentaner

    Joined:
    Jun 17, 2013
    Posts:
    20
    Hi I am having trouble finding a way to use my own pre unwraped lightmaps in UV1.
    Can someone point me in the right direction?
    Thanks
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    Unfortunately it's far from trivial to do this.
     
  3. Mentaner

    Mentaner

    Joined:
    Jun 17, 2013
    Posts:
    20
    I would love to understand the problem. I can do that in Unreal with the push of a button but in Unity I constantly encountering destorted lightmaps UVs. I have created my own lightmap UVs for many years now and i am not satisfied with the auto generation. Plese help
     
  4. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    So, to clarify: are you trying to use your own lightmaps in Unity, or are you trying to have Unity use your custom lightmap UVs?

    If it is the former, then the setup is pretty straightforward: author lightmap UVs in a 3D application of you choice and save them the second UV channel (UV2). After importing the asset with custom lightmap UVs into Unity, make sure that you have Generate Lightmap UVs unchecked in the mesh importer settings.
     
    Chen_Can_PW and Dhruvin3 like this.
  5. carodan

    carodan

    Joined:
    Jul 9, 2018
    Posts:
    1
    Was going to start a new thread but figured I'd continue this one for 2019

    Unity Version 2019.2.4f1
    CPU Progressive light mapper
    The scene object I'm experimenting with has 2 sets of UVs, one for diffuse textures and one for light maps.
    I've rendered out a nice light map in an external renderer and am trying to use it in Unity on my scene object.


    I'm also wanting to find a way to use my own externally light maps in Unity.
    Without some level of scripting (which I've been avoiding as it's not my strong suit), I've so far found 2 ways to do this (sort of) but neither really suits my needs or works reliably.

    1. The easiest is to just use my externally rendered light map in the emission slot of a shader and have it use the UVs I've created for this purpose. The obvious downside of this is that the standard shader can only have one set of UVs assigned to it and my object has 2 distinctly different sets of UV's. It also means I can't use the emissive channel for anything else - be better to be able to assign my map to the light mapper data.

    2. I can tell Unity to use my objects light map UVs rather than generating them itself. Theoretically I should be able to generate a progressive light mapper bake using these UVs (a quick bake at very low settings) and then save my external render over the top of the .exr Unity creates. I've actually done this successfully but can't seem to reliably repeat the process.
    The problem comes in trying to get Unity to use the full UV layout (mine fits perfectly in a square format with UV's touching all sides). Unity changes the size of my UV layout in the rendered light map depending on the light map resolution setting in the lighting menu, and/or the 'Scale in Light map' setting in the inspector.
    In my successful test I set the lightmap resolution to 1, the lightmap size to 2048, and the 'Scale in Light map' setting to 2048, and this rendered a quick map with my UV's filling the mapping space. My externally rendered image fit perfectly and looked good.
    In the latest test with all the same settings, Unity hangs before the end of the bake and I have to do a hard restart of my machine as everything becomes unresponsive.
    I've looked at this for days now and can't understand why this example fails when the first succeeds - hence the unreliable process.

    So, I'm wondering why this is a problem at all. Why can't we tell Unity to use our own external light maps on scene objects, or define how big our UVs are in a given light map?
    I suspect there may be a better per object (custom) shader option for using different textures with different UV allocations, but I've been reluctant to go down this route just yet.
    Reference on this seems scant for some reason, another reason why I suspect I may be coming at this from the wrong angle.

    Any suggestions or help greatly appreciated.
     
    JamesArndt likes this.
  6. Dhruvin3

    Dhruvin3

    Joined:
    Nov 9, 2018
    Posts:
    1

    thanks man you made my day
     
    kristijonas_unity likes this.
  7. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    Man I remember having this discussion in a lightmapping thread last year. Unity staff had said they might get to working on a "pass through UVs" setting for meshes. Would allow 3D application created UVs to pass through without being altered by Unity. I was getting very frustrated with Unity not leaving my 3ds Max generated lightmap UVs alone. It is always trying to shuffle and repack them and it screws up my ability to use my own lightmaps.



    @rasmusn Did Unity or yourself ever get around to implementing the "pass through UVs" for meshes in any of the newer Unity versions? I figured rather than digging through endless release notes I'd ping you first.
     
  8. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    As @kristijonas_unity mentioned above you can provide your own per-object UVs (they need to be stored in the second UV channel). However, do note that the lightmapper still rescales and translates your "UV square" into a final destination in the lightmap space. The rescaling happens in order to adhere to the desired Lightmap Resolution and Scale In Lightmap settings. For example, if the resolution is set to 10 texels per unit distance, then the "UV square" you provided may have to be scaled by a certain amount in lightmap space. The reason we also translate (move) your UVs in addition to scaling is simply that we need this to pack objects into the final lightmap.

    So although you can provide your own per-object unwrapping, you (currently) cannot determine the exact UV coordinates used at runtime (since Unity will scale and translation them as part of baking).

    Being able to "pass-through" your own UVs without Unity modifying them at all is a feature that has been request several years ago by @JamesArndt and others. We haven't yet been able to find the time to do it but we still want to. With the recent focus on quality and stability, I hope we can finally set aside time to do this.
     
  9. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Hi carodan! It seems you are asking about a lot of different things at once. I'll try to cover each topic. Let me know if I have missed anything.

    I don't know of a way to achieve what you want without some amount of scripting and/or shader programming. I think if you provide your own lightmaps then you also have to hook them up with your own shaders.

    One thing you could try is to manually replace the contents of LightmapSettings.lightmaps but that would require that your rendered lightmaps have the same layout as the Unity-baked lightmaps in the scene's Lighting Data Asset. I think you could make that work, but it would be messy. Perhaps you can find some inspiration in The Lightmap Switching Tool.

    I don't think this is something we support. The problem here is that the Lighting Data Asset references the actual Texture2D objects (and not the underlying asset) that was created at the end of a bake. When you replace the EXR file, the TextureImporter kicks in and creates a new Texture2D. The Lighting Data Asset will still be point to the old object (which may be freed at this point, I'm not sure).

    Ah, that's clever! Let me explain why I think this works. The effective lightmap resolution for a particular MeshRenderer is [Lightmap Resolution] * [Scale In Lightmap]. By setting it to a large value (1*2048 in your case) Unity will try to scale up your object's UVs to the point where your object receives 2048 texels per unit distance in the final lightmap. 2048 texels per meter is a lot (2048^2 texels per square meter!). So Unity will try to scale up your UVs a lot. However it ultimately has to fit into [Lightmap Size] which I guess that you have set to 2K, so therefore the object's UV square is clamped to fit into a 2K lightmap. The net result is that your single object will take up an entire 2K lightmap (with no translation). To test my hypothesis you can try to set Scale in Lightmap to 4096 or maybe even 1024 and see what happens. I think you will see the same result as with 2048 simply because even a 1024 value will make Unity try to scale the UV's to be larger than the 2K limit at which point the clamping kicks in (as described above). I'd love to hear the results if you try this out :).

    It seems to me you are asking two orthogonal questions here: one about user-generated lightmaps and one about UV authoring.

    Regarding user-generated lightmaps: The short answer is that this is simply something that we don't support (currently, at least). Whenever you add more flexibility to a system (such as the ability to provide custom lightmaps), you also add more complexity. I think when the lightmapping system was originally designed they focused on solving the problem of users baking lightmaps inside Unity (which I guess makes sense since this is the common case). But this does not mean you cannot provide you own lightmaps in Unity. You are free to provide your own lightmaps but then you also have to deal with the complexity of hooking them up in your own custom shaders. You probably know this, but you could just copy Unity's Standard shader (or something like that) and then modify it to use your lightmaps.

    Regarding UV authoring: Unity scales and translates the user-provided UVs in order to 1) achieve the desired Lightmap Resolution and 2) Pack the objects into a lightmap. Users have requested the ability to skip this step and instead have full control over the UVs. This is something that has been on our todo-list for a (way too) long time, unfortunately. Note that this feature is useful when you let Unity bake lightmaps and when you provide them yourself. Please also see my response to Mentaner just above.

    Finally I'd like to ask you something. Why are you using an external lightmap baker? I'm curious. I know we cannot fit everyone's needs but I am always interested to hear what caused people to opt out of Unity's own lightmapper.
     
    Last edited: Sep 17, 2020
  10. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    No, not yet, unfortunately. Personally, I would love us to have devoted more time to UV packing in the last year, but other things took priority. We did release the Auto Pack Margin feature but there's so much more that needs to be done (pass-through UVs included).

    With the recent focus on quality, I hope we will be allowed to set aside more time to improve and fix existing workflows (as opposed to adding new stuff).
     
    JamesArndt likes this.