- Home /
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
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).
Your answer
Follow this Question
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