Ejemplo n.º 1
0
        public static PictureFlusher.RenderDraw stroke0(PictureFlusher.RenderLayer layer, Paint paint,
                                                        float alpha, MeshMesh mesh)
        {
            var mat = _stroke0Mat.getMaterial(paint.blendMode, layer.ignoreClip);

            _getShaderPassAndProps(layer, paint, mesh, alpha, out var pass, out var props);

            return(new PictureFlusher.RenderDraw {
                mesh = mesh,
                pass = pass,
                material = mat,
                properties = props,
            });
        }
Ejemplo n.º 2
0
        public static PictureFlusher.RenderDraw tex(PictureFlusher.RenderLayer layer, Paint paint,
                                                    MeshMesh mesh, Image image)
        {
            var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);

            _getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);

            image.texture.filterMode = paint.filterMode;
            props.SetTexture("_tex", image.texture);
            props.SetInt("_texMode", image.texture is RenderTexture ? 1 : 0); // pre alpha if RT else post alpha

            return(new PictureFlusher.RenderDraw {
                mesh = mesh,
                pass = pass,
                material = mat,
                properties = props,
                image = image, // keep a reference to avoid GC.
            });
        }
Ejemplo n.º 3
0
        public static PictureFlusher.RenderDraw convexFill(PictureFlusher.RenderLayer layer, Paint paint,
                                                           MeshMesh mesh)
        {
            var mat = _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);

            _getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);

            return(new PictureFlusher.RenderDraw {
                mesh = mesh,
                pass = pass,
                material = mat,
                properties = props,
            });
        }
Ejemplo n.º 4
0
        public static PictureFlusher.RenderDraw fill1(PictureFlusher.RenderLayer layer, Paint paint,
                                                      MeshMesh mesh)
        {
            var mat = _fill1Mat.getMaterial(paint.blendMode);

            _getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);

            return(new PictureFlusher.RenderDraw {
                mesh = mesh.boundsMesh,
                pass = pass,
                material = mat,
                properties = props,
            });
        }
Ejemplo n.º 5
0
        void _drawPath(Path path, Paint paint)
        {
            D.assert(path != null);
            D.assert(paint != null);

            if (paint.style == PaintingStyle.fill)
            {
                var state = this._currentLayer.currentState;
                var cache = path.flatten(state.scale * this._devicePixelRatio);

                bool convex;
                var  mesh = cache.getFillMesh(out convex).transform(state.matrix);

                Action <Paint> drawMesh = p => {
                    if (!this._applyClip(mesh.bounds))
                    {
                        return;
                    }

                    var layer = this._currentLayer;
                    if (convex)
                    {
                        layer.draws.Add(CanvasShader.convexFill(layer, p, mesh));
                    }
                    else
                    {
                        layer.draws.Add(CanvasShader.fill0(layer, mesh));
                        layer.draws.Add(CanvasShader.fill1(layer, p, mesh.boundsMesh));
                    }
                };

                if (paint.maskFilter != null && paint.maskFilter.sigma != 0)
                {
                    this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter);
                    return;
                }

                drawMesh(paint);
            }
            else
            {
                var   state       = this._currentLayer.currentState;
                float strokeWidth = (paint.strokeWidth * state.scale).clamp(0, 200.0f);
                float alpha       = 1.0f;

                if (strokeWidth == 0)
                {
                    strokeWidth = this._fringeWidth;
                }
                else if (strokeWidth < this._fringeWidth)
                {
                    // If the stroke width is less than pixel size, use alpha to emulate coverage.
                    // Since coverage is area, scale by alpha*alpha.
                    alpha       = (strokeWidth / this._fringeWidth).clamp(0.0f, 1.0f);
                    alpha      *= alpha;
                    strokeWidth = this._fringeWidth;
                }

                var cache = path.flatten(state.scale * this._devicePixelRatio);
                var mesh  = cache.getStrokeMesh(
                    strokeWidth / state.scale * 0.5f,
                    paint.strokeCap,
                    paint.strokeJoin,
                    paint.strokeMiterLimit).transform(state.matrix);

                Action <Paint> drawMesh = p => {
                    if (!this._applyClip(mesh.bounds))
                    {
                        return;
                    }

                    var layer = this._currentLayer;

                    layer.draws.Add(CanvasShader.stroke0(layer, p, alpha, mesh));
                    layer.draws.Add(CanvasShader.stroke1(layer, mesh));
                };

                if (paint.maskFilter != null && paint.maskFilter.sigma != 0)
                {
                    this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter);
                    return;
                }

                drawMesh(paint);
            }
        }
