Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
2 captures
11 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 donijoya · Jul 08, 2015 at 10:14 AM · meshvertexdeformation

Mesh vertices array order

I want to make simple mesh deformer. What I'm doing is iterating vertex array of a mesh on which my script is added and modifying vertexes coordinate depending on specific condition. But, for performance issue, I want to know the order of vertex, which is how vertex is stored in vertices[] in what order.

My guess is that there is no order. Because, as I know, each vertex has index which is totally dependent on model maker. It means, if model maker just put vertexes on any order, vertices[] order is also almost random. Is my guess true?

If it is true.. I think it's not possible to optimize my deformer algorithm. Simply, I want to access vertexes in order of increasing y or decreasing y. (local y)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Thomas-Mountainborn · Jul 08, 2015 at 10:48 AM

You're right, vertex indices are not linked in any way to their position.

You could sort your vertices once, at the start of the script, and have an array that stores the indices of the vertices in a sorted manner. You can then always access the sorted vertices without having to sort them each frame.

Alternatively, depending on what it is exactly you're doing, you could consider writing a vertex shader to deform the object instead of deforming it by script.

Comment
Add comment · Show 3 · 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 donijoya · Jul 09, 2015 at 12:10 AM 0
Share

Thanks for the answer. I have one more question then.

As I know, there is triangle information array(int[]) which saves vertex indexes for mesh triangles. Question is that if I sort vertex array, this triangle information will be messed up(maybe UV as well).

Is it O$$anonymous$$ to sort vertex array? If not, I think I have to make int array to save sorted index of vertex ins$$anonymous$$d of directly changing vertex array.

avatar image _Gkxd · Jul 09, 2015 at 12:22 AM 0
Share

Unless you reconstruct the triangle index array and sort all the other arrays as well (normals, texcoords, colors, etc.), your vertex positions will no longer match up properly with the rest of the vertex information. This is fine if you don't care about the other information (i.e. the shader that you use with the mesh doesn't use per-vertex normals, texcoords, colors, etc.), but otherwise, you need to sort these with the vertices.

The simplest way to do this is to create a data structure that holds the vertex position, normal, texcoord, colors, etc. for each vertex, then sort a list of that data structure based off of the position. Then using the sorted list, you can reconstruct all of the arrays that the mesh needs.

You will still have to build the triangle index array. To do this, you will have to get the vertex position in the original vertex array using the old index from the triangle array. Then search for the vertex position in the sorted list, which will be the new triangle index.

avatar image Thomas-Mountainborn · Jul 09, 2015 at 04:36 AM 0
Share

Right, I didn't mean that you sort the vertices array and then write it back to the mesh, because that will indeed mess everything up. I meant that you should leave mesh.vertices as it is, and create your own separate int array that holds the indices of vertices in mesh.vertices in a sorted manner. You then go mesh.vertices[sortedVertices[i]].

avatar image
0

Answer by Darkgaze · Jun 08, 2020 at 10:29 AM

Just FYI, if you are using Terrains instead of a Mesh, and you are getting the TerrainData vertex data. Vertices are stored first Zs and then X, not the other way around. So the index is X * resolutionZ + Z

In case it is important for you.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast on mesh deformed by shader 0 Answers

Simple mesh deformation : Mesh.vertices or vertex shader with displacement texture ? 1 Answer

Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers

Any script to 'fix' normals/winding order? 3 Answers

Blender Armature Deformation in Unity 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