Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[FREE] Runtime OBJ Loader

Discussion in 'Assets and Asset Store' started by ZO5KmUG6R, Nov 4, 2015.

  1. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    upload_2015-11-29_22-38-21.png

    Features

    -Ability to load OBJ file from stream
    -mtl file loading, and texture loading (dds, crn, jpg, jpeg, png, tga, bmp)
    -Triangle, Quad, and Ngon import
    -Fast loading times
    -Works in both the editor, and runtime*
    -Usable in the editor

    *Runtime Usage Requirements
    In order for this to work in runtime, you must go to the "Graphics" tab in your project, and add "Standard (Specular Setup)" to the always included shaders. This will make your next build take a while.

    If you don't do this, the required shader / shader variants won't exist.

    How Do I Use It?


    Editor
    Select the GameObject menu (up top)
    Then select "Import from OBJ"

    Runtime
    Code (CSharp):
    1. using Dummiesman;
    2. using System.IO;
    3. using UnityEngine;
    4.  
    5. public class ObjFromFile : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         //file path
    10.         string filePath = @"I:\random\cylinder.obj";
    11.         if (!File.Exists(filePath))
    12.         {
    13.             Debug.LogError("Please set FilePath in ObjFromFile.cs to a valid path.");
    14.             return;
    15.         }
    16.  
    17.         //load
    18.         var loadedObj = new OBJLoader().Load(filePath);
    19.     }
    20. }
    21.  
    Example Imports
    upload_2019-1-4_7-30-7.png

    upload_2019-1-4_7-32-5.png

    Downloads
     

    Attached Files:

    Last edited: Jan 4, 2019
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I submitted this to the asset store on the day I submitted the last update here on the forum (nov 13th). Hopefully you can find it soon :) (Pending Approval atm)
     
    Last edited: Nov 30, 2015
    SpandanaMatapathi likes this.
  3. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Thank you, Aaro4130. :cool:
     
    ZO5KmUG6R likes this.
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Package has just been approved on the asset store. :D
     
    heartingNinja and GibTreaty like this.
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Here is version 1.1 early (since it'll take ~3 weeks at the current Asset Store approval rate)

    Changes
    -Now uses Standard shader
    -Added transparency support
    -Added normal map support
    -Added support for reading partially malformed or improperly created OBJ files
     

    Attached Files:

    CrazyTegger and theANMATOR2b like this.
  6. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    This fixed my index out of bounds issue, my models can now successfully import. Thanks for the quick response and fix.
     
  7. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Here is version 1.11 early

    Changes
    -Added 'searchPaths' variable
    -Added 'splitByMaterial' variable

    Use splitByMaterial if meshes have too many vertices in them.
     

    Attached Files:

  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    One last update

    Version 1.12
    Changes
    -Material names, mesh names, textures, etc now support spaces.
    -Script optimizations
    -Fixed an issue where search paths in MTL files weren't working correctly
     

    Attached Files:

    Last edited: Jan 7, 2016
  9. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    First off, thank you for making this!
    Now, I'm having problem making this work. I called OBJLoader.LoadOBJFile("C:/Users/gamer/Documents/image.obj");
    and when i run on unity editor on windows, unity freezes. I'm not even sure if my file path name is in the correct format. Please help.

    Thank you again.
     
  10. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    Unity just unfroze and I get the errors:
    Mesh.vertices is too large.
    Mesh.normals is out of bounds
    Mesh.uv is out of bounds
    Failed setting triangles

    I am trying to load a medical image exported as obj and it is quite large: 20MB. Is it too large?

    Thank you.
     
  11. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    I had the same issue before some optimization. Try setting the spitbymaterial to true
     
  12. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    Tried it, got same errors.
     
  13. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Could you screenshot the error log for me?
     
  14. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    Here it is
     

    Attached Files:

  15. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Ah, you'll need to divide the mesh from where it's being exported from. If that's not possible, i can see if I can work something out for you.
     
  16. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    Wow 800k vertices. can unity even handle meshes that large?
     
  17. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    Yes. If you import it in the editor, unit splits it up into different meshes. But I don't know how to do it myself. It's an obj file from a catscan so its pretty big.
     
  18. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I could work something out for you :)
     
  19. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    That would be beautiful. I'm still clueless on how to solve this problem.
     
  20. VrTechEx

    VrTechEx

    Joined:
    Aug 4, 2013
    Posts:
    40
    I suggest you could make vertices to be queued and rendered as 1 mesh with 65000 vertices. Then dequeue the remaining :) to finish
     
  21. SAOTA

    SAOTA

    Joined:
    Feb 9, 2015
    Posts:
    220
    I'm having a problem where an obj does not load in a built project. it works perfectly in the editor. thoughts?
     
  22. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Problem solved over PM :)
     
  23. SAOTA

    SAOTA

    Joined:
    Feb 9, 2015
    Posts:
    220
    Pre-load the standard shaders everyone :) Works great !
     
  24. Showoffz

    Showoffz

    Joined:
    Feb 12, 2016
    Posts:
    8
    Hello.

    I have this simple script that tries to load OBJ.:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using System;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class script : MonoBehaviour {
    9.  
    10.     [SerializeField]
    11.     Button btn;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         btn.onClick.AddListener(new UnityEngine.Events.UnityAction(askForFileInput));
    23.     }
    24.     private void askForFileInput()
    25.     {
    26.         // Displays Dialog
    27.         EditorUtility.DisplayDialog("Select OBJ",
    28.                     "You Must Select a OBJ file!",
    29.                     "Ok");
    30.         // Asks for file input
    31.         var path = EditorUtility.OpenFilePanel(
    32.                     "Load obj",
    33.                     "",
    34.                     "obj");
    35.         // Custon OBJLoader (C) 2015 AARO4130 - Free from Asset Store (Runtime OBJ Importer) 25.04.2016
    36.         GameObject loadedObj = OBJLoader.LoadOBJFile(path);
    37.         // Save file
    38.  
    39.  
    40.         Debug.LogWarning(path);
    41.         throw new NotImplementedException();
    42.     }
    43. }
    44.  
    Including OBJLoader.LoadOBJFile line freezes my unity.
     
    faziii likes this.
  25. joeydmars

    joeydmars

    Joined:
    Sep 1, 2013
    Posts:
    2
    Hello I seem to be having the same problem as user SAOTA. My project loads the .obj file just fine in the editor but does not render it once running in a stand alone application. preloading the standard shaders i believe i have done correctly however i am not sure. any help would be much appreciated!
     
  26. joeydmars

    joeydmars

    Joined:
    Sep 1, 2013
    Posts:
    2
    nevermind i figured it out on my own! thanks for making an excellent OBJ loader!!
     
  27. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    @Showoffz Sorry for the late response. The OBJ Loader does not run async by default. You will need to write your own code for that. For this reason, it will lock the thread while performing it's loading operations, in turn, freezing Unity for a bit.
     
  28. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Hello aaro, I'm having some problems with testing your loader. So I have this farmhouse that I'm trying to load in, but only part of it is loaded.

    Code is just a simple

    void Start () {
    OBJLoader.LoadOBJFile("C:/Users/Intern/Downloads/OBJ/Farmhouse OBJ.obj");
    }

    in an empty gameobject

    Any idea what I'm doing wrong?
    farmhouse.png

    Edit: I tried with a smaller model of grass and it worked.

    Edit2: Also does your TextureLoader support materials? And if it does, can you show me an example on how to apply the material? Sorry if I'm asking too much.
     
    Last edited: May 13, 2016
  29. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    It works with materials yes. You probably have moved the OBJ file from it's original location, breaking materials.

    See if your exporter has an option for "Relative Pathing" or similar.
     
  30. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Hello again aaro, sorry for pestering you. I'm trying to apply a .MTL onto my .OBJ with this code, it's not working (probably my misunderstanding).

    Code (CSharp):
    1.  GameObject grass = OBJLoader.LoadOBJFile("C:/Users/Intern/Desktop/ObjectFile/Grass pack/Grass_01.obj");
    2.         Debug.Log(grass.name);
    3.         Material[] grassmaterial = OBJLoader.LoadMTLFile("C:/Users/Intern/Desktop/ObjectFile/Grass pack/Grass_01.MTL");
    4.         Debug.Log(grassmaterial[0].name);
    5.         grass.GetComponentInChildren<Renderer>().material = grassmaterial[0];
     
  31. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    The OBJ file contains a reference to the MTL file. LoadMTLFile is only supposed to be used internally by the loader, and is automatically called with LoadOBJFile

    The reason you don't see your MTL file is most likely because your exporter exported a non relative path, and you have moved the file, OR you are trying to load incorrect texture formats.
     
  32. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Yup, you're right, I just moved the MTL and OBJ back into same folder. Now works perfect. Thanks! On a side note, do you plan to include other formats like .fbx in the future.
     
  33. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Most likely not. Their format specifications are gibberish to me :D
     
    Jilbarkus likes this.
  34. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41

    Hi again aaro, how can I change the position where the OBJ loader loads the object at.

    Trying to do something like in the image.
     
    Last edited: May 18, 2016
  35. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Setting .transform.position should work fine.
     
  36. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Something else in my scripts was changing the position :p Now it works fine.
     
  37. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Hi Aaro, just wanted to confirm with you. Do I need to put the .png/.jpg textures together with the .obj and .mtl or only need .obj and .mtl?

    Edit: Also do I need to use the textureloader function or is that also only called by LoadOBJ I also have multiple png/jpegs.
     
    Last edited: May 20, 2016
  38. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Yes you need to have the textures alongside the files
    TextureLoader doesn't need to be touched.
     
  39. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    Thanks Aaro, hopefully my last question, does this work for webplayer games? Also if it doesn't, anyway to change that?

    Edit: Tried building webplayable doesn't work, know how can I change this to support webplayer games?
     
    Last edited: May 23, 2016
  40. miguelrmillan

    miguelrmillan

    Joined:
    Apr 25, 2016
    Posts:
    2
    Hey, I'm trying to do my own OBJ parser for academic purposes but I have the 65k vertices per mesh issue. Can you give me some hints to split a big mesh into sub-meshes of less than 65k-vertices in order to load big models without loosing the index vertices references? Thanks!
     
  41. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    I'm in the same boat. I'd like to see if we can have some sort of implementation that splits the mesh, similar to what Unity does. This is the one feature I really need before I can use it in production.
     
  42. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I think I could add a dictionary for vertex count per mesh, and upon each "f" statement, check it's highest number. If > 64500 (something less than 65000 in case someone has a really large n-gon, I'll probably support those eventually), then set the current object to "current object " + "1" or something of that sorts.
     
  43. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    Whatever you think - I can provide some obj files for testing if you'd like?
     
    Last edited: Oct 5, 2016
  44. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    PM'd you the files I've been using for reference in testing
     
  45. goodwin74nk

    goodwin74nk

    Joined:
    Nov 10, 2016
    Posts:
    2
    I'm having a problem where an obj does not load in a built project. It works perfectly in the editor. thoughts?

     
  46. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    I don't think the shader you're trying to use is included in the build.
     
  47. James76

    James76

    Joined:
    Dec 1, 2016
    Posts:
    1
    Hi aaro,
    Trying to load up an obj using your script and I'm getting a DirectoryNotFoundException:

    DirectoryNotFoundException: Could not find a part of the path "/Users/jamespostlethwaite/Documents/UNITY5/RTC LAYOUT PLANNING APP_v3/http:/www.jp1976.tk/OBJ/untitled.obj".

    The full path is http:/www.jp1976.tk/OBJ/untitled.obj however the script is adding /Users/jamespostlethwaite/Documents/UNITY5/RTC LAYOUT PLANNING APP_v3/ popping up the error.

    How can I correct this?

    Best regards,
    James.
     
  48. CrazyTegger

    CrazyTegger

    Joined:
    Feb 15, 2015
    Posts:
    57
    If you open up the obj file, is that full path specified in the mtl file?
     
  49. gibarian

    gibarian

    Joined:
    Jun 23, 2016
    Posts:
    1
    Hi aaro4130, thanks for this beautiful asset! I am running into the same problem - could you please let me (us) know how to break the object into several meshes if the vertice count is too large? Seeting the splitByMaterial to true doesn't do the trick for me :/ Thanks so much in advance!
     
  50. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    Hi @aaro4130,
    Thank you for the wonderful asset. I have to load model which has more than 64k vertices.
    I cannot modify the models as they are user generated. Could you tell me how can I break the single mesh
    into sub meshes at runtime. Waiting for the reply.