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

SimpleOBJ - Runtime importer for OBJ models

Discussion in 'Assets and Asset Store' started by Orbcreation, Nov 25, 2014.

  1. karaman76

    karaman76

    Joined:
    Mar 30, 2016
    Posts:
    1
    Hi, @Orbcreation webgl Simple OBJ errcode err.jpg
    I dont know what is this erro
     
  2. miguelrmillan

    miguelrmillan

    Joined:
    Apr 25, 2016
    Posts:
    2
    Hello OrbCreation! I'm creating a simple OBJ parser for academic purposes and I want to load more than 64K vertices.
    My idea is to parse one superlist of vertices, one superlist of triangles, etc.

    Then I'm generating sub-vertex-list A, sub-vertex-list B and so on with less than 64K vertices. Then I would generate sub-triangles-list A from sub-vertex list A, etc... My question is: how should I generate sub-triangles-lists in order to contain only vertex from a single sub-vertex-list? I'm afraid that lots of triangles will be formed by vertex from different sub-vertex-lists and that will be a problem.

    I'll be very grateful with any help or different implementation ideas
     
  3. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    The process you describe is roughly how Unity's build in importer works. It doesn't have any logic than to take the first 64K vertices and build a mesh from that, then copy all the vertices from that 1st mesh to the 2nd that share a triangle with a vertex in the 2nd mesh.

    The problem with this approach is that if you are unlucky you end up with a multiple meshes that in total have way more vertices than you original.

    SimpleLOD is a bit smarter than that. It starts with the trangles and puts them in the new mesh 1 by one. all vertices that are needed are copied with it until the 64K limit is reached and then it will proceed with the 2nd mesh. This way you will still have vertices that exists in both mesh 1 and mesh 2 and the total nr of vertices is still bigger than the original, but it is much more efficient.

    I don't think it is possible to prevent duplicating vertices when you split up a mesh into 64K parts.
     
    miguelrmillan likes this.
  4. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    You tried to upload a file, and WebGL ran out of memory. It is the most common problem with WebGL.
     
  5. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    I'm trying to use the Simple OBJ for a hololens app and I am getting thread errors during build. I read somewhere else that you cannot use Thread for UWP. Is there any work around this?
     
  6. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Yes, you need to get rid of the Threading in ObjImporter.cs At the top of the file it says:
    using System.Threading;
    That line needs to go.
    On line 56 - 99 are 2 functions called ImportInBackground()
    They need to go as well
    Instead of calling ImportInBackground() you use the alternative function Import() which does the same thing without any threading.
    After this you can compile your project. unless you stil have the demo script SimpleObjDemo.cs in there. You can either delete that or comment line 287 and comment out line 288.
     
  7. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    Thank you! It is working on hololens.
     
  8. carlosucha92

    carlosucha92

    Joined:
    Jan 12, 2017
    Posts:
    1
    Hello,
    Can you import objects in builded programs with this asset?
     
  9. Josef_Wolf

    Josef_Wolf

    Joined:
    Apr 28, 2016
    Posts:
    2
    Hi,

    I just bought SimpleObj and it works fine for a single object at a time. But as soon as I want to load more objects one after another, I run into problems. Here's my code. This one just loads the last object in the string array "objFileName". Is there a way to solve this? Thx for help.

    Code (CSharp):
    1. public class SimpleObjTest : MonoBehaviour {
    2.  
    3.     public string url = "http://www.someURL.com/3DObjects/";
    4.     public string[] objFileName;
    5.  
    6.     void Start () {
    7.  
    8.         for (int i = 0; i < objFileName.Length; i++)
    9.         {
    10.             StartCoroutine (DownloadAndImportOBJ (url + objFileName [i] + ".obj"));
    11.         }
    12.     }
    13.  
    14.     private IEnumerator DownloadAndImportOBJ (string url) {
    15.         string objString = null;
    16.         string mtlString = null;
    17.         Hashtable textures = null;
    18.         GameObject importedObject = null;
    19.  
    20.         yield return StartCoroutine( DownloadFile (url, retval => objString = retval) );
    21.  
    22.         if(objString!=null && objString.Length>0) {
    23.             importedObject = ObjImporter.Import (objString, mtlString, textures);
    24.         }
    25.     }
    26.  
    27.     private IEnumerator DownloadFile(string url, System.Action<string> result) {
    28.         Debug.Log("Downloading "+url);
    29.         WWW www = new WWW(url);
    30.         yield return www;
    31.         if(www.error!=null) {
    32.             Debug.Log(www.error);
    33.         } else {
    34.             Debug.Log("Downloaded "+www.bytesDownloaded+" bytes");
    35.         }
    36.         result(www.text);
    37.     }
    38. }
     
  10. LuckyHamster

    LuckyHamster

    Joined:
    Oct 28, 2014
    Posts:
    50
    How do you place the texture files? I just read on the documentation it will import array of textures. Is this array in the .mtl file? I just got an artist to make a 3d model for me in .obj but did not send me any textures. There is an .mtl file but no textures are loaded when I use simple .obj to import them.
     
  11. Arctous

    Arctous

    Joined:
    Aug 25, 2014
    Posts:
    26
    Has this been tested on iOS?
     
  12. arne1201

    arne1201

    Joined:
    Feb 23, 2017
    Posts:
    1
    Hi

    Does this asset still work on Unity 5.5? I get a warning that it might be incompatible with my version because the last update has been made with Unity 4.

    Does somebody already had problems with it?
     
  13. toddhacker

    toddhacker

    Joined:
    Nov 28, 2016
    Posts:
    2
    Working in 5.6.0f3 with no problems
     
  14. jayjin890118

    jayjin890118

    Joined:
    Apr 28, 2017
    Posts:
    1
    Hello Orbcreation.
    Is this asset working on iOS?
    I mean iphone and ipad.
     
  15. beckmann13

    beckmann13

    Joined:
    Jan 6, 2017
    Posts:
    1
    Hi ,
    after updating to Unity 5.6 we have some problems with the materials. In unity (5.6) the import is working, in the stand - alone - version ( windows/Linux .. ) the importet Obj looses its color. We tried changing the shader - name in "ObjImporter.cs" but no effect.

    line 563----------------------------------------------------------------------------------

    if(meshTrianglesForSubMesh.Count > 0) {
    - Material mat = new Material(Shader.Find("Diffuse"));
    + Material mat = new Material(Shader.Find("Legacy Shaders/Diffuse"));
    mat.SetColor("_Color", Color.white);

    ---------------------------------------------------------------------------------------------

    Any Ideas?
     
  16. jmkzn

    jmkzn

    Joined:
    May 24, 2017
    Posts:
    5
    I'm trying to keep the imported model(s) as part of the scene after ending the playmode, so I could import several models in different runtime sessions. Have you considered to add this functionality to Simple .Obj?
     
  17. misio07

    misio07

    Joined:
    Jan 23, 2017
    Posts:
    1
    I want to buy SimpleObj, but I need to know, as someone else already asked, if it will work with iOS and Android. Thanks.
     
    Last edited: May 24, 2017
  18. Stega

    Stega

    Joined:
    May 22, 2015
    Posts:
    1
    Hello all! I have the same question than misio07, does SimpleObj work on Android? Thanks.
     
  19. Ben-Howard

    Ben-Howard

    Joined:
    Jul 21, 2014
    Posts:
    23
    Great Asset!

    Having a little trouble applying the .mtl file, can you please give me an example of how or where to get the HashTable or texture array? Thanks!!
     
  20. adamclarey

    adamclarey

    Joined:
    Aug 24, 2017
    Posts:
    1
    I'm building a Hololens app and need to render highly complex models. The models are life-size buildings but they will be scaled down to table-top size models for this display.

    The problem I have is that the memory limit is being blown in Hololens (in emulator and actual device) at around 1GB.

    Is there any tweaks I can make to improve the efficiency of this process? Loading the model in Unity Editor works fine.
     
  21. the_greenlig

    the_greenlig

    Joined:
    Feb 5, 2013
    Posts:
    29
    Hard problem. The OBJ importer essentially works with big strings, and you have to be super careful with garbage collection to get it to parse without eating up heaps of memory. We had the same problem when trying to load big files with WebGL. There are ways to convert OBJs to vertex buffer objects, and dump them directly onto the video card, but I think that's well outside the scope of this asset, and would take a fair bit of work to roll your own.
     
  22. Freznosis

    Freznosis

    Joined:
    Jul 16, 2014
    Posts:
    298
    Does anyone have a problem where nothing at all happens when calling Import()? My code below.
    Code (CSharp):
    1.     public void ImportModel()
    2.     {
    3.         ModelResult = ObjImporter.Import(GameDataPath + "Models/" + ModelName, GameDataPath + "Models/" + MaterialName, ModelTextures);
    4.     }
    I also tried putting it into a Coroutine and still nothing happens.

    Code (CSharp):
    1.  
    2.     public IEnumerator ImportModel()
    3.     {
    4.         yield return  ObjImporter.ImportInBackground(GameDataPath() + "Models/" + ModelName, GameDataPath() + "Models/" + MaterialName, ModelTextures, retval => ModelResult= retval);
    5.     }
    The demo scene works fine but using this code straight from the website seems to do nothing.

    Edit:

    Ahh.. I see it doesn't take a regular path and instead a URL. I wish that would've been specified better as I didn't see anything throughout the documentation.

    Code (JavaScript):
    1. Fixed by adding File:/// in front of all paths.
     
    Last edited: Aug 28, 2017
  23. rublb

    rublb

    Joined:
    Apr 15, 2017
    Posts:
    2
    The importer works just fine while running in Unity. Mtl and textures are downloaded and applied correctly. Great! On Andoid however the material won`t get apllied and the entire model gets the pink default material. Any Idea where this can come from or where to start working on it? thanks in advance!
     
  24. Ben-Howard

    Ben-Howard

    Joined:
    Jul 21, 2014
    Posts:
    23
    Anyone ever have any issues with .obj imported in inverted - like mirrored? Same model brought in via Unity pipeline is correct....
     
  25. Ben-Howard

    Ben-Howard

    Joined:
    Jul 21, 2014
    Posts:
    23
    Bump!
     
  26. Ben-Howard

    Ben-Howard

    Joined:
    Jul 21, 2014
    Posts:
    23
    Bump!
     
  27. OT_Tiramisu

    OT_Tiramisu

    Joined:
    Oct 7, 2015
    Posts:
    5
    Hello,
    We are having problem while loading an .obj file from local. We are using demo scene you provided.

    We are just changing the URL with our local URL like "File:///C:/MyModel.obj". The error log is down below.

    Code (CSharp):
    1. ArgumentOutOfRangeException: Argument is out of range.
    2. Parameter name: index
    3. System.Collections.Generic.List`1[UnityEngine.Vector3].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
    4. ObjImporter.MakeGameObject (System.Collections.Generic.List`1 geometries, System.Collections.Hashtable[] matSpecs, Quaternion rotate, Vector3 scale, Vector3 translate, Boolean usesRightHandedCoordinates) (at Assets/SimpleObj/ObjImporter.cs:592)
    5. ObjImporter+<ImportInBackground>c__Iterator1.MoveNext () (at Assets/SimpleObj/ObjImporter.cs:97)
    6. UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
     
  28. NicolasEcononom

    NicolasEcononom

    Joined:
    Mar 28, 2017
    Posts:
    2
    Hello !

    I can't find any information about Hololens compatibilty, could you confirm that your plugin is compatible for Hololens ?

    Thank you
     
  29. the_greenlig

    the_greenlig

    Joined:
    Feb 5, 2013
    Posts:
    29
    Yes, it works with Hololens :)
     
  30. NicolasEcononom

    NicolasEcononom

    Joined:
    Mar 28, 2017
    Posts:
    2
    Thanks !
     
  31. Digital-Portal

    Digital-Portal

    Joined:
    Dec 7, 2017
    Posts:
    1
    I had the same issue. If you make sure that the final bool "usesRightHanded" is true then this should sort out the mirrored look as your import is currently using left-handed co-ordinate system.

    private IEnumerator DownloadAndImportFile(string url, Quaternion rotate, Vector3 scale, Vector3 translate, bool gameObjectPerGrp, bool subMeshPerGrp, bool usesRightHanded)
     
  32. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    Does this plugin work with android?
     
  33. the_greenlig

    the_greenlig

    Joined:
    Feb 5, 2013
    Posts:
    29
    Yes. Watch out for memory usage with large files though, your app might crash if the OBJ is big.
     
  34. johnluxford

    johnluxford

    Joined:
    Oct 14, 2015
    Posts:
    24
    I've successfully integrated SimpleObj and it's worked really well. Just noticed one issue, however, which is that objects are coming in reversed. For example, I imported the following OBJ + MTL files as a test and you can see that the letters on the side of the ship are backwards and the whole thing is reversed.

    https://poly.google.com/view/bzRjbJ74JCr

    Attached is another example that loads correctly using three.js in the browser, but also comes in reversed in Unity. Anything I can do to fix it via the import options?
     

    Attached Files:

  35. Freznosis

    Freznosis

    Joined:
    Jul 16, 2014
    Posts:
    298
    private IEnumerator DownloadAndImportFile(string url, Quaternion rotate, Vector3 scale, Vector3 translate, bool gameObjectPerGrp, bool subMeshPerGrp, bool usesRightHanded)
     
  36. johnluxford

    johnluxford

    Joined:
    Oct 14, 2015
    Posts:
    24
    Thanks! Just had a chance to dig into the code and noticed this too :)
     
  37. abedit

    abedit

    Joined:
    Feb 14, 2018
    Posts:
    1
    Hello

    I got this asset and I was happy that it works. However, I'm having problems loading the textures. I am giving the mtl path but all I get is "404 not found" in the console and the object loads without its textures. Could it be from the mtl file or from the code?

    This is the code

    // Use this for initialization
    void Start () {

    url = "File:///" + PlayerPrefs.GetString("Path"); //get the path
    StartCoroutine(DownloadAndImportFile(url, Quaternion.Euler (270, 0, 0), new Vector3 (1, 1, 1), Vector3.zero, false, false, false));

    }
    private IEnumerator DownloadAndImportFile(string url, Quaternion rotate, Vector3 scale, Vector3 translate, bool gameObjectPerGrp, bool subMeshPerGrp, bool usesRightHanded) {
    string objString = null;
    string mtlString = null;
    Hashtable textures = null;

    yield return StartCoroutine(DownloadFile(url, retval => objString = retval));
    yield return StartCoroutine(DownloadFile(url.Substring(0,url.Length-4)+".mtl", retval => mtlString = retval));
    if(mtlString!=null && mtlString.Length>0) {
    string path = url;
    int lastSlash = path.LastIndexOf('/',path.Length-1);
    if(lastSlash>=0) path = path.Substring(0,lastSlash+1);
    Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString);
    for(int i=0;i<mtls.Length;i++) {
    if(mtls[i].ContainsKey("mainTexName")) {
    Texture2D texture = null;
    string texUrl = path+mtls[i]["mainTexName"];
    yield return StartCoroutine(DownloadTexture(texUrl, retval => texture = retval));
    if(texture != null) {
    if(textures == null) textures = new Hashtable();
    textures[mtls[i]["mainTexName"]] = texture;
    }
    }
    }
    }

    if(objString!=null && objString.Length>0) {
    yield return StartCoroutine(ObjImporter.ImportInBackground(objString, mtlString, textures, rotate, scale, translate, retval => importedObject = retval, gameObjectPerGrp, subMeshPerGrp, usesRightHanded));
    Debug.Log("Done importing model");

    MeshFilter[] meshFilters = importedObject.GetComponentsInChildren<MeshFilter>();
    for(int i=0;i<meshFilters.Length;i++)
    {
    Mesh mesh = meshFilters[i].sharedMesh;
    mesh.RecalculateNormals();
    }
    }
    }

    private IEnumerator DownloadFile(string url, System.Action<string> result) {
    Debug.Log("Downloading "+url);
    WWW www = new WWW(url);
    yield return www;
    if(www.error!=null) {
    Debug.Log(www.error);
    } else {
    Debug.Log("Downloaded "+www.bytesDownloaded+" bytes");
    }
    result(www.text);
    }

    private IEnumerator DownloadTexture(string url, System.Action<Texture2D> result) {
    Debug.Log("Downloading "+url);
    WWW www = new WWW(url);
    yield return www;
    if(www.error!=null) {
    Debug.Log(www.error);
    } else {
    Debug.Log("Downloaded "+www.bytesDownloaded+" bytes");
    }
    result(www.texture);
    }


    Thank you.
     
  38. johnluxford

    johnluxford

    Joined:
    Oct 14, 2015
    Posts:
    24
    Been using this asset more and noticed some material settings don't import quite as you'd expect. When I have some time I'm going to do a deeper comparison with a bunch of MTL files, but here's one that seems to improve things for now:

    On line 415, the Ns value is calculated as 1/s where s is in the range of 0.1-1000. This means a value of 400 comes in at 0.0025 instead of 0.4. Changing this to s/1000 seems to improve the shininess of objects.

    Hope that helps!
     
  39. akhtarsalmaan2

    akhtarsalmaan2

    Joined:
    May 6, 2018
    Posts:
    2
    Hi I am a kid and i really want to use this asset but I cannot buy.I will not make any money out of this.It s for a school project.so could you please mail me the assests
     
  40. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    there are free alternatives for .obj importers, in asset store also, try some of those.
     
  41. ismaelnascimentoash

    ismaelnascimentoash

    Joined:
    Apr 2, 2017
    Posts:
    30
    Hello,

    The asset Simple Obj, keep file in device ?

    Regards,
     
  42. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Is it possible to download the demo on its own to test this asset? Unfortunately the web player version doesn't work.

    According to the asset store page, this "can run in background thread". Are we talking about an actual thread or a coroutine and is this still the case (I need it to import .obj files from HDD at runtime)?

    If so, how does it work? I know that you can't generate a GameObject in a background thread because this stuff isn't thread-safe, so what does it return? Did Orbcreation find a way around that and still gives you the complete GameObject or do you get all the data in e.g. arrays (one for vertices, one for UVs,...), which you can then use to create your own GameObject?
     
  43. param_appm

    param_appm

    Joined:
    Jun 14, 2017
    Posts:
    41
    Hello,

    I plan on buying this asset, but I have one issue regarding the obj file.

    I tried using your links, and when I open those in browser it opens as text file.

    But when I am uploading my obj over my server and run over browser, it gets downloaded.
    http://181.224.159.172/~quickyco/appmechanic/test2.obj

    But if I check your links, it opens as text file
    http://orbcreation.com/SimpleObj/teapot.obj

    How can I make my obj file read as text file from server and them import in Unity ?

    Looking forward hearing back !

    Thanks & Regards,
    Paramjot Singh
     
  44. prajwalshetty

    prajwalshetty

    Joined:
    Mar 6, 2018
    Posts:
    1
    Hello,

    I have purchased the simple .obj plugin for unity and the Loading of .obj files at runtime is working as intended. But the problem is that the Obj imported GameObjects are using legacy shaders instead of the standard shaders, Will you be able to help me out?

    Thanks and regards
     
  45. ehsan_wwe1

    ehsan_wwe1

    Joined:
    Apr 16, 2011
    Posts:
    22
    thanks i use your importer easily
    your add-on help me full
    i'm happy for buying your product
     
  46. abhiramelf

    abhiramelf

    Joined:
    Oct 3, 2016
    Posts:
    1
    It seems that the plugin doesn't work for android. When the shaders were legacy, atleast I used to get pink objects. Now with the new update, with standard shader becoming the default, even though the models are loaded they are not rendered in my scene in android. Could you please help me out. In desperate need for a solution.
     
  47. sundasfatima

    sundasfatima

    Joined:
    Dec 24, 2017
    Posts:
    3
    It can import models with annotations like given below runtime?
     
  48. Gaurav_Setia

    Gaurav_Setia

    Joined:
    Apr 9, 2017
    Posts:
    4
    Hi,
    I am using the package to import obj and while the obj is getting imported, the editor freezes, same happens with an exe. i have checked in the task manager, the editor is not responding for the time.
     
    Last edited: Nov 12, 2018
  49. the_greenlig

    the_greenlig

    Joined:
    Feb 5, 2013
    Posts:
    29
    How long is the freeze? Permanent or temporary? The importer just parses the huge OBJ string, so it can hog the main thread and hold everything up if you don't use the Async loading function.
     
  50. Gaurav_Setia

    Gaurav_Setia

    Joined:
    Apr 9, 2017
    Posts:
    4
    It freezes till the model gets imported into the application. the freeze is temporary. is there any way to load it using a different thread