Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to import fully 3D objects (Final post)

IgnatzIgnatz Mod
edited January 2014 in Code Sharing Posts: 5,396

The OBJ format for 3D objects is in plain text, with all the vertices listed. With a bit of manipulation, you can get them into Codea, as shown above.

«13

Comments

  • Posts: 2,820

    =D> (As usual)

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    Here's another, of a Cessna aircraft. It is nearly 28,000 lines of data (made up of 3,750 points and 24,000 vertices using those points). Codea loads and runs the model quite quickly, but editing code is a real strain and very slow.

    The code for both this and the shuttle are on Codea Community.

    Clearly, it is going to be very difficult to import and use anything complex.

    (As an aside, why go to all the trouble of building this model without wheels (so it is flying) and then show propellors that aren't going round?)

  • Posts: 2,820

    Also, the reason this interests me so much is because I've been looking for a use for an app I purchased a while back called Verto Studio 3DM that makes 3d OBJ files. So now I can make models on my iPad and put them in Codea!
    Thanks!

  • IgnatzIgnatz Mod
    Posts: 5,396

    Well, there is some work to do. I used Excel to convert the obj. files to Codea format, because there is so much data and Codea doesn't read and write files easily. And Excel VBA lets you step through line by line.

    I'm happy to share my program, but it seems every time I find a new obj. file, it has some quirks that require changes to the VBA program!

  • Jmv38Jmv38 Mod
    Posts: 3,295

    I see you are using very nice shading in your videos. Very important to get the 3D feeling.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Jmv38 - yes, lighting makes a big difference!

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Boy are you barking up the same tree as me. I have been trying to get into 3D for months - making painfully slow progress. I am currently trying to learn Blender but it's a mamoth mountain to climb (probably actually an age thing!!).

    You can export to a number of different formats and, if you can program in Python, can also build your own export add-on. Export formats include 3ds, JSON , obj etc. I'm sure you've been here.

    I'm trying to make a number of models for a personal project and hope to generate something soon. Most of the models I use and hand coded and messy. I've only just started exporting models in JSON format but need to be able to read them into Codea - most of the links to JSON reading in the forum I find a little bit vague.

    I was going to look into reading an obj file, but it looks like you have done it. With Excel - do you read in and wrtite out in CSV format?

    I'll be in touch later.

    Thanks,

    Bri_G

    =D>

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Just downloaded your model display software and the 4 models - very impressive. Are you moving on to simpler models and UV mapping for them?

    So far I've managed to build simple models with single colour faces and progressed as far as the shading by giving each vertex (in each triangular face) a different colour.

    That's why I'm trying to get into Blender - first target - a UV mapped cube.

    Thanks again,

    Bri_G

    =D> =D> =D> =D>

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Bri_G - I've been using Blender to convert from 3ds to obj, so I can read the vertices.

    My Excel code takes an obj file and converts it to the layout you'll see in the Data tab, ie a list of point vec3's, followed by a list of vertices. Some "faces" in the obj file have more than 3 points, and in this case, I calculate the midpoint and make triangles for every pair of adjacent faces. My VBA dumps the list of data in a worksheet, and then I have to fiddle about manually, transferring it to Codea.

    At the moment, I'm ignoring normals (where provided in the obj file) and textures. I calculate normals based on the average at each vertex, which seems to work pretty well. I'd like to extend to using normals and vertex mappings as well, but the immediate problem is - as you will notice on loading any of these projects - the amount of data puts the Codea editor under massive strain.

    So right now I'm looking at encoding the point and vertex data in an image instead of in code. If I can do that, and if it isn't too slow to unwrap, I can then extend to normals and texture mappings.

    I haven't looked at UV mappings, no. First things first. I've only been doing this for a day!

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Bri_G - I have managed to save all my lunar lander data to a 70kb image, which makes it much more manageable, as no data needs to be stored in the code. In the next day or two I'll test this approach on the other models, and then look at vertices and normals...

  • Posts: 1,365

    Hi @Ignatz,

    Curious and curiouser, I downloaded and ran your files - great. Then tried to transfer them to my PC to digest. Main and Shader - no probs. Data crashes Codea. Must be on the data limit. Yet downloading from cc worked fine. Any ideas - how did you edit and transfer data?

    Thanks
    Bri_G

    =;

  • I tried downloading the Files from CC but it doesnt download or run any of the projects, nit even my backups! Any Help?

  • IgnatzIgnatz Mod
    Posts: 5,396

    I copied and pasted the data tab from a text file I put in my normal Dropbox folder. But now I'm going to see if I can store the data in an image outside of Codea, so it can handle larger models.

    In other words, a helper program will turn an obj file into an image that Codea can read without having to store any data in the code itself.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Is there a problem with txt files handling? I've been using them a lot for my work on backups, and i have no pb with that.

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Moving to JSON file loaded from Documents or Dropbox or are you going to load a 'png' file from the codea spritepack in Dropbox?

    I favour the JSON file approach as it can be expanded in sections - the vertices, faces, colours and later the UV map just by changing the JSON file structure.

    I'd try with a simple model first.

    Interesting project.

    Thanks,

    Bri_G

    =;

  • Interesting discussion guys. I've written a similar tool in Python for some 3d games im writing, that takes .obj s that I've built, uv'd and textured in Maya, and converts them to a bunch of vec3's for vertices, uv's and normals that you can cut and paste via Dropbox into Codea..

    As @Ignatz (and others in previous threads) has pointed out - vec3 lists are way too verbose for anything other than very simple models. Representing them as images is obviously the way to go or until Codea can easily support importing 3d assets. Hopefully the new asset browser (in the next version) could provide a route for doing this in future.

    Happy to share my Python code if anyone's interested. :-)

  • dave1707dave1707 Mod
    Posts: 7,865

    @CodeaNoob Are you on an iPad 1. I am and "cc" is totally useless on it. When I try to display the code, most of the code doesn't show. If there are multiple tabs, switching tabs does nothing most of the time. It just keeps showing what was in the first tab. It used to work and I could do everything, but something changed and now it doesn't. I don't even try to use it anymore.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @andymac3d - yes please to the code

    @dave1707 - I'll post code soon on a gist, just refining it

    @Bri_G - I haven't thought about a JSON file because I haven't used that file format yet, nor tried to save/load them in Codea. Do you have any examples?

  • edited December 2013 Posts: 835

    @dave1707, I'm on an iPad Mini, and CC has worked two days ago, just not now

  • IgnatzIgnatz Mod
    Posts: 5,396

    See here for a demo of how object data can be downloaded and viewed. It gets stored in Documents so it downloads only once.

    https://gist.github.com/dermotbalson/7820304

  • Posts: 77

    @Ignatz. I have today downloaded your 3D Object viewer on CC. Very impressive. Thank you very much for the code.

  • IgnatzIgnatz Mod
    Posts: 5,396

    The github version above has several models to choose from(and I will add more), try that

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    @Bri_G - I've settled on creating a textfile containing a list of point positions and vertices, that can be downloaded into Codea and saved to the documents folder. It can be read in from there, and loadstring used to activate the lists. This also means we can include anything we like in this textfile, eg texture mappings, normals and any code we want to run, giving us a lot of versatility.

    The one thing that puzzles me is texture mappings, in particular how to store the images. Some models have a lot of little images, and I don't want those cluttering the Documents folder of anyone running the models.

  • Posts: 1,365

    Hi @Ignatz,

    Just downloaded your latest 3D viewer, using gist file/document file - superb, that's the way to go. Like the new models. I assume that the models are vertices only as the colour data has been lost, or should I say fixed with shader lighting to give the perspective.

    This certainly gives me a way forward - thanks for the code. You seem to have an excellent grasp of coding (and loadsa time to use it!!).

    Thanks again.

    Bri_G

    =D>

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    @Bri_G - being recently retired helps :D

    Yes, all the models are vertices only, with some diffuse lighting. I was amused to find my algorithm for calculating normals works well most of the time, except on the teapot image, where it reverses them, so the inside is lit!! I also have one or two images with strange artifacts, but I'll deal with those later.

    Yes, I think this approach is extremely flexible and allows us to include all sorts of data and code for different models. Also, I'm hoping Codea can hold a lot more data in memory than it can in the code editor!

    I'm scouting for all sorts of different models at the moment, but I'll have to think about texturing next. I have a couple of ideas, but managing texture images is messy - perhaps the best approach is to approximate with straight colours where possible.

  • Posts: 502

    so how many verts can you handle? any chance of getting distance clipping and other tools for optimization, so we can use this for loading a 3d level map/scene?

  • edited December 2013 Posts: 1,365

    HI @Ignatz,

    Started playing with your code and tried to set up one of your models on my Gist depository, in a public Gist. But I keep getting an error from the loadstring(s)() saying that it gets a nil value. I think this may be because I am not putting in the correct address to the file. I am using :

    "https://gist.github.com/Bri-G/7825317"

    which points to my Gist file page. I have tried adding raw etc like:

    ".../raw/file.txt"

    but it still fails.

    I've never read from a Gist file directly before - is there a trick to this?

    Bri_G

    [-O<

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    Yes, there's a trick. When you've created your gist, look at the right of the header just above your text, you'll see a link symbol, then < > . Press on the < > and you will get the raw text. Copy that URL.

    Note that if you edit your gist and resave it, you need to do this again, because the raw text url will change too.

    Note - I tried Dropbox and Google drive, and their links returned web pages rather than the raw text. That's why I used gists.

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    @se24vad - I don't know what the limits are, but I'm guessing they're much greater than the editors limits.

    It's fairly easy to test. Just create a file with everything you want in it, including any code you want to run. Store it in a gist and get the link as described immediately above, and substitute for one of the links in my code. Then see if you can break Codea .

    And if you can show me what some of these tools do, I'm happy to help test it....

  • IgnatzIgnatz Mod
    Posts: 5,396

    As an aside, I notice that several of these models are not centred, ie 0,0,0 is not at the centre of mass, so rotation is uneven. I think I may have to calculate this and translate to it.....

  • Posts: 577

    @Ignatz a bit of topic but congratulations on being retired!!! <:-P

  • IgnatzIgnatz Mod
    Posts: 5,396

    I updated the project (on Github) with more models. The shuttle model has colouring based simply on splitting the model into 8 meshes, each with a different colour. This was based on the comments in the obj file.

    I've tried to add the capability to touch the model on screen and see which vertex number you touched. This may give the ability to split the model into additional meshes or selectively colour certain vertices. At the moment, I don't think it's printing the correct vertex that was touched, though.

  • Posts: 2,820

    Very nice.

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Just a quick note - am now building up my own library of small simple models. Will release them when I get to know how to colour and wire-frame them, hopefully soon.

    Just a simple note - wasted a lot of time when I fed in a bad model file (face and vertice data confused). Any changes seemed to have little effect, when I realised that the model loaded was the Document saved model with the corrupt file present.

    I'm going to modify the code but may take some time to get it right. I intend having the app load a model up from the document folder when run. Then all other models to be downloaded from the Dropbox source so that changes can be tested. Also to put in a parameter option for 'save' or 'no save' so that when you load a new model it can be saved to documents if set (automatically resets to no save after the new model has been saved. This should help development and keep the Document folder tidy.

    Hope to have this soon but my time is limited at the moment - home decoration!!

    Thanks,

    Bri_G

    :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Bri_G - hold your horses, I am writing a Codea project that stores and reads OBJ files (and the MTL files that come with them), and applies the correct colours to different parts of the model. At the moment I am working on also storing the texture images and applying texture coords as well.

    Below is an example with shading applied according to the Obj file, before texturing.

    If I can achieve this, then I can import and display any Obj file directly from Codea.

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Wow - I know you're retired, so am I; but I don't seem to be able to find the time to spend on these projects. Also when I do it takes me a while to digest resources and understand what's required to produce the package. You've obviously got the right background and abilities.

    I've started to use Blender but I recently found an excellent little package for model conversion. This should help anyone interested in this field. Look up:

    3DRACS

    Hope that helps you with the project, I've only just started playing with it but am finding it really useful.

    Thanks,

    Bri_G

    =;

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    @Bri_G - this took me all morning, but now I can read in an OBJ format image, including its texture mappings and images

    This is a complex model, with 20 texture images and 65000 lines of settings, and it only runs at about 25 FPS.

    There is a .obj and .mtl text files, which I'm storing as local data, and the texture images, which I'm storing in Documents. The important thing about both of these is that they can be listed or viewed in Codea, allowing manual editing. (If I stored the text files in Documents, then there would be no way to list them, and therefore no way to delete them unless you knew their names).

    I'm going to try with some other images now...

  • edited December 2013 Posts: 502

    *badass*!

  • Posts: 2,820

    @-)

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Amazing !!!!!

    The work that goes into these models is staggering - the detail and complexity. Got to congratulate you on understanding both the model itself and the ability to put this into Codea =D> =D> =D> .

    When you say local data, and editing it, I assume you mean placing it in a Tab? Wouldn't it be better loading from the remote file and editing on a PC/Mac. I find the editing with Codea on large files slow and messy.

    25 FPS sounds good for the amount of data handled - what iPad version and OS are you using? Is there any scope for streamlining your code I usually find my initial code a little bloated and inefficient and can usually find faster alternatives - but in this case it's probably just the sheer level of data.

    Apart from the CPU, buses and ram speeds in the newer pads - have there been any additional improvements in the graphic processors so that the pipelines are faster and more efficient. It would be interesting to compare FPS on different systems.

    Are you planning another Codea report on how to display models? Seems like you've done the groundwork.

    The different model formats generally appear to have all the same data in - have you looked at different file formats? Are you reading in the actual files or have you extracted and compacted the data from them. If so you may find the 3DRACS useful.

    Finally - morning! Where are you based it's 08:18 here in the UK, my mornings only just beginning.

    Thanks again,

    Bri_G

    =D>

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Bri_G - I'm in Perth Australia, 8 hours ahead of you.

    What I mean by local data is the command saveLocalData, which can be used to save strings to the iPad hard disk. This is ideal for the .obj and .mtl text files. There is also a command to list all stored strings, which is important because there is a tendency to get messy and lost track of all the stuff you've stored.

    I'm using an iPad 3 on iOS7. I'm sure I can find some performance improvements, but all I'm doing is replicating the instructions in the data files to create textured meshes, and some of them have tens of thousands of vertices.

    I'm storing the original .obj, .mtl and image files, and not compacting them. This provides much greater flexibility in importing new models (no need to pre-process them), but may cause some delay in loading each model, of course. Compacting can come later if need be.

    I haven't looked at all the model formats, just .obj, but that one is quite logical and (generally) not too complicated.

    I will do a post(s) on this, as soon as I feel I have mastered it. I'm still wrestling with little issues, like models that don't work, or some vertices that vibrate. I'll load a few more models and when I think it's reasonably stable, I'll share the code. (If I can get a couple of little quirks sorted out, I'll send it to you soon).

  • edited December 2013 Posts: 1,365

    Hi @Ignatz,

    Thanks for the update - so you're a working day ahead of me, explains your rapid progress.

    On the vibrating vertices - is this down to overlapping faces? I think your first model showed this in the engine housing - (looked a bit like a circular spacecraft) as the object rotated you could see a flickering. I think this may be due to the algorithm for drawing the faces in the correct depth order. Saw something about it on a WebGL course - I'll try to find it to see how you can resolve it.

    I've probably got a model junkyard on my pad's disk now - I'll have to tidy it up. I use iExplorer for that. Perhaps @Simeon could include a simple file command structure in the next big release of Codea - 'Son of Codea'. Ooops better not get into the naming game here - there's a thread on that now - my preference is 'Codea'.

    Thanks,

    Bri_G

    :))

  • IgnatzIgnatz Mod
    edited December 2013 Posts: 5,396

    @Bri_G - the flickering is most probably caused by overlapping (or too close) faces, wrong z order, or misreading the vertex points in some way. I got a good dose of that when I was learning to build 3D scenes! I'll look at it soon.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Bri_G - try the code below. It contains specs for 6 models, but none of the data. That is downloaded on demand (the first time) and then stored on the iPad.

    https://gist.github.com/dermotbalson/7871147

    Choose a model with the slider at the top and press Load, then wait for the files to download. The model should then show itself (you can zoom in and out).

    Don't try model #5, it doesn't work (yet).

    When you're done, there's a button at the bottom of the parameters to delete all the downloaded files for whatever model is selected at the top.

    wrt the flickering on #6 (shuttle), it might also be because some obj files define "faces" with more than 3 vertices (ie polygons). What I do is calculate the average of all the face positions, then create a triangle between each pair of points and this average. Possibly this is responsible for the flickering, but I don't know yet.

  • dave1707dave1707 Mod
    Posts: 7,865

    @Ignatz You mentioned above that if you store files in Documents, you have no way to list them, so you store information with saveLocalData. The bad part of that is, if you saveLocalData in a project and then delete that project, that local data is still there and there's no way to see it unless you create another project with that same name. You have to clearLocalData before deleting the project. So if you create test projects and save these large obj file as local data and then delete those test projects, you can have a lot of memory tied up without a way to clear it unless you remember the project names that created them. The same thing goes for project data.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @dave1707 - well, I'm open to suggestions!

  • Posts: 2,042

    Why not use global data? That way you can always go back and remove it, or use it in other projects

  • dave1707dave1707 Mod
    Posts: 7,865

    @Ignatz I'm not saying don't use local or project data, just remember to clear it if you're going to delete the project. Like @JakAttak, I always use global data for saving things. That way I don't have to remember to clear anything when I delete projects. Every now and then I'll run a programs that lists all of the global data and let me delete what I don't want anymore.

  • Posts: 1,365

    Hi @Ignatz,

    Ran your latest program - seemed to have it just about nailed. Interesting issue on global data, I never considered that - only used global data occasionally.

    @dave1707 - could you point us to the way we should be managing our data, what is the app you run to check global data? Is it within Codea or external.

    Thanks guys, learning a lot at the moment.

    @Ignatz the viewer is now almost perfect, gonna try it with some simple models and I'll feed back any findings.

    Thanks,

    Bri_G

    =;

  • dave1707dave1707 Mod
    Posts: 7,865

    @Bri_G There really isn't any special way to manage the data. I just wanted to point out that if you don't clear any local or project data before deleting the project, then that data will still be there. There isn't any way to remove it if you don't remember the original project name. As for the program for global data, that's all it is. It just lists the global data and allows you to delete what you don't want. Not clearing small amounts of local/project data won't matter that much, but the size of the data that @Ignatz is saving could take up a large amount of memory if it's not cleared and you delete the project thinking it's gone.

Sign In or Register to comment.