Example #1
0
        void clipRRect()
        {
            var pictureRecorder = new PictureRecorder();
            var canvas          = new RecorderCanvas(pictureRecorder);

            var paint = new Paint {
                color = new Color(0xFFFF0000),
            };

            var path = new Path();

            path.moveTo(10, 10);
            path.lineTo(10, 110);
            path.lineTo(90, 110);
            path.lineTo(110, 10);
            path.close();

            canvas.drawPath(path, paint);

            paint = new Paint {
                color = new Color(0xFFFFFF00),
            };

            var rect   = Unity.UIWidgets.ui.Rect.fromLTWH(10, 150, 100, 100);
            var rrect  = RRect.fromRectAndCorners(rect, 0, 4, 8, 16);
            var rect1  = Unity.UIWidgets.ui.Rect.fromLTWH(18, 152, 88, 92);
            var rrect1 = RRect.fromRectAndCorners(rect1, 0, 4, 8, 16);

            canvas.drawDRRect(rrect, rrect1, paint);

            canvas.rotate(-45 * Mathf.PI / 180.0f, new Offset(150, 150));

//            paint = new Paint {
//                color = new Color(0xFF00FFFF),
//                blurSigma = 3,
//            };
//            canvas.drawRectShadow(
//                Rect.fromLTWH(150, 150, 110, 120),
//                paint);

            var picture = pictureRecorder.endRecording();

            Debug.Log("picture.paintBounds: " + picture.paintBounds);

            var editorCanvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                       this._meshPool);

            editorCanvas.rotate(-5 * Mathf.PI / 180);
            editorCanvas.clipRRect(RRect.fromRectAndRadius(Unity.UIWidgets.ui.Rect.fromLTWH(25, 15, 250, 250), 50));
            editorCanvas.rotate(5 * Mathf.PI / 180);

            editorCanvas.drawPicture(picture);

            editorCanvas.rotate(-15 * Mathf.PI / 180);
            editorCanvas.translate(100, 100);

            editorCanvas.drawPicture(picture);

            editorCanvas.flush();
        }
Example #2
0
        void drawRect()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            var paint = new Paint {
                color = new Color(0xFFFF0000),
            };

            canvas.rotate(15 * Mathf.PI / 180);
            var rect  = Unity.UIWidgets.ui.Rect.fromLTWH(10, 10, 100, 100);
            var rrect = RRect.fromRectAndCorners(rect, 0, 4, 8, 16);

            canvas.drawRRect(rrect, paint);

            paint = new Paint {
                color = new Color(0xFF00FF00),
            };

            rect  = Unity.UIWidgets.ui.Rect.fromLTWH(10, 150, 100, 100);
            rrect = RRect.fromRectAndCorners(rect, 0, 4, 8, 16);
            canvas.drawRRect(rrect, paint);

            rect  = Unity.UIWidgets.ui.Rect.fromLTWH(150, 150, 100, 100);
            rrect = RRect.fromRectAndCorners(rect, 10, 12, 14, 16);
            var rect1  = Unity.UIWidgets.ui.Rect.fromLTWH(160, 160, 80, 80);
            var rrect1 = RRect.fromRectAndCorners(rect1, 5, 6, 7, 8);

            canvas.drawDRRect(rrect, rrect1, paint);

            canvas.flush();
        }
