Hi! I am having some problem understanding bit masking…hoping to get some help.
here is the method i came up with to change color of the texture based on bit pattern:
note:
texture dimensions are 4 x 4
int 12 corresponds to 1100 so my understanding was that lines should yield following:
but result I am having is that everywhere is false thus rendering all pixels yellow.
Also, originally i wanted to have a text file with bit mask as strings like this
0010
1100
1110
0000
and later to use that as mask but I didn’t know how to convert this to appropriate value, i would need
some help with that as well!
Thanks!
public void Mark()
{
var texture = _target.sharedMaterial.mainTexture as Texture2D;
int bits = 12;
for (int i = 0; i < _height; i++)
{
for (int j = 0; j < _width; j++)
{
SetPixel(i, j, (bits & 1) == 1 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 2) == 1 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 4) == 1 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 8) == 1 ? Color.black : Color.yellow, texture);
}
}
}
private void SetPixel(int row, int column, Color color,Texture2D texture)
{
texture.SetPixel(row, column, color);
texture.Apply();
}
I fixed the code and still doesn’t work, but i think it’s the logic that is wrong not bit shifts. texture becomes either yellow or black there is no individual pixels set (i was trying to find out the value in bits)
If this is the complete code another problem is that you make the same comparisons in every step of the loop. So I would expect the last line (SetPixel(i, j, (bits & 8) == 8 ? Color.black : Color.yellow, texture) always wins.
Maybe you forgot to change bits in the loop?