Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is Mesh.MarkDynamic ?

Discussion in 'Scripting' started by dogzerx2, Aug 29, 2013.

Thread Status:
Not open for further replies.
  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,960
    There's no example for this, but it says you get better performance.

    I tried marking the mesh dynamic at the start, and just before assigning vertices... but I get no change in frame rate!

    What is the proper use of this function? It would be great if I could improve performance for handling meshes!

    Current code I'm using:

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var skinnedMeshRenderer : SkinnedMeshRenderer;
    4. private var mesh : Mesh;
    5.  
    6. private var defaultVertices : Vector3[];
    7.  
    8. var target : Transform;
    9.  
    10. var markMeshDynamic : boolean;
    11.  
    12. function Start () {
    13.     skinnedMeshRenderer = GetComponentInChildren(SkinnedMeshRenderer);
    14.     mesh = Instantiate(skinnedMeshRenderer.sharedMesh);
    15.     skinnedMeshRenderer.sharedMesh = mesh;
    16.     defaultVertices = mesh.vertices;
    17.      
    18. }
    19.  
    20. function Update () {
    21.     if(Input.anyKeyDown){
    22.         markMeshDynamic = !markMeshDynamic;
    23.     }
    24.     var vertices : Vector3[] = mesh.vertices;
    25.     for (var i = 0; i < vertices.Length; i++){
    26.         var push : float;
    27.         push = Mathf.Max(0,1-
    28.         Vector3.Distance(target.position, skinnedMeshRenderer.transform.TransformPoint(defaultVertices[i])));
    29.         vertices[i] = defaultVertices[i] + mesh.normals[i] * push * .1;
    30.     }
    31.    
    32.     if(markMeshDynamic){
    33.         mesh.MarkDynamic();
    34.     }
    35.     mesh.vertices = vertices;
    36. }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The documentation says everything that is necessary. If you have frequent vertex updates, call MarkDynamic () before you assign the vertices. There are many optimizations in Unity that are especially needed for certain platforms. Some optimizations can barely be measured on PC/Mac, but it can make a huge difference on mobiles. Don't know when it makes sense, just do it to be on the safe side. In the case you have trouble, you have always the option to leave it away.

    Beside that your code has a lot of optimization potential.
    - Line 24 is a waste of resources. In each frame you get a copy of the vertices of your mesh! Make an array for them like it already exists for defaultVertices.
    - TransformPoint on line 28 is not the fastest option you have and calling transform so many times is also not optimal. After line 24 insert a Matrix4x4 which is the the local to world one from the skinned mesh renderer. Then you only need to use MultiplyPoint3x4 with the cached matrix at line 28.

    Your for loop may have some better optimizations I didn't catch, but it is likely that there is even more potential.
     
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,960
    Ooo nice!!! THANK YOU for these tips, they work like a charm. I'll keep them mind when processing big amounts of information.

    Maybe I could avoid updating if both the object and target are not changing position... so it would only slow down when needed.

    Is there a quicker way to calculate distance between points?

    Anyway, I will use mark mesh dynamically then. But is it done once at startup, or once per frame before assigning vertices? I would really like to know this before using it D-:

    Here's my new code btw:

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var skinnedMeshRenderer : SkinnedMeshRenderer;
    4. private var mesh : Mesh;
    5.  
    6. private var defaultVertices : Vector3[];
    7. private var vertices : Vector3[];
    8.  
    9. var target : Transform;
    10.  
    11. function Start () {
    12.     skinnedMeshRenderer = GetComponentInChildren(SkinnedMeshRenderer);
    13.     mesh = Instantiate(skinnedMeshRenderer.sharedMesh);
    14.     skinnedMeshRenderer.sharedMesh = mesh;
    15.     defaultVertices = mesh.vertices;
    16.     vertices = new Vector3[mesh.vertices.Length];
    17. }
    18.  
    19. function Update () {
    20.     var localToWorld : Matrix4x4 = skinnedMeshRenderer.transform.localToWorldMatrix;
    21.    
    22.     for (var i = 0; i < vertices.Length; i++){
    23.         var push : float;
    24.         push = Mathf.Max(0,1-
    25.         Vector3.Distance(target.position, localToWorld.MultiplyPoint3x4(defaultVertices[i])));
    26.         vertices[i] = defaultVertices[i] + mesh.normals[i] * push * .1;
    27.     }
    28.     mesh.vertices = vertices;
    29. }
     
    Last edited: Aug 29, 2013
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,500
    I would like to know:

    1) Do you call this just once for the life of the mesh or every time your set vertices?
    2) Is this value serialized?

    Hopefully a Unity dev can answer this. I don't see how I could even come up with tests on #1 to figure it out myself.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    At the moment you shouldn't call it at all, because it produces major issues. You can easily crash your application if it uses DX11 and the same seems to be true for other platforms like Vita.

    I have reported a bug about it and got an email that Unity was able to reproduce the issue, but it is not (yet) in the issue tracker.
    Case 588959
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,500
    Wow, okay thanks! Great to know. You probably saved me tons of support requests and days of bug hunting. I'll keep an eye on it and implement if they ever fix it.
     
  7. Xonatron

    Xonatron

    Joined:
    Jan 14, 2013
    Posts:
    31
    So, if it is now fixed, should you call it once at start up or every vertex update?
     
    Aratow and lukepend like this.
  8. UnityMaru

    UnityMaru

    Community Engagement Manager Unity Technologies

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    A lot would have changed in nearly seven years - you may want to avoid necroposting :p
     
    JoNax97 likes this.
Thread Status:
Not open for further replies.