/// <summary> /// Load a PS1 TIM image to a Unity Texture2D. /// </summary> /// <param name="tim">The loaded TIM image.</param> /// <param name="flip">Whether or not we should flip the loaded image.</param> /// <returns></returns> public static Texture2D GetTextureFromTIM(TIM tim, bool flip = true) { // get the image data TimData data = GetImageDataFromTIM(tim); // create a texture with the required format Texture2D tex = new Texture2D(data.Width, data.Height, TextureFormat.ARGB32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Point }; // set the texture's pixels to the TIM pixels tex.SetPixels(TimDataToColors(data, flip)); tex.Apply(); return(tex); }
/// <summary> /// Convert a TimData struct to an array of Colors to be used for setting pixels in a texture. /// </summary> /// <param name="data">The TimData to use.</param> /// <param name="flip">Whether or not to flip the image.</param> /// <returns>The Color array corresponding to this TIM data.</returns> public static Color[] TimDataToColors(TimData data, bool flip = true) { // create the array Color[] imageData = new Color[data.Colors.Length]; // iterate through each pixel and create its entry in the array int i = 0; if (flip) { for (int y = data.Height - 1; y >= 0; y--) { for (int x = 0; x < data.Width; x++) { IColor col = data.Colors[y, x]; imageData[i] = new Color(col.Red, col.Green, col.Blue, col.Alpha); i++; } } } else { for (int y = 0; y < data.Height; y++) { for (int x = 0; x < data.Width; x++) { IColor col = data.Colors[y, x]; imageData[i] = new Color(col.Red, col.Green, col.Blue, col.Alpha); i++; } } } return(imageData); }