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

Move existing Lightmap UV2s to a new UV4 set for an Editor Combined Mesh

Discussion in 'Global Illumination' started by Quatum1000, May 17, 2019.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Hi everyone,

    I require a copy of a "already created light map UV2" into UV4 Set

    The "already generated light map UV2" contains a single AO-Map only.
    This UV2 Set I want to transfer to UV4. Then render it with a custom shader.

    Described here: https://forum.unity.com/threads/custom-uv-sets-for-shaders-tutorial.645670/

    Code (CSharp):
    1. Unwrapping.GenerateSecondaryUVSet()
    will create a secondary UV2 set.

    1) But how to create an additionally UV4 set. And then copy the existing Lightmap UV2 set correctly to UV4? Does anyone how that can be done?

    Another problem seems clearing bake will flush per-renderer unity_lightmapST variable, that changes the lightmapScaleOffset and lightmapIndex property.

    In the end I want to have a resistant UVset that contains a single AO-Map per renderer, can be rendered through a custom shader.

    Thank a lot
     
    Last edited: May 17, 2019
  2. Kuba

    Kuba

    Unity Technologies

    Joined:
    Jan 13, 2009
    Posts:
    416
    Hey @Quatum1000,

    First copy your UV2 to UV4 using those two properties:
    https://docs.unity3d.com/ScriptReference/Mesh-uv2.html
    https://docs.unity3d.com/ScriptReference/Mesh-uv4.html
    and then call Unwrapping.GenerateSecondaryUVSet(), as that will override UV2.

    Where are you creating your AO? In your 3D modelling package? Are you packing it into an atlas that matches what Unity generated during the bake and placed into lightmapScaleOffset and lightmapIndex?
    I'm trying to understand:
    a. why do you even need those properties
    b. why are you clearing the bake

    Cheers!
     
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Hey @Kuba Thanks for you answers.

    We must use project based day-night cycle on a large outdoor environment. (Keep this in mind)
    Real-time global illumination is NOT possible in this case.

    1) We bake AO-Maps by UNITY onto each Scene part with RelatimeGlobalIllumnation = Off.
    Buildings of a scene contain Interiors as well. These are loaded separately on player position.

    Untitled-2.jpg

    2) After the AO Map baking processed by UNITY, we optimize the AO maps by a software process that results in a superior visual result. Then want to move the complete Unity based (untouched) UV2 to any independent and resistant UV_X Set.

    An independent UV set is required, because now we let UNITY bake the environment lightning Sky again (without AO) to have the ability to change the Environment Lightning (probe) during the day to night cycle.
    Untitled-1.jpg

    In case of the whole production process, it's much more efficient, convenient and cost-efficient to bake in Unity with a software baking optimization, than baking external.

    But when copying UV2 to UV4 and using a custom shader it works well as long unity_LightmapST.xy + unity_LightmapST.zw are not flushed, by any rebake.

    Code (CSharp):
    1. fixed3 rgb = DecodeLightmap(tex2D(_UNITY_UV2_AO, IN.uv2_UNITY_UV2_AO * unity_LightmapST.xy + unity_LightmapST.zw)).rgb;
    2. o.Emission = rgb * 2; // Visibility Test
    3.  
    Can get the current unity_LightmapST.xy and unity_LightmapST.zw per c#`? LightmapST is not a part of any LightmapSettings API.

    Or do you have an other idea to solve this?
     
    Last edited: May 22, 2019