Ejemplo n.º 1
0
 /// <summary>
 /// Loads the embedded texture data.
 /// </summary>
 /// <returns><c>true</c>, if embedded texture data was loaded, <c>false</c> otherwise.</returns>
 /// <param name="scene">Scene.</param>
 /// <param name="path">Path.</param>
 /// <param name="finalPath">Outputs the final path.</param>
 /// <param name="data">Outputs the data.</param>
 /// <param name="isRawData">Outputs <c>true</c> if data is uncompressed, <c>false</c> otherwise.</param>
 /// <param name="width">Outputs the texture width.</param>
 /// <param name="height">Outputs the texture height.</param>
 public static bool LoadEmbeddedTextureData(IntPtr scene, string path, out string finalPath, out byte[] data, out bool isRawData, out int width, out int height)
 {
     if (scene != IntPtr.Zero && !string.IsNullOrEmpty(path))
     {
         var texture = AssimpInterop.aiScene_GetEmbeddedTexture(scene, path);
         if (texture != IntPtr.Zero)
         {
             isRawData = !AssimpInterop.aiMaterial_IsEmbeddedTextureCompressed(scene, texture);
             var dataLength = AssimpInterop.aiMaterial_GetEmbeddedTextureDataSize(scene, texture, !isRawData);
             data      = AssimpInterop.aiMaterial_GetEmbeddedTextureData(scene, texture, dataLength);
             width     = AssimpInterop.aiMaterial_GetEmbeddedTextureWidth(texture);
             height    = AssimpInterop.aiMaterial_GetEmbeddedTextureHeight(texture);
             finalPath = Path.GetFileNameWithoutExtension(path);
             return(true);
         }
     }
     finalPath = null;
     data      = new byte[0];
     isRawData = false;
     width     = 0;
     height    = 0;
     return(false);
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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
            }
        }