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

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
Add comment · 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 $$anonymous$$arkDynamic, 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 SIC$$anonymous$$!! 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 'Create$$anonymous$$esh()' 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 Create$$anonymous$$esh() 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.

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

How to add vertex points to a mesh 1 Answer

What is the order/connection of array elements in mesh.vertices? 2 Answers

NullReferenceException on assigning vector3 to mesh.vertices - no effect on assignment? 1 Answer

Array of Verticies 1 Answer

Mesh Vertex 0 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