public ITextureLoadHandle Load(byte[] data) { TextureLoadHandle textureLoadHandle = new TextureLoadHandle(data); createTextureQueue.Enqueue(textureLoadHandle); return(textureLoadHandle); }
/// <summary> /// Loads a <see cref="UnityEngine.Texture2D"/> from an external source. /// </summary> /// <param name="scene">Scene where the texture belongs.</param> /// <param name="path">Path to load the texture data.</param> /// <param name="name">Name of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="material"><see cref="UnityEngine.Material"/> to assign the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="propertyName"><see cref="UnityEngine.Material"/> property name to assign to the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="checkAlphaChannel">If True, checks every image pixel to determine if alpha channel is being used and sets this value.</param> /// <param name="textureWrapMode">Wrap mode of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="basePath">Base path to lookup for the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="onTextureLoaded">Event to trigger when the <see cref="UnityEngine.Texture2D"/> finishes loading.</param> /// <param name="textureCompression">Texture loading compression level.</param> /// <param name="textureFileNameWithoutExtension">Texture filename without the extension.</param> /// <param name="isNormalMap">Is the Texture a Normal Map?</param> /// <returns>The loaded <see cref="UnityEngine.Texture2D"/>.</returns> public static Texture2D LoadTextureFromFile(IntPtr scene, string path, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string basePath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, string textureFileNameWithoutExtension = null, bool isNormalMap = false) { if (string.IsNullOrEmpty(path)) { return(null); } bool assimpUncompressed; string finalPath; byte[] data; var texture = AssimpInterop.aiScene_GetEmbeddedTexture(scene, path); if (texture != IntPtr.Zero) { assimpUncompressed = !AssimpInterop.aiMaterial_IsEmbeddedTextureCompressed(scene, texture); var dataLength = AssimpInterop.aiMaterial_GetEmbeddedTextureDataSize(scene, texture, !assimpUncompressed); data = AssimpInterop.aiMaterial_GetEmbeddedTextureData(scene, texture, dataLength); finalPath = Path.GetFileNameWithoutExtension(path); } else { string filename = null; finalPath = path; data = FileUtils.LoadFileData(finalPath); if (data.Length == 0 && basePath != null) { finalPath = Path.Combine(basePath, path); } data = FileUtils.LoadFileData(finalPath); if (data.Length == 0) { filename = Path.GetFileName(path); finalPath = filename; } data = FileUtils.LoadFileData(finalPath); if (data.Length == 0 && basePath != null && filename != null) { finalPath = Path.Combine(basePath, filename); } data = FileUtils.LoadFileData(finalPath); if (data.Length == 0) { #if ASSIMP_OUTPUT_MESSAGES Debug.LogWarningFormat("Texture '{0}' not found", path); #endif return(null); } assimpUncompressed = false; } bool loaded; Texture2D tempTexture2D; if (assimpUncompressed) { //TODO: additional DLL methods to load actual resolution var textureResolution = Mathf.FloorToInt(Mathf.Sqrt(data.Length / 4)); tempTexture2D = new Texture2D(textureResolution, textureResolution, TextureFormat.ARGB32, true); tempTexture2D.LoadRawTextureData(data); tempTexture2D.Apply(); loaded = true; } else { #if USE_DEVIL && (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN) loaded = IlLoader.LoadTexture2DFromByteArray(data, data.Length, out tempTexture2D); #else tempTexture2D = new Texture2D(2, 2, TextureFormat.RGBA32, true); loaded = tempTexture2D.LoadImage(data); #endif } tempTexture2D.name = name; tempTexture2D.wrapMode = textureWrapMode; if (loaded) { var colors = tempTexture2D.GetPixels32(); var finalTexture2D = new Texture2D(tempTexture2D.width, tempTexture2D.height, TextureFormat.ARGB32, true); if (isNormalMap) { for (var i = 0; i < colors.Length; i++) { var color = colors[i]; color.a = color.r; color.r = 0; color.b = 0; colors[i] = color; } finalTexture2D.SetPixels32(colors); finalTexture2D.Apply(); } else { finalTexture2D.SetPixels32(colors); finalTexture2D.Apply(); if (textureCompression != TextureCompression.None) { tempTexture2D.Compress(textureCompression == TextureCompression.HighQuality); } } if (checkAlphaChannel) { checkAlphaChannel = false; foreach (var color in colors) { if (color.a != 255) { checkAlphaChannel = true; break; } } } if (material != null) { material.SetTexture(propertyName, finalTexture2D); } if (onTextureLoaded != null) { onTextureLoaded(finalPath, material, propertyName, finalTexture2D); } return(finalTexture2D); } else { #if ASSIMP_OUTPUT_MESSAGES Debug.LogErrorFormat("Unable to load texture '{0}'", path); #endif } return(null); }
/// <summary> /// Loads a <see cref="UnityEngine.Texture2D"/> from a local source. /// </summary> /// <param name="scene">Scene where the texture belongs.</param> /// <param name="path">Path to load the texture data.</param> /// <param name="name">Name of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="material"><see cref="UnityEngine.Material"/> to assign the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="propertyName"><see cref="UnityEngine.Material"/> property name to assign to the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="checkAlphaChannel">If True, checks every image pixel to determine if alpha channel is being used and sets this value.</param> /// <param name="textureWrapMode">Wrap mode of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="basePath">Base path to lookup for the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="onTextureLoaded">Event to trigger when the <see cref="UnityEngine.Texture2D"/> finishes loading.</param> /// <param name="textureCompression">Texture loading compression level.</param> /// <param name="textureFileNameWithoutExtension">Texture filename without the extension.</param> /// <param name="isNormalMap">Is the Texture a Normal Map?</param> /// <returns>The loaded <see cref="UnityEngine.Texture2D"/>.</returns> public static Texture2D LoadTextureFromFile(IntPtr scene, string path, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string basePath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, string textureFileNameWithoutExtension = null, bool isNormalMap = false) { if (scene == IntPtr.Zero || string.IsNullOrEmpty(path)) { return(null); } string finalPath; byte[] data; bool isRawData; var width = 0; var height = 0; if (!LoadEmbeddedTextureData(scene, path, out finalPath, out data, out isRawData, out width, out height)) { string filename = null; finalPath = path; data = FileUtils.LoadFileData(finalPath); if (data.Length == 0 && basePath != null) { finalPath = Path.Combine(basePath, path); data = FileUtils.LoadFileData(finalPath); } if (data.Length == 0) { filename = FileUtils.GetFilename(path); finalPath = filename; data = FileUtils.LoadFileData(finalPath); } if (data.Length == 0 && basePath != null && filename != null) { finalPath = Path.Combine(basePath, filename); data = FileUtils.LoadFileData(finalPath); } if (data.Length == 0) { #if TRILIB_OUTPUT_MESSAGES || ASSIMP_OUTPUT_MESSAGES Debug.LogWarningFormat("Texture '{0}' not found", path); #endif return(null); } } Texture2D tempTexture2D; if (ApplyTextureData(data, isRawData, out tempTexture2D, width, height)) { return(ProccessTextureData(tempTexture2D, name, material, propertyName, ref checkAlphaChannel, textureWrapMode, finalPath, onTextureLoaded, textureCompression, isNormalMap)); } #if TRILIB_OUTPUT_MESSAGES || ASSIMP_OUTPUT_MESSAGES Debug.LogErrorFormat("Unable to load texture '{0}'", path); #endif return(null); }
/// <summary> /// Proccesses the texture. /// </summary> /// <returns>The texture.</returns> /// <param name="tempTexture2D">Temp <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="name">Name.</param> /// <param name="material">Material.</param> /// <param name="propertyName">Property name.</param> /// <param name="checkAlphaChannel">Check alpha channel.</param> /// <param name="textureWrapMode">Texture wrap mode.</param> /// <param name="finalPath">Final path.</param> /// <param name="onTextureLoaded">On texture loaded.</param> /// <param name="textureCompression">Texture compression.</param> /// <param name="isNormalMap">If set to <c>true</c> is normal map.</param> public static Texture2D ProccessTextureData(Texture2D tempTexture2D, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string finalPath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, bool isNormalMap = false) { if (tempTexture2D == null) { return(null); } tempTexture2D.name = name; tempTexture2D.wrapMode = textureWrapMode; var colors = tempTexture2D.GetPixels32(); Texture2D finalTexture2D; if (isNormalMap) { #if UNITY_5 finalTexture2D = new Texture2D(tempTexture2D.width, tempTexture2D.height, TextureFormat.ARGB32, true); for (var i = 0; i < colors.Length; i++) { var color = colors[i]; color.a = color.r; color.r = 0; color.b = 0; colors[i] = color; } #else finalTexture2D = Texture2D.Instantiate(AssetLoader.NormalBaseTexture); finalTexture2D.filterMode = tempTexture2D.filterMode; finalTexture2D.wrapMode = tempTexture2D.wrapMode; finalTexture2D.Resize(tempTexture2D.width, tempTexture2D.height); #endif finalTexture2D.SetPixels32(colors); finalTexture2D.Apply(); } else { finalTexture2D = new Texture2D(tempTexture2D.width, tempTexture2D.height, TextureFormat.ARGB32, true); finalTexture2D.SetPixels32(colors); finalTexture2D.Apply(); if (textureCompression != TextureCompression.None) { tempTexture2D.Compress(textureCompression == TextureCompression.HighQuality); } } if (checkAlphaChannel) { checkAlphaChannel = false; foreach (var color in colors) { if (color.a != 255) { checkAlphaChannel = true; break; } } } if (material != null) { material.SetTexture(propertyName, finalTexture2D); } if (onTextureLoaded != null) { onTextureLoaded(finalPath, material, propertyName, finalTexture2D); } return(finalTexture2D); }
/// <summary> /// Loads a <see cref="UnityEngine.Texture2D"/> from memory. /// </summary> /// <param name="data">Texture data.</param> /// <param name="path">Path to load the texture data.</param> /// <param name="material"><see cref="UnityEngine.Material"/> to assign the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="propertyName"><see cref="UnityEngine.Material"/> property name to assign to the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="checkAlphaChannel">If True, checks every image pixel to determine if alpha channel is being used and sets this value.</param> /// <param name="textureWrapMode">Wrap mode of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="onTextureLoaded">Event to trigger when the <see cref="UnityEngine.Texture2D"/> finishes loading.</param> /// <param name="textureCompression">Texture loading compression level.</param> /// <param name="isNormalMap">Is the Texture a Normal Map?</param> /// <param name="isRawData">Is the data in raw format?</param> /// <param name="width">Texture width.</param> /// <param name="height">Texture height.</param> /// <returns>The loaded <see cref="UnityEngine.Texture2D"/>.</returns> public static Texture2D LoadTextureFromMemory(byte[] data, string path, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, bool isNormalMap = false, bool isRawData = false, int width = 0, int height = 0) { if (data.Length == 0 || string.IsNullOrEmpty(path)) { return(null); } Texture2D tempTexture2D; if (ApplyTextureData(data, isRawData, out tempTexture2D, width, height)) { return(ProccessTextureData(tempTexture2D, StringUtils.GenerateUniqueName(path.GetHashCode()), material, propertyName, ref checkAlphaChannel, textureWrapMode, null, onTextureLoaded, textureCompression, isNormalMap)); } #if TRILIB_OUTPUT_MESSAGES || ASSIMP_OUTPUT_MESSAGES Debug.LogErrorFormat("Unable to load texture '{0}'", path); #endif return(null); }
/// <summary> /// Loads a <see cref="UnityEngine.Texture2D"/> from an external source. /// </summary> /// <param name="scene">Scene where the texture belongs.</param> /// <param name="path">Path to load the texture data.</param> /// <param name="name">Name of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="material"><see cref="UnityEngine.Material"/> to assign the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="propertyName"><see cref="UnityEngine.Material"/> property name to assign to the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="textureWrapMode">Wrap mode of the <see cref="UnityEngine.Texture2D"/> to be created.</param> /// <param name="basePath">Base path to lookup for the <see cref="UnityEngine.Texture2D"/>.</param> /// <param name="onTextureLoaded">Event to trigger when the <see cref="UnityEngine.Texture2D"/> finishes loading.</param> /// <param name="textureCompression">Texture loading compression level.</param> /// <param name="textureFileNameWithoutExtension">Texture filename without the extension.</param> public static void LoadTextureFromFile(IntPtr scene, string path, string name, Material material, string propertyName, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string basePath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, string textureFileNameWithoutExtension = null) { if (string.IsNullOrEmpty(path)) { return; } bool assimpUncompressed; string finalPath; byte[] data; if (path[0] == '*') { UInt32 slotIndex; if (UInt32.TryParse(path.Substring(1), out slotIndex)) { assimpUncompressed = !AssimpInterop.aiMaterial_IsEmbeddedTextureCompressed(scene, slotIndex); var dataLength = AssimpInterop.aiMaterial_GetEmbeddedTextureDataSize(scene, slotIndex, !assimpUncompressed); data = AssimpInterop.aiMaterial_GetEmbeddedTextureData(scene, slotIndex, dataLength); } else { #if ASSIMP_OUTPUT_MESSAGES Debug.LogWarningFormat("Unable to process embedded texture '{0}'", path); #endif return; } finalPath = StringUtils.GenerateUniqueName(path); } else { finalPath = path; if (!File.Exists(finalPath)) { if (basePath != null) { finalPath = Path.Combine(basePath, path); } } if (!File.Exists(finalPath)) { var filename = Path.GetFileName(path); if (basePath != null) { finalPath = Path.Combine(basePath, filename); } } if (!File.Exists(finalPath)) { #if ASSIMP_OUTPUT_MESSAGES Debug.LogWarningFormat("Texture '{0}' not found", path); #endif return; } data = File.ReadAllBytes(finalPath); assimpUncompressed = false; } bool loaded; Texture2D texture2D; if (assimpUncompressed) { //TODO: additional DLL methods to load actual resolution var textureResolution = Mathf.FloorToInt(Mathf.Sqrt(data.Length / 4)); texture2D = new Texture2D(textureResolution, textureResolution, TextureFormat.ARGB32, false); texture2D.LoadRawTextureData(data); texture2D.Apply(); loaded = true; } else { #if USE_DEVIL && (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN) loaded = IlLoader.LoadTexture2DFromByteArray(data, data.Length, out texture2D); #else texture2D = new Texture2D(2, 2, TextureFormat.RGBA32, false); loaded = texture2D.LoadImage(data); #endif } texture2D.name = name; texture2D.wrapMode = textureWrapMode; if (loaded) { if (textureCompression != TextureCompression.None) { texture2D.Compress(textureCompression == TextureCompression.HighQuality); } material.SetTexture(propertyName, texture2D); if (onTextureLoaded != null) { onTextureLoaded(finalPath, material, propertyName, texture2D); } } else { #if ASSIMP_OUTPUT_MESSAGES Debug.LogErrorFormat("Unable to load texture '{0}'", path); #endif } }