I want to show the percentage of painted surface on the wall. Here is the code I used to paint the wall.
private void MyPaintOn(Vector2 textureCoord, float[,] splashTexture, Color targetColor)
{
if (m_isEnabled)
{
lock (m_lockFlag)
{
Stopwatch sw = new Stopwatch();
sw.Start();
int reqnx = splashTexture.GetLength(0);
int reqny = splashTexture.GetLength(1);
int reqX = (int)(textureCoord.x * textureSize) - (reqnx / 2);
int reqY = (int)(textureCoord.y * textureSize) - (reqny / 2);
int right = m_texture.width - 1;
int bottom = m_texture.height - 1;
int x = IntMax(reqX, 0);
int y = IntMax(reqY, 0);
int nx = IntMin(x + reqnx, right) - x;
int ny = IntMin(y + reqny, bottom) - y;
Color[] pixels = m_texture.GetPixels(x, y, nx, ny);
int counter = 0;
for (int i = 0; i < nx; ++i)
{
for (int j = 0; j < ny; ++j)
{
float currAlpha = splashTexture[i, j];
if (currAlpha == 1)
pixels[counter] = targetColor;
else
{
Color currColor = pixels[counter];
// resulting color is an addition of splash texture to the texture based on alpha
Color newColor = Color.Lerp(currColor, targetColor, currAlpha);
// but resulting alpha is a sum of alphas (adding transparent color should not make base color more transparent)
newColor.a = pixels[counter].a + currAlpha;
pixels[counter] = newColor;
}
counter++;
}
m_texture.SetPixels(x, y, nx, ny, pixels);
m_texture.Apply();
sw.Stop();
}
}
}
}