I have a script which uses raycasting to the mouse point to paint over a mask in the material to remove dirt on a item in game and it works in the editor fine but after I build it doesn't work
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit raycastHit))
{
Vector2 textureCoord = raycastHit.textureCoord;
int pixelX = (int)(textureCoord.x * dirtMaskTexture.width);
int pixelY = (int)(textureCoord.y * dirtMaskTexture.height);
Vector2Int paintPixelPosition = new Vector2Int(pixelX, pixelY);
Debug.Log("UV: " + textureCoord + "; Pixels: " + paintPixelPosition);
int paintPixelDistance = Mathf.Abs(paintPixelPosition.x - lastPaintPixelPosition.x) + Mathf.Abs(paintPixelPosition.y - lastPaintPixelPosition.y);
int maxPaintDistance = 7;
if (paintPixelDistance < maxPaintDistance)
{
// Painting too close to last position
return;
}
lastPaintPixelPosition = paintPixelPosition;
int pixelXOffset = pixelX - (dirtBrush.width / 2);
int pixelYOffset = pixelY - (dirtBrush.height / 2);
for (int x = 0; x < dirtBrush.width; x++)
{
for (int y = 0; y < dirtBrush.height; y++)
{
Color pixelDirt = dirtBrush.GetPixel(x, y);
Color pixelDirtMask = dirtMaskTexture.GetPixel(pixelXOffset + x, pixelYOffset + y);
float removedAmount = pixelDirtMask.g - (pixelDirtMask.g * pixelDirt.g);
dirtMaskTexture.SetPixel(
pixelXOffset + x,
pixelYOffset + y,
new Color(0, pixelDirtMask.g * pixelDirt.g, 0)
);
}
}
dirtMaskTexture.Apply();
}
After adding
Debug.Log("UV: " + textureCoord + "; Pixels: " + paintPixelPosition);
in unity this is what I get and how it should look when mouse raycasts onto the objects UV
UV: (0.49, 0.50)Pixels: (252, 257)
This is what I get in build
UV: (0.00, 0.00)Pixels: (0, 0)