• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DanaScully · Nov 21, 2016 at 01:00 PM · meshmeshfilter

Mesh vertex info per frame

I have a piece of cloth in my game and I want tot extract the vertex position in each frame. However the numbers in the list do not change. How can I update these positions? In my code I try to access the mesh component of the cloth then extract the vertex positions but it only shows the initial positions and when I hit start the list won't get updated.

 public Vector3[] clothMeshVerts;
     public int numOfVerts;
     Vector3[] normals;
     public int[] trianlges;
     SkinnedMeshRenderer smr;
 
 
     void Start()
     {
          smr = GetComponent<SkinnedMeshRenderer>();
      }
 
     void Update()
     {
         
         clothMeshVerts = smr.sharedMesh.vertices;
         numOfVerts = smr.sharedMesh.vertexCount;
               
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Nov 21, 2016 at 07:49 PM

In the past there was no way to access the current state of a SkinnedMeshRenderer as the skinning happens either on the GPU or in the engine's core. Of course the sharedMesh it the "unskinned" Mesh or the "source" Mesh.

They now added a way to access the current state of the skinned mesh. You just have to use BakeMesh of your SkinnedMeshRenderer and pass a temp Mesh object which will be "filled" with the current snapshot of the skinned mesh.

Since you want to access the data every frame you might want to re-use that temp Mesh, so don't create a new one each frame. If you do create a new Mesh, keep in mind that the old one need to be Destroy()-ed or it will hang around in memory and cause a memory leak.

edit

Ahh, ok that's just another "oddity" of how Unity implements certain things ^^. It seems that BakeMesh doesn't work with cloth objects. However the Cloth component itself has a "vertices" property which return the current "skinned" vertex positions. I quickly created this script and used a standard plane mesh and it works as intended:

 using UnityEngine;
 
 public class ViewClothMesh : MonoBehaviour
 {
     Cloth cloth;
     int[] tris;
     void Start ()
     {
         cloth = GetComponent<Cloth>();
         Mesh mesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
         tris = mesh.triangles;
     }
     
     void Update ()
     {
         var verts = cloth.vertices;
         for(int i = 0; i < tris.Length; i+=3)
         {
             var v1 = transform.TransformPoint(verts[tris[i    ]]);
             var v2 = transform.TransformPoint(verts[tris[i + 1]]);
             var v3 = transform.TransformPoint(verts[tris[i + 2]]);
             Debug.DrawLine(v1, v2);
             Debug.DrawLine(v2, v3);
             Debug.DrawLine(v3, v1);
         }
     }
 }

ps: the Cloth component also has a "normals" property in case you need the skinned normals as well.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanaScully · Nov 22, 2016 at 08:42 AM 0
Share

Hi, thanks for the reply. So i changed my code in Update() like this:

  void Update()
     {
 
         $$anonymous$$esh sample = new $$anonymous$$esh();
         smr.Bake$$anonymous$$esh(sample);
         cloth$$anonymous$$eshVerts = sample.vertices;
         numOfVerts = sample.vertexCount;
         Destroy(sample);
      }

But still my cloth is moving but the vertex positions are not changing.

avatar image Bunny83 DanaScully · Nov 22, 2016 at 02:04 PM 0
Share

Well, i barely used Cloth objects myself so i never was in need of that functionality. I've updated my answer with a working example ^^.

avatar image tanoshimi Bunny83 · Nov 22, 2016 at 02:24 PM 0
Share

"Well, i barely used Cloth objects myself" - ah, then you've been missing out a treat there.... just wait until you try to get them to place nice with collisions :)

avatar image DanaScully · Nov 22, 2016 at 09:34 AM 0
Share

Just to be clear, I want to be able to output the deformation info (e.g. new vertex positions) in each frame (till the end of let's say simulation sequence).

avatar image DanaScully · Nov 28, 2016 at 09:32 AM 0
Share

Thanks @Bunny83 for the information. I simply used the cloth vertices, as in your example, ins$$anonymous$$d of the vertices of the shared mesh and now the vertex positions change per frame as well as when the cloth is being hit by a ball (Sphere object).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Creating a mesh for an overview map 0 Answers

Number of mesh vertices increase when imported into unity 1 Answer

Why zero height mesh gets black independent of shader 1 Answer

How to set UV (Map) for Mesh made by script without duplicating vertices? 2 Answers

How to change mesh on button press? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges