public static byte[] GenerateMipmaps(RGBImage image) { int width = image.Width; int height = image.Height; if (!MathUtils.IsPowerOfTwo(width) || !MathUtils.IsPowerOfTwo(height)) { throw new Exception("Image dimensions must by power of 2"); } long size = ComputeTextureSize(width, height, TextureCompressionFormat.Uncompressed); byte[] result = new byte[size]; image.CopyRawData(result); Debug.Log($"Generated level {0} mipmap ({image.Size} bytes, width: {width}, height: {height})"); GenerateMipmaps(image, result, image.Size, 1); return(result); }
private static void GenerateMipmaps(RGBImage image, byte[] result, long resultIndex, int level) { int mipWidth = MathUtils.Clamp(image.Width >> 1, 1); int mipHeight = MathUtils.Clamp(image.Height >> 1, 1); RGBImage mipImage = new RGBImage(mipWidth, mipHeight); for (int x = 0; x < mipWidth; x++) { for (int y = 0; y < mipHeight; y++) { mipImage.SetPixel(x, y, image.GetAverage(x << 1, y << 1, 2, ImageBoundaryMode.Wrap)); } } mipImage.CopyRawData(result, resultIndex); Debug.Log($"Generated level {level++} mipmap ({mipImage.Size} bytes, width: {mipWidth}, height: {mipHeight})"); if (mipWidth == 1 && mipHeight == 1) { return; } GenerateMipmaps(mipImage, result, mipImage.Size + resultIndex, level); }