Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by MarkD · Dec 25, 2013 at 10:25 PM · positionvertexrealtimeskinnedmesh

Realtime vertex position of a SkinnedMesh

Hello, I am making a script that should be able to instantiate objects for every vertex detected on a mesh, now I already managed to do this for static objects, as it is simply iterating with a loop trough every vertex of the Mesh.vertices function.

My question now is, since there is no vertices function inside the SkinnedMesh. How would one approach this?

I do not ask for ready to go scripts or solutions. Just a push in the right direction.

note: I script with JavaScript so if you have code related answers please take this into account.

Many greetings and merry Christmas.

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

3 Replies

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

Answer by angelonit · May 08 at 10:16 PM

Sorry to necro but this worked for me so here it is (in C# for those who, like me, arrived at the question in the far future):

  Transform tOwner;
  SkinnedMeshRenderer skinnedMeshRenderer;
  List<Vector3> meshVertices = new List<Vector3>();
  public void GetVertices()
  {
      Mesh mesh = new Mesh();
      skinnedMeshRenderer.BakeMesh(mesh, true);
      mesh.GetVertices(meshVertices);
      tOwner = skinnedMeshRenderer.transform;
  }
  public Vector3 GetPositionFromVertex(int i)
  {
      Vector3 worldPosVertex = tOwner.localToWorldMatrix.MultiplyPoint3x4(meshVertices[i]);
      return worldPosVertex;
  }
Comment
Add comment · Show 2 · 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 MarkD · May 11 at 09:32 AM 0
Share

Haha wauw, I did not expect a reply 9 years later. Since in the current version of Unity your method is more fitting I will update this as the correct Answer :). If my memory is correct, I also ended up creating a similar solution to this back in the day 9 years ago. Totally forgot about this post, should have updated it and spared you the trouble.

avatar image angelonit · May 11 at 10:10 AM 0
Share

Haha I guess some stuff keeps being needed forever :)