Example #3
0
        RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float devicePixelRatio,
                                            MeshPool meshPool)
        {
            var bounds = transform.mapRect(picture.paintBounds);

            var desc = new RenderTextureDescriptor(
                Mathf.CeilToInt((float)(bounds.width * devicePixelRatio)),
                Mathf.CeilToInt((float)(bounds.height * devicePixelRatio)),
                RenderTextureFormat.Default, 24)
            {
                useMipMap        = false,
                autoGenerateMips = false,
            };

            if (QualitySettings.antiAliasing != 0)
            {
                desc.msaaSamples = QualitySettings.antiAliasing;
            }

            var renderTexture = new RenderTexture(desc);

            renderTexture.hideFlags = HideFlags.HideAndDontSave;

            var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool);

            canvas.translate((float)-bounds.left, (float)-bounds.top);
            canvas.concat(transform);
            canvas.drawPicture(picture);
            canvas.flush();

            return(new RasterCacheResult(new Image(renderTexture), picture.paintBounds, devicePixelRatio));
        }
        RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float devicePixelRatio,
                                            int antiAliasing, MeshPool meshPool)
        {
            var boundRect     = transform.mapRect(picture.paintBounds);
            var bounds        = boundRect.withDevicePixelRatio(devicePixelRatio);
            var boundsInPixel = boundRect.roundOutScale(devicePixelRatio);

            var desc = new RenderTextureDescriptor(
                Mathf.CeilToInt(boundsInPixel.width),
                Mathf.CeilToInt(boundsInPixel.height),
                RenderTextureFormat.Default, 24)
            {
                useMipMap        = false,
                autoGenerateMips = false,
            };

            if (antiAliasing != 0)
            {
                desc.msaaSamples = antiAliasing;
            }

            var renderTexture = new RenderTexture(desc);

            renderTexture.hideFlags = HideFlags.HideAndDontSave;

            var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool);

            canvas.translate(-bounds.left, -bounds.top);
            canvas.concat(transform);
            canvas.drawPicture(picture);
            canvas.flush();
            canvas.dispose();

            return(new RasterCacheResult(new Image(renderTexture), picture.paintBounds, devicePixelRatio));
        }
Example #5
0
        public Canvas getCanvas()
        {
            if (this._canvas == null)
            {
                this._canvas = new CommandBufferCanvas(
                    this._renderTexture, this.devicePixelRatio, this._meshPool, this._antiAliasing);
            }

            return(this._canvas);
        }
Example #6
0
        public Canvas getCanvas()
        {
            if (this._canvas == null)
            {
                this._canvas = new CommandBufferCanvas(
                    this._renderTexture, (float)this.devicePixelRatio, this._meshPool);
            }

            return(this._canvas);
        }
Example #7
0
        void drawPicture()
        {
            var pictureRecorder = new PictureRecorder();
            var canvas          = new RecorderCanvas(pictureRecorder);

            var paint = new Paint {
                color = new Color(0xFFFF0000),
            };

            var path = new Path();

            path.moveTo(10, 10);
            path.lineTo(10, 110);
            path.lineTo(90, 110);
            path.lineTo(100, 10);
            path.close();
            canvas.drawPath(path, paint);

            paint = new Paint {
                color = new Color(0xFFFFFF00),
            };

            var rect   = Unity.UIWidgets.ui.Rect.fromLTWH(10, 150, 100, 100);
            var rrect  = RRect.fromRectAndCorners(rect, 0, 4, 8, 16);
            var rect1  = Unity.UIWidgets.ui.Rect.fromLTWH(18, 152, 88, 92);
            var rrect1 = RRect.fromRectAndCorners(rect1, 0, 4, 8, 16);

            canvas.drawDRRect(rrect, rrect1, paint);

            canvas.rotate(-45 * Mathf.PI / 180, new Offset(150, 150));

            paint = new Paint {
                color      = new Color(0xFF00FFFF),
                maskFilter = MaskFilter.blur(BlurStyle.normal, 3),
            };
            canvas.drawRect(
                Unity.UIWidgets.ui.Rect.fromLTWH(150, 150, 110, 120),
                paint);

            var picture = pictureRecorder.endRecording();

            Debug.Log("picture.paintBounds: " + picture.paintBounds);

            var editorCanvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                       this._meshPool);

            editorCanvas.drawPicture(picture);

            editorCanvas.rotate(-15 * Mathf.PI / 180);
            editorCanvas.translate(100, 100);
            editorCanvas.drawPicture(picture);
            editorCanvas.flush();
        }
