Search Unity

Transferring Atlas Mapping to UV2

Discussion in 'Editor & General Support' started by Filto, Apr 17, 2013.

  1. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    Hi.

    I'm trying to figure out how to transfer the atlas mapping to UV2 in Unity after I have baked lights.

    Basically what I want to do is. Bake lightmaps for my scene in Unity an use these lightmaps with my own shaders instead of Unity.

    If I bake the lightmaps, copy them to a different location I can clear the lightmapping and still have the lightmaps stored. Everything fine there.

    If I clear the lightmapping the UV2 with correct atlas placement is gone though so I can't really use the maps.
    I noticed that if I check the "lock atlas" though before I clear lightmapping I can export the mesh and the UV2 is correct in the exported version but for for some reason its still wrong in Unity? Superweird

    Ofcourse I can export everything and reimport it again but it is a kinda tedious process especially since I get alot of other problems exporting the mesh.

    Soooo... any ideas of a workflow for utilizing the lightbaking in Unity without having to apply them the "Unityway"
     
  2. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    Did you apply tiling/offset to your lightmap?
    Here is a simple shader which uses the Beast lightmap:
    Code (csharp):
    1. Shader "Custom/Lightmapped" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5. SubShader {
    6.     Pass {
    7.        
    8.         CGPROGRAM
    9.         #pragma vertex vert
    10.         #pragma fragment frag
    11.         #include "UnityCG.cginc"
    12.        
    13.         sampler2D _MainTex;
    14.         float4 _MainTex_ST; //scale  position of _MainTex
    15.        
    16.         sampler2D unity_Lightmap;//Beast lightmap
    17.         float4 unity_LightmapST; //scale  position of Beast lightmap
    18.        
    19.        
    20.         // vertex input: position, UV0, UV1
    21.         struct appdata {
    22.             float4 vertex   : POSITION;
    23.             float2 texcoord : TEXCOORD0;
    24.             float2 texcoord1: TEXCOORD1;
    25.         };
    26.        
    27.         struct v2f {
    28.             float4 pos  : SV_POSITION;
    29.             float2 txuv : TEXCOORD0;
    30.             float2 lmuv : TEXCOORD1;
    31.         };
    32.        
    33.         v2f vert (appdata v) {
    34.             v2f o;
    35.             o.pos   = mul( UNITY_MATRIX_MVP, v.vertex );
    36.             o.txuv  = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
    37.             o.lmuv  = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    38.             return o;
    39.         }
    40.        
    41.         half4 frag( v2f i ) : COLOR {
    42.             half4 col   = tex2D(_MainTex, i.txuv.xy);
    43.             half4 lm    = tex2D(unity_Lightmap, i.lmuv.xy);
    44.             col.rgb     = col.rgb * DecodeLightmap(lm);
    45.             return col;
    46.         }
    47.         ENDCG
    48.         }
    49.     }
    50. }
    EDIT:
    Also keep in mind that batching will alter your UV2 for baked objects, so you have to use the provided tiling/scaling!
     
    Last edited: Apr 17, 2013
  3. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    Thanks.

    No I don't apply tiling or offset to my lightmap. I'd like to have so that this doesn't need to be handled in the shader but using UV2 should be enough. Can't understand why simply export and then reimport adds the correct UV:s. Must be some kind of "history" in unity that is cleared by doing so. Hmm...
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Baking lightmaps does not alter UV2s in any way.

    What it does do is to set hidden 'lightmapIndex' and 'lightmapTilingOffset' property values on all baked renderers. In particular, 'lightmapTilingOffset' is supplied to the shader (as a constant) and used to scale+offset UV2 values for each vertex.
     
  5. bbedwell

    bbedwell

    Joined:
    Nov 5, 2012
    Posts:
    37
    I like this shader but it doesn't allow cast shadows. How can I get this?

     
  6. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    @bbedwell: After the pass (line 50) add: Fallback "Diffuse"

    Yeah, but static batching does (in play mode). Otherwise similar objects with different lightmap atlas couldn't be batched.
     
  7. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Ahh, yes, I see what you mean.
     
  8. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    Well it does alter them on export (I use collada exporter from assets store) so I guess the exporter takes this tilingoffset in account and remap the UV2 accordingly.
     
  9. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    So is this batching something I can use for my purpose then perhaps?
     
  10. bbedwell

    bbedwell

    Joined:
    Nov 5, 2012
    Posts:
    37
    I tried that but the object with this shader still doesn't receive a shadow from another mesh.
     
  11. bbedwell

    bbedwell

    Joined:
    Nov 5, 2012
    Posts:
    37