• 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 msjunior091995 · Jan 14, 2019 at 12:40 PM · meshmesh verticesmesh manipulationmesh-deformation

make one mesh follow the deformations of the other,adjust mesh to follow deformations of another(cloth, hair)

Hi, I usually use daz studio models in my projects, the problem is that every time I download a new morph for the main model (Genesis 3), I have to reimport all others like hair and clothes. To facilitate t$$anonymous$$s process I decided to write the script below, it searches for each vertex of the clot$$anonymous$$ng, the vertex closest to the main mesh and based on the distance between the two turns t$$anonymous$$s vertex into a parent (t$$anonymous$$s process is very slow), causing the secondary mesh follows the deformations of the main mesh, but it is not behaving as it should, the mesh gets very strange. What can I do to improve the script?

I'm sorry for bad English.

 public class MorphConform : MonoBehaviour {
 
     public GameObject TargetMesh;
     Vector3[] TargetVertices;
 
     public GameObject SourceMesh;
     Vector3[] SourceVertices;
 
     int[] VertParent;
     Vector3[] LocalPos;
 
 
 
     void Update(){
 
         if (Input.GetKeyDown("a"))
         {
 
             GetOriginal ();
         }
 
         if (Input.GetKeyDown("b"))
         {
 
             Conform();
         }
 
     }
 
     void GetOriginal () {
 
         TargetVertices = TargetMesh.GetComponent<MeshFilter> ().mesh.vertices;
         Mesh provmesh = new Mesh ();
         SourceMesh.GetComponent<SkinnedMeshRenderer> ().BakeMesh (provmesh);
         SourceVertices = provmesh.vertices;
 
         VertParent = new int[TargetVertices.Length];
         LocalPos = new Vector3[TargetVertices.Length];
 
         for (int t = 0; t < TargetVertices.Length; t++) {
 
             bool FirtsTime = true;
             float AtDist = 0.0000000f;
             float MinDist = 0.0000000f;
 
             for (int s = 0; s < SourceVertices.Length; s++) {
 
                 Vector3 wt = TargetMesh.transform.TransformPoint (TargetVertices[t]);
                 Vector3 ws = SourceMesh.transform.TransformPoint (SourceVertices[s]);
 
                 AtDist = Vector3.Distance (ws, wt);
 
                 if(AtDist < MinDist || FirtsTime){
                     FirtsTime = false;
                     MinDist = AtDist;
                     VertParent[t] = s;
                     LocalPos[t] = wt - ws;
                 }
 
 
             }
         }
         
     }
     
     void Conform () {
 
         TargetVertices = TargetMesh.GetComponent<MeshFilter> ().mesh.vertices;
         Mesh provmesh = new Mesh ();
         SourceMesh.GetComponent<SkinnedMeshRenderer> ().BakeMesh (provmesh);
         SourceVertices = provmesh.vertices;
 
         for (int t = 0; t < TargetVertices.Length; t++) {
 
             Vector3 ws = SourceMesh.transform.TransformPoint (SourceVertices [VertParent [t]]);
             Vector3 lt = TargetMesh.transform.InverseTransformPoint (ws);
             TargetVertices [t] = lt + LocalPos [t];
 
         }
 
         TargetMesh.GetComponent<MeshFilter>().mesh.vertices = TargetVertices;
         TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
         TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
         TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateTangents ();
         
     }

alt textalt text

1.png (79.9 kB)
2.png (107.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

108 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Problem with setting vertices positions and smoothing groups. 1 Answer

How to manipulate(flatten) the Mesh of an object 0 Answers

How can i generate my mesh to get a mesh for 2d light? 0 Answers

How can I deform 3D object with a bomb? 2 Answers

Sort a list based on edge connections 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