Example #8
0
        public void Dispose()
        {
            if (this._renderTexture)
            {
                this._renderTexture = ObjectUtils.SafeDestroy(this._renderTexture);
            }

            if (this._canvas != null)
            {
                this._canvas.reset();
                this._canvas = null;
            }
        }
Example #9
0
        void drawParagraph()
        {
            var pb = new ParagraphBuilder(new ParagraphStyle {
            });

            pb.addText("Hello drawParagraph");
            var paragraph = pb.build();

            paragraph.layout(new ParagraphConstraints(width: 300));
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            canvas.drawParagraph(paragraph, new Offset(10f, 100f));
            canvas.flush();
        }
Example #10
0
        void drawRectShadow()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            var paint = new Paint {
                color      = new Color(0xFF00FF00),
                maskFilter = MaskFilter.blur(BlurStyle.normal, 3),
            };

            canvas.clipRect(Unity.UIWidgets.ui.Rect.fromLTWH(25, 25, 300, 300));

            canvas.rotate(-Mathf.PI / 8.0f);

            canvas.drawRect(
                Unity.UIWidgets.ui.Rect.fromLTWH(10, 10, 100, 100),
                paint);

            paint = new Paint {
                color       = new Color(0xFFFFFF00),
                maskFilter  = MaskFilter.blur(BlurStyle.normal, 5),
                style       = PaintingStyle.stroke,
                strokeWidth = 55,
                shader      = Gradient.linear(new Offset(10, 10), new Offset(180, 180), new List <Color>()
                {
                    Colors.red, Colors.green, Colors.yellow
                }, null, TileMode.clamp)
            };

            canvas.drawRect(
                Unity.UIWidgets.ui.Rect.fromLTWH(10, 150, 200, 200),
                paint);

            canvas.drawImage(new Image(texture6, true),
                             new Offset(50, 150),
                             paint);

            canvas.flush();
        }
Example #11
0
        void drawImageRect()
        {
            if (this._stream == null || this._stream.completer == null || this._stream.completer.currentImage == null)
            {
                return;
            }

            var canvas = new CommandBufferCanvas(this._renderTexture, (float)Window.instance.devicePixelRatio, this._meshPool);

            var paint = new Paint {
                // color = new Color(0x7FFF0000),
                shader = Gradient.linear(new Offset(100, 100), new Offset(280, 280), new List <Color>()
                {
                    Colors.red, Colors.black, Colors.green
                }, null, TileMode.clamp)
            };

            canvas.drawImageRect(this._stream.completer.currentImage.image,
                                 Unity.UIWidgets.ui.Rect.fromLTWH(100, 50, 250, 250),
                                 paint
                                 );
            canvas.flush();
        }
Example #12
0
        void saveLayer()
        {
            var pictureRecorder = new PictureRecorder();
            var canvas          = new RecorderCanvas(pictureRecorder);
            var paint1          = new Paint {
                color = new Color(0xFFFFFFFF),
            };

            var path1 = new Path();

            path1.moveTo(0, 0);
            path1.lineTo(0, 90);
            path1.lineTo(90, 90);
            path1.lineTo(90, 0);
            path1.close();
            canvas.drawPath(path1, paint1);


            var paint = new Paint {
                color = new Color(0xFFFF0000),
            };

            var path = new Path();

            path.moveTo(20, 20);
            path.lineTo(20, 70);
            path.lineTo(70, 70);
            path.lineTo(70, 20);
            path.close();

            canvas.drawPath(path, paint);

            var paint2 = new Paint {
                color = new Color(0xFFFFFF00),
            };

            var path2 = new Path();

            path2.moveTo(30, 30);
            path2.lineTo(30, 60);
            path2.lineTo(60, 60);
            path2.lineTo(60, 30);
            path2.close();

            canvas.drawPath(path2, paint2);

            var picture = pictureRecorder.endRecording();

            var editorCanvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                       this._meshPool);

            editorCanvas.saveLayer(
                picture.paintBounds, new Paint {
                color = new Color(0xFFFFFFFF),
            });
            editorCanvas.drawPicture(picture);
            editorCanvas.restore();

            editorCanvas.saveLayer(Unity.UIWidgets.ui.Rect.fromLTWH(45, 45, 90, 90), new Paint {
                color    = new Color(0xFFFFFFFF),
                backdrop = ImageFilter.blur(3f, 3f)
            });
            editorCanvas.restore();

            editorCanvas.flush();
        }
