Search Unity

import 3d objects from server by scripting

Discussion in 'Scripting' started by Lexy, Apr 17, 2008.

  1. Lexy

    Lexy

    Joined:
    Feb 18, 2008
    Posts:
    19
    I'm not sure if there is a way to do this, but I want to import a 3d object (let's say a fbx file) from a server into unity by scripting. In my game the user can upload a 3d object to a server and then this object should be shown in the webplayer. With pictures and videos this is no problem. Therefor I can use the www class for downloading data and then add www.movie or www.LoadImageIntoTexture to the texture of a new game object.

    But how can I import a 3d object into unity after building the webplayer game? Normally I import objects by putting them to the asset folder, unity imports them and then I can drag the objects into the scene. I think if I build the game, this content in the asset folder go into the game.unity3d (and then this is used from game.hmtl). So it wouldn't make sence to upload the objects to the asset folder afterwards, right? Maybe there is a way to rebuild the mesh in unity with the binary data from www?

    Any ideas would be useful...
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes; you "just" have to parse the data yourself and build the mesh from that. Note that this isn't exactly trivial. ;) This is a basic demo of an external .obj importer I did a while ago. If you change the URLs for the .obj file and texture, it should load those in, assuming the texture is a .jpg or .png, and assuming the .obj parser can deal with the .obj file (it works on everything I've tried so far, but .obj can occasionally be a bit vague). If there's more than one object in the file, it will only display the first one. You can also try cube.obj at the same address if you don't have another .obj URL handy, to view an incredibly exciting cube. :roll:

    --Eric
     
  3. Lexy

    Lexy

    Joined:
    Feb 18, 2008
    Posts:
    19
    Allright... so I know what to do now. Thank you very much for the new input. :)
    I tested your object file loader with 5 different .obj files I got from a free 3d model space in web. 2 objectc couldn't be loaded cause there polygons weren't quads or triangles, 1 object looks like a very small cloud of points (should be an usb flash driver) and 2 other objects look quite right but are huge so that I couldn't really see. With your 2 testobjects everything was fine. Of course I haven't seen the objects in a 3d application, so I can't tell if they were well done, but it seems to me, as if there are a lot of thinks to take notice of. And I need a very general working object loader.

    So, is there really no easier way to do this? :roll:
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Any chance you could give me a link to the objects that don't work? Also I added zooming with the scroll wheel so you can hopefully see the other objects. :)

    Not at this time. UT said they want to add this option to Unity at some point, so that would make things a lot easier if they do this. A possible option in the meantime is to use the mesh serializer script from the wiki so save out objects in a standard format, instead of trying to parse .obj or .fbx files. However, you still have to load in objects into Unity once and then save them out again for that to work, so users can't just supply arbitrary 3D files themselves. But if you want to have a library of objects that you supply yourself, that would work.

    --Eric
     
  5. tbjod

    tbjod

    Joined:
    Feb 25, 2008
    Posts:
    19
    Diffrent .obj exporters has its own definition of what is standard when it comes to the formatting of lets say vertex normals.

    model 1
    f v0/vt0/vn0 v1/vt1/vn1 ...
    could look likt this in model 2
    f v0 vt0 vn0
    f v1 vt1 vn0


    Therefore it can be a real pain to write a parser that can handle "all obj formats".
    I had a link lying around somewhere describing most of the diffrent formatting diffrent exporters use. Can't find it atm...

    Cheers,
    Tobias
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Small update to the .obj reader....obj files have the ability to have an arbitrary number of vertices per polygon, instead of just triangles or quads, which I dealt with before by throwing an error, although I didn't manage to find any such objects myself. However, thanks to Lexy I have one--Blender loads such files in, and apparently deals with these kind of polygons by ignoring them. If it's good enough for Blender, it's good enough for me. ;) You can see this by loading "table.obj". It's freakin' huge (a 1km table!), and I don't do any scaling, but it's also created off-origin, so you can sort of see it as it rotates around.

    --Eric
     
  7. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    I've exported an object with the ObjExporter to an .obj-file...

    So the only way to get the mesh, is to iterate through all of the values and build the mesh myself?

    Edit: I really don't get it...
    I've extracted the vertices, normals and uv's from the .obj-file.

    Now I want to set the vertices to the mesh:

    Code (csharp):
    1.  
    2. GetComponent(MeshFilter).mesh.vertices = vertices;
    3.  
    vertices is an Array with Vector3-values.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep. Not sure about the error message you're getting; have a look at the procedural scripting demos for some examples of building meshes.

    --Eric