Search Unity

ObjReader - load .obj files at runtime

Discussion in 'Assets and Asset Store' started by Eric5h5, May 7, 2013.

  1. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
    How can i download and display if .mtl file have & don't have..It's not depend on me. USer can upload their obj model into my server. So, how can i download if model have .mtl file or if don't have .mtl file?
     
  2. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to put the .mtl and any texture files where the .obj file expects them to be; see the included examples such as Spot.obj, which has a related .mtl file and a texture.

    --Eric
     
  4. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
    Is there anyway to check the missing .mtl file is missing etc? var objData = ObjReader.use.ConvertFileAsync (url, true, standardMaterial);
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It doesn't know if the .mtl file is missing unless the .obj file specifies that there should be one, and then it tries to download it and gets an error message.

    --Eric
     
  6. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    In my case for some reason the box collider does not have the same size of the object, I thought that by passing the collider created in the inspector I could have some more control on the size by visually manipulating it.
    If it is not possible to use the one from the inspector is there away to force the size of the box collider created in the script being the same size as the bounding box of the imported object?
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use the one in the inspector by adding a boxcollider component to the created object, and copying the properties over. You can always set the box collider properties to whatever size you want using whatever method you want.

    --Eric
     
  8. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Thanks , that worked fine!
    Thanks a lot for your help.
    Just wanted to say also that I could not use a local file instead that the one online. It says the file cannot be opened...
     
  9. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
    How to use the standard size for the downloading game object? It's varying from downloading and loading in the scene..
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    File permissions problem, perhaps?

    ObjReader just uses whatever size the object is in the file. If you want to change it, you can use the scaleFactor variable.

    --Eric
     
  11. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Ok Eric it worked! I had omitted the drive letter for some reasons.:rolleyes:

    Still having some problems with the mesh such as the diffuse issue, but I will tryt to compare your meshes with mine and solve the problem by myself.

    Lastly if is not too much I would like to know what would it be the best procedure for importing objects one at the time.
    I will have a 2 arrow buttons(forward and backward) which will cycle trough them. So, for instance, when it imports the second it destroys the first and so on... is this possible?
     
  12. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sure, just call ConvertFile when needed and use Destroy on any existing objects that have already been imported.

    Unity uses the question mark texture when the texture didn't download for whatever reason (no permission, doesn't exist, bad file, etc.). It would be more helpful if it failed with an error message, but there's not much I can do about that.

    --Eric
     
  14. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
    ObjReader taking the file size whatever upload. So, which is the best size to upload the file to let to the user when they are uploading their obj to my website. It's not depend on me. So, i need to tell user to upload proper size otherwise the model will be too small or big in the mobile screen.
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity uses 1 unit = 1 meter by default, so that's probably what you should advise. Otherwise it depends entirely on what you're doing with the models.

    --Eric
     
  16. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Sorry Eric but I still don't get how to get all the models I have inside a folder and display them one at the time (with a button which I already have as a gui texture and which I am using it with OnMouseDown for other things ).
    I would like to have a folder to fill with more objects when I need them without rebuilding the web application.

    also I get this error when loading my file from the web
    Rejected because no crossdomain.xml policy file was found
     
    Last edited: Nov 18, 2014
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    See the docs about crossdomain.xml. Getting all models in a folder can be done with Directory.GetFiles.

    --Eric
     
  18. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Hi Eric
    Directory.GetFiles is a bit to complicate for me, maybe I will try to understand it another time.

    Regarding the crossdomain.xml I have put the file into my root directory of my server and now I get

    Error reading crossdomain policy: Expected element
    UnityEngine.WWW:get_isDone()
     
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I can't really help with regard to that, sorry; probably it would be best to start a topic about it. All I know about it comes from the Unity docs.

    --Eric
     
  20. dskillsaw

    dskillsaw

    Joined:
    Apr 26, 2012
    Posts:
    13
    Hi Eric, Great plugin!

    I'm in a situation where I need to be able to import obj's beyond Unity's 65k vert limit. Do you have any suggestions on where I might start? I assume it will involve modifying your source code, and I saw in an earlier post that you'd considered adding support for that in the future, so thought you may already have plans for it. Thanks!
     
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The easiest thing is if the obj has groups where no single group exceeds 65K verts, in which case it will work as-is, as long as you keep the groups as separate GameObjects. Otherwise, it would involve splitting the mesh somehow, and creating separate objects for each mesh.

    --Eric
     
  22. dskillsaw

    dskillsaw

    Joined:
    Apr 26, 2012
    Posts:
    13
    Thanks Eric! In this case, other people are creating the 65k+ obj files, so it will have to be some cleverness on the import side of things. I'll report back if I come up with any clever solutions.
     
  23. karthees

    karthees

    Joined:
    Oct 15, 2013
    Posts:
    13
    I'm getting .mtl not found
    Error loading http://myserver.com/Unity/3DModel_New/t rex/t_rex.mtl: 404 Not Found but i have .MTL file in my server path see the image and is there anyway to load .obj file if don't have .mtl file..It's frustrating
     
  24. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Probably an uppercase vs lowercase problem. Make sure the names match exactly.

    --Eric
     
  25. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    I downloaded the models from some free website and used it..but all the models showing .mtl is not found. Is there any way to load .obj model with or without .mtl?
     
    Last edited: Dec 21, 2014
  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Try editing the .obj file so the .mtl file is correctly named.

    --Eric
     
  27. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    Can you please tell me the exact size of this object. Because it's perfectly working ans showing and we can use size in instruction page to user upload exact this size.

    http://www.starscenesoftware.com/objtest/Spot.obj

    And, i tested with some 3d model but the model is not showing proper, some parts appear here & there. Why it's happening?
     
  28. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can see the size by loading it into Unity or a 3D app. If parts of a model aren't displaying, it could be because the surface normals are pointing in the wrong direction.

    --Eric
     
  29. Eric-Pratt

    Eric-Pratt

    Joined:
    Apr 28, 2014
    Posts:
    30
    I'm trying to figure out how to change the shader applied to loaded objects from VertexLit to Self-Illumin. I've gathered that's done through standardMaterial from reading in the Hints but beyond that I'm lost. Is there an example I can cadge to illustrate this for me?
    Thanks,
    Eric
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The Example3 scene uses ConvertFile where it passes in standardMaterial, so replace the material in the inspector with a different material that uses a different shader.

    --Eric
     
  31. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    I don't have team of people to develop 3D model. So, downloaded the free model support with .mtl from http://tf3dm.com/3d-models/all/1/mtl . But it's not displaying proper and when i click the button to remove the model from scene only some part is removing and some parts are showing in the scene. Not sure why it's happening. Is there any idea about this? Really frustrating using of this library....
     
  32. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    Is there a way to load .obj model without .mtl file?
     
  33. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    See the docs for the ConvertFile function, specifically useMtl.

    --Eric
     
  34. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    Where is the docs for the ConvertFile function? Where can i find it?
     
  35. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The docs are in the ObjReader folder, called "ObjReader documentation".

    --Eric
     
  36. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    I have read the convertfile but without .MTL i'm getting .mtl not found and the texture also not applying to the my obj file it showing red color question mark but if i load the same obj from locally it's loading the textures. Why it's not loading from server and using .obj i run time?


    WWW www = new WWW (jsonURL);
    yield return www;

    String wwwtext = www.text;

    modal = GameObject.Find (target);

    if (!modal) { //fetch model from the URL


    loadingText = (GUIText)GameObject.Find ("LoadingText").GetComponent(typeof(GUIText));


    var objData = ObjReader.use.ConvertFileAsync (jsonURL , true);

    //var objData = ObjReader.use.ConvertFileAsync (jsonURL, true, standardMaterial);

    // Debug.Log("objData is "+ objData);

    //var objData = ObjReader.use.ConvertFileAsync (objFileName, true, standardMaterial);

    while (!objData.isDone) {
    loadingText.text = "Loading... " + (objData.progress*100).ToString("f0") + "%";
    yield return null;
    }

    loadingText.enabled = false;

    string modelName = objData.gameObjects[0].name;

    }

    modalClone = Instantiate( modal ) as GameObject; //clone the model


    mesh = modalClone.AddComponent<MeshCollider>();


    modalClone.transform.localScale += new Vector3(250,250,250);
     
    Last edited: Dec 26, 2014
  37. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ObjReader can only do what you tell it to do. If there's an MTL file but the required texture isn't there, you get the standard Unity "loading error" question mark texture. If you don't want to use MTL files, use false for the useMTL parameter. Note that if you don't use an MTL file, you can't have a texture unless you supply it yourself in Unity.

    --Eric
     
  38. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    I have the texure in the same path and i didn't get loading error in the debug. I used false but still i'm getting .mtl not found.

    var objData = ObjReader.use.ConvertFileAsync (jsonURL , false);
     
    Last edited: Dec 26, 2014
  39. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    Can you please tell me what are the conditions should i apply while creating 3D model because i'm not 3D designer and some one going to create. So, i need to specifically tell them to use condition for load model using your library.
     
  40. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you get a question mark texture that means Unity was not able to load the texture (wrong format, etc.). I would prefer if we could get an error instead, but that's not how Unity works so there's little I can do about it. But if you have the objects set up correctly with the correct file names, then it will work, as shown in the examples. Using false with ConvertFile means ObjReader doesn't look for the MTL file at all, so there can't be any MTL errors. I confirmed this by using ConvertFileAsync (objFileName, false) in the WWW example included with ObjReader.

    I don't understand your code about instantiating GameObjects, since that's what ObjReader already does. I'd recommend looking at the example code included for how to actually use it.

    --Eric
     
  41. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    ok..Can you please tell me the conditions to apply while creating the 3D model?
     
  42. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Export the file in .obj format, and include any relevant .mtl and referenced texture files (which must be in .png or .jpg format so they can be loaded by Unity). There are several sample .obj files included which you can use for reference.

    --Eric
     
  43. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's hard for me to tell much from that screenshot, but I would say the model isn't constructed properly. If it doesn't load into Unity correctly then it won't be right in ObjReader either. Constructing 3D models is not in the scope of this topic, though. If you need more help with that, I'd recommend starting a new topic.

    --Eric
     
  44. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    I have the textures inside path where the .mtl & .obj but the texture is not loading in runtime
     
  45. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Then there is something wrong with them (incorrect name or format).

    --Eric
     
  46. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    No..I have textures and file names are correct and it's loading fine in locally but it's not loading in runtime. Please see the image
     
    Last edited: Dec 26, 2014
  47. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    TGA files won't load. As mentioned, they must be jpg or png. If Unity can't load them with the WWW class, then ObjReader can't either.

    --Eric
     
  48. raj231

    raj231

    Joined:
    Nov 18, 2014
    Posts:
    46
    No...I have png and jpg file also inside folder...but now i delete TGA and used only jpg and the textures are not loading. but inside unity (local) the textures are loading.
     
  49. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If anything works locally but not online, then there is an issue with the files online, which unfortunately is not something I can help with.

    --Eric
     
  50. critic

    critic

    Joined:
    Jun 14, 2013
    Posts:
    8
    I'm having an odd issue, after importing some objects, positions of all imported objects (transform.position) are reported as (0,0,0). Objects are all sitting on the root node in unity, obviously there should be a position reported as visually the objects are not in the same place.