Example #13
0
        void drawLine()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            var paint = new Paint {
                color       = new Color(0xFFFF0000),
                style       = PaintingStyle.stroke,
                strokeWidth = 10,
                shader      = Gradient.linear(new Offset(10, 10), new Offset(180, 180), new List <Color>()
                {
                    Colors.red, Colors.green, Colors.yellow
                }, null, TileMode.clamp)
            };

            canvas.drawLine(
                new Offset(10, 20),
                new Offset(50, 20),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(100, 100),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(10, 50),
                paint);

            canvas.drawLine(
                new Offset(40, 10),
                new Offset(90, 10),
                paint);


            canvas.drawArc(Unity.UIWidgets.ui.Rect.fromLTWH(200, 200, 100, 100), Mathf.PI / 4,
                           -Mathf.PI / 2 + Mathf.PI * 4 - 1, true, paint);

            paint.maskFilter  = MaskFilter.blur(BlurStyle.normal, 1);
            paint.strokeWidth = 4;

            canvas.drawLine(
                new Offset(40, 20),
                new Offset(120, 190),
                paint);

            canvas.scale(3);
            TextBlobBuilder builder = new TextBlobBuilder();
            string          text    = "This is a text blob";

            builder.allocRunPos(new TextStyle(), text, 0, text.Length);
            builder.setBounds(Unity.UIWidgets.ui.Rect.fromLTWH(-10, -20, 200, 50));
            builder.positions = new Vector2d[] {
                new Vector2d(10, 0),
                new Vector2d(20, 0),
                new Vector2d(30, 0),
                new Vector2d(40, 0),
                new Vector2d(50, 0),
                new Vector2d(60, 0),
                new Vector2d(70, 0),
                new Vector2d(80, 0),
                new Vector2d(90, 0),
                new Vector2d(100, 0),
                new Vector2d(110, 0),
                new Vector2d(120, 0),
                new Vector2d(130, 0),
                new Vector2d(140, 0),
                new Vector2d(150, 0),
                new Vector2d(160, 0),
                new Vector2d(170, 0),
                new Vector2d(180, 0),
                new Vector2d(190, 0),
            };

            var textBlob = builder.make();

            canvas.drawTextBlob(textBlob, new Offset(100, 100), new Paint {
                color      = Colors.black,
                maskFilter = MaskFilter.blur(BlurStyle.normal, 5),
            });
            canvas.drawTextBlob(textBlob, new Offset(100, 100), paint);

            canvas.drawLine(
                new Offset(10, 30),
                new Offset(10, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.1f
            });

            canvas.drawLine(
                new Offset(20, 30),
                new Offset(20, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.333f
            });

            canvas.flush();
        }
