Exemple #1
0
        RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigmaX, float sigmaY, RenderLayer parentLayer)
        {
            sigmaX = BlurUtils.adjustSigma(sigmaX, out var scaleFactorX, out var radiusX);
            sigmaY = BlurUtils.adjustSigma(sigmaY, out var scaleFactorY, out var radiusY);

            var textureWidth = Mathf.CeilToInt((float)maskLayer.width / scaleFactorX);

            if (textureWidth < 1)
            {
                textureWidth = 1;
            }

            var textureHeight = Mathf.CeilToInt((float)maskLayer.height / scaleFactorY);

            if (textureHeight < 1)
            {
                textureHeight = 1;
            }

            var blurXLayer = RenderLayer.create(
                rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
                width: textureWidth,
                height: textureHeight,
                layerBounds: maskLayer.layerBounds,
                filterMode: FilterMode.Bilinear,
                noMSAA: true
                );

            parentLayer.addLayer(blurXLayer);

            var blurYLayer = RenderLayer.create(
                rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
                width: textureWidth,
                height: textureHeight,
                layerBounds: maskLayer.layerBounds,
                filterMode: FilterMode.Bilinear,
                noMSAA: true
                );

            parentLayer.addLayer(blurYLayer);

            var blurMesh = ImageMeshGenerator.imageMesh(null, uiRectHelper.one, maskLayer.layerBounds);

            var kernelX = BlurUtils.get1DGaussianKernel(sigmaX, radiusX);
            var kernelY = BlurUtils.get1DGaussianKernel(sigmaY, radiusY);

            blurXLayer.draws.Add(CanvasShader.maskFilter(
                                     blurXLayer, blurMesh, maskLayer,
                                     radiusX, new Vector2(1f / textureWidth, 0), kernelX));

            blurYLayer.draws.Add(CanvasShader.maskFilter(
                                     blurYLayer, blurMesh.duplicate(), blurXLayer,
                                     radiusY, new Vector2(0, -1f / textureHeight), kernelY));

            return(blurYLayer);
        }
Exemple #2
0
        RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigma, RenderLayer parentLayer)
        {
            sigma = BlurUtils.adjustSigma(sigma, out var scaleFactor, out var radius);

            var textureWidth = Mathf.CeilToInt((float)maskLayer.width / scaleFactor);

            if (textureWidth < 1)
            {
                textureWidth = 1;
            }

            var textureHeight = Mathf.CeilToInt((float)maskLayer.height / scaleFactor);

            if (textureHeight < 1)
            {
                textureHeight = 1;
            }

            var blurXLayer = new RenderLayer {
                rtID        = Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
                width       = textureWidth,
                height      = textureHeight,
                layerBounds = maskLayer.layerBounds,
                filterMode  = FilterMode.Bilinear,
            };

            parentLayer.layers.Add(blurXLayer);

            var blurYLayer = new RenderLayer {
                rtID        = Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
                width       = textureWidth,
                height      = textureHeight,
                layerBounds = maskLayer.layerBounds,
                filterMode  = FilterMode.Bilinear,
            };

            parentLayer.layers.Add(blurYLayer);

            var blurMesh = ImageMeshGenerator.imageMesh(null, Rect.one, maskLayer.layerBounds);

            var kernel = BlurUtils.get1DGaussianKernel(sigma, radius);

            blurXLayer.draws.Add(CanvasShader.maskFilter(
                                     blurXLayer, blurMesh, maskLayer,
                                     radius, new Vector2(1f / textureWidth, 0), kernel));

            blurYLayer.draws.Add(CanvasShader.maskFilter(
                                     blurYLayer, blurMesh, blurXLayer,
                                     radius, new Vector2(0, -1f / textureHeight), kernel));

            return(blurYLayer);
        }