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

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: Custom UV Sets for Shaders (tutorial)

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

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!

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.

4565005--424363--Untitled-2.jpg

  1. 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.
4565005--424351--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.

fixed3 rgb = DecodeLightmap(tex2D(_UNITY_UV2_AO, IN.uv2_UNITY_UV2_AO * unity_LightmapST.xy + unity_LightmapST.zw)).rgb;
o.Emission = rgb * 2; // Visibility Test

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?