Example #14
0
        void drawPloygon4()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, Window.instance.devicePixelRatio,
                                                 this._meshPool);

            var paint = new Paint {
                color  = new Color(0xFFFF0000),
                shader = Gradient.linear(new Offset(80, 80), new Offset(180, 180), new List <Color>()
                {
                    Colors.red, Colors.black, Colors.green
                }, null, TileMode.clamp)
            };

            var path = new Path();

            path.moveTo(10, 150);
            path.lineTo(10, 160);
            path.lineTo(140, 120);
            path.lineTo(110, 180);
            path.winding(PathWinding.clockwise);
            path.close();
            path.addRect(Unity.UIWidgets.ui.Rect.fromLTWH(0, 100, 100, 100));
            path.addRect(Unity.UIWidgets.ui.Rect.fromLTWH(200, 0, 100, 100));
            path.addRRect(RRect.fromRectAndRadius(Unity.UIWidgets.ui.Rect.fromLTWH(150, 100, 30, 30), 10));
            path.addOval(Unity.UIWidgets.ui.Rect.fromLTWH(150, 50, 100, 100));
            path.winding(PathWinding.clockwise);

            if (Event.current.type == EventType.MouseDown)
            {
                var pos = new Offset(
                    Event.current.mousePosition.x,
                    Event.current.mousePosition.y
                    );

                Debug.Log(pos + ": " + path.contains(pos));
            }

            canvas.drawPath(path, paint);

            canvas.rotate(Mathf.PI * 15 / 180);

            canvas.translate(100, 100);

            paint.shader = Gradient.radial(new Offset(80, 80), 100, new List <Color>()
            {
                Colors.red, Colors.black, Colors.green
            }, null, TileMode.clamp);
            canvas.drawPath(path, paint);


            canvas.translate(100, 100);
            paint.shader = Gradient.sweep(new Offset(120, 100), new List <Color>()
            {
                Colors.red, Colors.black, Colors.green, Colors.red,
            }, null, TileMode.clamp, 10 * Mathf.PI / 180, 135 * Mathf.PI / 180);
            canvas.drawPath(path, paint);


            canvas.translate(100, 100);
            //paint.maskFilter = MaskFilter.blur(BlurStyle.normal, 5);
            paint.shader = new ImageShader(new Image(texture6, true), TileMode.mirror);
            canvas.drawPath(path, paint);

            canvas.flush();
        }
Example #15
0
        void drawLine()
        {
            var canvas = new CommandBufferCanvas(this._renderTexture, (float)Window.instance.devicePixelRatio, this._meshPool);

            var paint = new Paint {
                color       = new Color(0xFFFF0000),
                style       = PaintingStyle.stroke,
                strokeWidth = 10,
                shader      = Gradient.linear(new Offset(10, 10), new Offset(180, 180), new List <Color>()
                {
                    Colors.red, Colors.green, Colors.yellow
                }, null, TileMode.clamp)
            };

            canvas.drawLine(
                new Offset(10, 20),
                new Offset(50, 20),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(100, 100),
                paint);

            canvas.drawLine(
                new Offset(10, 10),
                new Offset(10, 50),
                paint);

            canvas.drawLine(
                new Offset(40, 10),
                new Offset(90, 10),
                paint);

            paint.maskFilter  = MaskFilter.blur(BlurStyle.normal, 1);
            paint.strokeWidth = 4;

            canvas.drawLine(
                new Offset(40, 20),
                new Offset(120, 190),
                paint);

            canvas.scale(3);
            TextBlob textBlob = new TextBlob("This is a text blob", 0, 19, new Vector2d[] {
                new Vector2d(10, 0),
                new Vector2d(20, 0),
                new Vector2d(30, 0),
                new Vector2d(40, 0),
                new Vector2d(50, 0),
                new Vector2d(60, 0),
                new Vector2d(70, 0),
                new Vector2d(80, 0),
                new Vector2d(90, 0),
                new Vector2d(100, 0),
                new Vector2d(110, 0),
                new Vector2d(120, 0),
                new Vector2d(130, 0),
                new Vector2d(140, 0),
                new Vector2d(150, 0),
                new Vector2d(160, 0),
                new Vector2d(170, 0),
                new Vector2d(180, 0),
                new Vector2d(190, 0),
            }, Unity.UIWidgets.ui.Rect.fromLTWH(0, 0, 200, 50), new TextStyle());

            canvas.drawTextBlob(textBlob, new Offset(100, 100), paint);

            canvas.drawLine(
                new Offset(10, 30),
                new Offset(10, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.1
            });

            canvas.drawLine(
                new Offset(20, 30),
                new Offset(20, 60),
                new Paint()
            {
                style = PaintingStyle.stroke, strokeWidth = 0.333
            });

            canvas.flush();
        }