Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[FREE] Runtime OBJ Loader

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

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Good stuff guys!
     
  2. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    430
    Another update pushed, now supporting BumpScale , and also skipping over other texture args

    e.g.
    map_Kd -s 1 1 1 -o 0 0 0 -mm 0 1 chrome.mpc
    should no longer think "-s 1 1 1 -o 0 0 0 -mm 0 1 chrome.mpc" is the texture name
     
  3. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    147
    I've got a couple of questions:

    - The first post says that you have to add a specular shader to make it work. Can you use your own shaders for the imported objects too?
    - Is there already anything in place to import objects asynchrously (a coroutine or a thread)?

    - I know this says "2018.3.1 or higher" but has anyone tried running it with Unity 2017 yet (and succeeded?)?
    - Has anyone compared the speed of the new version to other importers yet?
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Yes, you can change the shader in the code.

    Not sure.

    Should work fine in 2017, any version I suppose.

    Yes, it works great. Fast and light.
     
  5. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    147
    Nice, thanks.
    Does anyone know?
    I'm currently using a different asset (without any support for coroutines or threads) but I'm usually importing 20+ small .obj files, which often makes my game freeze for a split second.
    Unfortunately I can't download it through the asset store (it says that I have to upgrade) and the upload linked in post #93 is offline.
    May I ask what importers you've tested it against?
     
  6. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Most of the ones on the asset store. The author of this one is a friend of mine personally, so I know the code is well-structured (I'm using it in my own modifiable racing game) and quick (he's very keen on micro-optimizations). I load models with it over 50,000 tris in almost no time.
     
  7. Jake_Bellamy

    Jake_Bellamy

    Joined:
    Aug 21, 2018
    Posts:
    2
    Hey, just recently found this amazing asset (thanks for releasing it for free btw) and I'm having trouble loading an obj with its mtl file streaming from a server. Is there an example of this anywhere or can someone provide an example so I can see what I may be doing wrong? I can get it load and it Does put materials on it, it just doesn't color it correctly. As a test, I'm trying to load in a yellow Lamborghini but when it loads in its just black and grey. So..yeah, any help is greatly appreciated!

    Edit: So, with more fiddling I figured out it ended up being the mtl file associated with the obj I was testing with. This is all still fairly new to me and I was having issues with it reading transparent materials properly (by adjusting the mtl settings for the specific material it was supposed to be creating) and I ended up finding another option. Had to pay for it though, but, whatever works.
     
    Last edited: Mar 21, 2019
  8. FireGreeks

    FireGreeks

    Joined:
    Jan 14, 2017
    Posts:
    4
    Hello @aar4010
    I get an error while using your asset (I am surely missing something)...
    OBJLoader
    namespace couldn't be found... Am I missing a reference?

    Thank you in Advance

    Sincerely
     
  9. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    430
    Are these errors happening in the sample scenes, or when trying from custom scripts?
     
  10. Sicerath

    Sicerath

    Joined:
    Feb 2, 2019
    Posts:
    5
    Would anyone be able to find some guidance as to how I can download the associated .mtl and .jpg files from a server? Aaro had mentioned passing in a stream for the MTL and overriding the texture loader, but to be honest I'm not sure how to go about accomplishing this. C# and Unity are pretty new to me, so I'm floundering a bit.

    I'm working on this project for my capstone class at University, so any help is greatly appreciated ASAP.
     
    Last edited: Mar 15, 2019
  11. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    430
    I'm aware there's an error when building for IOS btw. When I get a decent amount of free time I'll deal with the asset upload process again and fix it. If you know code, you'll be able to fix it in the meantime.

    Sorry for the lack of responsiveness in private messages btw, working all the time D:
     
  12. Sicerath

    Sicerath

    Joined:
    Feb 2, 2019
    Posts:
    5
    No problem! I've got almost everything working at this point. My only issue is that calling OBJLoader.Load(objStream, mtlStream) completely freezes the app. Is there any way at all to get around this? I've tried using Ninja Multithread coroutines, but that won't work because there are some unity functions being called in there (so multithreading isn't an option). I timed it and the Load function freezes the app for about 5 seconds, which is a lot haha.

    Any tips @aaro4130?
     
    Last edited: Mar 27, 2019
  13. AnandSG

    AnandSG

    Joined:
    Oct 5, 2018
    Posts:
    9
    where i can find this loader ? is this work with UWP ? what is the limit for the size of the 3 d model file ( i want to load 200mb file)
     
  14. Xulain

    Xulain

    Joined:
    Oct 14, 2017
    Posts:
    21
    Exception: An error occured while loading DirectDraw Surface: Cannot load DDS due to an unsupported pixel format. Needs to be DXT1 or DXT5.

    the thing is these dds textures import into unity fine manually. So how do I make obj the importer ignore the textures?.


    Edit: NVM Unity can import obj files without this asset duh :) .
     
    Last edited: Mar 31, 2019
  15. prabhusam

    prabhusam

    Joined:
    Aug 3, 2017
    Posts:
    6
    I have loaded the 3D object via stream in .obj and .mtl formats successfully. I don’t know how to handle and apply the texture files. Kindly provide me with the possible solutions.

    Screen Shot 2019-04-05 at 5.02.05 PM.png
     
  16. Sicerath

    Sicerath

    Joined:
    Feb 2, 2019
    Posts:
    5
    You have to download the image files separately and then save them! That was my big mistake. I would look into using WebClient to download the images as I found it was a lot faster.
     
  17. Sicerath

    Sicerath

    Joined:
    Feb 2, 2019
    Posts:
    5
    Has anyone had any luck putting the OBJLoader.load() function into a background thread? It's freezing and even crashing my app at some points. Getting a bit frustrating.

    [Edit] It's looking like a really bad memory leak in ImageLoader... Memory usage spikes to almost 900mb within that class.
     
    Last edited: Apr 12, 2019
    smartinetz likes this.
  18. prabhusam

    prabhusam

    Joined:
    Aug 3, 2017
    Posts:
    6
    Hi Sicerath,
    Thank you for reply. Its really helpful to me.
     
  19. AnandSG

    AnandSG

    Joined:
    Oct 5, 2018
    Posts:
    9
    Do you found any solution? facing same problem here, its just stuck the scene for so long.
     
    smartinetz likes this.
  20. Prakash1206

    Prakash1206

    Joined:
    Jun 21, 2019
    Posts:
    6
    Issue with loading model on webgl
    if i run the code directly in unity, it works well - tried on unity 2017(old version of obj package) and unity 2019 (latest version of obj package)

    Code (CSharp):
    1. GameObject model = new OBJLoader.Load("I_Normal.obj");
    but if i build to webgl, build succeeds but model doesn't load on browser.
    on browser console i see message "FileNotFoundException" (for detailed log i have attached image)
    [tried with full file path with double back slash and single forward slash. nothing worked]

    what i noticed is that it always appends a forward slash to file path before loading model.
    in C# even if i specify full path as file:///D:/modelName.obj in browser console i see
    How to fix this?
    Thanks & Regards
     

    Attached Files:

  21. fxmdeveloper

    fxmdeveloper

    Joined:
    Aug 13, 2018
    Posts:
    1
    Hi, is this asset work for hololens? I cannot seem to get it working with Hololens. It does momentary stuck (if using big obj) but the model not loaded at all.

    This is the last debug I get before the object not loaded (I don't know if this is the right debug, I don't really know how to debug at hololens)

    Code (CSharp):
    1. Exception thrown at 0x77222DC2 in DTSA Template Hololens.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x0252F49C.
    2. Exception thrown at 0x77222DC2 in DTSA Template Hololens.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x0252F514.
    3. Exception thrown at 0x77222DC2 in DTSA Template Hololens.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x0252F5B4.
    4. Exception thrown at 0x77222DC2 in DTSA Template Hololens.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x0252F68C.
     
  22. mabakay

    mabakay

    Joined:
    Jun 10, 2016
    Posts:
    19
    Is there way to download this asset from external site that assetstore? Im using older version of Unity.
     
  23. smartinetz

    smartinetz

    Joined:
    Dec 8, 2017
    Posts:
    46
    Hi ..

    I am using your script and it works just fine in the editor. After I publish it to iOS however I get this error when I try to load my Model from Application.persistentDataPath. I use the same path to load images and my database, so I guess that's not the issue here. I am clueless right now.. any takers?

    I just call: loadedObject = new OBJLoader().Load(Configuration.Mypath + fileName);

    Why does this work in the editor but not on the device!??

    Code (CSharp):
    1. ArgumentNullException: Value cannot be null.
    2. Parameter name: shader
    3.   at UnityEngine.Material.CreateWithShader (UnityEngine.Material self, UnityEngine.Shader shader) [0x00000] in <00000000000000000000000000000000>:0
    4.   at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in <00000000000000000000000000000000>:0
    5.   at Dummiesman.OBJLoaderHelper.CreateNullMaterial () [0x00000] in <00000000000000000000000000000000>:0
    6.   at Dummiesman.OBJObjectBuilder.Build () [0x00000] in <00000000000000000000000000000000>:0
    7.   at Dummiesman.OBJLoader.Load (System.IO.Stream input) [0x00000] in <00000000000000000000000000000000>:0
    8.   at Dummiesman.OBJLoader.Load (System.String path, System.String mtlPath) [0x00000] in <00000000000000000000000000000000>:0
    9.   at Dummiesman.OBJLoader.Load (System.String path) [0x00000] in <00000000000000000000000000000000>:0
    10.   at ObjFromFile.OnGUI () [0x00000] in <00000000000000000000000000000000>:0
    11.  
     
  24. smartinetz

    smartinetz

    Joined:
    Dec 8, 2017
    Posts:
    46
    If anybody ever stumbles across this, I solved it by adding the Standard Specular Setup in
    Project Settings > Graphics > Always Included shaders

    Yes, I know, it says so in the included Readme ;) :p
     
    WhiteFangLee likes this.
  25. Midiphony

    Midiphony

    Joined:
    Jan 25, 2019
    Posts:
    3
    This asset was exactly what I needed ! The scripts are well documented so anyone should be able to use this importer within minutes (an hour max) :D

    However with the current version of Unity, it takes forever, while building to compile the Standard and Standard (Specular Setup) shaders. I am pretty sure the problem comes from Unity or my project settings (and not this asset!) and am currently seeking help :\
     
  26. JCUDOS

    JCUDOS

    Joined:
    Oct 11, 2018
    Posts:
    1
    Praise be aaro4130!
    I saw this feature in Besiege (which allows users to create skins themselves) before I started developing games and now I find out I don't even have to start from scratch to add it to my game, this is great...
    Praise be aaro4130!!!
     
  27. dandib

    dandib

    Joined:
    Jan 6, 2017
    Posts:
    2
    Hey @aaro4130 ,
    First of all great asset!

    I am loading multiple OBJs one after the other. they are loaded in the same position as they were when they were created in a different app. However, when loaded into Unity all gameobjects have the same position of 0,0,0.
    I've tried many things to try to get their actual position but I always receive 0,0,0.

    I really need to access their real positions, Is there a way to do this?

    Thanks! posProblem.png
     
  28. smartinetz

    smartinetz

    Joined:
    Dec 8, 2017
    Posts:
    46
    Hey... did any of you have success with async loading?
     
  29. TheDav1311

    TheDav1311

    Joined:
    Sep 30, 2019
    Posts:
    1
    I found a bug.
    If you load a obj-file which was created by Blender the mesh is all over the place.

    This should be a icosahedron. bug.png
     
  30. ianxyz100

    ianxyz100

    Joined:
    May 10, 2018
    Posts:
    3
    Hey, I have used this asset for desktop and it works great, but when I try to build for IOS, I get the errors:

    ImageLoader.cs error CS0117: 'TextureFormat' does not contain a definition for 'DXT1Crunched'
    ImageLoader.cs error CS0117: 'TextureFormat' does not contain a definition for 'DXT5Crunched'
    ImageLoader.cs error CS0117: 'TextureFormat' does not contain a definition for 'ETC2_RGBA8Crunched'

    Any help would be greatly appreciated :)
     
  31. prosoftvr

    prosoftvr

    Joined:
    Mar 23, 2017
    Posts:
    1
    Merhaba, Birden fazla obj dosyası nasıl eklenir?
     
  32. Elharter

    Elharter

    Joined:
    Sep 20, 2015
    Posts:
    45
  33. Risodon

    Risodon

    Joined:
    Jan 9, 2018
    Posts:
    1
    Hi, first of all thanks for the asset and your hard work.

    I am trying to load a .obj (preferably with texture but without is fine) from another computer to show it with unity. This should happen constantly every 5 sec. When I use your asset, there is a giant lag during the loading of the mesh. Do you think this is fixable within your scripts or am I out of luck? Maybe you can point me in the right direction since I already do the download in the background now, but the processing of the .obj file text stream seems to lock the main thread.

    Thanks again,
     
  34. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    15
    Hello!

    Thank you for a wonderful plugin!
    Quick question, does this work on Android? I have been able to get it working on engine perfectly, but no luck with an exported Android app which I would have a dire need to. It simply does nothing when I load a model on an Android app. I have added the 'Standard Specular Setup'.

    Edit: might be my foolishness in not allowing android permissions. Testing if this fixes my issue.
     
    Last edited: Mar 9, 2020
  35. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,115
  36. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    15
    Apologies! My code mistake, it works. Huge thanks to the creator of this!
    Sorry for the inconvenience with the erroneous post. 5* Review submitted.
     
    Last edited: Mar 9, 2020
  37. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    23
    This asset looks really neat.

    I'm wondering though. Is it possible to have the loader hand you the data as a mesh and not a game object?

    I use Unity's Graphics class a lot to draw things directly, and so I wouldn't have much use for instantiation.
    I would pretty much just destroy the object after copying the mesh data.

    If this is indeed not possible right now, I think it would be worth considering reusability wise. Perhaps performance as well if you could load the mesh data on a separate thread. I know Unity doesn't like it when you try to use its API outside the main thread.
     
  38. Marco_Vise_U

    Marco_Vise_U

    Joined:
    Apr 9, 2020
    Posts:
    1
    Hi @aaro4130 ,
    I used this code, which is the example above, so it should definitely work:
    1. using Dummiesman;
    2. using System.IO;
    3. using UnityEngine;

    4. public class ObjFromFile : MonoBehaviour
    5. {
    6. void Start()
    7. {
    8. //file path
    9. string filePath = @"I:\random\cylinder.obj";
    10. if (!File.Exists(filePath))
    11. {
    12. Debug.LogError("Please set FilePath in ObjFromFile.cs to a valid path.");
    13. return;
    14. }

    15. //load
    16. var loadedObj = new OBJLoader().Load(filePath);
    17. }
    18. }

    It actually import the obj file but i cannot visualize it. I tried to move it in front of the camera. It just not appear. Can you tell what could be the problem??
     
  39. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    147
    @Marco_Vise_U
    I had the same problem when I first tested the asset almost 2 years ago. I eventually gave up on it and now use a different asset.
    How do you know that it imported the file? Is there a GameObject in the hierarchy? Does it show it (or at least an outline) in the "Scene" tab? If the answer to both of these questions is "yes", then the most common causes are:

    - No lighting but the object uses a shader that relies on lighting
    - UVs/texture vectors point the wrong way (then the object is only visible from below/behind)
    - No texture
    - No shader

    If the GO shows up in the hierarchy but there's no outline, something probably went wrong importing.
     
  40. radacadabra

    radacadabra

    Joined:
    Feb 20, 2020
    Posts:
    2
    Hey @aaro4130 ,

    Fantastic asset, great work. Thank you for this.

    I am encountering a problem that I believe nobody reported before in this thread - the imported mesh has its rendering mode set to "Transparent" by default, so it appears invisible. When I import the same mesh with Unity importer everything is fine.

    I tried to solve this by changing the shader and rendering mode on runtime with this piece of code which iterates through all children and all materials:

    Code (CSharp):
    1.         Renderer[] mr = loadedOBJmesh.GetComponentsInChildren<Renderer>();
    2.         foreach (Renderer renderer in mr)
    3.         {
    4.             for (var i = 0; i < renderer.materials.Length; i++)
    5.             {
    6.                 renderer.materials.shader = Shader.Find("Standard");
    7.                 renderer.materials.SetFloat("_Mode", 0.0f); // mode 0 sets Rendering mode to Opaque
    8.             }
    9.         }
    The shader changes, but the rendering mode is still transparent, until - believe it or not (I am having troubles believing it) - I expand the material properties in the Inspector while the game is running. I don't actually change anything, just expand the properties, and the rendering mode changes (even though the code that should change it is called on "Start"). I cannot wrap my head around how or why this is happening.

    I did add the shader "Standard (Specular setup)" to always included ones per importer instructions.

    Did you (or anyone else) encounter similar problems? What am I doing wrong? Thanks :)
     
  41. radacadabra

    radacadabra

    Joined:
    Feb 20, 2020
    Posts:
    2
    @Marco_Vise_U
    Maybe you have the same problem as I - if the object is loaded but invisible, check in Inspector if the shader Rendering mode is set to Transparent?
     
  42. unity_FLgZGOCMOxJ3cw

    unity_FLgZGOCMOxJ3cw

    Joined:
    May 25, 2020
    Posts:
    1
    Is this capeable of creating groups and seperate children in our working tree?
     
  43. thvas

    thvas

    Joined:
    Mar 13, 2020
    Posts:
    2
    Would you mind sharing what kind of error you encountered? I seemingly have the same problem, inside the editor on play, the loader works fantastic, but the apk does not display the objects.

    Thank you!
     
  44. aaro4130

    aaro4130

    Joined:
    Jul 15, 2010
    Posts:
    430
    See the readme, it explains why this happens and how to fix it.
     
  45. thvas

    thvas

    Joined:
    Mar 13, 2020
    Posts:
    2
    Hello again,

    if you are referring to the Specular Setup I have already included it. Do you know what else it could be?
    Thank you!
     
  46. jex_gmail

    jex_gmail

    Joined:
    Apr 6, 2019
    Posts:
    2
    hello @aaro4130

    thank you for the great asset you provided us :)
    i need to be able not to read a mtl file but instead to add a specific material i have in the ressources folder
    i tryed multiple things but i'm not able to find a working solution, the best i have return a

    MissingComponentException: There is no 'Renderer' attached to the "Cucube" game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object "Cucube". Or your script needs to check if the component is attached before using it.
    UnityEngine.Renderer.set_material (UnityEngine.Material value) (at <3dc54541a2574ac7826a004a212a4332>:0)


    my line of code is

    Code (CSharp):
    1. obj.GetComponent<Renderer>().material = M_generic;

    that i have added into your code here

    Code (CSharp):
    1. //finally, put it all together
    2.             GameObject obj = new GameObject(_objInfo != null ? Path.GetFileNameWithoutExtension(_objInfo.Name) : "WavefrontObject");
    3.             obj.transform.localScale = new Vector3(-1f, 1f, 1f);
    4.  
    5.             foreach (var builder in builderDict)
    6.             {
    7.                 //empty object
    8.                 if (builder.Value.PushedFaceCount == 0)
    9.                     continue;
    10.  
    11.                 var builtObj = builder.Value.Build();
    12.                 builtObj.transform.SetParent(obj.transform, false);
    13.             }
    14.             MeshCollider mc = obj.AddComponent(typeof(MeshCollider)) as MeshCollider;
    15.  
    16.             // Assigns a material named "Assets/Resources/DEV_Orange" to the object.
    17.             // Material newMat = Resources.Load("M_generic", typeof(Material)) as Material;
    18.             // obj.GetComponent<Renderer>().material = newMat;
    19.             obj.GetComponent<Renderer>().material = M_generic;
    20.  
    21.  
    22.             return obj;
    23.         }
    i'm quite a newby in code but damned i have manage to add a mesh colider at import i m lost here :(
     
  47. jex_gmail

    jex_gmail

    Joined:
    Apr 6, 2019
    Posts:
    2
    ok i finally found a way for those who need here is what's changedd

    Code (CSharp):
    1.  //finally, put it all together
    2.             GameObject obj = new GameObject(_objInfo != null ? Path.GetFileNameWithoutExtension(_objInfo.Name) : "WavefrontObject");
    3.             obj.transform.localScale = new Vector3(-1f, 1f, 1f);
    4.  
    5.             foreach (var builder in builderDict)
    6.             {
    7.                 //empty object
    8.                 if (builder.Value.PushedFaceCount == 0)
    9.                 continue;
    10.  
    11.  
    12.                 var builtObj = builder.Value.Build();
    13.                 MeshCollider mc = builtObj.AddComponent(typeof(MeshCollider)) as MeshCollider;
    14.                 Material newMat = Resources.Load("M_generic", typeof(Material)) as Material;
    15.                 builtObj.GetComponent<Renderer>().material = newMat;
    16.  
    17.                 builtObj.transform.SetParent(obj.transform, false);
    18.  
    19.             }
    20.  
    21.             return obj;
    22.         }
     
unityunity