Search Unity

Why don't lights import?

Discussion in 'Asset Importing & Exporting' started by oaklandjosh, Dec 1, 2009.

?

would you use a "import lights" script if I wrote one?

  1. yes! this would be really useful

    83.3%
  2. occasionally, maybe.

    2.8%
  3. no need for it - I just build lights in Unity.

    5.6%
  4. there's a good reason why Unity doesn't do this already.

    8.3%
  1. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    I'm wondering why I can't import basic lights from my 3DS Max scene into Unity. It seems like it'd be so simple to do, so there must be a reason...?

    If there's not a good reason, maybe I'll write a script to do so. WOuld anyone use it if I did? See poll.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no standard for lights; they are different in every 3D app.

    --Eric
     
  3. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    Thanks for the reply Eric. Almost all 3D applications have a 'point', 'spot', and 'directional' light entity, as does Unity, and the settings (hotspot, falloff, etc) are very similar.

    In any case, I'm only interested in 3DS Max for now. Other modeling apps can come later :)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, but they all behave differently, with different apps using different values for various brightness settings and so on. There's no feasible way to have lights import sensibly in a generic fashion. Even lights in Unity behave differently depending on whether they are pixel or vertex, and what shaders you're using. Since the way lighting looks is so dependent on factors unique to Unity, there's not much point in trying to set up lighting outside Unity. (Unless you're baking the lighting, in which case you don't actually need the lights to transfer at all.)

    --Eric
     
  5. SewerShark

    SewerShark

    Joined:
    Nov 27, 2009
    Posts:
    12
    They may differ, but, if you can at least import their position, type, orientation and an approximated value (even if it is wrong and you have to tweak it), it would help a lot.

    And if the scripted is based on an specific 3D app, the parameter conversion would be easier.
     
  6. holmeren

    holmeren

    Joined:
    Dec 12, 2006
    Posts:
    300
    Hey
    Some of my lights get imported just as a Empty Game Object and I think its pos. and rot. to.
    Else I usually ad a box or a dummy with the lights transforms and export that..
    I mean you cant have 100 lights in Unity ;) So no I would never use it.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If they were vertex lights, you can. :)

    --Eric
     
  8. holmeren

    holmeren

    Joined:
    Dec 12, 2006
    Posts:
    300
    yes yes,,, heheh
     
  9. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class CrazyImporter : AssetPostprocessor {
    6.  
    7.  
    8.    void OnPostprocessModel(GameObject go){
    9.  
    10.    
    11.  
    12.     try{
    13.         go.AddComponent(go.name);
    14.     } catch(Exception e) { Debug.Log(e); }
    15.    
    16.     SetUpChildren(go.transform);
    17.  
    18.    
    19.   }
    20.  
    21.  
    22.   void SetUpChildren(Transform t){
    23.     foreach(Transform child in t){
    24.         try{
    25.             child.gameObject.AddComponent(child.name);
    26.         } catch(Exception e) { Debug.Log(e); }
    27.         SetUpChildren(child);
    28.     }
    29.   }
    30.  
    31.  
    32. }
    33.  
    34.  

    :twisted:
     
  10. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    That's right! I have scenes in Max that are lit correctly, using few basic lights. I don't want to relight them in Unity. It seems a nobrainer to import the lights, along with cameras and other basic entities.

    What basic light settings are different between apps. Specifically, I mean type of light (point, spot, directional); falloff, range, color, intensity, direction, position, name? Especially from the most common packages - Maya and Max, Modo, etc.?

    So, I haven't yet seen why Unity doesn't import lights. I'll check on this thread later, see if anyone's got more ideas.

    Thanks yall for the replies so far.

    -Josh
     
  11. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    Does that do what I think it does?! (ie, can I buy a comment or quick explanation? :)
     
  12. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    This may be better for you
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class CrazyImporter : AssetPostprocessor {
    6.  
    7.  
    8.    void OnPostprocessModel(GameObject go){
    9.      SetUpChildren(go.transform);
    10.    }
    11.  
    12.  
    13.   void SetUpChildren(Transform t){
    14.     foreach(Transform child in t) {
    15.         if(child.name.Contains("Light")){
    16.         Light light = child.gameObject.AddComponent("Light");
    17.        
    18.     }
    19.         SetUpChildren(child);
    20.     }
    21.   }
    22.  
    23. }
    24.  
     
  13. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    Awesome, thanks!!

    Yes, it does look like it will do what I thought it would. :)

    You've saved me untold hours of learning scripting. :) I'm new to Unity, but I'll work out how and where to run this script.

    I'll try it and report back.

     
  14. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    quick correction

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class CrazyImporter : AssetPostprocessor {
    6.  
    7.  
    8.    void OnPostprocessModel(GameObject go){
    9.      SetUpChildren(go.transform);
    10.    }
    11.  
    12.  
    13.   void SetUpChildren(Transform t){
    14.     foreach(Transform child in t) {
    15.           if(child.name.Contains("Light")){
    16.             Light light = child.gameObject.AddComponent("Light") as Light;
    17.        
    18.         }
    19.         SetUpChildren(child);
    20.     }
    21.   }
    22.  
    23. }
    24.  
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Pretty much all of them, except for color. In Blender, for example, the Dist value for point lights is the distance at which the light intensity is half, whereas in Unity, the equivalent Range value is the total distance that light affects objects at all. In Blender, you have a number of settings for the falloff, but none of them make any difference in the preview (they only work on renders), whereas Unity only has Attenuate on or off, and it does work in the preview. With the exact same setup in Blender and Unity (point light 1 unit above the plane, range of 1.2), you get this in Blender:



    and this in Unity:



    Trying to set up realtime lighting outside Unity is basically pointless; what you see in other 3D apps is not really anything like what you get in Unity. If lights did import, you'd just have to go through them all and change their settings, which is worse than useless IMO. You are of course free to experiment with editor scripts, but as you can see, there is in fact a good reason why Unity doesn't do this already. ;)

    --Eric
     
  16. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
     
  17. oaklandjosh

    oaklandjosh

    Joined:
    Dec 1, 2009
    Posts:
    13
    Cool, It works! At least it found the light entity and throws in a point light. That alone saves some work. sure, it's crude (some mesh named "SecretSpot" would get a light too) but it's a start.

    It doesn't look easy to get anything but the name - Does the importer extract, from FBX, the light's color? directoin? range, falloff? I doubt it; why would it if htey're not importing lights?

    Thus quickest, hackiest way I can think of is encode all the attributes in the name, in 3DS max (using a Maxscript) and then decode the name in this script. e.g. "Spot03-0.5-7CFF20-0-30-625" = Lightname-Intensity-Color-DecayType-HotspotConeAngle-FalloffDistance"

    Another hacky way is a script that reads the FBX file , finds the light entities, matches up names, and modifies Unity's light component to match.

    ...but best is to extend Unity's FBX importer itself, so it extracts light's attributs from FBX and applies lights. That clearly would be best since it doesn't break the awesome automatic-update importing workflow. :) I didn't see any way to do that, without Unity source code, but I am new to Unity - maybe i missed it?

    Code (csharp):
    1.  
    2. ////////
    3. // ImportLights - grabs entities with a certain name,
    4. // and replaces them with a light
    5. //
    6. //Put this script in a new folder "Editor" in your
    7. //  project's root folder.  It'll run automatically
    8. // when you import a model.
    9. //
    10. // Note the hardcoded hacked search text "Fspot"!
    11. //
    12.  
    13. using UnityEngine;
    14. using UnityEditor;
    15.  
    16. public class CrazyImporter : AssetPostprocessor {
    17.  
    18.    void OnPostprocessModel(GameObject go){
    19.      SetUpChildren(go.transform);
    20.    }
    21.    
    22.   void SetUpChildren(Transform t){
    23.     foreach(Transform child in t) {
    24.           if(child.name.Contains("Fspot")){
    25.             Light light = child.gameObject.AddComponent(child.name) as Light;
    26.        
    27.         }
    28.         SetUpChildren(child);
    29.  
    30.     }
    31.   }
    32.  
    33. }
     
  18. Tysoe

    Tysoe

    Joined:
    Jul 6, 2009
    Posts:
    577
    It would be very useful to be able to import lights. Especially if your using lightmaps and want the original lights and falloff attenuation settings to only light animated objects so lighhting matches up with the lightmaps in your scene.

    It should be possible to do a good representation of the lighting if someone wrote a custom exporter, I've seen similar for other engines. It would only work with fixed function (non shader lights). With shaders most lighting info is stored in the material independent of the scene unless its custom written to take data from your user defines scene settings. So probably not very practical with shaders.

    Evem so, just getting the colour and position is a big plus.
     
  19. beayfergm

    beayfergm

    Joined:
    Feb 11, 2010
    Posts:
    14
    I´m trying to do something like a "custom importer" but I´ve found that when I add a component to the empty object of the scene, the rotation that's been imported from the FBX (3DStudio)for this object, is not the correct.

    I think that it would be some kind of conversion but I´ve tried very hard with no result...

    Can anyone help me?

    PS: I´m also trying to add cameras in the same way, with the same problem.
     
  20. Tysoe

    Tysoe

    Joined:
    Jul 6, 2009
    Posts:
    577
    FBX supports lights to some extent. Would be nice if unity supported lights properly in v3.0. Especially with deferred shading where you can normally have a lot more than 3 pixel lights without it severely effecting performance.
     
  21. Nensi

    Nensi

    Joined:
    Jun 11, 2010
    Posts:
    1
    It seems good and I hope you will keep up the good work in future as well
     
  22. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I don't agree very much with this.
    You CAN set up lights somewhat similar to how they will be exported.
    Of course you'll cut off some parameters and only use basic ones, but that's surely a start.
     
  23. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    i second the motion
     
  24. Tysoe

    Tysoe

    Joined:
    Jul 6, 2009
    Posts:
    577
    I think light support is a must for future versions, especially if unity supports the new instance features in FBX 2011.

    I know there are various ways that intensity and the falloff is calculated between different engines. squared, inverse squared and hoe bright the hotspot is etc. (I'm an artist so I don't know the techie stuff)

    But with instancing support it should be trivial to export your lights and their basic values and tweak instances of one to edit them globally in unity later.

    Lighting is one of the most important thing to making a scene look good, and with environments, even if your calculating lightmaps inside the engine, your 3D app is most likely still MUCH better at object placement than the unity editor. Not that unity is bad, it's just a bit clunky compared to something like 3dsmax.
     
  25. Atix illustrations

    Atix illustrations

    Joined:
    May 27, 2011
    Posts:
    3
    It's probably very easy to implement this function...
    create the light with good position color and perhaps the intensity...
    I use maxscript to create automatic scenes with lights with intensity calculated and I don't want have to do the same work in unity....

    For material import it's probably more difficult but opacity would be great too, and reflection?....

    There is a lack of import function it's obvious. I bought unity and these functions are very important for me...
     
  26. Atix illustrations

    Atix illustrations

    Joined:
    May 27, 2011
    Posts:
    3
    Hi, I don't understand how to use these script... could you give me the solution?
    I copy your script in a ImportLights.cs file and save it in projectname\Editor\ImportLights.cs but it doesn't work.

    Help me please ^^, thanks
     
  27. Atix illustrations

    Atix illustrations

    Joined:
    May 27, 2011
    Posts:
    3
    ////////
    // ImportLights - grabs entities with a certain name,
    // and replaces them with a light
    //
    //Put this script in a new folder "Editor" in your
    // project's root folder. It'll run automatically
    // when you import a model.
    //
    // Note the hardcoded hacked search text "Fspot"!
    //

    using UnityEngine;
    using UnityEditor;

    public class CrazyImporter : AssetPostprocessor {

    void OnPostprocessModel(GameObject go){
    SetUpChildren(go.transform);
    }

    void SetUpChildren(Transform t){
    if(t.name.ToLower().Contains("fspot"))
    {
    Light light = t.gameObject.AddComponent(typeof(Light)) as Light;
    }
    // Recurse
    foreach(Transform child in t)
    SetUpChildren(child);

    }

    }