Ejemplo n.º 6
0
        static void _getShaderPassAndProps(
            PictureFlusher.RenderLayer layer, Paint paint, MeshMesh mesh, float alpha,
            out int pass, out MaterialPropertyBlock props)
        {
            Vector4 viewport = layer.viewport;

            props = new MaterialPropertyBlock();
            props.SetVector("_viewport", viewport);
            props.SetFloat("_alpha", alpha);

            switch (paint.shader)
            {
            case null:
                pass = 0;
                props.SetVector("_color", _colorToVector4(paint.color));
                return;

            case _LinearGradient linear:
                pass = 1;
                props.SetMatrix("_shaderMat", linear.getGradientMat(
                                    _getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
                props.SetTexture("_shaderTex", linear.gradientTex.texture);
                props.SetVector("_leftColor", _colorToVector4(linear.leftColor));
                props.SetVector("_rightColor", _colorToVector4(linear.rightColor));
                props.SetInt("_tileMode", (int)linear.tileMode);
                return;

            case _RadialGradient radial:
                pass = 2;
                props.SetMatrix("_shaderMat", radial.getGradientMat(
                                    _getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
                props.SetTexture("_shaderTex", radial.gradientTex.texture);
                props.SetVector("_leftColor", _colorToVector4(radial.leftColor));
                props.SetVector("_rightColor", _colorToVector4(radial.rightColor));
                props.SetInt("_tileMode", (int)radial.tileMode);
                return;

            case _SweepGradient sweep:
                pass = 3;
                props.SetMatrix("_shaderMat", sweep.getGradientMat(
                                    _getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
                props.SetTexture("_shaderTex", sweep.gradientTex.texture);
                props.SetVector("_leftColor", _colorToVector4(sweep.leftColor));
                props.SetVector("_rightColor", _colorToVector4(sweep.rightColor));
                props.SetInt("_tileMode", (int)sweep.tileMode);
                props.SetFloat("_bias", sweep.bias);
                props.SetFloat("_scale", sweep.scale);
                return;

            case ImageShader image:
                pass = 4;
                props.SetMatrix("_shaderMat", image.getShaderMat(
                                    _getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
                props.SetTexture("_shaderTex", image.image.texture);
                props.SetInt("_tileMode", (int)image.tileMode);
                return;

            default:
                throw new Exception("Unknown paint.shader: " + paint.shader);
            }
        }
Ejemplo n.º 7
0
        public static PictureFlusher.CmdDraw texAlpha(PictureFlusher.RenderLayer layer, Paint paint,
                                                      MeshMesh mesh, TextBlobMesh textMesh, Texture tex)
        {
            var mat        = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);
            var meshMatrix = mesh != null ? mesh.matrix : textMesh.matrix;

            _getShaderPassAndProps(layer, paint, meshMatrix, 1.0f, out var pass, out var props);
            tex.filterMode = paint.filterMode;
            props.SetTexture(_texId, tex);
            props.SetInt(_texModeId, 2); // alpha only

            return(new PictureFlusher.CmdDraw {
                mesh = mesh,
                textMesh = textMesh,
                pass = pass,
                material = mat,
                properties = props,
            });
        }
Ejemplo n.º 8
0
 public void drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter, Paint paint)
 {
     //var path = new Path();
 }
Ejemplo n.º 9
0
        public void drawArc(Rect rect, float startAngle, float sweepAngle, bool useCenter, Paint paint)
        {
            var path = new Path();

            //path.(c.dx, c.dy, radius);

            this._recorder.addDrawCmd(new DrawPath {
                path  = path,
                paint = new Paint(paint),
            });
        }
Ejemplo n.º 10
0
 public static PictureFlusher.CmdDraw texAlpha(PictureFlusher.RenderLayer layer, Paint paint,
                                               TextBlobMesh textMesh, Texture tex)
 {
     return(texAlpha(layer, paint, null, textMesh, tex));
 }
Ejemplo n.º 11
0
 public void drawImageNine(Image image, Rect center, Rect dst, Paint paint)
 {
     this.drawImageNine(image, null, center, dst, paint);
 }
Ejemplo n.º 12
0
 public void drawImageRect(Image image, Rect dst, Paint paint)
 {
     this.drawImageRect(image, null, dst, paint);
 }
Ejemplo n.º 13
0
        RenderLayer _createMaskLayer(RenderLayer parentLayer, Rect maskBounds, Action <Paint> drawCallback, Paint paint)
        {
            var textureWidth = Mathf.CeilToInt((float)maskBounds.width * this._devicePixelRatio);

            textureWidth = Mathf.Max(1, textureWidth);

            var textureHeight = Mathf.CeilToInt((float)maskBounds.height * this._devicePixelRatio);

            textureHeight = Mathf.Max(1, textureHeight);

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

            parentLayer.layers.Add(maskLayer);
            this._layers.Add(maskLayer);

            var parentState = parentLayer.states.Last();
            var maskState   = maskLayer.states.Last();

            maskState.matrix = parentState.matrix;

            drawCallback(Paint.shapeOnly(paint));

            var removed = this._layers.removeLast();

            D.assert(removed == maskLayer);

            return(maskLayer);
        }
Ejemplo n.º 14
0
        public static PictureFlusher.RenderDraw texRT(PictureFlusher.RenderLayer layer, Paint paint,
                                                      MeshMesh mesh, PictureFlusher.RenderLayer renderLayer)
        {
            var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);

            _getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
            props.SetInt("_texMode", 1); // pre alpha

            return(new PictureFlusher.RenderDraw {
                mesh = mesh,
                pass = pass,
                material = mat,
                properties = props,
                layer = renderLayer,
            });
        }
Ejemplo n.º 15
0
        RenderLayer _createMaskLayer(RenderLayer parentLayer, Rect maskBounds, Action <Paint> drawCallback, Paint paint)
        {
            var textureWidth = Mathf.CeilToInt(maskBounds.width * this._devicePixelRatio);

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

            var textureHeight = Mathf.CeilToInt(maskBounds.height * this._devicePixelRatio);

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

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

            parentLayer.layers.Add(maskLayer);
            this._layers.Add(maskLayer);
            this._currentLayer = maskLayer;

            var parentState = parentLayer.states[parentLayer.states.Count - 1];
            var maskState   = maskLayer.states[maskLayer.states.Count - 1];

            maskState.matrix = parentState.matrix;

            drawCallback(Paint.shapeOnly(paint));

            var removed = this._layers.removeLast();

            D.assert(removed == maskLayer);
            this._currentLayer = this._layers[this._layers.Count - 1];

            return(maskLayer);
        }
Ejemplo n.º 16
0
        public static PictureFlusher.RenderDraw texAlpha(PictureFlusher.RenderLayer layer, Paint paint,
                                                         MeshMesh mesh, Texture tex)
        {
            var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);

            _getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
            tex.filterMode = paint.filterMode;
            props.SetTexture("_tex", tex);
            props.SetInt("_texMode", 2); // alpha only

            return(new PictureFlusher.RenderDraw {
                mesh = mesh,
                pass = pass,
                material = mat,
                properties = props,
            });
        }
Ejemplo n.º 17
0
        public void drawArc(Rect rect, float startAngle, float sweepAngle, bool useCenter, Paint paint)
        {
            var path = new Path();

            if (useCenter)
            {
                var center = rect.center;
                path.moveTo(center.dx, center.dy);
            }

            bool forceMoveTo = !useCenter;

            while (sweepAngle <= -Mathf.PI * 2)
            {
                path.arcTo(rect, startAngle, -Mathf.PI, forceMoveTo);
                startAngle -= Mathf.PI;
                path.arcTo(rect, startAngle, -Mathf.PI, false);
                startAngle -= Mathf.PI;
                forceMoveTo = false;
                sweepAngle += Mathf.PI * 2;
            }

            while (sweepAngle >= Mathf.PI * 2)
            {
                path.arcTo(rect, startAngle, Mathf.PI, forceMoveTo);
                startAngle += Mathf.PI;
                path.arcTo(rect, startAngle, Mathf.PI, false);
                startAngle += Mathf.PI;
                forceMoveTo = false;
                sweepAngle -= Mathf.PI * 2;
            }

            path.arcTo(rect, startAngle, sweepAngle, forceMoveTo);
            if (useCenter)
            {
                path.close();
            }

            this._recorder.addDrawCmd(new DrawPath {
                path  = path,
                paint = new Paint(paint),
            });
        }