private static void CheckForAssimpAsset(string str)
        {
            if (!TriLibCheckPlugins.PluginsLoaded)
            {
                return;
            }
            var extension = Path.GetExtension(str);

            if (extension == null)
            {
                return;
            }
            foreach (var unityExtension in UnityExtensions)
            {
                if (unityExtension == extension.ToLower())
                {
                    return;
                }
            }
            if (AssimpInterop.ai_IsExtensionSupported(extension))
            {
                TriLibAssetImporter.Import(str);
#if TRILIB_OUTPUT_MESSAGES
                Debug.LogFormat("Asset imported: {0}", str);
#endif
            }
        }
Пример #2
0
 private static IntPtr CustomDataCallback(string resourceFilename, int resourceId, ref int fileSize)
 {
     //Checks if our custom mtl file has been requested.
     if (resourceFilename == mtlFilename)
     {
         var mtlBytes = Encoding.UTF8.GetBytes(mtlData);               //Transforms our mtlData into a byte array.
         fileSize = mtlBytes.Length;                                   //Important: sets the fileSize output to our data array length.
         var dataBuffer   = AssimpInterop.LockGc(mtlBytes);            //Lock the GC to don't destroy our array while being used.
         var fileLoadData = AssetLoaderBase.FilesLoadData[resourceId]; //Retrieves the fileLoadData created for this request.
         ((GCFileLoadData)fileLoadData).LockedBuffers.Add(dataBuffer); //Add the locked GC buffer to the request buffers list.
         return(dataBuffer.AddrOfPinnedObject());                      //Returns our locked buffer pointer.
     }
     //Returns a null IntPtr if our custom mtl file hasn't been requested.
     return(IntPtr.Zero);
 }
Пример #3
0
 static TriLibCheckPlugins()
 {
     try
     {
         AssimpInterop.ai_IsExtensionSupported(".3ds");
         PluginsLoaded = true;
     }
     catch (Exception exception)
     {
         if (exception is DllNotFoundException)
         {
             if (EditorUtility.DisplayDialog("TriLib plugins not found", "TriLib was unable to find the native plugins.\n\nIf you just imported the package, you will have to restart Unity editor.\n\nIf you click \"Ask to save changes and restart\", you will be prompted to save your changes (if there is any) then Unity editor will restart.\n\nOtherwise, you will have to save your changes and restart Unity editor manually.", "Ask to save changes and restart", "I will do it manually"))
             {
                 EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
                 var projectPath = Directory.GetParent(Application.dataPath);
                 EditorApplication.OpenProject(projectPath.FullName);
             }
         }
     }
 }
Пример #4
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);
 }
Пример #5
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);
        }
Пример #6
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
            }
        }