• 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 ShroomWasTaken · Jan 22, 2018 at 04:45 PM · meshverticesmeshfilter

Why does my mesh's vertices all set to 0 when I try to access it using Mesh.vertices?

I'm having a really weird problem where all the vertices of my mesh returns Vector3.zero when I try to copy them from Mesh.vertices.

When the GeneratePreviewNoise() function is called, the entire Mesh becomes invisble in the scene because all the vertices have the same location (0, 0, 0).

Why does this happen? o.O



My code currently looks like this:

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class WorldManager : MonoBehaviour
 {
     public Vector3 noisePos;
 
     [SerializeField] private int m_size;
 
     [SerializeField] private float m_amplitude;
     [SerializeField] private float m_frequency;
     [SerializeField] private float m_lacunarity;
     [SerializeField] private float m_invLacunarity;
     [SerializeField] private int m_octaves;
 
     [SerializeField] private GameObject m_quad;
     [SerializeField] private List<GameObject> m_chunks = new List<GameObject>();
     private Material m_sharedQuadMat;
 
     private void Start()
     {
         //m_sharedQuadMat = m_quad.GetComponent<MeshRenderer>().sharedMaterial;
     }
 
     public void GeneratePreviewNoise()
     {
         m_sharedQuadMat = m_quad.GetComponent<MeshRenderer>().sharedMaterial;
         Texture2D tex = new Texture2D(m_size, m_size);
 
         for (int x = 0; x < m_size; x++)
         {
             for (int y = 0; y < m_size; y++)
             {
                 float noiseValue = Noise.GeneratePerlin2D(x + noisePos.x, y + noisePos.y, m_frequency, m_octaves, m_amplitude, m_lacunarity, m_invLacunarity);
                 tex.SetPixel(x, y, new Color(noiseValue, noiseValue, noiseValue));
             }
         }
 
         tex.Apply();
         m_sharedQuadMat.mainTexture = tex;
 
         Mesh mesh = m_chunks[0].GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices; // Right here, mesh.vertices are all set to Vector3.zero.
         Vector3[] newVertices = new Vector3[vertices.Length];
 
         for (int i = 0; i < vertices.Length; i++)
         {
 
             Vector3 currentVertex = vertices[i];
             Vector3 worldPos = m_chunks[0].gameObject.transform.TransformPoint(vertices[i]);
             float noiseValue = Noise.GeneratePerlin2D(worldPos.x + noisePos.x, worldPos.y + noisePos.y, m_frequency, m_octaves, m_amplitude, m_lacunarity, m_invLacunarity);
             noiseValue *= 100;
 
             currentVertex = new Vector3(currentVertex.x, noiseValue, currentVertex.z);
             newVertices[i] = currentVertex;
 
             Debug.Log("Value: " + currentVertex);
         }
 
         mesh.vertices = newVertices;
 
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.RecalculateTangents();
 
         //m_chunks[0].GetComponent<Material>().mainTexture = tex;
 
         //m_sharedQuadMat.SetTexture("_BumpMap", tex);
         //m_sharedQuadMat.SetTexture("_HeightMap", tex);
         //m_sharedQuadMat.SetTexture("_Occlusion", tex);
     }
 
 }
 
 #if UNITY_EDITOR
 
 [CustomEditor(typeof(WorldManager))]
 public class WorldManagerEditor : Editor
 {
     private Vector3 lastPos;
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("Generate Preview"))
             ((WorldManager)target).GeneratePreviewNoise();
     }
 }
 
 #endif

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
0

Answer by victorbisaev · Feb 01, 2018 at 10:35 PM

Please inspect your scene setup as the code looks good and works as expected when used on correct mesh. Could it be that m_chunks[0] has a wrong mesh at some moment?

In general, Vector3[] vertices = mesh.vertices; should get correct vertices.

For the beginning I also would suggest to use currentVertex = new Vector3(currentVertex.x, currentVertex.y, currentVertex.z); (identity) and see if it causes any problems (shouldn't).

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.

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

83 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

Related Questions

Mirror vertices procedurally 2 Answers

Selecting a single polygon / face at runtime 1 Answer

How to Reference Vertices of a Mesh of a meshFilter of a GameObject? 1 Answer

Extruding faces of a mold. Need to properly assign vertices to triangles 0 Answers

Why vertex positions appear (0.0, 0.0, 0.0) ? 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