SebastianLague

Aziz

May 4th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MapReader : MonoBehaviour {
  6.  
  7. public Texture2D image;
  8. bool[,] map;
  9. public bool showMapGizmos;
  10.  
  11. void Start () {
  12. MapFromImage();
  13. }
  14.  
  15. void MapFromImage()
  16. {
  17. Color[] cols = image.GetPixels();
  18. map = new bool[image.width, image.height];
  19. float thresholdIntensity = .6f;
  20.  
  21. for (int i = 0; i < image.width; i++)
  22. {
  23. for (int j = 0; j < image.height; j++)
  24. {
  25. Color c = cols[j * image.width + i];
  26. float pixelIntensity = c.grayscale;
  27. map[i, j] = pixelIntensity > thresholdIntensity;
  28. }
  29. }
  30. }
  31.  
  32. private void OnDrawGizmos()
  33. {
  34. if (map != null && showMapGizmos)
  35. {
  36. for (int i = 0; i < map.GetLength(0); i++)
  37. {
  38. for (int j = 0; j < map.GetLength(1); j++)
  39. {
  40. Vector3 pos = new Vector3(-map.GetLength(0) / 2f + i, 0, -map.GetLength(1) + j);
  41. Gizmos.color = (map[i, j]) ? Color.green : Color.red;
  42. Gizmos.DrawCube(pos, new Vector3(1, .1f, 1));
  43. }
  44. }
  45. }
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment