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. neroziros

    neroziros

    Joined:
    Aug 25, 2012
    Posts:
    129
    Still no terrain support?
     
  2. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    What do you mean? We have lightmaps on our terrain and it works pretty much in a similar way. So: terrain is supported.
     
  3. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Can anyone provide some more instructions for using this PrefabLightmapData script? I followed the instructions at the start of this thread but I can't get it to work. Should it work with directional lights and terrains? And does it work in Unity 5.3? Thanks
     
  4. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    See my previous reply.

    The fixes on the last few pages are for 5.3.x.
     
  5. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    ok thanks
     
  6. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    HI guys I am excited to announce that I have made editor utility for Prefabmap lightmap,

    With regards to existing features ,You can now select texture size and format for generated lightmaps ,
    It also has LOD options.




    Also plz feel free to customize it more and share with community.

    Let me know your review and suggestions.

    Happy lightmapping.


    ***UPDATE***


    It is not actually my work, it is excellent work of @Centripetal , when we were having Unity bug (You can read about it from page 2) .I just added LOD groups and GUI layer above it.

    Fortunately after bug case to Unity, later version of Unity did solve that bug.

    Now,I believe original script by @Joachim_Ante do work.

    Check if that solves your problem, here is modified version of that script by @WillemKokke ,
    https://forum.unity3d.com/threads/problems-with-instantiating-baked-prefabs.324514/#post-2229465
     

    Attached Files:

    Last edited: Nov 13, 2016
    haolly and alexmeister92 like this.
  7. Studvio

    Studvio

    Joined:
    Mar 11, 2016
    Posts:
    6
    Hi guys, I've been trying to sort out the lightmapping problems for the past couple of days, with no success. The story is:
    - I have this scene that takes ages for the lights to bake (around 6 hours)
    - When the baking is done, even if I adjust just one light (from so many), or even if I DON*T CHANGE ANYTHING, when I click "Build" under Lighting to bake the lights (whoever thought that it would be a good idea to leave that to AUTO by default... well, just to say that it wasn't a good idea), the whole process starts all over again. Yep, 6 hours...
    - Searching for a smarter alternative, I came upon this forum thread and I though: "God, I'm saved". But not so fast!
    - I'm not that good at coding, so finding how to implement the C# code was a pain in the a..e, but I got it eventually (note to people posting here: maybe it would help adding some instructions on how to actually implement your stuff, please)
    - Only to find that it's not actually working the way I way expecting. Or maybe I'm that stupid that I don't know how to do it right. Anyway, below is an explanatory video:


    I would be more that happy to find a way that actually works in order to:
    - being able to make minor adjustment in the scene without having to wait for the WHOLE scene to be light baked again.
    OR
    - being able to save the lightmaps that will give the expected results, and so being able to work on different part of the scene without affecting the whole lightmaps
     
  8. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495

    Here is small tutorial I made,plz check it out and let me know if it helps,
     
    Last edited: Mar 12, 2016
    KRGraphics likes this.
  9. Studvio

    Studvio

    Joined:
    Mar 11, 2016
    Posts:
    6
    Hi idurvesh,
    Thank for your reply. I will try to follow your tutorial and let you know of the outcome (please, please work!!!! :) )
     
    idurvesh likes this.
  10. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    Something is wrong. I make a prefab, I hit Build and bake the scene (directional specular), and everything is fine.
    Then I add the Prefab Lightmap Data script to the root of the prefab, I click on Bake Prefab Lightmaps.
    Stuff happens but my prefab lightmap ambient suddenly becomes black or very dark. I look in the Lightmaps tab and see 2 copies of the same lightmap stacked in the list.

    If I try to open a new scene, disable Auto bake, set the atlas size etc., drag the prefab in, and apply the lightmap, it's still black (AND it does NOT have 2 copies of the same lightmap in the Lightmaps tab).

    I tried a bunch of different ways, and sometimes hitting "Bake Prefab Lightmaps" will even assign an extra copy of the PrefabLightmapData script onto the prefab but will apply the prefab with only the first script instance. (the copy is in bold on the prefab in the inspector)

    Tried a bunch of versions of your script, including @idurvesh 's version with the custom editor (and followed his video) and the same happened.
    Tried unity 5.3.3, 5.3.4 and 5.4.0b10

    [UPDATE]:
    I created a new empty project, brought the scripts in, and they worked.
    Then I started baking a "real" prefab, it worked. Then I tried another prefab but it went back to black ambients. And it won't go back to working.

    Now I'm making a new project each time I need to bake a prefab...


    Note: I noticed you have to make sure you baked your Reflection Source or ambient source / probe whatever, in your target main scene, or else the lightmapped prefab you bring in will have black speculars. But this still only works for me if/when I make a fresh new project for each prefab :C
     
    Last edited: Mar 16, 2016
  11. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    i thought this script does not work with directional specular?
    correct me if am wrong (oh please let me be wrong ^^)
     
  12. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    When it works at all, it also works with directional specular. Make sure you bake with dir spec, and when you load the prefab in, you also have dir spec turned on in that scene, and that you have baked your main scene with your Reflection Source before you bring in the lightmapped prefab.

    WE NEED TO POST A GUIDE AND THE LATEST CODE IN THE TOP POST. (also would be nice if unity took care of this since it's their work we're doing here)
     
    Last edited: Mar 16, 2016
    Astiolo and Arganth like this.
  13. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Can you share screenshot? I just tried here in my system, it worked fine.
     
  14. Studvio

    Studvio

    Joined:
    Mar 11, 2016
    Posts:
    6
    Having the same problem:
    - I bake the scene and everything looks fine
    - using the script to bake the prefab lightmap will turn the scene to black
    - updating the prefab lightmap will turn off baking on the previously baked scene, and that lightmap will be completely lost, i.e. I have to start baking again.
    I thought I was the only one having this issue by doing something wrong, but apparently not. By the way, I followed idurvesh's video. I'm not saying that it's not working at all, only it's not working for some people.
     
  15. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    just watched that video for the first time
    i do it differently
    (the only time i get black prefabs/tiles is when i dont set them to static)

    so first i set the tile to lightmap static, disable rotation
    lightining is set to baked gi
    general gi is set to directional specular, default-high resolution
    auto build is always disabled

    then i just click on backe prefab lightmaps under the asset tab
    I dont bake with the lightining tab

    i dont assign lightmaps manually at all
    i dont have the prefab lightmap data script on my prefabs





    so tried it like its done in the video
    and it still works

    cant help you here .. :|
     
  16. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    I'm getting this sort of thing:
    Baked from lighting tab:

    After saving the baked lightmaps to prefab, and also after loading the lightmaps from prefab, looks like this:


    This doesn't always happen. If I start a new project it always works.
    Then I do stuff and suddenly it doesn't work. (couldn't identify yet what)
    Also, if I copy a project to a new computer it stops working.

    I'm thinking somehow something in the Library or Project Settings folders gets screwed up after multiple uses of the prefab lightmaps script with different settings on different bakes.



    <-- I cannot remove this image insert lol
     
  17. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    hmm did you remove the "old lightmap script" when you do a new bake?
    When i assign one to my tiles there is a new script for every bake

    usually i dont assign any scripts it all and it still works magically ^^
    and i am using DunGen for my runtime tests
     
  18. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Adding to what @Arganth already asked.... Have you set Lightning Data Asset for new scene to which "Baked scene" created?
    Also make sure you select proper file format while baking ...Try with "RGB_ETC 4bit"

    http://www.imageno.com/lsycu67mss7zpic.html
     
    Arganth likes this.
  19. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    That might be what causes the issue. Maybe I should be careful to always remove the old lightmap script, and maybe do a scene bake with all assets turned off to "cleanse" the cached lightmap assets, before I do a new proper bake.

    But I just tried that now on a project I know got screwed up a few days ago, and it's still screwed up, even if I do the above. Still might help prevent it though.

    Baking goes fine:


    Assigning to prefabs not so fine:



    I think in that case the custom editor button will trigger a bake, but if there's no script on the prefab then the prefab won't store its lightmap indexes and references and stuff.
     
  20. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    oh i did not know that was possible ^^
    seems i have been lucky that the folder was the right none (or it gets created automatically)
     
    idurvesh likes this.
  21. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    could you upload this scene file?
    i really would like to test it for myself (later when i am actually home ^^)
     
    idurvesh likes this.
  22. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Yes plz upload scene files or you can PM me your "Team viewer" details...I will connect you remotely
     
    Arganth likes this.
  23. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    @Arganth & @idurvesh OK. I added 2 small projects. One in Unity 5.3.4f1 and one in 5.4.0b10.

    After cleaning up some baked lightmap folders in the 5.4.0b10 project, I'm now actually getting an error in the console in that one (when I try to bake into prefab), about index mismatch with the LightmapSettings (even though the prefabs have a fresh script component on them with no references). Haven't had time to read the code.

    Either way, both projects should give you dark lightmaps, and at least in one of them there are examples of already baked dark lightmaps. Check the scenes (there are 2 or 3 in each) and the prefabs.

    Cheers

    The projects are here: https://drive.google.com/open?id=0B50afmg1TY-eNnZfNkZxSDhCTlE
     
    Last edited: Mar 21, 2016
  24. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    so after I discovered the other script with the editor window lol
    I tried it now on my laptop (without dungen) just the test scene

    and it still worked AFTER i set the textureimporterformat to argb32
    the default alpha8 just blacked out for me
     
  25. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    ok i can test them later thouroughly ^^

    i just gave it a quick test on my laptop
    and i get the same errors you mentioned
     
    Tudor likes this.
  26. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Are you guys getting error only on 5.4 or 5.3 too?
     
  27. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    idurvesh likes this.
  28. Studvio

    Studvio

    Joined:
    Mar 11, 2016
    Posts:
    6
    I guess I found a way around this problem. I guess... No scripts required.
    I create a new scene. In Lighting - Scene I set Directional mode to Non-Directional, and uncheck the Auto option (no auto light baking).
    I then create an Empty object, and use it as parent for all the objects in the scene. Lights will be set to Baked, with Hard / Soft Shadows. Then I set the Empty object to static, and bake the lights (Lighting - Build). Save the scene.
    I then create a second scene, simillar to above. When baking on the second scene is done, from the Project window I drag the first scene into Hierarchy window. And voila, the first scene is there, with baked lights.
    MAKE SURE THAT AUTO OPTION UNDER LIGHTING IS UNCHECKED, if don't want your scene to be baked again.
     
    kamathaj likes this.
  29. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    The error is in the 5.4 project only, but it's not because it's 5.4. It happened because in that project, to clean it up, I renamed a lightmapped scene and its lightmaps manually (in unity, and the meta files should have been cool with that). It used to "work" identically to the 5.3 project before I did that. My "mistake" I guess.

    Yes, that project works. So it's strange that when I moved a project from one pc to another it stopped working. Maybe it's some weird unity settings on the other computer. But there's also (for an as of yet unknown reason) the problem with projects eventually breaking on the same machine after baking multiple things.


    Oh, so you mean without using the PrefabLightmapData script in this thread?
    Ok, I guess in that case, when you load scenes additively, unity might use the ambient light from the main scene onto the other scenes as well. But if you bake without directionalSpecular in your secondary scene, I don't think it will get specularity from your active scene.
     
  30. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    NO, WAIT, I found the problem!

    Open this project, then go to Player settings, set it to Deferred rendering and Linear color. Then bake lightmap and it's fine. THEN, "bake prefab lightmap" and it goes black!

    So this S*** doesn't work in deferred? That's a pretty big issue.

    The reason it worked "in new projects" was because all new projects are Forward by default. Has nobody here used this script in deferred mode, srsly?


    [EDIT] I can confirm that creating a new unity project, setting it to deferred and linear, baking lights, then baking lightmaps to prefab, will result in black ambients. So it's not the fact that you switch from having baked in forward to baking in deferred that breaks it.

    [EDIT2] Ambients retain 0 information, it's not that they're just darker. Tried baking with max ambient intensity, max indirect intensity max bounce boost, and after baking to prefab, the whole scene is almost white except for the shadows / ambient light regions which are solid black.

    [EDIT3] I was wrong: it's only the Color Space: Linear that breaks it. Deferred + Gamma works. Though gamma is not an acceptable color space for my project(s).
    @Joachim_Ante do you have any idea why lightmaps don't work in linear color space?
     
    Last edited: Mar 22, 2016
  31. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277

    hmm have to try that later
    just had the problem that i could not get my scenes "black" again....

    have to test with ambience on
    usually I set it to 0 for all my scenes
     
  32. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    You are correct, Linear color space mess up "Direction Specular" lightmaps BTW "Directional" works quite well...Seems like reflection causing problem with Linear space..... @Centripetal please have look in...
     
  33. Studvio

    Studvio

    Joined:
    Mar 11, 2016
    Posts:
    6
    Going back to the drawing board :-(
    Baking the scenes separately, without the script, works fine. Creating a new scene and dragging all necessary the scenes into hierarchy works fine as well. The problem is when I want to Build the project, and that's when only one scene from the Hierarchy (the Active one) will be exported, and not all of them as I was expecting.
    In the Hierarchy, dragging the scene content from one scene to another - in order to have all the objects within one Active scene - will break the lightmaps.
    So I guess I'll be trying to find a solution with the scripts method again. Arrrrrrrrrrghhhh.... One thing is for sure: after finishing with my school project in 6 weeks, I won't get near Unity again for a couple of years, until some basic problems would have been (hopefully) properly dealt with.
     
  34. MatiasPi

    MatiasPi

    Joined:
    Jul 13, 2015
    Posts:
    6
    I'm having a problem with this script when I build the game on Android. Here's a screenshot:

    Screenshot_2016-03-25-20-55-40 (1).png


    And here's how it's supposed to look (This is the PC build):

    pc build.png

    Does anyone know what the problem is? Thanks!
     
  35. hakankaraduman

    hakankaraduman

    Joined:
    Aug 27, 2012
    Posts:
    354
    not sure but could be a gamma/linear thing
     
  36. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    What type of shaders you use? try disabling directional light and check.....
     
  37. MatiasPi

    MatiasPi

    Joined:
    Jul 13, 2015
    Posts:
    6
    I was using the Standard shader, but I just tried with Mobile/Diffuse. I also tried disabling the directional light. There's no difference :(
     
  38. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    Has anyone here had any issues with baking Reflection Probes?
    I have a main scene with some baked stuff, then I bring in some lightmapped prefabs, and I Build Reflection Probes.

    The reflection probes are kinda sorta up-side down but not really:



    How could this possibly have screwed up in such an inconsistent way?
    It's actually not up side down, it's rotated around the world X axis by 180. DA FUQ

    Is it related to when and how I bake my main scene and whether I have updated the scene with prefab lightmaps?


    [UPDATE] It wasn't due to the baked lightmap to prefab workflow. It happens when you turn off HDR on the probe (still weird as hell).
     
    Last edited: Apr 3, 2016
  39. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Hey guys.

    I'm having a problem with my lightmaps when reassigning them and maybe some of you had also that problem:

    When assigning the lightmaps back from our prefabs, we get a weird second shadow projection onto all assets.

    This is before assigning lightmaps loaded scene:

    battleground_and_camp_loaded.png

    This is after:

    moved.png

    You can see the strange shadow on the front of the tent, which is actually a shadow by the tent on itself in a smaller version. We can't figure out why the shadow is duplicated or where it comes from as we only have one light in the scene.

    Every asset in the scene is downloaded from an assetbundle and then instantiated as prefab. Works fine except for the second shadow.

    Did anyone else saw such a problem?

    Thanks for help!
     
  40. w_adams

    w_adams

    Joined:
    Apr 7, 2016
    Posts:
    34
    I have been trying the editor tool idurvesh provided with varying levels of success, but one issue I have is I don't know what texture import format I should be using. How do I know this?

    Thanks!
     
  41. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Hey guys good news, I got mail from Unity bug team, they have solved issue with " LightmapSettings.lightmaps[0].lightmapFar " which caused us to find alternate hard solution ...

    Now I suppose script made by @Joachim_Ante shall work fine and all this duplicate/hectic work can be saved in 5.3.4p4
     
    ilmario likes this.
  42. kamathaj

    kamathaj

    Joined:
    Jan 13, 2015
    Posts:
    2
    Thanks buddy..Your solution is working perfectly!
     
  43. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    Thanks much... can this solution work with bundles also ?
     
  44. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Yes, I can confirm that.
     
  45. MatiasPi

    MatiasPi

    Joined:
    Jul 13, 2015
    Posts:
    6
  46. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    Thanks much ajay and pahe...
    One more question.... number of pixel lights on baking... I have ambient, one point, and 2 spots on a statue... in real time it lokos great, but when baked it ignores one of the spot lights. why ? I have number of pixel lights set to 8 (lots for the hell of it) but I would have thought you can have as many pixel lights as you want when baking.. (but limited for real time) but it seems the opposite... many ok in real time, but only a couple when baked.
    any idea what can cause this ?
     
    Last edited: May 11, 2016
  47. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    How / What are you bundling to make this work? I have a scene with empty object as a parent to everythign in the scene. This empty obejct, is my prefab. I bake lighting and export this prefab as a bundle... but when loading from server into app, it is all dark, and has no light mapping. After following AjayTaker3's instructions above... what prefab would you bundle to make that work please ? help appreciated, thanks.

    actually, I just tried AjayTaker3 technique, and I just ended up with a 2 color level.. just white and black only. no textures, no lightmap. (looks like a 2 color display image) so I guess that doesn;t work for me anyway.
     
    Last edited: May 10, 2016
  48. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    @JonnyHilly The lightmaps are existing in the assetbundle? So they are found and not null when you reassign them?
    If they are not in the bundle, make sure that you don't reference the generated lightmaps from Unity in the prefab, but instead your own copies! The generated lightmaps will not be copied into the assetbundles, as they are not linked to the prefab, but the scene instance.
     
  49. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    OK got it working with a bundled prefab, plus automatically working after downloading and instantiating bundled prefab (or a regular non bundled prefab) ... uses a modified version of the script posted above.
    There are instructions in the manager script, but here they are again...

    Make empty object... put everything inside it, make that your prefab. Put the LightMapManager script onto this object.
    Note:- if your scene is static, and you dont need the lights.. you can put them outside of your main prefab. (but in the same scene, they will create the lightmap, but not be included in the prefab this way.
    Bake your lightmap, then hit the checkboxes on the script, which copies the lightmaps into the array on that prefab, and it add scripts to all other objects with renderers in that scene ( and store the lightmap settings per rendered object)
    Once done, just click the checkbox that says READY_TO_GO and click apply on the prefab. Now when you load the prefab, it auto sets up the lightmap textures, and applies the stored settings to each render object. Tested in various scenes, and with bundle uploaded to server, and downloaded into another project.
    These can probably be cleaned up more.... maybe make sure there is no update() running in the LightMapObject script.... but anyway, here they are... LightMapManager.cs and LightMapObject.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //1. Put this script on root prefab that contains all your light mapped object to be bundled
    5. //2. then render your lightmaps
    6. //3. then click the m_getLightmapArray  checkbox,  (make sure scene tightmap textures were copied into the array)
    7. //4. then the m_initRenderObjects checkbox.   (make sure each object with renderer now has a LightMapObject component, with saved lightmap values)
    8. //5. then turn on the READY_TO_GO checkbox.
    9. //6. click apply to your prefab. (prefab is ready to use , and works as a prefab, or as a bundled prefab)
    10. //7. Note:- LightMapObject has an Update() for test purposes... suggest you remove this for performance in your final version.
    11.  
    12. [ExecuteInEditMode]
    13. public class LightMapManager : MonoBehaviour
    14. {
    15.     public bool READY_TO_GO = false;    //set this tru, once you have grabbed
    16.     public bool m_getLightmapArray = false;
    17.     public bool m_setLightmapArray = false;
    18.     public bool m_initRenderObjects = false;
    19.  
    20.     public Texture2D[] m_lightmapArray;
    21.  
    22.  
    23.     public void Start()
    24.     {
    25.         if(READY_TO_GO)
    26.         {
    27.             setSceneLightMapTextures();
    28.             readyLightMappedRenderObjects();
    29.         }
    30.     }
    31.  
    32.     public void getSceneLightMapTextures()
    33.     {
    34.  
    35.         if (LightmapSettings.lightmaps.Length > 0)
    36.         {
    37.             int num = LightmapSettings.lightmaps.Length;
    38.             Debug.Log("GET LIGHT MAP ARRAY, NUM =" + num);
    39.             m_lightmapArray = new Texture2D[num];
    40.  
    41.             for (int i = 0; i < num; i++)
    42.             {
    43.                 m_lightmapArray[i] = LightmapSettings.lightmaps[i].lightmapFar;
    44.             }
    45.         }
    46.         else
    47.             Debug.Log("GET LIGHT MAP ARRAY, NUM Lightmaps = 0");
    48.     }
    49.  
    50.  
    51.     public void setSceneLightMapTextures()
    52.     {
    53.         if (m_lightmapArray.Length > 0)
    54.         {
    55.             Debug.Log("SET LIGHT MAP ARRAY, NUM =" + m_lightmapArray.Length);
    56.             LightmapData[] lightmapData = new LightmapData[m_lightmapArray.Length];
    57.  
    58.             for (int i = 0; i < lightmapData.Length; i++)
    59.             {
    60.                 lightmapData[i] = new LightmapData();
    61.                 lightmapData[i].lightmapFar = m_lightmapArray[i];
    62.             }
    63.  
    64.             LightmapSettings.lightmaps = lightmapData;
    65.         }
    66.         else
    67.             Debug.Log("SET LIGHT MAP ARRAY, NUM =0");
    68.  
    69.     }
    70.  
    71.     //add LightMapObject script to all objects with renderes, then run it to save lightmap settings to them all
    72.     public void setupLightMappedRenderObjects()
    73.     {
    74.         Renderer[] objs = gameObject.GetComponentsInChildren<Renderer>();
    75.         foreach(Renderer rend in objs)
    76.         {
    77.             LightMapObject lmo = rend.gameObject.GetComponent<LightMapObject>();        //check for existing
    78.             if(lmo==null)
    79.                 lmo = rend.gameObject.AddComponent < LightMapObject>();         //if none, add the component
    80.             if(lmo!=null)
    81.             {
    82.                 lmo.getValues();
    83.             }
    84.         }
    85.     }
    86.  
    87.     //get the object ready to render after loading bundle, and pre saved data
    88.     public void readyLightMappedRenderObjects()
    89.     {
    90.         Renderer[] objs = gameObject.GetComponentsInChildren<Renderer>();
    91.         foreach (Renderer rend in objs)
    92.         {
    93.             LightMapObject lmo = rend.gameObject.GetComponent<LightMapObject>();    //check for lightmap values
    94.             if (lmo != null)
    95.             {
    96.                 lmo.setValues();
    97.             }
    98.         }
    99.     }
    100.  
    101.     void Update ()
    102.     {
    103.  
    104.         if(m_getLightmapArray)      //copy lightmaps from unity into this prefab
    105.         {
    106.             m_getLightmapArray = false;
    107.             getSceneLightMapTextures();
    108.         }
    109.  
    110.         if (m_setLightmapArray)
    111.         {
    112.             m_setLightmapArray = false;
    113.             setSceneLightMapTextures();
    114.         }
    115.  
    116.         if(m_initRenderObjects)
    117.         {
    118.             m_initRenderObjects = false;
    119.             setupLightMappedRenderObjects();
    120.         }
    121.     }
    122. }
    123.  


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. //used in conjunction with LightMapManager.cs
    6. //added automatically to objects with renderers in scene to be lightmapped and bundled (manager will add it automatically if not there)
    7. [ExecuteInEditMode]
    8. public class LightMapObject : MonoBehaviour
    9. {
    10.     public bool m_setLightmapValues;    //for test
    11.     public bool m_getLightmapValues;    //for test
    12.  
    13.     public int m_lightmapIndex = 255;
    14.     public float m_lightmapTilingX = 1f;
    15.     public float m_lightmapTilingY = 1f;
    16.     public float m_lightmapOffsetX = 0f;
    17.     public float m_lightmapOffsetY = 0f;
    18.  
    19.     public void setValues()
    20.     {
    21.  
    22.         Renderer r;
    23.  
    24.         if (r = GetComponent<Renderer>())
    25.         {
    26.             Debug.Log("SET LIGHT MAP VALUES");
    27.  
    28.             r.lightmapIndex = m_lightmapIndex;
    29.             r.lightmapScaleOffset = new Vector4(m_lightmapTilingX, m_lightmapTilingY, m_lightmapOffsetX, m_lightmapOffsetY);
    30.         }
    31.         else
    32.             Debug.Log("SET LIGHT MAP VALUES, can't find renderer");
    33.     }
    34.  
    35.     public void getValues()
    36.     {
    37.         Renderer r;
    38.  
    39.         if (r = GetComponent<Renderer>())
    40.         {
    41.             Debug.Log("GET LIGHT MAP VALUES");
    42.  
    43.             m_lightmapIndex = r.lightmapIndex;
    44.             m_lightmapTilingX = r.lightmapScaleOffset.x;
    45.             m_lightmapTilingY = r.lightmapScaleOffset.y;
    46.             m_lightmapOffsetX = r.lightmapScaleOffset.z;
    47.             m_lightmapOffsetY = r.lightmapScaleOffset.w;
    48.         }
    49.         else
    50.             Debug.Log("GET LIGHT MAP VALUES, can't find renderer");
    51.  
    52.     }
    53.  
    54.     //this function is for debug... if this works for you... just comment out this update function for performance reasons
    55.     void Update ()
    56.     {
    57.         if (m_setLightmapValues)
    58.         {
    59.             m_setLightmapValues = false;
    60.  
    61.             setValues();
    62.  
    63.         }
    64.  
    65.         if (m_getLightmapValues)
    66.         {
    67.             m_getLightmapValues = false;
    68.  
    69.             getValues();
    70.         }
    71.  
    72.     }
    73. }
    74.  
     
    Last edited: May 11, 2016
    buttmatrix likes this.
  50. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    Thanks for your prefab assetbundle code. I'm using Unity 5.3.4p6 and even it looks to work in Editor, I'm not able to make it work using the assetbundle on the Android device. In my case it is the map in the game which is lightmapped, and I get it dark. Any guess?
     
Thread Status:
Not open for further replies.