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

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

Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer

Getting the world position of mesh vertices? 2 Answers

Mesh Vertices Welding 0 Answers

MeshCollider is not getting updated when adding vertex to mesh 1 Answer

Is it possible to make vertices defined by a mesh and mesh filters animating? 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