Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by cj-carey · Sep 07, 2018 at 09:16 AM · meshprocedural meshprimitivesvertice manipulation

Trouble assigning vertices in a mesh to connect to other meshes vertices.

So what I'm trying to do is to take an array of meshes and connect them by changing the vertices to be at the location of the mesh in front of it in the array.

Right now, my algorithm only works for the first pair in the array. Images below. alt text

The problem is when I try to make more than one connection, I get the image below.

alt text

Here is the algorithm I use, I found the vertices to connect by a lot of testing, using the default unity cubes. Assigning the locations of the front face and all connecting points, to be at the locations of the back face of the mesh in front of it. I don't understand why it works fine for the first connection but after that it is so off

    void ConnectMeshes()
     {
 
 
         for (int i = 0; i < meshes.Length-1; i++)
         {
 
 
             Vector3[] verts = meshes[i].vertices;
    
 
             verts[0] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
             verts[1] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
             verts[2] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
             verts[3] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
             verts[8] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
             verts[9] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
             verts[13] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
             verts[14] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
             verts[16] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
             verts[17] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
             verts[22] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
             verts[23] =  markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
 
             int[] tris = meshes[i].triangles;
 
 
             meshes[i].Clear();
             meshes[i].vertices = verts;
             meshes[i].triangles = tris;
             meshes[i].RecalculateNormals();
             meshes[i].RecalculateBounds();
             meshes[i].RecalculateTangents();
 
 
         }
     }



The name of the gameObjects of associated meshes are called Markers in the code. I know the meshes are in the same order because of this code.

 void Start()
 {
  meshes = new Mesh[markers.Length];
 
         for (int i = 0; i < markers.Length; i++)
         {
             meshes[i] = markers[i].GetComponent<MeshFilter>().mesh;
         }
 }



Any clues to where I've gone wrong?

combined-connect.png (87.7 kB)
connect-3.png (75.5 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

1 Reply

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

Answer by Bunny83 · Sep 07, 2018 at 04:46 PM

You correctly use "TransformPoint" but you incorrectly use "InverseTransformVector" which should be InverseTransformPoint. "InverseTransformVector" expects a Vector4 with homogeneous coordinates. Since you pass in a Vector3 it gets implicitly converted to Vector4 but the w component will be 0 instead of 1. Therefore the translation is not applied, only rotation and scale. This does work correctly:

     for (int i = 0; i < meshes.Length - 1; i++)
     {
         Vector3[] verts = meshes[i].vertices;
         Vector3[] verts2 = meshes[i + 1].vertices;
         Transform t = markers[i].transform;
         Transform t2 = markers[i + 1].transform;

         verts[0] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
         verts[1] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
         verts[2] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
         verts[3] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
         verts[8] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
         verts[9] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
         verts[13] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
         verts[14] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
         verts[16] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
         verts[17] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
         verts[22] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
         verts[23] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));

         meshes[i].vertices = verts;
         meshes[i].RecalculateNormals();
         meshes[i].RecalculateBounds();
         meshes[i].RecalculateTangents();
     }

Note that you really should cache the "verts2" like i did. Every time you access mesh.vertices Unity will create a copy of the whole vertices array. Since you do this 12 times each iteration you create a lot of unnecessary garbage.

You could improve this further by removing duplicate positions. A Cube has "8" logical but 24 actual vertices since they need to be split for each face. However that means there are always 3 vertices with the same position. So you can calculate that position only once and assign it to the corresponding vertices. Just like that:

 verts[0] = verts[13] = verts[23] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
 verts[1] = verts[14] = verts[16] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
 verts[2] = verts[8] = verts[22] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
 verts[3] = verts[9] = verts[17] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));

I would have suggested creating a Matrix4x4 by combining the two transformations into one, that way each point only need to be transformed once, directly from source to target. However since we now only have 4 points this is quite irrelevant. However you could have done this as well:

 Matrix4x4 m = t.worldToLocalMatrix * t2.localToWorldMatrix;
 verts[0] = verts[13] = verts[23] = m.MultiplyPoint3x4(verts2[6]);
 verts[14] = verts[1] = verts[16] = m.MultiplyPoint3x4(verts2[7]);
 verts[2] = verts[22] = verts[8] = m.MultiplyPoint3x4(verts2[10]);
 verts[17] = verts[3] = verts[9] = m.MultiplyPoint3x4(verts2[11]);


Also note there's no reason to call Mesh.Clear. This should only be used when you actually change the number of vertices or when you create different triangles than there were before. Since you just modify the existing mesh, just update the vertices. The triangles will stay the same.

Comment
Add comment · Show 1 · 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 cj-carey · Sep 07, 2018 at 08:41 PM 0
Share

Thank you very much for this answer. The mistake of mixing up InverseTransformPoint and InverseTransformVertex is an example of reliance on intellisense and copy+paste. Also, I appreciate the tips to let me take this project farther. $$anonymous$$uch to consider with this post, much respect from me. Thank you.

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

101 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

Related Questions

Converting 2d sprite to mesh - looking better algorithm 1 Answer

Why does this script put Unity into a not responding state? 0 Answers

Prevent seams on a generated mesh with split edges. 0 Answers

if I rotate an object, do they vertices of its mesh change? 1 Answer

Normal maps not applying to certain faces of a mesh 2 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