Search Unity

Imported .fbx animated 3D file: Updating the MeshCollider?

Discussion in 'Physics' started by surfcode, Dec 10, 2018.

  1. surfcode

    surfcode

    Joined:
    Oct 10, 2018
    Posts:
    15
    Hi all, long time reader, first time poster ...

    I'm trying to update the MeshCollider of a simple animated cube where a single edge moves towards the middle of the cube.

    Unity version: 2017.4.15f1.



    i have imported a .fbx animated file from Blender, trying to animate the Mesh Collider by updating the shape in response to the changing shape of the object. The object (simple cube) was animated via shape keys in Blender.

    i tried writing this script and applying the script to the cube object in the hierarchy window.
    the code is not working, my coding might be incorrect, or perhaps the format of animation exported via .fbx from Blender might be causing issues somehow?

    public class ObjectMeshToPhysicsSurface : MonoBehaviour {

    Mesh m;
    MeshCollider mc;
    MeshFilter mf;

    // Use this for initialization
    void Start () {

    Debug.Log ("Object mesh script is active.");

    mf = GetComponent<MeshFilter> ();

    m = mf.mesh;
    mc = GetComponent<MeshCollider> ();

    m.RecalculateBounds ();

    }

    // Update is called once per frame
    void Update () {

    m.RecalculateBounds ();
    mc.sharedMesh = null;
    mc.sharedMesh = m;


    }
    }
     
  2. JuozasK

    JuozasK

    Unity Technologies

    Joined:
    Mar 23, 2017
    Posts:
    84
  3. surfcode

    surfcode

    Joined:
    Oct 10, 2018
    Posts:
    15
    cheers. as it happens the issue is that the exported .fbx file from Blender has reference to shape key animation and so it requires a different coding syntax and approach.

    tried exporting using the Alembic file format .abc - with a similar style of animation happening in the Blender project, using shape keys ... and managed to update the mesh collider with this code:




    // this script is functional for updating the MeshCollider shared mesh

    // of an imported animated 3D object Alembic file

    // apply to the actual object in the flyout arrow list of the bundle on the Hierarchy panel

    // also apply a MeshCollider and a RigidBody set to Convex.

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class accessDynamicSharedMesh : MonoBehaviour {


    // public Mesh m;- not required.
    
 public MeshFilter mf;

    public MeshCollider mc;




    void Start () {



    mf = GetComponent<MeshFilter> ();


    mc = GetComponent<MeshCollider> ();
 

 


    }



    void Update () {



    mf.sharedMesh.RecalculateBounds ();


    mc.sharedMesh = mf.sharedMesh;


    mc.sharedMesh.RecalculateBounds ();
 


    }


    }



    that was cool and a cause for a moderate amount of celebration, although i did find an issue when doing a scene build to an osx player, the animation did not play (it did however play when pressing play in the project window).

    i'm also interested in MegaCache ability to import a sequence of .obj files and set the mesh collider from each .obj mesh data. the publisher does mention this should be approached with caution regarding vertex count of the obj mesh if the mesh collider is being updated.

    it's quite possible that the best idea is to approximate the mesh with simple shapes for the physics surface, like most advice generally indicates when it comes to updating the position or shape of a mesh collider.
     
  4. JuozasK

    JuozasK

    Unity Technologies

    Joined:
    Mar 23, 2017
    Posts:
    84
    Could you check the output of the OSX player logs? Maybe there's an error message that would let us know of some sort of platform support issues.

    https://docs.unity3d.com/Manual/LogFiles.html

    "~/Library/Logs/Unity/Player.log"
     
  5. barge9

    barge9

    Joined:
    May 13, 2015
    Posts:
    12
    Responding to this thread with an Enthusiastic Upvote! After pouring through the forums for days, THIS simple script worked for me. Thank you very very much.

    My Scenario should anyone else read this and have the same or similar challenge.

    1) Created an Array Animation in Blender using a cube, and repeating it with the array modifier. I did not click "Apply" to the modifier. Instead, I animated the object offset using keyframes in the Blender Timeline.

    2) Exported it as an Alembic file.

    3) Imported the file into Unity. (be sure to install alembic package from the package manager window.).

    4) Placed the cube array alembic file into the scene.

    5) Created an empty GameObject, renamed it to Master Timeline.

    6) With it still selected in the Hierarchy Window, Clicked the create button in the Timeline window, to create its director component.

    7) Dragged the Alembic (Cube Array) GameObject into the timeline, and selected: "Add Clip with Alembic Track".

    8) With the Cube Array GameObject selected in the Hierarchy, set it's Playable Director Component's Wrap Mode to "Loop" in the Inspector (to loop the animation.)

    9) With the Alembic Track itself selected in the Timeline Window, Set the "Speed Multiplier" to .01 in the Inspector (to slow down the animation)

    10) Created a new C# Script, and renamed it to: "Alembic Collide".

    11) Opened it in Visual Studio, and adapted the Example Script provided by SURFCODE above. (I'll re-include my adaptation again at the bottom of this thread... it's basically the same, except with the new class name)

    12) Saved it, and then dragged the script onto the GameObject's Child (expanded the triangles, and placed the script onto the child object named CubeShape.)

    13) The CubeShape GameObject's components are: a) Dyn: Cube Shape (Mesh Filter) -- (must've been automatically included w/ alembic import. b) Mesh Renderer. c) Mesh Collider (with Convex checkbox selected), and d) new C# script "Alembic Collide".

    14) In Play Mode, Animation was sure glitchy. I realized that the cube array was partially inside the terrain. I raised it up to ensure it wasn't touching anything else.

    15) Tested in Play Mode and BINGO! Player was able to collide off of the cubes! Fantastic!

    16) Tested in Mac OS Build Standalone, and it works there too! (I read surfcode's original post and it said that there was an issue w/ the build. Worked OK for me.)

    Thanks a Million, Blessings upon ye! Here's a Unicorn.
    Unicorn.png

    (Adapted Code below:)
    ----------------------------------------------

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class alembicCollide : MonoBehaviour
    {
    public MeshFilter mf;
    public MeshCollider mc;

    void Start()
    {
    mf = GetComponent<MeshFilter>();
    mc = GetComponent<MeshCollider>();
    }

    void Update()
    {
    mf.sharedMesh.RecalculateBounds();
    mc.sharedMesh = mf.sharedMesh;
    mc.sharedMesh.RecalculateBounds();
    }
    }