Example #1
0
        static void drawShadowFast(Canvas canvas, Path path, Vector3 zPlaneParams, Vector3 devLightPos,
                                   float lightRadius, uiColor ambientColor, uiColor spotColor, int flags)
        {
            Matrix3 viewMatrix = canvas.getTotalMatrix();

            //debug shadow
            #pragma warning disable CS0162
            if (debugShadow)
            {
                var isRRect = path.isNaiveRRect;
                if (isRRect)
                {
                    ambientColor = uiColor.fromColor(Colors.red);
                    spotColor    = uiColor.fromColor(Colors.red);
                }
                else
                {
                    ambientColor = uiColor.fromColor(Colors.green);
                    spotColor    = uiColor.fromColor(Colors.green);
                }
            }
            #pragma warning restore CS0162

            //ambient light
            float devSpaceOutset = ambientBlurRadius(zPlaneParams.z);
            float oneOverA       = ambientRecipAlpha(zPlaneParams.z);
            float blurRadius     = 0.5f * devSpaceOutset * oneOverA;
            float strokeWidth    = 0.5f * (devSpaceOutset - blurRadius);

            _shadowPaint.color       = new Color(ambientColor.value);
            _shadowPaint.strokeWidth = strokeWidth;
            _shadowPaint.style       = PaintingStyle.fill;
            canvas.drawPath(path, _shadowPaint);

            //spot light
            float radius = 0.0f;
            if (!getSpotShadowTransform(devLightPos, lightRadius, viewMatrix, zPlaneParams, path.getBounds(),
                                        _shadowMatrix, ref radius))
            {
                return;
            }

            canvas.save();
            canvas.setMatrix(_shadowMatrix);

            _shadowPaint.color       = new Color(spotColor.value);
            _shadowPaint.strokeWidth = 0;
            _shadowPaint.style       = PaintingStyle.fill;
            float sigma2 = convertRadiusToSigma(radius);
            _shadowPaint.maskFilter = path.isNaiveRRect ? MaskFilter.fastShadow(sigma2) : MaskFilter.blur(BlurStyle.normal, sigma2);
            canvas.drawPath(path, _shadowPaint);

            canvas.restore();

            _shadowPaint.maskFilter = null;
        }
Example #2
0
        void _drawWithMaskFilter(Rect meshBounds, Action <Paint> drawAction, Paint paint, MaskFilter maskFilter)
        {
            var layer      = this._currentLayer;
            var clipBounds = layer.layerBounds;

            Rect stackBounds;
            bool iior;

            layer.clipStack.getBounds(out stackBounds, out iior);

            if (stackBounds != null)
            {
                clipBounds = clipBounds.intersect(stackBounds);
            }

            if (clipBounds.isEmpty)
            {
                return;
            }

            var   state = layer.currentState;
            float sigma = state.scale * maskFilter.sigma;

            if (sigma <= 0)
            {
                return;
            }

            float sigma3     = 3 * sigma;
            var   maskBounds = meshBounds.inflate(sigma3);

            maskBounds = maskBounds.intersect(clipBounds.inflate(sigma3));
            if (maskBounds.isEmpty)
            {
                return;
            }

            var maskLayer = this._createMaskLayer(layer, maskBounds, drawAction, paint);

            var blurLayer = this._createBlurLayer(maskLayer, sigma, layer);

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

            if (!this._applyClip(blurMesh.bounds))
            {
                return;
            }

            layer.draws.Add(CanvasShader.texRT(layer, paint, blurMesh, blurLayer));
        }