//Rotate an image by increments of 90 degrees public static PixelMatrix RotateSq(PixelMatrix original, int rotates) { //No more rotates if (rotates == 0) { return(original); } //Setup new rotated int side = Mathf.Max(original.width, original.height); PixelMatrix rotated = new PixelMatrix(side, side, Color.clear); //Rotate 90º for (int i = 0; i < original.width; i++) { int finalI = original.width - i - 1; for (int j = 0; j < original.height; j++) { rotated.SetPixelSafe(j, finalI, original.GetPixelSafe(i, j)); } } //Call new rotation rotated = RotateSq(rotated, rotates - 1); return(rotated); }
//Add a decal to an image public static PixelMatrix Decal(PixelMatrix original, PixelMatrix decal, int x, int y) { //Offsets to apply int widthOffset = -decal.width / 2 + x; int heightOffset = -decal.height / 2 + y; for (int i = 0; i < decal.width; i++) { //X offset int finalX = i + widthOffset; //Skip column if out of original if (finalX < 0 || finalX > original.width - 1) { continue; } for (int j = 0; j < decal.height; j++) { //Get color Color cl = decal.GetPixelSafe(i, j); //Skip transparent pixels //TODO Blend with transparency? if (cl.a < 0.7) { continue; } //Y offset int finalY = j + heightOffset; //Apply color original.SetPixel(finalX, finalY, cl); } } return(original); }