Import 3D model to Xcode with its nodes - swift

I have 3D Models which have many parts and are designed by some 3D editors such as: AutoCad, Blender...
Those 3D Models are combined many parts, so I want to import them to Xcode with same parts, then add Node to each part.
I want to interact with each part in ScenceView.
3D model from:
https://drive.google.com/file/d/11MccUtXd61wDWQldMTleotvg_yoHnzWP/view?usp=sharing

As i can see on the link provided by you, the 3D model is available in FBX and OBJ formate. for using in Xcode you can try using following steps.
Convert your model into DAE or .SCN formate. (you can use Blender) .DAE and .SCN formate contains all the nodes in a single file.
import your Model with associate texture folder in the art.scnassets folder.
Load and use your model.

Related

How to save modified mesh at runtime?

In my game, I modify mesh at runtime using a damage algorithm.
After that, I would like to save them.
I get all meshes with:
MeshFilter[] meshfilters = MyObject.GetComponentsInChildren<MeshFilter>();
then i modify single mesh.
How to save that in my original FBX file ?
Thanks
Unity's internal mesh type is not the same as an FBX, FBX is a file storage format, while Unity uses a different internal structure at runtime, for greater efficiency.
As far as I know there is no standard way to save out a runtime mesh as an FBX (this is a feature of 3D modelling software, not a game engine)
You can save your deformations somehow and re-apply them when you reload the model, when I did a similar thing (custom generated terrain meshes at runtime) I would generate a mesh at runtime, then save the data required to generate that mesh, then pass it into the same flow I used to generate the mesh in the first place.
It is possible to serialise meshdata in the unity editor as meshdata is actually a different format from FBX (Unity reads an FBX, then generates a mesh asset from the FBX's data, then associates that mesh with the FBX) You do this by playing with AssetDatabase.CreateAsset() https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html
But I don't think this is what you're intending to do.
If you want the full FBX suite of features (skinning, animation, use in other unity projects) and you want them at runtime, you don't really have any options that I know of. Short of implementing your own FBX serialisation library (not really a reasonable solution)

How to import complex .dae model to SceneKit?

I know how to export a single model (like a car) from Blender as a .dae file, and then import it and show it using SceneKit. And also with an animation for that model.
But I'm wondering what the best way to import more complex model's. Like a small part of a city. Like a scene with multiple cars and, buildings and people, with different animations.
Is there a way to do this without exporting everything as one model with one animation, and then through code in SceneKit comebine and place everything? So that as much as possible is defined in Blender/other 3D tool.
It's obvious that you must export complex models from 3D packages divided into smaller parts. And, of course, you do not necessarily need to export all your 3D models separately (per one model basis). In any case, a preparation of all your 3D models for using in Game Engine is extremely time consuming process. There's no one button solution.
Complex scenes like city could be logically divided to groups of static objects: skyscrapers, posts, asphalt, houses, benches, etc. But animated objects, like people, trees or cars, must be exported from Blender and imported into SceneKit separately.
Remember, all corresponding textures for these 3D objects (whether it's a single object or a group of objects) must be saved as UV-mapped square jpeg or png files (like 512x512 or 1024x1024 pix). And do not forget about low-poly collision meshes for dynamics.
Look at WWDC 2015 SceneKit session. You'll see how to build 3D scene in Xcode's Scene Editor.
To accomplish your goal you need to export smaller parts (logically divided, as I mentioned earlier) of your 3D scene from Blender, import all the parts into SceneKit's (ARKit) project and then combine them all through Swift code. Also, many 3D packages can export multiple animations as a single animation with, so called, sub-animations. In this SO post you can find how to handle it.
Actually, there is a way to do this.
If you watch the WWDC video about Model I/O, the guy demonstrates how to iterate through a .USD file to easily capture the nodes, geometries, associated hierarchies, materials, animations, etc.
Unfortunately, he didn’t do this for a .dae file.
The process goes like this:
Create an array of the nodes in the scene file.
Create an array that describes the parent nodes of each node.
Create an array that describes the instances of those nodes.
Create an array that describes the materials for the nodes...
Create an array that describes the animations... do this by creating an array of the bones, their attached vertices, transforms, etc.
After all that, you have to code a function that reassembles the scene that has been described as an array of arrays.
I’m not skilled enough to do this... and I hope somebody creates an example so I can study it.
But that’s the logic.

How to create a DAE file to use in SceneKit?

How do you create a .dae file from a 3D model? I've created a 3D model from a drone areal mapping and now have a very large file I can import in to Photoshop, but I can't figure out how to create a .dae file I can use in SceneKit.
The default game example for Xcode has a SceneKit that shows a rotating aircraft, and the asset is a .dae file, but I don't see any documentation on how to create one of those from a 3D model, and how to correctly apply a texture to it.
To create a 3D model and export it as a Collada .dae file you can use any of the following 3D authoring tools: Autodesk Maya, Blender, Autodesk 3dsMax, The Foundry Modo, Maxon Cinema 4D, SideFX Houdini, etc. The simplest way is to use a non-commercial student version of Autodesk Maya 2022. It's free. You can download it from HERE.
There are countless examples in YouTube how to model and uv-map in Maya software. Look at this example of UV-mapping in Maya. So, when your 3D model (and its UV-texture) is ready for use, you can export it as one of the four formats supported by SceneKit:
animated Collada DAE
animated Pixar USDZ (for iOS 12 and higher)
animated Autodesk FBX
single-frame Sony Alembic
single-frame Wavefront OBJ
In Maya Export Type for your 3D geometry must be DAE_FBX export:
A texture for you model (square UV-mapping 1K or 2K) you can export as JPEG or PNG file. It may look like this:
This UV-square-texture you must assign to a Diffuse slot of properties in Lighting Model (shader) in Show the Material Inspector.
And here's some Swift code if you wanna make it programmatically:
let scene = SCNScene(named: "art.scnassets/mushroom.scn")!
let mushroom = scene.rootNode.childNode(withName: "mushroom",
recursively: true)!
let mushroomMaterial = SCNMaterial()
mushroomMaterial.diffuse.contents = UIImage(named: "mushroom.png")
P.S. Working with Pixar's USDZ file format:
If you need a .usdz for your 3D scene, you can convert .usda using the following command in Terminal:
usdzconvert file.usda
Here you can read about usdzconvert command.

To convert 3d model from obj,3ds to .ac file format

I am having a 3d model of a car in .3ds format.I want to convert .3ds/.obj files into .ac format. Is there any tool to do this?
Here's one solution.
Blender (http://www.blender.org/) can open .3DS and .OBJ formats.
File Formats
Blender comes packed with import/export support for many different programs.
3D
3D Studio (3DS), COLLADA (DAE), Filmbox (FBX), Autodesk (DXF),
Wavefront (OBJ), DirectX (x), Lightwave (LWO), Motion Capture (BVH),
SVG, Stanford PLY, STL, VRML, VRML97, X3D.
And this AC3D Exporter plugin for Blender will allow you to export to .AC.
Export selected meshes to AC3D's .ac file format.

XNA 4: import FBX problem

I have a problem with importing 3D model from FBX file.
Source model contains 575 objects + 1 camera and looks like this: http://habreffect.ru/files/23d/542fa7f67/source_model.png
In XNA prepared with content pipeline model contains 82 meshes, and 576 bones.
So, when I draw my model, I see only part of source model. Result picture like following:
http://habreffect.ru/files/28a/6e61c0215/Result_view.png
My drawing code:
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[_model.Bones.Count];
_model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (var mesh in _model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.LightingEnabled = true;
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * _world;
effect.View = _view;
effect.Projection = _proj;
}
mesh.Draw();
}
base.Draw(gameTime);
How can I get all 575 meshes in my Model instance in XNA?
Thanks!
UPD: I tried to import my FBX model to 3ds max, select all meshes and use "Export selected". Result FBX file is 11x bigger. Then I add it as content to XNA project, loaded model contains all 575 meshes, and it rendered correctly.
Unfortunately, this manual converting method don't suit me - I need to render varied fbx models from mutable repository.
So, what FBX file are "good" for XNA content processor (I use XNA 4)?
So, on MSDN I found that FbxImporter designed to work with 2006.11 version of FBX format.
Recently Autodesk released FBX Converter 2012.1, which contains other tools, like FBX Eplorer, FBX Viewer.
FBX explorer can show structure of FBX file, and I compare exported from 3D MAX file, and source FBX file. They have different internal format. I tried to make following conversion: FBX -> Collada -> FBX, and the result FBX file contains similar to exported from MAX data structure.
So, I simply add the result FBX to Content of my XNA app, and it's rendered well :)
Another way to make it work is to use Autodesk FBX SDK to manually read model, and draw it in XNA.
Conclusion:
XNA FbxImporter correct work doesn't depend on version (2006, 2011, etc) and form (binary, ascii) of FBX file. Internal FBX data structure much more important.
To make FBX "readable" for XNA Importer you can use double conversion like FBX -> Collada -> FBX
You also can use FBX SDK to manually load data from FBX
Open the 3d model in latest 3dx max 2012 and export it in .fbx format. This .fbx is loaded properly with the xna model loader. While exporting you can even embed the resources, so you dont have to add textures to it through XNA.
You are using instancing in 3DS MAX. This isn't supported directly by XNA.
You have to draw the mesh for each instance bone yourself.
Ideally you would instead use DirectX instancing to draw each mesh once per bone in a single Draw() call.
But you have to roll your own code to do that, by converting the bones into instance vertices.
By default XNA only supports the most basic operations.