Hyluss

[Unity] MapDisplay

Mar 2nd, 2018
504
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MapDisplay : MonoBehaviour {
  6.  
  7.     [SerializeField] Renderer _textureRenderer;
  8.  
  9.     public void DrawNoiseMap(float[,] noiseMap)
  10.     {
  11.         int width = noiseMap.GetLength(0);
  12.         int height = noiseMap.GetLength(1);
  13.  
  14.         Texture2D texture = new Texture2D(width, height);
  15.  
  16.         // do this beacuse set pixels is faster than set pixels singly
  17.         Color[] colourMap = new Color[width * height];
  18.         for (int y = 0; y < height; y++)
  19.         {
  20.             for (int x = 0; x < width; x++)
  21.             {
  22.                 //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
  23.                 colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]);
  24.             }
  25.         }
  26.         texture.SetPixels(colourMap);
  27.         texture.Apply();
  28.  
  29.         _textureRenderer.sharedMaterial.mainTexture = texture;
  30.         _textureRenderer.transform.localScale = new Vector3(width, 1, height);
  31.  
  32.     }
  33. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment