Beispiel #1
0
        public void TestTextureToBytesRoundtrip()
        {
            // Get the texture.
            string    imageFile = "Assets/Editor/Tests/TestData/TiltBrushLogo.jpg";
            Texture2D texture   = AssetDatabase.LoadAssetAtPath <Texture2D>(imageFile);

            // Reconstruct the texture.
            byte[]    textureBytes         = ImageCache.BytesFromTexture(texture);
            Texture2D reconstructedTexture = ImageCache.TextureFromBytes(textureBytes);

            // Compare the textures.
            CompareTextures(texture, reconstructedTexture);
        }
Beispiel #2
0
        // Attempts to load the icon from the cache. Returns true if successful.
        public bool RequestLoadIconCache()
        {
            if (m_Icon == null)
            {
                // Try to load from cache.
                m_Icon = ImageCache.LoadIconCache(FilePath, out m_ImageAspect);
                if (m_Icon != null)
                {
                    m_State = ImageState.Ready;
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
 /// Returns a full-resolution Texture2D.
 /// The lifetime of this texture is handled by ReferenceImage, so should not be destroyed
 /// by the user of this method.
 public void AcquireImageFullsize(bool runForeground = false)
 {
     m_FullSizeReferences++;
     if (m_FullSizeReferences == 1)
     {
         // Try the cache first.
         m_FullSize = ImageCache.LoadImageCache(FilePath);
         if (m_FullSize == null)
         {
             // Otherwise, this will generate a cache.
             m_FullSize = Object.Instantiate(Icon);
             var co = LoadImage(FilePath, m_FullSize, runForeground).GetEnumerator();
             App.Instance.StartCoroutine(co);
         }
     }
 }
Beispiel #4
0
        // Like RequestLoadCoroutine, but allowed to use main thread CPU time
        IEnumerator <Timeslice> RequestLoadCoroutineMainThread()
        {
            // On main thread! Can decode images using WWW class. This is about 10x faster
            using (WWW loader = new WWW(PathToWwwUrl(m_Path)))
            {
                while (!loader.isDone)
                {
                    yield return(null);
                }
                if (string.IsNullOrEmpty(loader.error))
                {
                    // Passing in a texture with mipmapCount > 1 is how you ask for mips
                    // from WWW.LoadImageIntoTexture
                    Texture2D inTex = new Texture2D(2, 2, TextureFormat.RGBA32, true);
                    loader.LoadImageIntoTexture(inTex);
                    DownsizeTexture(inTex, ref m_Icon, ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION);
                    m_Icon.wrapMode = TextureWrapMode.Clamp;
                    m_ImageAspect   = (float)inTex.width / inTex.height;
                    ImageCache.SaveIconCache(m_Icon, FilePath, m_ImageAspect);
                    yield return(null);

                    // Create the full size image cache as well.
                    int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
                    if (inTex.width > resizeLimit || inTex.height > resizeLimit)
                    {
                        Texture2D resizedTex = new Texture2D(2, 2, TextureFormat.RGBA32, true);
                        DownsizeTexture(inTex, ref resizedTex, resizeLimit);
                        ImageCache.SaveImageCache(resizedTex, m_Path);
                        Object.Destroy(resizedTex);
                    }
                    else
                    {
                        ImageCache.SaveImageCache(inTex, m_Path);
                    }
                    Object.Destroy(inTex);
                    m_State = ImageState.Ready;
                    yield break;
                }
            }

            // OK, take the slower path instead.
            foreach (var ret in RequestLoadCoroutine())
            {
                yield return(ret);
            }
        }
        void Awake()
        {
            m_Instance       = this;
            m_RequestedLoads = new Stack <int>();

            App.InitMediaLibraryPath();
            App.InitReferenceImagePath(m_DefaultImages);
            m_ReferenceDirectory = App.ReferenceImagePath();

            if (Directory.Exists(m_ReferenceDirectory))
            {
                m_FileWatcher = new FileWatcher(m_ReferenceDirectory);
                m_FileWatcher.NotifyFilter        = NotifyFilters.LastWrite;
                m_FileWatcher.FileChanged        += OnChanged;
                m_FileWatcher.FileCreated        += OnChanged;
                m_FileWatcher.FileDeleted        += OnChanged;
                m_FileWatcher.EnableRaisingEvents = true;
            }

            ImageCache.DeleteObsoleteCaches();

            m_Images = new List <ReferenceImage>();
            ProcessReferenceDirectory(userOverlay: false);
        }
Beispiel #6
0
        IEnumerable <Timeslice> RequestLoadCoroutine()
        {
            var reader = new ThreadedImageReader(m_Path,
                                                 ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION,
                                                 App.PlatformConfig.ReferenceImagesMaxDimension);

            while (!reader.Finished)
            {
                yield return(null);
            }

            RawImage result = null;

            try
            {
                result = reader.Result;
            }
            catch (FutureFailed e)
            {
                ImageLoadError imageLoad = e.InnerException as ImageLoadError;
                if (imageLoad != null)
                {
                    ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true);
                }
            }

            if (result != null)
            {
                while (ReferenceImageCatalog.m_Instance.TexturesCreatedThisFrame >=
                       ReferenceImageCatalog.TEXTURE_CREATIONS_PER_FRAME)
                {
                    yield return(null);
                }

                if (m_Icon == null)
                {
                    m_Icon = new Texture2D(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true);
                }
                else
                {
                    m_Icon.Resize(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true);
                }
                m_ImageAspect   = result.ColorAspect;
                m_Icon.wrapMode = TextureWrapMode.Clamp;
                m_Icon.SetPixels32(result.ColorData);
                m_Icon.Apply();
                ReferenceImageCatalog.m_Instance.TexturesCreatedThisFrame++;
                reader = null;
                yield return(null);
            }
            else
            {
                // Problem reading the file?
                m_State = ImageState.Error;
                reader  = null;
                yield break;
            }

            m_State = ImageState.Ready;
            ImageCache.SaveIconCache(m_Icon, FilePath, m_ImageAspect);
        }
Beispiel #7
0
        // Helper for GetImageFullsize
        IEnumerable LoadImage(string path, Texture2D dest, bool runForeground = false)
        {
            // Temporarily increase the reference count during loading to prevent texture destruction if
            // ReturnImageFullSize is called during load.
            m_FullSizeReferences++;
            var reader = new ThreadedImageReader(path, -1,
                                                 App.PlatformConfig.ReferenceImagesMaxDimension);

            while (!reader.Finished)
            {
                if (!runForeground)
                {
                    yield return(null);
                }
            }

            RawImage result = null;

            try
            {
                result = reader.Result;
                if (result != null && dest != null)
                {
                    int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
                    if (result.ColorWidth > resizeLimit || result.ColorHeight > resizeLimit)
                    {
                        // Resize the image to the resize limit before saving it to the dest texture.
                        var tempTexture = new Texture2D(
                            result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true);
                        tempTexture.SetPixels32(result.ColorData);
                        tempTexture.Apply();
                        DownsizeTexture(tempTexture, ref dest, resizeLimit);
                        Object.Destroy(tempTexture);
                    }
                    else
                    {
                        // Save the the image to the dest texture.
                        dest.Resize(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true);
                        dest.SetPixels32(result.ColorData);
                        dest.Apply();
                    }

                    // Cache the texture.
                    ImageCache.SaveImageCache(dest, path);
                }
            }
            catch (FutureFailed e)
            {
                ImageLoadError imageLoad = e.InnerException as ImageLoadError;
                if (imageLoad != null)
                {
                    ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true);
                }
            }
            finally
            {
                // Reduce the reference count again. This ensures the image gets properly released if
                // ReturnImageFullSize is called before loading finished.
                ReleaseImageFullsize();
            }
        }