Search Unity

Problems with instantiating baked prefabs

Discussion in 'Global Illumination' started by GSdev, May 8, 2015.

Thread Status:
Not open for further replies.
  1. eventropy

    eventropy

    Joined:
    Oct 4, 2012
    Posts:
    255
    I had that same issue where the demo project would display in black in white. I noticed this was fixed if you open the BakeLightmapPrefabs scene and then run Bake PrefabLightmaps again
     
  2. Streamfall

    Streamfall

    Joined:
    Aug 8, 2011
    Posts:
    43
    I'm in the same spot.

    My procedure is the same as Racsoh in #311.

    However I'm instantiating the prefab tiles on awake, then starting PrefabLightData.cs's Awake function after the prefabs are instantiated.

    The result is black or just black and white.

    I've tried this with several versions of Unity, including 2017.2.0f3 and 5.6.3.

    Screen Shot 2017-11-20 at 4.29.25 PM.png Screen Shot 2017-11-20 at 4.29.04 PM.png

    Update :
    'Update Scene with Lightmap Prefabs' from the above script results in...

    Update 2 :
    I was able to get this working, by using 'PrefabLightMap_with_editor_utilities.unitypackage' from a previous poster.
     

    Attached Files:

    Last edited: Nov 21, 2017
    haolly likes this.
  3. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    So is this working yet?
     
  4. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Depends on what you're talking about. Yes, you can bake lightmaps, assign them to prefabs and deliver them out via assetbundles (via the workaround and scripts mentioned in this thread). Is lightmap baking working correctly? Maybe not. At least not in all cases and surely not in all Unity versions.
     
  5. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    I'm on 2017.1 and unfortunately I can't save Lightmapping to prefab levels which is a real pain. Even with the script I lose my lighting :-(
     
  6. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    UPDATE: So the script is working, but light probes are not saved with the prefab scene. I opened the original scene where it is being built, and in the standalone build using UFE, the light map is "there" but the probes are not even calculated. I am also using Mixed Lighting, so that might be an issue.
     
  7. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    Here is a shot from my test level with the light probes enabled so you can see what I am talking about. I turned on Light Probe Visualisation
    upload_2017-11-29_15-36-19.png


    And there is the same shot only running in UFE. The lightmaps saved, but there are no light probes saved AT ALL. And I am using Mixed Lighting. I also notice that my settings are overridden for this level too... the light is set to MIXED, but when I run my game, there is no way to set up the rendered like in the original scene.
    upload_2017-11-29_15-37-45.png
     
  8. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    So in further tests, the builds are loading my lightmaps, which I've tested rigorously for all of my levels... the only thing missing is preserving the light probes which show up in my normal scenes but not in standalone builds, especially in scenes with characters and dynamic objects.
     
  9. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    UPDATE: So I have updated to Unity 2017.2 and the script appears to be working with the Progressive Lightmapper (quite well in fact) it's still white but I am not going to complain about it. Is there a way to get the light probes saved to this as well. It is strange that this is the only omission to an otherwise working script.
     
  10. Galahad

    Galahad

    Joined:
    Feb 13, 2012
    Posts:
    72
    Were in the code could i change it to use progressive lightmapper?
     
  11. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    I'm using the script stock. I learnt that it works with the PL by accident, and it looks SO DAMN GOOD! The only thing left is finding a way to get the light probe data to stick with it.

    Whoever created the script can hopefully shed some light on this
     
    Galahad likes this.
  12. Dev-Burak19

    Dev-Burak19

    Joined:
    Dec 23, 2016
    Posts:
    3
    I am using the same script posted by joachim Ante in unity 2017.2.0f2 and still cant save lightmap on terrains in a prefab. All other objects with mesh renderers are ok. just no lightmap on terrain for instantiated prefab. Terrain looks fine in the scene where i baked light but not in the instatiated prefab though.
    I know its an old thread but still hoping agains hope. Any help ?
     
  13. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    Hi all,

    It doesn't work in 2017.3.. :(
     
    Last edited: Feb 28, 2018
  14. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    How do I prevent my baked prefabs from being lit twice? In the scene that I am instantiating the prefabs in, I would like to have a light that makes the prefabs cast shadows, but not brighten them any more. Is this possible?

    I'm using Unity 5.6.

    Thanks
     
  15. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    I was having some problems with 2017.3.
    It was creating a lightmap with xxx_comp_dir.png when the code was expecting a .exr file type.

    Perhaps there's a way to get the light mapper to create .exr file but the solutiuon below worked for me.

    I changed the line at 223 in PrefabLightmapData.cs to check for .png instead.

    Code (CSharp):
    1. lightmap_Remap.lightmap2 = GetLightmapAsset(filename + "_comp_dir.png", resourcePath + "_dir", originalLightmapIndex, lightmap2);
     
    Last edited: Mar 5, 2018
  16. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Can anyone help with my double lighting issue above? It feels like it must be a common problem with an easy fix. Thanks
     
  17. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    I ended up hacking together couple of shaders to get the effect I wanted.

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. // Unlit shader. Simplest possible textured shader.
    4. // - SUPPORTS lightmap
    5. // - no lighting
    6. // - no per-material color
    7.  
    8. Shader "Mobile/Unlit (Supports Lightmap and Shadows)" {
    9. Properties {
    10.     _MainTex ("Base (RGB)", 2D) = "white" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags { "RenderType"="Opaque" }
    15.     LOD 100
    16.    
    17.     // Non-lightmapped
    18.     Pass {
    19.         Tags { "LightMode" = "Vertex" }
    20.         Lighting Off
    21.         SetTexture [_MainTex] {
    22.             constantColor (1,1,1,1)
    23.             combine texture, constant // UNITY_OPAQUE_ALPHA_FFP
    24.         }
    25.     }
    26.    
    27.     // Lightmapped, encoded as dLDR
    28.     Pass {
    29.         Tags { "LightMode" = "VertexLM" }
    30.  
    31.         Lighting Off
    32.         BindChannels {
    33.             Bind "Vertex", vertex
    34.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    35.             Bind "texcoord", texcoord1 // main uses 1st uv
    36.         }
    37.        
    38.         SetTexture [unity_Lightmap] {
    39.             matrix [unity_LightmapMatrix]
    40.             combine texture
    41.         }
    42.         SetTexture [_MainTex] {
    43.             constantColor (1,1,1,1)
    44.             combine texture * previous DOUBLE, constant // UNITY_OPAQUE_ALPHA_FFP
    45.         }
    46.     }
    47.    
    48.         // Pass to render object as a shadow caster
    49.         Pass
    50.         {
    51.             Name "ShadowCaster"
    52.             Tags { "LightMode" = "ShadowCaster" }
    53.            
    54.             Fog {Mode Off}
    55.             ZWrite On ZTest LEqual Cull Off
    56.             Offset 1, 1
    57.    
    58.             CGPROGRAM
    59.             #pragma vertex vert
    60.             #pragma fragment frag
    61.             #pragma multi_compile_shadowcaster
    62.             #pragma fragmentoption ARB_precision_hint_fastest
    63.             #include "UnityCG.cginc"
    64.    
    65.             struct v2f {
    66.                 V2F_SHADOW_CASTER;
    67.             };
    68.    
    69.             v2f vert( appdata_base v )
    70.             {
    71.                 v2f o;
    72.                 TRANSFER_SHADOW_CASTER(o)
    73.                 return o;
    74.             }
    75.    
    76.             float4 frag( v2f i ) : COLOR
    77.             {
    78.                 SHADOW_CASTER_FRAGMENT(i)
    79.             }
    80.             ENDCG
    81.         }
    82.    
    83.     // Lightmapped, encoded as RGBM
    84.     Pass {
    85.         Tags { "LIGHTMODE"="VertexLMRGBM" "RenderType"="Opaque" }
    86.         CGPROGRAM
    87.         #pragma vertex vert
    88.         #pragma fragment frag
    89.         #pragma target 2.0
    90.         #include "UnityCG.cginc"
    91.         #pragma multi_compile_fog
    92.         #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
    93.  
    94.             // uniforms
    95.             float4 _MainTex_ST;
    96.  
    97.             // vertex shader input data
    98.             struct appdata
    99.             {
    100.                 float3 pos : POSITION;
    101.                 float3 uv1 : TEXCOORD1;
    102.                 float3 uv0 : TEXCOORD0;
    103.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    104.             };
    105.  
    106.             // vertex-to-fragment interpolators
    107.             struct v2f
    108.             {
    109.                 fixed4 color : COLOR0;
    110.                 float2 uv0 : TEXCOORD0;
    111.                 float2 uv1 : TEXCOORD1;
    112.             #if USING_FOG
    113.                 fixed fog : TEXCOORD2;
    114.             #endif
    115.                 float4 pos : SV_POSITION;
    116.                 UNITY_VERTEX_OUTPUT_STEREO
    117.             };
    118.  
    119.             // vertex shader
    120.             v2f vert(appdata IN)
    121.             {
    122.                 v2f o;
    123.                 UNITY_SETUP_INSTANCE_ID(IN);
    124.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    125.                 half4 color = half4(0, 0, 0, 1.1);
    126.                 float3 eyePos = UnityObjectToViewPos(float4(IN.pos, 1));
    127.                 half3 viewDir = 0.0;
    128.                 o.color = saturate(color);
    129.                 // compute texture coordinates
    130.                 o.uv0 = IN.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    131.                 o.uv1 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    132.                 // fog
    133.             #if USING_FOG
    134.                     float fogCoord = length(eyePos.xyz);  // radial fog distance
    135.                     UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
    136.                     o.fog = saturate(unityFogFactor);
    137.             #endif
    138.                 // transform position
    139.                 o.pos = UnityObjectToClipPos(IN.pos);
    140.                 return o;
    141.             }
    142.  
    143.             // textures
    144.             sampler2D _MainTex;
    145.  
    146.             // fragment shader
    147.             fixed4 frag(v2f IN) : SV_Target
    148.             {
    149.                 fixed4 col, tex;
    150.  
    151.                 // Fetch lightmap
    152.                 half4 bakedColorTex = UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv0.xy);
    153.                 col.rgb = DecodeLightmap(bakedColorTex);
    154.  
    155.                 // Fetch color texture
    156.                 tex = tex2D(_MainTex, IN.uv1.xy);
    157.                 col.rgb = tex.rgb * col.rgb;
    158.                 col.a = 1;
    159.        
    160.                 // fog
    161.             #if USING_FOG
    162.                 col.rgb = lerp(unity_FogColor.rgb, col.rgb, IN.fog);
    163.             #endif
    164.                 return col;
    165.             }
    166.  
    167.         ENDCG
    168.     }
    169. }
    170. }
    171.  
    172.  
    173.  
    174.  
     
  18. Dev-Burak19

    Dev-Burak19

    Joined:
    Dec 23, 2016
    Posts:
    3
    Hi All,
    I have a scene with 4 prefabs with the lightmapdata script added baked the light , now the scene has total 3 2048x2048 lightmaps, everything looks fine. The problem is when I reload the prefabs in gameplay the Scene lighting show 48 2048x2048 lightmaps loaded. I mean shouldn't it use the same loaded lightmaps for the scene for the optimal performance rather that loading the same lightmaps again and again for each prefab ? 12 times to be precise. The lighmap size jumps from 8.0MB to 128MB I am a mobile game developer something doesn't seem right.

    Second thing when I reload the same scene for a restart of the game lightmaps wont load untill after 4-5 tries maybe those number of lightmaps take some time to load I dont know for sure yet.

    any help will be greatly appreciated as I am on the verge of completing this long project.
     
  19. Dev-Burak19

    Dev-Burak19

    Joined:
    Dec 23, 2016
    Posts:
    3
    Thank you so much it solved my huge problem. Best Regards
     
  20. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    Finally got this working for me in Unity 2017.2. Works fine with ProBuilder objects and the Progressive lightmapper.

    Using the Editor script from this post: https://forum.unity.com/threads/problems-with-instantiating-baked-prefabs.324514/page-5#post-2519284

    Importantly, I had to make a change to PrefabLightmapData.cs
    on line 222 where the script hard codes the comp_dir lightmap file:
    Code (CSharp):
    1. lightmap_Remap.lightmap2 = GetLightmapAsset(filename + "_comp_dir.exr", resourcePath + "_dir", originalLightmapIndex, lightmap2);
    I changed the extension to png as my bake wasn't generating a file ending in "_comp_dir.exr" but a png instead:
    Code (CSharp):
    1. lightmap_Remap.lightmap2 = GetLightmapAsset(filename + "_comp_dir.png", resourcePath + "_dir", originalLightmapIndex, lightmap2);
    This has become a bit of a monolithic thread with a lot of different advice. After making the above code change, what worked for me was
    1. turning off batching static, adding the PrefabLightmapData component on the parent of the prefab and
    2. baking using the Editor utility linked to above (Window > Prefabbed Lightmap).
    After clicking on Step 1 bake, you sometimes get a duplicate of the PrefabLightmapData component. Just remove it.
    3. I found I had to disable the lights on the prefab otherwise they still appeared in the destination scene (even though they were set to baked).
    4.Then finally apply changes to the prefab.
    5. In the destination scene, add a cube or something and bake a very basic lighmap. For some reason it's needed for the prefab lightmaps to work properly.
     
  21. valentinwinkelmann

    valentinwinkelmann

    Joined:
    Nov 3, 2014
    Posts:
    190
    Also works in Unity 2018.1.0b13.
    A very helpful script.
     
    jeromeWork likes this.
  22. greengremline

    greengremline

    Joined:
    Sep 16, 2015
    Posts:
    183
    So how does this work exactly?

    I have about 20 room prefabs I instantiate at runtime so baked lighting is out, does this allow me to store the lightmaps and at runtime 'bake' each instantiated room (there might be duplicates)? Taking into account the rooms could be positioned anywhere and rotated 0, 90, 270, or 360 degrees
     
  23. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    No, the point of this is that you bake your lightmap into the prefab. The only thing that happens at runtime is that you instantiate the prefabs. The baking is not done at runtime so it won't take into account rotation. You could, I guess, have different versions of each room (for each rotation). It is tricky to minimise the visible seams caused by lighting changes from room to room. The 'construction' process needs some thought, with identical lighting for all rooms, or at least at their joins.
     
  24. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    As long as you dont bake a directional light you should be fine.
    I had a similar setup where the prefabs (rooms in my case) could be rotated at 90 degree intervals.
    All rooms were lit with either spot or point lights and baked per prefab. In my case I joined the rooms so to get a smooth transition I added the same light above the joining point to all prefabs.
     
    zero_null likes this.
  25. youngmindz

    youngmindz

    Joined:
    Nov 15, 2013
    Posts:
    1
    did anyone get Light Probes to work on prefabs?
     
  26. Janoooba

    Janoooba

    Joined:
    Feb 9, 2016
    Posts:
    43
    I can't seem to get this working for the life of me, and without light probes it seems pretty much useless. How has it been 3 years without a Unity dev response in here? This isn't exactly an obscure niche feature request, it's practically needed for procedural generation, especially if you're going with VR. (Yeah. I'm salty.)
     
    sebastiansgames, pcg and zero_null like this.
  27. TitusLVR

    TitusLVR

    Joined:
    Apr 25, 2018
    Posts:
    1
    Hello guys! I have a problem when building project to device iOS it seems like it cant convert or compress exr texture to propper format to PVRTC_RGB4. Please help to solve this problem, because in Unity it works but on a device no.
     
  28. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    Does this script use the lighting window for getting its settings? It does not seem to respect it (such as increasing lightmap resolution via texel size does nothing to the final bake but it does if I use the direct baking window).

    Id really like to be able to use this workflow but control the bake. RIght now shadows are too weak and without most of settings in lighting window it is hard to change that :(
     
  29. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
  30. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    Now that's a good question! I've not tried it but would be interested to know too. I've personally not made it to 2018 quite yet.
     
    Last edited: Jul 20, 2018
  31. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    I was using LWRP and baking prefabs so yes it works up to a point.
    Where I had to abandon this is I could not get light probes to work as i wanted.
    I wanted to bake light probes per prefab and then spawn the prefabs and magically use the associated light probe group.
    In hindsight it was naive of me to think this would work once I looked further in to light probes etc.
    I'm still seeking a suitable solution do to what I want which is semi procedural 3d environment from prefabs with baked probes.

    EDIT: Actually I may not of been using LWRP at the time. I switched to additive scenes and LWRP at about the same time so TBH I'm not entirely sure. Sorry.
     
    haolly likes this.
  32. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977

    Okay thankyou for the fast response! Can anyone else here shed any light? @Joachim_Ante posted the original post so hoping he will chime in?
     
  33. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    MadeFromPolygons and pcg like this.
  34. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    I also got it to work in 2018.2 ,
    I did have to rename _dir.exr image to _dir.png, apparently thats what unity call that specific light map now.
    png reference:
    https://forum.unity.com/threads/problems-with-instantiating-baked-prefabs.324514/page-8#post-3458376

    The editor script i got from here:
    https://forum.unity.com/threads/problems-with-instantiating-baked-prefabs.324514/page-5#post-2519284

    I just use non-directional because in the past i had issues rotating objects at runtime with directional. i lose some normal map effect but its minor compared to being able to rotate properly.

    i highly recommend giving it a try with directional lightmaps as well, if you arent planning to rotate objects, it does appear to have proper support for directional lightmaps. and it looks really good when spawning a run time. But if you rotate objects at run time, then defintely non-directional in both the bake scene and the runtime destination scene.

    One issue i think is that it appears to be designed to only support 1 bake scene because you select the directory and it over writes the images.

    Thats unfortunate, if you have multiple bake scenes so that you can divide your objects up, thats the best because then the prefab can just point to that scenes lightmaps.

    Its a step backwards for that. I may have to modify it because some objects are quite large and any time i add an object i shouldnt need to rebake all of my old objects, specifically if they take up about 1024x1024 or higher each lightmap.

    Great job to everyone for contributing to improving it, i dont know why unity hasnt implemented this by default yet. i also think it might be helpful to have 2 separate arrays to make it faster to drag renderers into the array list. But thats minor in the grand scheme of things, couldnt take more than 1-2 seconds each time you want to modify/add/remove a renderer.

    *EDIT*
    I just realized, if you choose a different scene, you can atleast manually type in a different target lightmap directory.
     
    janla and jeromeWork like this.
  35. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    Oddly im having an issue where at runtime my objects end up black, i think it might have something to do with the lightmaps being moved incorrectly, i am not sure yet, anyone had this before? i dont have static batching on or anything, i even disabled it in the script itself.
     
  36. OP3NGL

    OP3NGL

    Joined:
    Dec 10, 2013
    Posts:
    267
    it was working in 2018.1.0f2, after upgrading to 2018.2.2, baked lightmaps no longer appear in my prefab folder

     
    Last edited: Aug 6, 2018
  37. OP3NGL

    OP3NGL

    Joined:
    Dec 10, 2013
    Posts:
    267
    i cannot get the plugin to bake into my prefab folder on 2018.2.2
     
  38. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    So i solved the issue, in 2018.2. you have to include the bake scene with your main project. thats regardless of the platform target. The script given was originally supposed to target a Resources folder so that it could include any missing files, unfortunately it appears that only moves the texture/asset but not the original light data asset that is in the scene itself. i am not sure why its being referenced still.

    But including the bake scene will make your lightmapped prefabs show correctly at run time
     
  39. unitymatrix

    unitymatrix

    Joined:
    Dec 29, 2009
    Posts:
    120
    hello everybody, I've load lightmap serveral months ago.and my new problem is that the light is quite dark than the orginal.
    this is original:
    upload_2018-8-13_21-22-35.png

    this is loaded :

    upload_2018-8-13_21-23-15.png

    by the way, I found the value of m_Lightmaps2 is always to be empty,so I changed the line 73 to
    "
    newLightmapData.lightmapDir = lightmaps;
    "
    otherwise, the scene will be like this:
    upload_2018-8-13_21-27-19.png



    I don't know what's wrong with it,any help will be appreciated.
     
  40. Davideogames

    Davideogames

    Joined:
    Sep 5, 2017
    Posts:
    7
    Hello,

    I wasted hours to figure out how to make it work, but it’s working fine now on PC and Android builds with Unity 2018.1, so I thought I should share my process:

    - Create an empty scene called “BakeLight”
    - Add point lights, set “Baked” mode
    - Add all the prefabs to be baked in the scene
    - Add in the Parent of all the prefabs the “PrefabLightmapData.sc” script from Joachim_Ante:
    https://forum.unity.com/attachments/lightmapinprefab-zip.146782/
    - Set MeshRenderer of the prefabs:
    “Lightmap Static” ON
    “Scale in Lightmap”: 0.1 (to have hundreds of 3d objets lightmap to fit in one texture. Otherwise, by default it will generate 1 lightmap per object, which is huge and not performant)

    - Open “Windows” – “Lighting” and do this setup:
    "Auto Generate" OFF (near button “Generate Lighting”)
    “Realtime GI” OFF
    “Lightmapper” Progressive
    “Bounces” 1 (faster for rendering, but a bit less precise)
    “Lightmap Size” 1024 (a good compromise for mobile)
    Then press “Generate Lighting”

    - Then use the tool from Joachim_Ante :“Assets” – “Bake prefab lightmaps”
    - Check in the game objects of the scene that the component PrefabLightmapData was not duplicated in the process (it happens). If so, just delete one of the 2 components.
    - “Apply” the modifications of all the game objects (GO) and save the scene

    Then, to use the created GO with lightmaps, this is where it was tricky to figure out. In my case, I use a totally empty Scene to instantiate the GO. If so, this is what you need to do:
    - If your target scene is empty, add a 3d cube or anything in 3d
    - Open the Lighting window
    - Use EXACTLY the same Lighting parameters as for the “BakeLight” scene
    - Generate the lightmap of this scene by pressing “Generate Lighting”
    - You can then delete the 3d cube as it was just needed to generate the texture

    => You should be all set to instantiate GO with lightmaps in it.

    THE TRAPS:

    => If you don’t generate at least once the lightmaps in your target scene, you will have dark lightmap in the Editor playback, because you need the LightingData file to be generated (in your “Scene” subfolder)

    => If your target scene is empty when you generated the lightmaps, it won’t generate the Lightmap-0_comp_light file (in your “Scene” subfolder), and the lightmap will work in the Editor, but will be dark in the Android or PC build (it took me a verrry long time to figure out this step)

    Hope this is helpful and will avoid hours of try-and-error for the next one arriving on this thread…
     
    Last edited: Aug 24, 2018
  41. mzr

    mzr

    Joined:
    Jul 25, 2014
    Posts:
    35
    That's an awesome post man, really useful.
    I have one issue that doesn't seem to be described before though: as soon as I add lightingdata to my scene I can't build the project anymore, I don't get any specific errors, it just stops at sharedassets3 file.
    Any ideas?

    EDIT: Ok, I had to remove 3 game objects from my scene and recreate them exactly the same for some reason, but the build works now. Except I still have black maps when I build to android, even though I have the Lightmap-0_comp_light file (it's all black though).
     
    Last edited: Aug 30, 2018
  42. Davideogames

    Davideogames

    Joined:
    Sep 5, 2017
    Posts:
    7
    For your black screen on Android build, it can be many problems... It can be the lightmap texture format that is not compatible with the Android device, or it can be the texture that is not included in the assets list in your Android build, or it can be that the script didn't generate the correct infos for each GO, or else...
    I would advise that you start eliminating problems:
    - Switch platform to PC, play with Unity player : All black = maybe a problem with the script. Check the Component "PrefabLightmapData" in the GO and see if Lightmaps point to readable textures. Check a few Elements a see if they point to the lightmaps and have correct coordinates.
    - If it's working on Unity player PC, try to make a Build PC : All black = maybe a problem with the lightmap texture not being loaded in the list of the assets of the build. You can add this piece of code in the PrefabLightmapData.cs:

    void OnGUI()
    {
    GUI.DrawTexture( new Rect( 0, 0, 256, 256 ), m_Lightmaps[ 0 ] );
    }
    It should display the loaded lightmap texture on screen (it's really dirty, but it's an easy way to check if the texture is properly loaded). If not, if you are using a assetholder or else, maybe you need to add the lightmap in the list of assets to be included in the build... or it could be the problem with the Lightmap-0_comp_light default file that was not created with your empty scene, or else...
    - If it's working in a PC Build, switch platform to Android and try in Unity player: All black = probably a problem with the lightmap file format. Test with the piece of code to display the texture.
    - If it's working on Android Unity player but not on Android build, then I have no idea :) sorry.
     
    mzr likes this.
  43. mzr

    mzr

    Joined:
    Jul 25, 2014
    Posts:
    35
    Thanks @Davideogames! I really appreciate the time you took to write this!
    I tried some of the things you mentioned and the OnGUI method shows my texture on the screen on Android. All the prefabs are still black though.
    I did find another lead, it looks like for some reason the lightmaps don't work at all in my Android build, even without the prefab script. So that's what I'll investigate now.

    EDIT:
    Ok, once I figured out the lightmaps don't work at all it was relatively easy to find the solution.
    I just had to go to Edit -> Project Settings -> Graphics -> Shader Stripping -> Lightmap Modes and enable "Baked non-directional".
    Thanks again for your help!
     
    Last edited: Sep 1, 2018
    Davideogames likes this.
  44. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I noticed a lot of people ask about light probes but no one r”ever responded... is it possible to include light probes in prefabs? If it is I can’t figure it out but I don’t really see the point of lightmapped prefabs without lightprobes.
     
  45. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    No I dont think its possible. The solution I have resorted to is as follows...

    I have all the prefabs in a single scene with light probes in etc, and bake it. I then use the Prefab Lightmap tool from this thread to bake the lightmaps on to the prefabs.

    My prefabs are rooms for my level and I have a trigger collider around the prefab so that when my character enters a new room I know which prefab he is in.
    I create a level from the prefabs.
    I have an empty game object which follows my character around but the position is translated to within the original prefab position in the scene in which it was baked. I use this empty game object as the Anchor override on my characters mesh renderer to solve the light probe problem.

    Unity has recently released something on the asset store explaining how scene lighting works.
    I've not looked at it yet so im not sure how helpful it will be but heres a link.

    https://assetstore.unity.com/packages/templates/tutorials/additive-loading-lighting-examples-129922
     
  46. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,467
    Have we figured out how to get prefabs working with GI? I'm using the UFE asset and sadly with the way the asset is set up, precomputed lighting, baked lights, and reflection probes DON'T WORK with it!! I've mentioned this to the devs numerous times and no one will listen to me! It uses prefabs as levels and in order for me to save reflection probes, I have to set it to Custom...

    And the last part is saving light probes...
     
  47. RupertTheArtist

    RupertTheArtist

    Joined:
    Mar 13, 2018
    Posts:
    1
    I just wanted to chime in with a few things:
    If you change the line that says
    Code (CSharp):
    1.  Lightmapping.Bake();
    to
    Code (CSharp):
    1. Lightmapping.BakeAsync();
    You'll get a Unity that doesn't lock up during baking, as well as a progress bar in the bottom right showing progress, as well as the ability to open the Lighting Settings window and Cancel a bake.

    to compress lightmaps and reflection probes (uncompressed are huge!), I made a separate script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. // Compress any texture in /Scenes/*, ie: lightmaps and reflection probes
    5.  
    6. class CrunchLightmaps : AssetPostprocessor
    7. {
    8.     void OnPreprocessTexture()
    9.     {
    10.         if (assetPath.Contains("Scenes"))
    11.         {
    12.             TextureImporter textureImporter = assetImporter as TextureImporter;
    13.  
    14.             textureImporter.textureCompression = TextureImporterCompression.CompressedHQ;
    15.             textureImporter.crunchedCompression = true;
    16.             textureImporter.compressionQuality = 100;
    17.          
    18.         }
    19.     }
    20. }
    I'm an artist not a programmer so apologies for any fugliness to these solutions ;)
     
    Last edited: Dec 17, 2018
    jeromeWork likes this.
  48. 8Shade

    8Shade

    Joined:
    Jun 14, 2014
    Posts:
    9
    Tested this in Unity 2018.3 - had black and white textures after the bake - solution:
    Lightning - Lightmapper settings - Directional mode - non-directional

    Other than that, thanks for that. Still doesn't look good at all, but at least it bakes :)

    BakeAsync
    Also somebody recommended UnityEditor.Lightmapping.BakeAsync(); to see the progress instead of UnityEditor.Lightmapping.Bake(); I spent two days finding out why my textures were misplaced all around incorrectly and it was because of this. So keep there Bake(). (BakeAsync worked for me only sometimes and only for one prefab, not with multiple in one bake scene)
    Best is actually to comment out this line, bake manually and then run the script to assign the lightmaps.
     
    Last edited: Dec 31, 2018
  49. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    Do the lights have to be points? Will directional lights work? The scene my prefabs are used in is outdoors, and has only directional lighting.
     
    Last edited: Jan 9, 2019
    joshcamas likes this.
  50. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Same. Is there a way to get directional mode to work?
     
Thread Status:
Not open for further replies.