Tuesday, August 7, 2018

Is it possible to export a simulation (animation) from Blender to Unity?


I created an explosion animation within Blender, something as shown in this video. To achieve this I used a particle-system, together with the "Explode" modifier.


This kind of animation (simulation) doesn't get exported to other file-formats though and I'd like to export the animation as FBX, so that I can use it in Unity. So I basically need some way to "bake" the animation to keyframes. I wonder if there's a way to achieve this?


After some testing I found out, that the only way the simulation gets exported is by writing a sequence of obj files. So an answer on how to integrate/animate such a sequence of obj files in Unity would also be welcome.


Side note: I also tried to get an answer to this over at the blender community.



Answer



After countless tests (and some coding) I was able to find two valid approaches to the problem. Both aren't optimal solutions, but apparently there is none (exporting a simulation from Blender to Unity simply doesn't work).


Approach 1: Export as obj sequence and swap meshes in Unity


Pretty straight-forward. Exporting a simulation using the obj exporter generates a separate file for every frame of the animation (if the "Animation" option has been toggled in the export options). Drag all these files into your Unity project (eg. into Assets/Models/MyAnimation). Then write a simple script to swap the mesh of the GameObject at runtime.


Pros:




  • Animation is the same as in Blender


Cons:



  • I haven't tested this with hi-poly meshes or multiple concurrently playing animations, but most likely it's going to have an impact on performance.

  • It's really cumbersome to drag each mesh file separately onto the script component. Not really feasible for an animation with lots of frames. See image below (each Element in the Meshes array is a file with the mesh for that frame):
    Ouch, mesh animation in Unity


Approach 2: Create the explosion procedurally within Unity



This approach is a mixture of the approach proposed by Jonathan Dickinson and the way the explode-modifier works in Blender. In Blender, the explode-modifier uses a particle-system to determine the movement of shards/shrapnels caused by the explosion.


I wrote a script that splits the mesh into triangles and uses a ParticleSystem to animate the triangles.


Pros:



  • Works with any mesh.

  • Behavior/animation can easily be tweaked by modifying the particle-system. For best results, the simulation has to be in world-space and the particle-renderer should be turned off (see screenshot below). Particle settings in Unity


Cons:



  • Explodes into plain old triangles, which doesn't look very natural.


  • With hi-poly meshes it hits the CPU hard when the mesh is being split into triangles. This could be optimized though.


Comparison


Here's a video that shows both types of animation in effect. The used scripts (both written in C#) can be found here:



Update: MetaMorph


Thanks to the Blender Community (especially the users chipmasque and enzyme69) I was pointed towards a third possible solution, which is MetaMorph. It's a Unity script and a Blender export addon which allows you to export shape-keys from Blender and play them in Unity. While there seems to be some issues with getting a shape-key animation from an explosion, it should work fine for other types of simulations and other shape-key driven animations (eg. facial animations/morphs etc.).


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...