Search Unity

[UPDATE] runtime obj importer

Discussion in 'Made With Unity' started by SavaB, Mar 10, 2010.

  1. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    After spending a couple of days in Unity, I figured I need to create some runtime import possibilities for my project. One of them needed to be a 3D model importer, so I wrote an OBJ importer that can be used on the web.

    ------
    this is still a work in progress, but it's sufficient enough for me at the moment. I'll work on optimizing when I have some more spare time.
    ------

    Every obj I've tested worked with this script. But as OBJ is such an undefined format, I'd like to hear if you find any that don't work.
    ------

    demo page

    // ****** EDIT ****** //
    new attachment has the new OBJ loader.
    The same stuff still applies: if you find any obj's that malfunction, or have any comments or contributions, I'll be happy to respond (and maybe even adjust ;-) )!

    code below (C#):
     

    Attached Files:

    Magiichan likes this.
  2. ChrisBolton

    ChrisBolton

    Joined:
    Nov 19, 2008
    Posts:
    41
    This is fantastic :D should be fun to play around with! It would be really kind of you to post up the code and a quick explanation of how to use it.

    Cheers!

    Chris
     
  3. ChrisBolton

    ChrisBolton

    Joined:
    Nov 19, 2008
    Posts:
    41
    The only model i have been able to import and view has been a cube.obj from the same website as the default cessna.obj everything else I try to view doesn't show up.

    Is this because the model models arn't centered? Unfortunately I havn't got access to a website to upload .obj's so I can't test it.

    Chris
     
  4. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    All the models I've found worked in the importer, but if you find any that don't work can you post the link to them so I can find out what's wrong. It might have something to do with that some exporters write 5+ white space characters between each coordinate, which is pretty annoying, because I only check so many times for white space replacements (=one of the things I still need to optimize).

    I'll post the code tomorrow when I have the time to upload it right :)
     
  5. ChrisBolton

    ChrisBolton

    Joined:
    Nov 19, 2008
    Posts:
    41
    Could this work on the I-phone?
     
  6. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    I don't see why it wouldn't work with the iPhone, but it is something you'll have to find out for yourself (at this moment I only work with Unity Indie).
     
  7. summer006

    summer006

    Joined:
    Feb 16, 2010
    Posts:
    14
    Hi, SavaB:

    Thanks for your great work~ , I love runtime stuff, and hope it can be involved in unity3 :)

    I tested this script , and found 2 problem here:

    1, scale, it's 1000000 bigger than its size ( import by u3d), I add a float (1e-06) to resize it.

    2, texture, my obj model have 3 files: obj, mtl, jpg.
    I'm not sure which one should be given in your texture path.
    I tested them both, but not working.
    There is no code to handle mtl file or label "mtllib" in obj.
    so I guess is it not complete ? or I made a mistake?

    thanks.
     
  8. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    as I said, it is still a work in progress, so .mtl files have to wait for a wile. however, just a basic .obj file with uv-coords and a jpg as texture should work.

    for the size, there is a simple explanation: i haven't optimized the vertex-count. Because I had to make a working script in just a weeks time, I had to 'cheat' a little with the vertices, so now every triangle is actually loose from all other triangles (I'll be fixing this thing soon, no worries) :)

    about the scale-thingy: what scale system did you use when exporting?

    if there is any problem with an .obj, you can allways pm me with the link to the file, and I'll try to solve the problem. It's my intention to make this into a fully functional free runtime 3D object loader, but while developing I don't have enough time to make enough test objects ;)
     
  9. GreyCloud

    GreyCloud

    Joined:
    Jan 11, 2010
    Posts:
    7
    Hi

    Great Code - it works nicely on my .obj file. Althuogh the mesh is too large - at what point should i add scaling code?

    Also i get odd results when loading the texture - any chance you could look at the attached?
     

    Attached Files:

  10. GreyCloud

    GreyCloud

    Joined:
    Jan 11, 2010
    Posts:
    7
    Hi

    Great Code - it works nicely on my .obj file. Althuogh the mesh is too large - at what point should i add scaling code?

    Also i get odd results when loading the texture - any chance you could look at the attached?
     

    Attached Files:

  11. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    k, sorry I've been away for a while. I got stuck in the US because of the vulcano-thingy..
    I'll look at it as soon as I can ;-)
     
  12. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    @greycloud: see PM
     
  13. Brandel

    Brandel

    Joined:
    May 6, 2010
    Posts:
    1
    I've noticed that it wasn't working with my .obj files at all - there were a lot of points clustered around the local origin. When I looked inside I found that it was to do with stripping out the decmial points in vertex definitions - if I had a value of 1.8629 it would be read in as 18629, but if I had a value of 1.5 it would be translated as 15.

    I'm guessing that a lot of exporters will specify points to the same number of significant figures, but the general scale issues are likely to be a result of this same issue. In any case, it seemed to be fine when I commented out the line:
    Code (csharp):
    1.  
    2. s = s.Replace(".", ",");
    3.  
    Thank you again, SavaB, for making this importer! I can see this becoming very handy in the near future :c)
     
  14. GreyCloud

    GreyCloud

    Joined:
    Jan 11, 2010
    Posts:
    7
    Thanks Brandel, that's fixed imports on my obj's

    two other things to note:
    1) it appears that tif's or bmp's are not supported [at least 2048x2048 size ones] - png works though
    2) if you want to have a collider on the mesh then add the following just before the yeild return on the loadclick method

    Code (csharp):
    1.  
    2. // add a mesh collider
    3. obj_gameobject.AddComponent("MeshCollider");
    4.  
    SavaB - Thanks for a great script!
     
  15. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    If anyone is interested, I made a new version: it's a little slower (takes about a second to load a obj of 4000 tris), but optimizes the mesh about as well as the OBJ loader of Unity itself.
     
  16. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    also fixed the language dependant issues.
     
  17. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    this is quite cool, i will give it a try,

    thanks for sharing 8)
     
  18. Deleted User

    Deleted User

    Guest

    Hello there!
    I've just noticed that your .obj loader does not load any material data, as in from the .mtl files which store color/texture information of the model you're loading.
    Any way you could implement that, please?
    I don't think it's very hard, all the other runtime I obj loaders I've checked had it, but I decided to stay with yours because it's simpler to work with and modify to fit my needs...

    So could someone do that please?

    Thanks
     
  19. Deleted User

    Deleted User

    Guest

  20. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    Hi,

    implementing the mtl is not that hard, if you look up the info about how .obj and .mtl works, you could easilly implement it yourself. I have put my Unity-projects (such as this) a little in the background at the moment (my student years are over, I'm a member of the working class now ;-) ) so I'm trying to focus my spare time at the moment on developping my design skills. This project will be updated in the near future (as there are some Unity projects coming to me, and it could be very handy), but I can't tell you exactly when.

    Hope you'll figure it out, and otherwise, see you in a few months! :D
     
  21. cram

    cram

    Joined:
    Jan 8, 2012
    Posts:
    21
    Hi SavaB,

    do you have any new version of the obj importer? Can't get this one to work properly....
    I just get some errors in unity.
    Thanks in advance! :)
     
  22. JAvaHell

    JAvaHell

    Joined:
    Jul 5, 2013
    Posts:
    4
    Hi SaveB.~ I'm korean unity WPF developer~

    It's a very interesting code!!

    But... not working in my project;;

    I'm wan't load the fbxObject use this script~

    First.. I'm attactched code at my 'Camera' Object after change class name, and replace the variable, that named "Text Field String" as my localhost address.(ex)C:/Users/Taesoon/Desktop/Unity_test/UnityInWPF_Message/testETRIAssetsResources/character/test.FBX.

    Second.. Error ouccered... and log is "ArgumentOutOfRangeException : Cannot be nagative Parameter name : length" ...

    V3 Version is create the object, but none mesh and Inspector component...

    V4 Version is nothing happen...;;:(:grin:

    Please help me... I'm very tired from this problem;;;
     
  23. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Since this seems to be a dead topic, I'd recommend ObjReader, which works well and has MTL support.

    --Eric
     
  24. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
  25. Maram Romdhane

    Maram Romdhane

    Joined:
    Mar 2, 2015
    Posts:
    1
    Hello , i tried the objReader code , i got the following exception :
    ArgumentException: Find can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    objReaderCSharpV4..ctor ()

    How can i fix it ?
     
  26. AnataMosnova

    AnataMosnova

    Joined:
    Jun 28, 2016
    Posts:
    1
    It is really great work and I appreciate you.
    Are you going to create runtime exporter?
     
  27. tonycentenoa

    tonycentenoa

    Joined:
    Feb 1, 2017
    Posts:
    2
    Hi,
    The script work really great! But now I have a question. It's possible use this script with photon? I have serveral days trying to share a object with all user in the same room but its impossible... :/
     
  28. bugscuffle

    bugscuffle

    Joined:
    Dec 8, 2015
    Posts:
    73
    thanks! Works really good, but some textures won't import. I don't know where the settings are on the OBJ importer..plus I am not doing it at RUNTIME..but in the SCENE view.
     
  29. mezzostatic

    mezzostatic

    Joined:
    Aug 18, 2016
    Posts:
    13
    I tried and failed on this one. no load error something. donno. i was trying other options for a couple of hours after and wound here again.