'Comparing two pixels and setting a texture to black depending on outcome
I am trying to compare two pixels of the same texture and then if they are different (For Unity BTW), then for it to take the X and Y and plug that into another texture to set the pixel. My first thought was this simple script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapDisplay : MonoBehaviour
{
[SerializeField]
Texture2D provinceMap;
[SerializeField]
GameObject mapObject;
Color borderColor = new Color(255, 0, 0);
private void Awake()
{
int provinceMapSizeX = provinceMap.width;
int provinceMapSizeY = provinceMap.height;
Texture2D mapDisplayBorders = new Texture2D(provinceMapSizeX, provinceMapSizeY);
for (int x = 0; x < provinceMapSizeX; x++)
{
for (int y = 0; y < provinceMapSizeY; y++)
{
Color previousPixel = provinceMap.GetPixel(x - 1, y - 1);
Color currentPixel = provinceMap.GetPixel(x, y);
if (previousPixel != currentPixel)
{
mapDisplayBorders.SetPixel(x - 1, y - 1, borderColor);
}
}
}
mapObject.GetComponent<Renderer>().material.mainTexture = mapDisplayBorders;
}
}
And at first I thought this would work, and on pressing play, the plane turned darker but nothing else. then I tried messing with borderColor (Hence 255), but nothing happened. The Texture2D provinceMap is a simple image that is 64x64 pixels, about half white and half black. I am trying to get it to add a pixel of black for every color change in this example (Its supposed to be on both pixels which can be fixed in one line of code but trying to test this right now). I don't understand why this wouldn't work though. Thanks for any help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
