public static Texture2D LoadTextureOIIO(string filePath, bool isLinear) { // TODO: need to flip? Repere bottom left, Y-up int ret = OIIOAPI.oiio_open_image(filePath); if (ret == 0) { Debug.LogWarning("Could not open image " + filePath + " with OIIO."); return(null); } int width = -1; int height = -1; int nchannels = -1; OIIOAPI.BASETYPE format = OIIOAPI.BASETYPE.NONE; ret = OIIOAPI.oiio_get_image_info(ref width, ref height, ref nchannels, ref format); if (ret == 0) { Debug.LogWarning("Could not get info about image " + filePath + " with OIIO"); return(null); } TexConv conv = new TexConv(); bool canConvert = Format2Format(format, nchannels, ref conv); if (!canConvert) { Debug.LogWarning("Could not create image from format: " + conv.format + " with option: " + conv.options); return(CreateSmallImage()); } // TMP else if (conv.options.HasFlag(TextureConversionOptions.SHORT_TO_FLOAT) || conv.options.HasFlag(TextureConversionOptions.SHORT_TO_INT)) { Debug.LogWarning("Could not create image from format: " + conv.format + " with option: " + conv.options); return(CreateSmallImage()); } Texture2D image = new Texture2D(width, height, conv.format, true, isLinear); // with mips var pixels = image.GetRawTextureData(); GCHandle handle = GCHandle.Alloc(pixels, GCHandleType.Pinned); ret = OIIOAPI.oiio_fill_image_data(handle.AddrOfPinnedObject(), conv.options.HasFlag(TextureConversionOptions.RGB_TO_RGBA) ? 1 : 0); if (ret == 1) { image.LoadRawTextureData(pixels); image.Apply(); } else { Debug.LogWarning("Could not fill texture data of " + filePath + " with OIIO."); return(null); } return(image); }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { // NOTE: repere bas gauche, Y up. int ret = OIIOAPI.oiio_open_image(assetPath); if (ret == 0) { Debug.Log("could not open " + assetPath); return; } int width = -1; int height = -1; int nchannels = -1; OIIOAPI.BASETYPE format = OIIOAPI.BASETYPE.NONE; ret = OIIOAPI.oiio_get_image_info(ref width, ref height, ref nchannels, ref format); if (ret == 0) { Debug.Log("Could not get width/height of " + assetPath); return; } imageDimensions.Set(width, height); TextureFormat textureFormat = Format2Format(format, nchannels); var image = new Texture2D(width, height, textureFormat, false, true); // with mips, linear int do_rgb_to_rgba = 0; if ((format == OIIOAPI.BASETYPE.FLOAT && nchannels == 3) || (format == OIIOAPI.BASETYPE.HALF && nchannels == 3)) { do_rgb_to_rgba = 1; } //Color[] pixels = image.GetPixels(); var pixels = image.GetRawTextureData(); GCHandle handle = GCHandle.Alloc(pixels, GCHandleType.Pinned); ret = OIIOAPI.oiio_fill_image_data(handle.AddrOfPinnedObject(), do_rgb_to_rgba); if (ret == 1) { image.LoadRawTextureData(pixels); //image.SetPixels(pixels); image.Apply(); } else { Debug.Log("Could not fill texture data of " + assetPath); return; } #if UNITY_2017_3_OR_NEWER var filename = Path.GetFileNameWithoutExtension(assetPath); ctx.AddObjectToAsset(filename, image); ctx.SetMainObject(image); #else ctx.SetMainObject(image); #endif }