PS: Edited the "new mesh" inside the function, since I forgot it outside :(

avatar image
2

Answer by MarkD · Dec 31, 2013 at 03:28 PM

After a long few days search I found the answer here

I converted the script to JavaScript. Note that depending on vertices this can be very very slow. But it does the job, vertex position of the animated mesh. Place this script on your skinnedmesh renderer.

 #pragma strict
 /// <summary>
 
 /// Compute a skinned mesh's deformation.
 
 /// 
 
 /// The script must be attached aside a SkinnedMeshRenderer,
 
 /// which is only used to get the bone list and the mesh
 
 /// (it doesn't even need to be enabled).
 
 /// 
 
 /// Make sure the scripts accessing the results run after this one
 
 /// (otherwise you'll have a 1-frame delay),
 
 /// </summary>
 
     @HideInInspector
     var mesh: Mesh;
     @HideInInspector
     var skin: SkinnedMeshRenderer;
 
  
 @HideInInspector
     var vertexCount:int=0;
 @HideInInspector
      var vertices:Vector3[];
 //@HideInInspector
   //  var normals:Vector3[];
 
 
 
 
  
 
     function Start() {
 
         skin = GetComponent(SkinnedMeshRenderer);
 
         mesh = skin.sharedMesh;
 
  
 
         vertexCount = mesh.vertexCount;
 
         vertices = new Vector3[vertexCount];
 
       //  normals = new Vector3[vertexCount];
       
        //animation example
        for (var b:int= 0; b < mesh.vertexCount; b++){
              var cube : GameObject= new GameObject.CreatePrimitive(PrimitiveType.Cube);
             cube.name=b.ToString();
             cube.transform.localScale.x=0.1;
             cube.transform.localScale.y=0.1;
             cube.transform.localScale.z=0.1;
 }
     }
 
     
 
     function LateUpdate(){
 
         var boneMatrices: Matrix4x4[]  = new Matrix4x4[skin.bones.Length];
 
         for (var i:int= 0; i < boneMatrices.Length; i++)
 
             boneMatrices[i] = skin.bones[i].localToWorldMatrix * mesh.bindposes[i];
          
 
 
         for (var b:int= 0; b < mesh.vertexCount; b++){
 
              var weight:BoneWeight = mesh.boneWeights[b];
 
  
 
               var bm0:Matrix4x4 = boneMatrices[weight.boneIndex0];
 
               var bm1:Matrix4x4 = boneMatrices[weight.boneIndex1];
 
               var bm2:Matrix4x4 = boneMatrices[weight.boneIndex2];
 
               var bm3:Matrix4x4 = boneMatrices[weight.boneIndex3];
 
  
 
                 var vertexMatrix:Matrix4x4 = new Matrix4x4();
 
  
 
             for (var n:int= 0; n < 16; n++){
 
                 vertexMatrix[n] =
 
                     bm0[n] * weight.weight0 +
 
                     bm1[n] * weight.weight1 +
 
                     bm2[n] * weight.weight2 +
 
                     bm3[n] * weight.weight3;
 
             }
 
  
 
             vertices[b] = vertexMatrix.MultiplyPoint3x4(mesh.vertices[b]);
             //   normals[i] = vertexMatrix.MultiplyVector(mesh.normals[i]);
             
             //animation example
             var fetch= GameObject.Find( b.ToString());
             fetch.transform.position = vertices[b];
             }
 
   
             
         
 
     }
 
Comment
Add comment · Show 2 · 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 Bunny83 · Dec 31, 2013 at 04:25 PM 0
Share

btw: That's not Java it's UnityScript which is similar to Javascript but it has no relation to Java at all.

avatar image MarkD · Dec 31, 2013 at 05:18 PM 1
Share

@Bunny83 Sorry, I meant JavaScript (which is a legit na$$anonymous$$g for UnityScript it's in the docs). You are right, I'l edit it out.

avatar image
1

Answer by Bunny83 · Dec 31, 2013 at 04:17 PM

Unity has a new method called SkinnedMeshRenderer.BakeMesh which lets you create a "snapshot" of the current Mesh-state.

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 MarkD · Dec 31, 2013 at 05:07 PM 0
Share

Doesn't that slow down your system even more? I use it for tracing each vertice every frame, the script above is slow. But isn't baking slower?

But thanks for the headsup, I will check it out.

avatar image Bunny83 · Dec 31, 2013 at 05:12 PM 0
Share

no, it certainly isn't slower than skinning each vertex manually. The Skinned$$anonymous$$eshRenderer is made for skinning.

avatar image MarkD · Dec 31, 2013 at 05:20 PM 0
Share

I don't use it to skin the vertices manually, I used it to transform objects at every vertex position. For instance for each vert in the mesh a cube will follow its position. Or doesn't that make any difference in this case?

EDIT: It seems that the performance is the same (for placing transforms at vertex position), also with Bake$$anonymous$$esh I get the wrong vertex orientation, all the results are rotated wrong or in negative space. To bad, it would be much easier since it requires only 3 lines of code with bake$$anonymous$$esh.

thx anyway

avatar image Bunny83 · Jan 01, 2014 at 11:42 PM 0
Share

I'm not sure what you mean... The vertex positions of the baked mesh are of course in local space, like always. If you need them in world space you have to use TransformPoint of the transform the mesh is rendered with.

avatar image MarkD · Jan 02, 2014 at 01:10 AM 0
Share

And that is why I use the above script. I have teste both your suggestion and the above script and the result in frame rate is identical.

But thank you very much for your time and input.

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.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find (Get) linerenderer vertex positions 2 Answers

How to get the position of a vertex in a particles-shader? 0 Answers

Shader Vertex Position During Animation 1 Answer

Shader : issue with vertex positions when multiple models are on the scene 1 Answer

How to get the screen position in an unlit shader. 1 Answer


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