コード例 #1
0
        public void RefreshBlurBackground()
        {
            DestroyBackgroundTexture();

            var windowRect = this.position;
            var position   = windowRect.position;
            var width      = (int)windowRect.width;
            var height     = (int)windowRect.height;

            if (width <= 0 || height <= 0)
            {
                return;
            }

            var pixels  = UnityEditorInternal.InternalEditorUtility.ReadScreenPixel(position, width, height);
            var baseTex = new Texture2D(width, height, TextureFormat.RGBA32, false);

            baseTex.hideFlags = HideFlags.DontSave;
            baseTex.SetPixels(pixels);
            baseTex.Apply();

            var blurOptions = new BlurOptions();
            var blurTexture = BlurUtility.BlurTexture(baseTex, blurOptions);

            Texture2D.DestroyImmediate(baseTex);
            backgroundTexture_ = blurTexture;
        }
コード例 #2
0
ファイル: BlurUtility.cs プロジェクト: yaustar/BCoTankGame
        public static Texture BlurTexture(Texture sourceTexture, BlurOptions options)
        {
            var blurMaterial = new Material(Shader.Find("Hidden/QuickSearch-Blur"));

            blurMaterial.SetColor("_Tint", options.tint);
            blurMaterial.SetFloat("_Tinting", options.tinting);
            blurMaterial.SetFloat("_BlurSize", options.blurSize);

            var destTexture = new RenderTexture(sourceTexture.width, sourceTexture.height, 0);

            destTexture.Create();

            var active = RenderTexture.active;

            try {
                var tempA = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
                var tempB = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);

                for (int i = 0; i < options.passes; i++)
                {
                    if (i == 0)
                    {
                        Graphics.Blit(sourceTexture, tempA, blurMaterial, 0);
                    }
                    else
                    {
                        Graphics.Blit(tempB, tempA, blurMaterial, 0);
                    }
                    Graphics.Blit(tempA, tempB, blurMaterial, 1);
                }

                Graphics.Blit(tempB, destTexture, blurMaterial, 2);

                RenderTexture.ReleaseTemporary(tempA);
                RenderTexture.ReleaseTemporary(tempB);
            } catch (Exception e) {
                Debug.LogException(e);
            } finally {
                RenderTexture.active = active;                 // Restore
            }

            Material.DestroyImmediate(blurMaterial);

            return(destTexture);
        }