Hi, I am trying to get border pixels of a texture.
Here is the script that I am using to generate 2D texture with random color (usually with 8 x 8 pixels) that is applied on the cube.
using UnityEngine;
using System.Collections;
public class TextureController : MonoBehaviour
{
private Renderer _target;
private int _width;
private int _height;
private Color[] _pixels;
private Texture2D _pixelTexture;
void Start()
{
}
public void InitColorControl(Renderer target,int dim)
{
_target = target;
SetPixelDimensions(dim);
}
public void SetPixelDimensions(int dimension)
{
_width = _height = dimension;
_pixels = new Color[_width * _height];
SetRandomColor();
ApplyTexture();
}
public void SetRandomColor()
{
for (int i = 0; i < _pixels.Length; i++)
_pixels[i] = new Color(Random.value, Random.value, Random.value, 1f);
}
private void ApplyTexture()
{
var texture = new Texture2D(_width, _height)
{
filterMode = FilterMode.Point
};
_target.material = new Material(Shader.Find("Diffuse"));
_target.sharedMaterial.mainTexture = texture;
for (int i = 0; i < _width; i++)
{
for (int j = 0; j < _height; j++)
{
var index = j + i * _height;
texture.SetPixel(i, _height - 1 - j, _pixels[index]);
}
}
texture.Apply();
}
}
in some other script I call this method with render target passed (cube) and dimensions
textureController.InitColorControl(cube,8);
later on I am chaning individal pixel of the texture like this (this will change all texture pixels to black in the coroutine:
public IEnumerator ChangeAllPixels()
{
var texture = _target.sharedMaterial.mainTexture as Texture2D;
for (int i = 0; i < _pixels.Length; i++)
{
_pixels[i] = Color.black;
}
for (int i = 0; i < _width; i++)
{
for (int j = 0; j < _height; j++)
{
var index = j + i*_height;
texture.SetPixel(i, _height - 1 - j, _pixels[index]);
_target.sharedMaterial.mainTexture = texture;
texture.Apply();
yield return null;
}
}
_target.sharedMaterial.mainTexture = texture;
texture.Apply();
}
I am not that good with algorithms, can somebody help me show me how to change only border pixels (it would be fairly simple for someone experienced)
thanks!