How to save modified mesh at runtime? - unity3d

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)

Related

FBX from maya importing to Unity with wrong joint rotation

I'm working on a project that needs to read the joint data from motion capture device to reconstruct the motion.
I already have a model in MAYA where all the joint are having Y axis up-pointed. However, when I import that model to unity, many koints' rotation changed. I've tried different importing method including exporting a FBX then load to Unity and ecpoting to unity directly. I've also changed the default rotation in MAYA from XYZ to ZXY. But the problem still exists.
The mesh will twist since due to the wrong rotation as shown in the picture below. I've googled a lot but still not find a solution.
error listed
Usually the joint direction doesn't matter, though I used to set "z" instead "y" in my character rig.
Please go to your fbx import settings, choose your Rig/Animation Type as Humanoid. Then, create a new avatar Definition from this model.
You should now be able to remap your wrong or missing joints. It's most likely that you have wrong order of character joints' mapping.

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.

Unity3d Imported trees wind zone not working

Does anyone know if I upload trees created in Blender or 3DS max? For example, why can I not use the wind and do not move the leaves?
Custom tree meshes can be used, they just need a bit more work.
It would be relatively easy to add a hinge constraint and apply the wind force directly on the tree mesh, but the tree would simply pivot around the hinge, and not bend as expected.
The built-in trees use geometry shaders to mutate the tree mesh using the wind forces. You would need to apply the same shaders to your tree to make the geometry bend, instead of just swinging.
The Unity Asset Store has a number of plugins available:
Advanced Terrain Shaders v2 (Free)
Custom Tree Importer extension which can simplify importing custom tree meshes (Paid)
Another workaround for a simple tree might be to use a character rig on the tree body, and use particles for the leaves / branches. This would be computationally expensive, and difficult to modify.

how to break apart a large obj model and large single texture into smaller models with seaparate textures

I have a large 3d model in .obj format with a large 20000*20000 texture. (it was generated by photogrammetry) I need to keep all the texture detail but want to use it with the Unity game engine. Unity only supports textures up to 8192*8192 so i think i need to break the model into smaller pieces and generate smaller textures(below 8192) from the original file.
How can i do this so i get separate textures for each piece? i.e. each model doesn't use the same large texture? I have access to 3DS Max
This is not the website to get those kind of answers. Autodesk has their own websites for asking these types of questions.

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.