Hyluss icon

[Unity] MapDisplay

Hyluss | PRO | 03/02/18 09:27:52 AM UTC | 0 ⭐ | 430 👁️ | Never ⏰ | []
C# |

1.11 KB

|

None

|

0 👍

/

0 👎

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MapDisplay : MonoBehaviour {
 
    [SerializeField] Renderer _textureRenderer;
 
    public void DrawNoiseMap(float[,] noiseMap)
    {
        int width = noiseMap.GetLength(0);
        int height = noiseMap.GetLength(1);
 
        Texture2D texture = new Texture2D(width, height);
 
        // do this beacuse set pixels is faster than set pixels singly
        Color[] colourMap = new Color[width * height];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                //we can get first index by first multiplying Y by the width of the map beacuse colourMap is 1-dimensional board and noiseMap is 2-dimensionals
                colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]);
            }
        }
        texture.SetPixels(colourMap);
        texture.Apply();
 
        _textureRenderer.sharedMaterial.mainTexture = texture;
        _textureRenderer.transform.localScale = new Vector3(width, 1, height);
 
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