Search Unity

Mesh Animator - Highly efficient animated crowds, fully animated GPU instancing, and more!

Discussion in 'Assets and Asset Store' started by jschieck, Dec 7, 2014.

  1. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    335
    @jschieck
    hi,
    does it support on Android?
    and URP? [Universal renderpipeline]
     
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    Yes it works on all platforms and doesn't require special shaders or materials so it should work for all shaders that work in that pipeline.
     
  3. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    335
    nice :]
    i want to using this on Emerald Ai ...
    so, is it possible? Emerald Ai have your own animation controlling system! howto do that?
    and howto using this on network?? like as Mirror?
     
  4. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    You would have to write custom scripts to work with a 3rd party system.
     
  5. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    335
    ok ... but can you inform me howto integrated this with ai systems theoretically?
    for example if i want to using this with uzAi, needs to bake the character prefabs with predefined animations before using on Ai or first defining that prefabs on Ai system and after (when ai was generated their animator and controllers and etc) have to baking with Mesh animator?? its confusing :\
    or what??

    thanks.
     
    JBR-games likes this.
  6. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    583
    My advice would be to bake a limited set of animations with Meah Animator. 2-4. (Idle, walk, run, attack) use some sort of LOD system and only use them for characters that are say 50m away. So you will use a very basic state Machine(like the one included with MA) that is in use at the proper LOD levels. From there you can still use the NavMesh agent and base the animations off the speed of the agent, or the ai state like attacking.
     
  7. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    583
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class JBR_MeshAnimator_Controller : MonoBehaviour {
    4.     private MeshAnimator meshAnimator;  //mesh animator component
    5.     public NavMeshAgent navAgent;        // if using a navmesh agent add it here
    6.     private float navAgentCurSpeed;        // current speed of navmesh agent
    7.     private float topSpeed;                //nav agents speed
    8.     public float transitionSpeed = .75f; // speed at which to transition from  walk to run Animation
    9.     public string curAnimation;            //last animation started
    10.     public float updateSpeed = 0.2f;        //how often animation data is checked, at .2 update 5 times a second ,   in most cases there is no need to check this everyframe.
    11.     public string MA_Walk;  //add the correct name for the walk animation here, this animation should be set to loop
    12.     public string MA_Run;    //add the correct name for the run animation here, this animation should be set to loop
    13. void Awake()
    14. {
    15.      navAgent = this.gameObject.GetComponentInParent<NavMeshAgent> ();
    16.      meshAnimator = this.gameObject.GetComponent<MeshAnimator> ();
    17. }
    18. void OnEnable()
    19.     {
    20.      topSpeed= navAgent.speed;  //max speed of navmesh agent
    21.      InvokeRepeating ("SlowUpdate", updateSpeed, updateSpeed);
    22. }
    23. void SlowUpdate()
    24. {
    25.     curAnimation = meshAnimator.currentAnimation.ToString ();// last animation started
    26.     navAgentCurSpeed = navAgent.velocity.magnitude;    //current speed of navmesh agent
    27. if (navAgentCurSpeed <= transitionSpeed)
    28. {
    29.    if (curAnimation == "Zomb_Walk (MeshAnimation)") //if we are already using the correct animation ,only adjust the speed.
    30.   {
    31.      meshAnimator.speed = navAgentCurSpeed /transitionSpeed;
    32.   } else {     // we need to set the correct animation
    33.      meshAnimator.Play (MA_Walk);
    34.      meshAnimator.speed = navAgentCurSpeed /transitionSpeed;
    35.   }
    36. if (navAgentCurSpeed > transitionSpeed)
    37.   {
    38.     if (curAnimation == "Zomb_Run (MeshAnimation)")
    39.     {
    40.        meshAnimator.speed = navAgentCurSpeed /topSpeed;
    41.     } else {
    42.        meshAnimator.Crossfade (MA_Run , 0.25f );
    43.        meshAnimator.speed = navAgentCurSpeed /topSpeed;
    44.            }
    45.         }
    46.      }
    47. }
    48.  
    This is something basic i posted some time ago may have to tweek it to work still.
     
    jschieck and Davood_Kharmanzar like this.
  8. noahx

    noahx

    Joined:
    Nov 22, 2010
    Posts:
    50
    Hi,
    I'm getting an error when closing the game, but unfortunately it is not consistent in the steps to reproduce it. The only thing consistent is the line of code that is throwing the error in the MeshAnimator.cs script. The problem happens when using Crossfade to play animations (instead of the simple "Play"). In terms of what I can see in game as player, I think the problem is that two crossfading events are happening sort of at the same time, causing some problems later. What I mean is, I play an animation using crossfade, and while the elapsed time for that crossfade is still in progress (let's say 0.5f seconds), I play another animation also using crossfade (before the first crossfade ends), and I think that's what is causing a shift in the index that is crashing the script.

    First things first, I'm not a coder but I understand some of the code more in the terms of logic and things out of place (errors) and not necessarily how to code/implement it. But this is the error left in console:

    NullReferenceException: Object reference not set to an instance of an object
    FSG.MeshAnimator.MeshAnimator.OnDestroy () (at Assets/MeshAnimator/Scripts/MeshAnimator.cs:335)


    More details of the same error:
    ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index
    at System.Collections.Generic.List`1[System.Collections.Generic.Stack`1[UnityEngine.Mesh]].get_Item (Int32 index) [0x0000c] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633
    at FSG.MeshAnimator.MeshAnimator.OnDestroy () [0x00142] in I:\Dev\P7\Assets\MeshAnimator\Scripts\MeshAnimator.cs:335


    And that line of code corresponds to this:
    meshStack = s_crossFadePool[crossFadePoolIndex];

    So, based on the error and on the idea of that line of code, it is clear that the index (crossFadePoolIndex) is out of bounds in relation to the stack s_crossFadePool.
    I added logs to check on the index value and the element count of that stack and it proved that situation:
    s_crossFadePool Count: 4
    CrossFadePoolIndex: 5
    That line of code is throwing an error because it is trying to get an element in the position 5 when the stack only has 4 elements. How did that index got increased to 5 and the stack got only 4 elements? I don't know and that's the part I've been dealing with.

    This is just a reference from the documentation, and going back to the initial description of the problem, the index (CrossFadePoolIndex) is getting increased but the stack is not, and I think it is because a crossfade is being called while another crossfade is still in progress (before the float speed parameter ends), so because the crossfade didn't complete, it is not added to the stack, but the index is increased and that's when the fun begins:
    meshAnimator.Crossfade(0, 0.5f);

    So, what I did as a "temporary fix/workaround" is, I added a validation before that block of code that is crashing:

    if (crossFadePoolIndex < s_crossFadePool.Count)
    {
    ....
    }


    If the above statement is true, then proceed to execute that block of code, otherwise do nothing. But my only concern is, are there any risks doing that in terms of memory leaks? Because the error is in the "OnDestroy" event, I wonder if because of the messed up index, it will mean that some meshes didn't get destroyed and it will continue to consume memory from scene to scene.

    Thanks.
     
  9. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    By exiting the game do you mean closing the application or switching scenes? In either scenario, your fix will work as the meshes generated will be cleaned up by Unity.
     
  10. noahx

    noahx

    Joined:
    Nov 22, 2010
    Posts:
    50
    Sorry. Closing the application. I haven't tried switching scenes. But if I switch to another scene, every object in the previous scene will execute the "OnDestroy" event, right?
     
  11. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    Correct. If your closing the app you don't need to worry about it. Everything in memory is getting disposed anyway, which is probably what is happening here.
     
  12. noahx

    noahx

    Joined:
    Nov 22, 2010
    Posts:
    50
    Well, I'll continue working on the game keeping that "fix" and I'll pay attention on the memory, to see is it goes ballooning at some point during memory benchmarks and I'll let you know.

    Thanks.
     
  13. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    Still my fave thing
     
  14. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    Looks legit
     
  15. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    335
    @jschieck
    hi,
    is it possible to indirect instancing mesh animators?
    i tried to do with GPU Instancer ... but does not working :[

    so, any suggestion?
    or can you provide an special indirect instancing for that?
    i have about 2K zombies!! on PC everything is good [60 fps] but on android with 10 zombies i got 8-12 fps :[
    thanks.
     
    Last edited: Oct 21, 2019
  16. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    I was thinking the same thing. in essence you would have to re work the instancing code to walk through the baked animations I guess. as opposed to it's own baking system (I thought)

    You would have to eyball the code.
     
  17. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    Yes while possible you'd have to manually compile all meshes and instances on a given frame. I don't include this functionality out of the box to support the wide range of users in the asset store, and I doubt it would be faster than unity's internal GPU instancing code.
     
    JBR-games and twobob like this.
  18. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    You might want to check that's not a geometry based cap on the Android. Just saying... sounds like a possibility.
    Certainly bears testing that the bottleneck is where you think it is.
     
  19. HenryChinaski

    HenryChinaski

    Joined:
    Jul 9, 2013
    Posts:
    97
    Hi,
    We are using MeshAnimator with low compression accuracy for artistic reasons.
    Now, I need one frame of the MeshAnimator Animation as a readable mesh file.
    I tried a few assets that allow you to export almost anything as a 3D model out of Unity in playmode, but in this case
    the exported object was randomly missing big chunks of faces.

    I just wanted to ask if you have any idea or a tip for us, how we can save one animation frame as a 3D model.
    I guess there is no such function included?

    Best regards,
    Daniel
     
    twobob likes this.
  20. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    would it need to be like rigged and skinned?
    or would just the mesh do?

    Seems like outing the mesh is something that is doable. as for the rig and and skinning. not so sure about that.
    I seem to recall there are editor scripts that let one walk meshes at a specific moment and copy them.

    https://answers.unity.com/questions/39311/editor-scripting-how-to-save-a-script-generated-me.html reasonably sure that will be helpful in getting you a few lines of code for job

    or even https://forum.unity.com/threads/how-do-i-duplicate-a-mesh-asset.35639/ and just rip out the editor bit

    which would be like hacking https://github.com/pharan/Unity-MeshSaver really
     
    Last edited: Oct 29, 2019
    HenryChinaski likes this.
  21. HenryChinaski

    HenryChinaski

    Joined:
    Jul 9, 2013
    Posts:
    97
    Hi and many thanks for your help!
    I want to use the RayFire asset to fracture the model. So I need only the mesh!
    The last plugin you linked worked perfectly!
     
  22. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,785
    No worries Henry.
     
  23. Yurijh

    Yurijh

    Joined:
    Nov 4, 2016
    Posts:
    5
    How can I control the MeshAnimator through Animator calls?

    I have baked with MeshAnimator a large number of FBX that I used to control through setFloat, setBool, setTrigger etc. I also have several transition options, exit times, etc everything set in a series of complex AnimatorController. Is there a way to make this communication transparent or I need to implement all the set methods inside MeshAnimator.cs?

    In the provided example you show how to control a MeshAnimator through an active Animator. But after the bake process, I have an object with no Animator, so I have no animators running.

    How do you suggest to cope with this problem? I think it's very common.
    I cannot rewrite all the animation calls, I really need to leave the calls and the animator logic untouched.
    I hope I was clear enough, if you need more details, just ask.

    Thanks in advance.
     
  24. noobynobody

    noobynobody

    Joined:
    May 9, 2019
    Posts:
    76
    Based on your experience, does this asset improve performance for mobile if there aren't that many models on the screen (let's say 10)?
     
  25. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    You can inspect the state of the animator and it's currently playing animation, then use that to call the Play method on the MeshAnimator. But beware using lots of Animator's has it's own performance overhead so leaving that on your prefabs will hurt overall performance when you start getting lots of animators.

    You probably won't see much performance difference with only 10.
     
  26. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    368
    Happy Thanksgiving everyone! I'm thankful for all of your support of Mesh Animator over the years!

    I've put together a beginner tutorial showing the process of what it's like converting an existing Animator prefab into a Mesh Animator one and the performance gains that can be had. Enjoy, and as always let me know any questions you may have!

    And if you haven't already, Mesh Animator is 50% off right now for Black Friday so go grab your copy!

     
    Last edited: Nov 28, 2019
  27. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    320
    @jschieck Lol. Bought this way back in Feb, 2017 and have managed to never actually use it! Now I'm ready for it and finally there's a video that makes sense of the process. I think I'm good to go! It's like the potential I saw in this asset all those years ago is about to pay off :D

    ps. it would really help if you talked through your videos. Your walk through's a little haphazard and it took me quite a few rewinds to work out quite what you were doing at certain points. I'm just talking about the Mesh Animator conversion, the normal mecanim and basic AI scripting makes perfect sense. It's just that it's really important to explain why you're making certain decisions when explaining an asset like this.

    I'd love to see a follow up detailing the set-up for characters that switch to normal mecanim NPCs at LOD0
     
  28. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    488
    Hi. I'm trying to start using MA for my character but I got couple of errors !! I tried to follow the doc and tutorial but not change. Here is my character file:
    https://gofile.io/?c=NnlDaW
    Check it please, I'm using 2019.2.17 ver.
     
    Last edited: Jan 8, 2020
unityunity