• 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
Question by Winj42 · May 30, 2018 at 01:04 AM · mesharrayverticesfor-loopvertex

Changing Y Value in Vertex Array?

I was working from a tutorial on procedural mesh generation, and I decided to try something out. I wanted to go through the array of points that I had created and make their Y values all 1. However, the program didn't really seem to do anything. Here's the program:

 public class ProceduralMesh : MonoBehaviour {
 
     Mesh mesh;
 
     Vector3[] vertices;
     int[] triangles;
 
     void Awake () {
         mesh = GetComponent<MeshFilter>().mesh;
     }
 
     // Use this for initialization
     void Start () {
         MakeMeshData();
         CreateMesh();
 
         for (int i = 0; i < vertices.Length; i++)
         {
             vertices[i].y = 1;
         }
     }
     
     // Update is called once per frame
     void MakeMeshData () {
         //Create array of verts
         vertices = new Vector3[] {new Vector3 (0, 0, 0), new Vector3 (0, 0, 1), new Vector3 (1, 0, 0), new Vector3 (1, 0, 1)};
         //Create array of ints
         triangles = new int[] {0, 1, 2, 2, 1, 3};
     }
 
     void CreateMesh () {
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
 
         mesh.RecalculateNormals();
     }
 }

The for loop is what I was using to make it go through the list. Basically, the mesh just stayed at y = 0. It's probably something super simple, but all help would be gratefully received:)

Comment

People who like this

0 Show 0
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

Answer by Tobychappell · May 30, 2018 at 01:12 AM

On the mesh object you wish to Edit, you want to call SetVertices and pass the collection of vertices.

https://docs.unity3d.com/ScriptReference/Mesh.SetVertices.html


After doing so you would need to tell the mesh to recalculate it's normals

https://docs.unity3d.com/ScriptReference/Mesh.RecalculateNormals.html


If you're going to be updating the mesh a lot, you'd be able to increase/save performance by calling the MarkDynamic method on the mesh object.

https://docs.unity3d.com/ScriptReference/Mesh.MarkDynamic.html

Comment
Bunny83

People who like this

1 Show 4 · 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 Tobychappell · May 30, 2018 at 01:31 AM 0
Share

I'm not 100% sure on this, but in your code the array of verts is the same array of vests in the mesh object. When you're setting y to be 1, that mesh is seeing that change, however this data hasn't been sent to the GPU, it would be bad to update the mesh vertex data every time a single vert was changed as that would consume allot more of the bandwidth between cpu and gpu. Hence MarkDynamic, not really sure what that does under the hood, but I guess it optimises the way it'll communicate with the GPU for this mesh. Again I'm not a graphics engineer, this is more of a gut feeling of WHY your mesh wasn't updating.

avatar image Winj42 · May 30, 2018 at 05:55 PM 0
Share

OH SICK!! Thank you so much! Do you know how exactly to use that SetVertices? I can't quite figure it out...

avatar image ShadyProductions Winj42 · May 30, 2018 at 06:02 PM 2
Share
 mesh.SetVertices(vertices);
 mesh.RecalculateNormals();

Or a quicker fix, you can simply call 'CreateMesh()' after you set the vertices y.

avatar image Winj42 ShadyProductions · May 31, 2018 at 01:04 PM 0
Share

WOW HAHA I cannot believe that I didn't think of calling CreateMesh() after setting the vertices! Thank you so much!!

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

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

Setting and storing vertex locations in a shader 0 Answers

UnityEngine.UI.Text characters mesh 0 Answers

Isolation of different vertices on a mesh 2 Answers

Editing vertices 1 Answer

Assigning new Mesh Vertices 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