Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MapReader : MonoBehaviour {
- public Texture2D image;
- bool[,] map;
- public bool showMapGizmos;
- void Start () {
- MapFromImage();
- }
- void MapFromImage()
- {
- Color[] cols = image.GetPixels();
- map = new bool[image.width, image.height];
- float thresholdIntensity = .6f;
- for (int i = 0; i < image.width; i++)
- {
- for (int j = 0; j < image.height; j++)
- {
- Color c = cols[j * image.width + i];
- float pixelIntensity = c.grayscale;
- map[i, j] = pixelIntensity > thresholdIntensity;
- }
- }
- }
- private void OnDrawGizmos()
- {
- if (map != null && showMapGizmos)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Vector3 pos = new Vector3(-map.GetLength(0) / 2f + i, 0, -map.GetLength(1) + j);
- Gizmos.color = (map[i, j]) ? Color.green : Color.red;
- Gizmos.DrawCube(pos, new Vector3(1, .1f, 1));
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment