- Home /
Question by
champend · Apr 14, 2021 at 09:46 PM ·
meshmeshrenderermeshfilter
Color not being applied properly to mesh
I decided to try to procedurally generate some terrain and I am at a point where I am trying to apply color values to the mesh depending on a list of float values. Here's the code that creates the colormap:
public List<Color> GenerateColorMap(List<float> _heightMap)
{
List<Color> _colors = new List<Color>();
for (int i = 0; i < _heightMap.Count; i++)
{
for (int z = 0; z < m_TerrainTypes.Count; z++)
{
if (_heightMap[i] <= m_TerrainTypes[z].m_Height)
{
_colors.Add(m_TerrainTypes[z].m_Color);
break;
}
}
}
return _colors;
}
And here is the code that actually creates the texture based off of the colormap.
public Texture2D GenerateTextureFromColorMap(Color[] _colors, int _width, int _height)
{
Texture2D _texture2D = new Texture2D(_width, _height);
_texture2D.filterMode = FilterMode.Point;
_texture2D.SetPixels(_colors, 0);
_texture2D.Apply();
transform.localScale = new Vector3(_texture2D.width, 1, _texture2D.height);
return _texture2D;
}
public void DrawTexture(Texture2D _texture2D)
{
m_Renderer.sharedMaterial.mainTexture = _texture2D;
Debug.Log($"Width: {_texture2D.width}, Height: {_texture2D.height}");
}
My problem is that it seems like the the color is not being assigned correctly to the texture as the texture comes out with only one color (the one who's height value is the lowest) and I haven't the foggiest why that is. Any help would be appreciated.
Comment