shadermask概述-程序员宅基地

According to official documentation, ShaderMask is a widget that applies a mask generated by a shader to its child.

根据官方文档, ShaderMask是一个小部件,可将着色器生成的蒙版应用于其子级。

What does this mean ? What is a Shader ? What does Mask mean ?

这是什么意思 ? 什么是ShaderMask是什么意思?

I’ll try to explain its meaning with an example. Let’s say, for instance, to paint a house, you need two basic things:

我将尝试通过一个例子来解释它的含义。 例如,假设要粉刷一栋房子,您需要两个基本的条件:

  • Color

    颜色
  • Paint tools (Brush, ladder and so on)

    油漆工具(画笔,梯子等)

The tools such as brush which is used to apply color on the walls of a house, is nothing but a shader. The color that is being applied, is a mask and the house (wall) of course is a widget. So, in simple terms, ShaderMask is a widget that is used to apply effects / color on it’s child.

诸如刷子之类的用于在房屋墙壁上shader不过是shader 。 所应用的颜色mask房子 (墙)当然是widget 。 因此,简单来说, ShaderMask是一个小部件,用于在其子级上应用效果/颜色。

Let’s now see an example of wrapping a widget with ShaderMask and what all properties and methods are offered by ShaderMask to achieve the effect we want on it’s child. We will have a simple Container with bounds along with an image as it’s child, as below:

让我们现在看看包装与小窗口的一个例子ShaderMask什么所有属性和方法是通过提供ShaderMask来实现我们想要它的孩子的影响。 我们将有一个带有边界的简单Container以及一个子图像,如下所示:

body: Center(
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover)
)
)
Image for post

When we wrap Container with ShaderMask, we also need to add required parameter named shaderCallback

ShaderMask包装Container时,还需要添加名为shaderCallback必需参数

Image for post

shaderCallback as the name indicates, is the callback that accepts bounds for Rect (stands for Rectangle) that helps to create the shader for the given bounds, ie, the process of identifying the area where to start coloring.

shaderCallback是接受Rect bounds (代表Rectangle)的回调,该回调有助于为给定边界创建shader ,即,确定开始着色的区域的过程。

Since we need to give effects to the child (or wall), we can make use of Gradients class to apply colors / effects. This is usually done using:

由于我们需要给孩子(或墙壁)添加效果,因此可以利用Gradients类来应用颜色/效果。 通常使用以下命令完成此操作:

  • LinearGradient

    线性渐变
  • RadialGradient

    径向渐变
  • SweepGradient

    扫描渐变

Let’s take a look at them one by one and see how we can apply effects to the child.

让我们一一看一下,看看如何将效果应用于孩子。

LinearGradient

线性渐变

This accepts begin and end as two optional bounds followed by color that accepts list of colors which will be applied to the child. Note that, begin and end accepts only Alignment objects, so we need to pass regular alignments that indicates how the effect will be aligned, as below:

这接受beginend作为两个可选边界,其后跟随color接受将应用于子项的颜色列表。 请注意, beginend仅接受Alignment对象,因此我们需要传递常规对齐方式,以指示效果的对齐方式,如下所示:

shaderCallback: (Rect bounds) {
        
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.red,
Colors.black]
)

Here, the callback implementation isn’t complete yet, that means, we have dipped the brush in the color and are ready to paint the wall, but, we haven’t received a go-ahead to get started with the task. The last piece of this implementation is another method named createShaders() that accepts the bounds on which the colors are to be applied. Let’s make that call:

在这里,回调实现尚未完成,这意味着我们已经将画笔浸入了颜色并准备为墙粉刷,但是,我们还没有获得批准开始执行该任务。 该实现的最后一部分是另一个名为createShaders()方法,该方法接受要在其上应用颜色的bounds 。 让我们打电话:

shaderCallback: (Rect bounds) {
        
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.red,
Colors.black]
).createShader(bounds);

This method tells ShaderMask to start applying / fill the colors on the child in the given rect bounds. Now, if we run this, we’ll see:

此方法告诉ShaderMask开始在给定的rect范围内应用/填充子项上的颜色。 现在,如果运行此命令,我们将看到:

Image for post

Here, we just applied gradient on an image and ShaderMask spanned those colors across the Container . But this is not enough for us to make use of ShaderMask. We also want to apply some effects on the image to make it more enhance, meaningful and appealing, right ?

在这里,我们仅在图像上应用了渐变, ShaderMask这些颜色跨越了Container 。 但这还不足以让我们使用ShaderMask. 我们还想对图像应用一些效果,以使其更加增强,有意义和吸引人,对吗?

How ? Using blendmode property.

怎么样 ? 使用blendmode属性。

When we take picture from our phone, there’s an option to edit it and add more effects to it, like, increase brightness, add contrast, add shadows and so on. On the same lines, we can apply similar effects to the image using blendmode property. It offers various options to apply effects on the image. Let’s see few of them.

当我们从手机上拍摄照片时,可以选择对其进行编辑并为其添加更多效果,例如增加亮度 ,添加对比度 ,添加阴影等。 在同一行上,我们可以使用blendmode属性将类似的效果应用于图像。 它提供了各种选项以在图像上应用效果。 让我们看看其中的几个。

BlendMode

混合模式

BlendMode effects work on the concept of source and destination . All types of blend mode effects are applied based on these two terms. You can read about these terms in detail here.

BlendMode效果适用于sourcedestination的概念。 基于这两个术语,将应用所有类型的混合模式效果。 您可以在此处详细了解这些术语。

  • BlendMode.color: This property mode simply paints the image with the given colors.

    BlendMode.color:此属性模式仅使用给定的颜色绘制图像。

Image for post
BlendMode.color
BlendMode.color
  • BlendMode.colorBurn: This property paints the image with invert effects based on colors provided.

    BlendMode.colorBurn:此属性根据提供的颜色用反转效果绘制图像。

Image for post
BlendMode.colorBurn
BlendMode.colorBurn
  • BlendMode.colorDodge: This property paints the image with brightness based on the colors provided.

    BlendMode.colorDodge:此属性根据提供的颜色为图像绘制亮度。

Image for post
BlendMode.colorDodge
BlendMode.colorDodge
  • BlendMode.clear: This property removes the source and destination and shows a transparent screen.

    BlendMode.clear:此属性删除sourcedestination并显示透明屏幕。

Image for post
BlendMode.clear
BlendMode.clear
  • BlendMode.src: Shorthand for source. This property shows the original / source widget on which the image is to be painted. In this case, Container

    BlendMode.src: source简写。 此属性显示要在其上绘制图像的原始/源窗口小部件。 在这种情况下, Container

Image for post
BlendMode.src
BlendMode.src

Variants of this blendmode are: srcIn, srcOut, srcOver, srcAtTop

此blendmode的变体是:srcIn,srcOut,srcOver,srcAtTop

  • BlendMode.dst: Shorthand for destination. This property shows the original destination (image) only.

    BlendMode.dst:目标的简写。 此属性仅显示原始目的地(图像)。

Image for post
BlendMode.dst
BlendMode.dst

Variants of this blendmode are: dstIn, dstOut, dstOver, dstAtTop

此blendmode的变体是:dstIn,dstOut,dstOver,dstAtTop

There are few more modes that I’ll skip (for sake of length of this article) and will include them in my github repo here.

有几个模式,我将跳过(本文的长度的缘故),将包括他们在我的GitHub库在这里

RadialGradient:

径向渐变:

This shows the gradient / color effects in concentric circles. Along with various blend modes, we can change appearance of the image as we need. Let’s see few examples:

这显示了同心圆中的渐变/颜色效​​果。 随着各种blend modes ,我们可以根据需要更改图像的外观。 让我们看几个例子:

body: Center(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return RadialGradient(
colors: [
Colors.green,
Colors.blue,
Colors.orange]
).createShader(bounds);
}, blendMode: BlendMode.screen,
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover)
)
)
)
Image for post
RadialGradient with screen blendmode
屏幕混合模式下的RadialGradient
return RadialGradient(
colors: [Colors.green, Colors.blue, Colors.orange],
).createShader(bounds);
},blendMode: BlendMode.hardLight,
Image for post
RadialGradient with hardlight blend
RadialGradient与强光混合

SweepGradient:

SweepGradient:

This shows the gradient / color effects in an arc. When it comes to arc, we think of angles, right ? Similarly, this gradient provides properties such as startAngle and endAngle to change appearance as required. Of course, the different blend modes can be used to enhance the effect. Below is one such example:

这显示了弧形中的渐变/颜色效​​果。 说到弧线,我们会想到角度,对吗? 同样,此渐变提供诸如startAngleendAngle属性,以根据需要更改外观。 当然,可以使用不同的blend modes来增强效果。 下面是一个这样的示例:

return SweepGradient(
colors: [Colors.indigo, Colors.blue, Colors.green,Colors.yellow,Colors.orange,Colors.red],
startAngle: 0.1,
endAngle: 1,
).createShader(bounds);
},blendMode: BlendMode.softLight,
Image for post
SweepGradient with softlight blend
SweepGradient与柔光混合

There are other properties that Gradient class provides specific to Linear, Radial and Sweep types we saw above, such as tileMode , focal , radius , center , stops and so on. Since these are specific to Gradient and not ShaderMask, I used only the minimum basic properties for demo. I’d suggest you try out these properties to see how they help to enhance and change appearance of the image.

还有其他性质Gradient类提供特定于LinearRadialSweep我们上面看到的类型,如tileModefocalradiuscenterstops等。 由于这些是特定于Gradient而不是ShaderMask ,因此我仅将最小基本属性用于demo。 我建议您尝试这些属性,以了解它们如何帮助增强和更改图像的外观。

Take a moment to note the impact blendMode values make on the appearance of the image. You may compare the effect by removing the blendmode property to see the image being painted and then putting back the blending again to see the difference / impact.

花一点时间注意blendMode值对图像外观的影响。 您可以通过删除blendmode属性来比较效果,以查看正在绘制的图像,然后再次放回混合以查看差异/影响。

Mimicking photo booth using ShaderMask

使用ShaderMask模拟照相亭

As we saw above various gradients along with blend modes to achieve desired effects on an image, let’s put all this together in a demo to mimic an app.

正如我们在各种渐变以及混合模式上看到的那样,可以在图像上实现所需的效果,让我们将所有这些放到一个演示中以模拟一个应用程序。

Photo booth is a fun little app that can be used to apply effects on a picture from camera, as below:

照相亭是一个有趣的小应用程序,可用于在相机的图片上应用效果,如下所示:

Image for post

Let’s try to recreate some of these effects using Gradient and ShaderMask’s blend modes.

让我们尝试使用GradientShaderMask's混合模式重新创建其中一些效果。

  • Sepia: This effect can be achieved by providing dark and light brown color and specifying color blend mode:

    棕褐色:可通过提供深棕色和浅棕色并指定color混合模式来实现此效果:

body: Center(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
colors: [Color(0xFF704214), Colors.brown],
).createShader(bounds);
},blendMode: BlendMode.color,
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover)
)
)
)
Image for post
  • Black & White: This effect can be achieved by providing white and black color and specifying hue blend mode:

    黑白:可以通过提供白色和黑色并指定hue混合模式来实现此效果:

body: Center(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
colors: [Colors.black, Colors.white],
).createShader(bounds);
},blendMode: BlendMode.hue,
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover)
)
)
)
Image for post
  • Thermal Camera: This type of effect is similar to scanning a normal picture through a Infrared radiation device and depending on colors we pass, we will see thermal variation of the picture. This effect can be achieved by providing red , yellow and green colors along with exclusion blend mode.

    热像仪:这种效果类似于通过红外辐射设备扫描普通图片,并且根据我们通过的颜色,我们将看到图片的热变化。 通过提供redyellowgreen以及exclusion混合模式可以实现此效果。

body: Center(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
colors: [Colors.yellow, Colors.red, Colors.greenAccent],
).createShader(bounds);
},blendMode: BlendMode.exclusion,
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover)
)
)
)
Image for post
  • X-Ray: This property shows image in stark black and white with inverted effect. We can use combination of color hexa codes along with difference blend mode to achieve closest possible version of original x-ray effect:

    X射线:此属性以鲜明的黑白显示图像,并具有反转效果。 我们可以结合使用六色代码和difference混合模式来获得最接近原始X射线效果的版本:

body: Center(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF191a1b),
Color(0xFFc8dde7),
Color(0xFFa8d3e3),
Color(0xFF394346),
Color(0xFF293135),
],
).createShader(bounds);
},
blendMode: BlendMode.difference,
child: Container(
width: double.infinity,
height: double.infinity,
child: Image.asset('assets/jpg.jpg', fit: BoxFit.cover))))
Image for post

Although the effects may not be similar to what Photo Booth shows, my idea was to show that similar effects can be achieved using ShaderMask in combination with gradient and blend modes.

尽管效果可能与Photo Booth所显示的效果不同,但我的想法是表明可以将ShaderMask与渐变和混合模式结合使用来实现相似的效果。

In this article, we saw how ShaderMask widget can be helpful to apply color effects and change appearance of it’s child. Instead of image , we can use other widgets too, such as a Text .

在本文中,我们了解了ShaderMask小部件如何有助于应用颜色效果并更改其子级的外观。 除了image ,我们还可以使用其他小部件,例如Text

With this, I barely scratched the surface of ShaderMask and believe that the potential of using widget is huge by using various gradient properties in combination with blend modes.

这样,我几乎没有ShaderMask的表面,并相信通过将各种渐变属性与混合模式结合使用,使用小部件的潜力是巨大的。

That’s all I have for today. Thanks for reading and feel free to comment below your thoughts / suggestion / feedback on this topic.

这就是我今天要做的。 感谢您的阅读,并随时在您对这个主题的想法/建议/反馈下方发表评论。

I can be reached via Twitter and LinkedIn.

可以通过TwitterLinkedIn与我联系。

You may check out my Medium profile below for other articles I wrote on Flutter.

您可以在下面查看我的中型个人资料,以了解我在Flutter上撰写的其他文章。

翻译自: https://medium.com/flutter-community/an-overview-on-shadermask-89201539ba8d

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_26727575/article/details/108924699

智能推荐

SaaS到底是什么,如何做?这份笔记讲明白了_saas如何开发-程序员宅基地

文章浏览阅读1.3k次。阅读本篇文章,您将可以了解:1、什么是SaaS;2、SaaS的商业模式;3、SaaS的技术架构;4、国内比较好的SaaS平台。_saas如何开发

摄像头接入_大华sdk frealdatacallback码流输出链接-程序员宅基地

文章浏览阅读1.5k次。摄像头接入目前摄像头直播的方案主要有以下几种方式: rtsp方式接入,只能实现视频预览 国标协议接入,实现比较复杂,需要多实现SIP服务器 通过netsdk获取到视频码流,推流到流媒体服务器,通过wsflv,flv,hls等流媒体协议播放,H265不支持 一、采用方案对比后最终采用了第三种方式,java使用jna的方式接入大华netsdk,获取到dav视频码流后去除大华头尾,拿到H264裸码流,通过javacv(对ffmpeg、opencv等库的封装)推送到.._大华sdk frealdatacallback码流输出链接

git-commit-amend踩坑_git remote-tracking 本地commit amend不了-程序员宅基地

文章浏览阅读1.4k次。因为改动比较小,所以我不想重建一个commit,于是我是用了git commit --amend命令,由于之前已经将该commit推送到远程仓库,导致修改后推送失败。百度后发现如果你的commit已经push到了远程仓库,那么使用--amend修改commit后,git push时一定要使用 --force-with-lease 参数来强制推送,否则就会报错。这是我自己推送失败的例子解决方式一、第一种使用后git commit --amend -m "修改Git学习(三)指令"注意:-m “._git remote-tracking 本地commit amend不了

AI实验1——八数码问题_人工智能实验八数码难题-程序员宅基地

文章浏览阅读2.5k次,点赞6次,收藏15次。AI实验1——八数码问题一、实验目的与要求实验目的:1 . 熟悉状态空间表示法;2.掌握深度优先、广度优先和等代价无信息搜索算法;3.掌握启发式函数设计,实现面向实际问题的A*搜索算法;二、实验内容与方法实验内容:利用无信息搜索算法实现八数码难题求解;设计启发式信息函数,利用A*搜索实现八数码难题求解;三、实验步骤与过程1,问题分析在八数码难题中,我们使用状态空间表示法,将八数码矩阵(即矩阵的状态)设置为一个节点类(Node),各个节点之间通过操作集(Operater)[‘U_人工智能实验八数码难题

【Js canvas实现是否透明,抠图等功能原理】_js-demo抠图-程序员宅基地

文章浏览阅读2.8k次,点赞4次,收藏11次。透明背景转jpg格式后变黑我们先看demo,您可以狠狠地点击这里:png图片是否含有透明像素JS检测demo如果是不含透明色的PNG图片,则会提示不含透明;如果是,则提示含透明,如下截图:是否背景透明的检测检测原理是借助canvas的getImageData()方法,关于此方法具体API和使用,可以参见““像素点信息获取”这里的详细介绍。_js-demo抠图

[附源码]java毕业设计高校班主任班级管理系统_高校班级管理系统-程序员宅基地

文章浏览阅读1.1k次。的需求进行调查研究,在对系统进行认真分析之后,得出开发整个系统的各项需求。为降低整个系统的复杂度,而使其更加便于修改,提高代码的可读性,我们会将系统模块化,模块间保持相对独立,且每个模块只完成一个子功能,并且与其他模块通过简单的接口链接,即高内聚低耦合原则,而使整个系统能够拥有一个高性能的结构,这边是系统概要设计最重要的目的。所以该系统的开发实现了最大的意义和价值,在系统完成后,利益是否大过于成本,是否能够达到预期效果,这些方面都要进行可行性分析,再通过分析之后,就可以决定是否开发此系统。_高校班级管理系统

随便推点

Linux SSH密码暴力破解技术及攻防实战_password top 1000-程序员宅基地

文章浏览阅读6k次,点赞8次,收藏55次。对于Linux操作系统来说,一般通过VNC、Teamviewer和SSH等工具来进行远程管理,SSH是 Secure Shell的缩写,由IETF的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和加粗样式其他网络服务提供安全性的协议。利用SSH协议可以有效防止远程管理过程中的信息泄露问题。SSH客户端适..._password top 1000

html静态页面微信分享带缩略图,【荐】静态页面实现微信分享带缩略图、标题和描述...-程序员宅基地

文章浏览阅读1.2k次。这篇文章主要为大家详细介绍了【荐】静态页面实现微信分享带缩略图、标题和描述,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。静态页面实现微信分享带缩略图、标题和描述,想法很现实,要想实现这样的功能这里就要用到大ajax技术,在这里361源码分享给大家。服务端文件jssdk.php代码:..._html分享链接加图片和描述不需要调用jssdk

VASP学习1入门知识_vaspwiki-程序员宅基地

文章浏览阅读5.9k次,点赞10次,收藏63次。大师兄科研实例使用方法:1从最基本的计算开始,通过示例讲解,结合一些脚本的使用,引导大家思考解决自己的问题。因此,在这本书的学习过程里,每一章节会对应一个例子,大家务必手动搭建模型,输入文件(切忌复制粘贴),然后进行计算,得到和大师兄一致的结果。为了引导大家主动浏览官网解决问题,很多都会采用VASP官网的例子。2如何学习本书,大师兄在学习程序时,受learn_python_the_hard_w..._vaspwiki

想说好多话_好多好多话-程序员宅基地

文章浏览阅读222次。以前觉得深圳好热,可是现在觉得沈阳的风好冷…每天不需要说太多的话,只要敲着喜欢的键盘,默默的清理掉禅道上的bug就好,这样的工作似乎是充实的,却总是少点什么,我也喜欢上了活在自己的小世界里,喜欢上了听歌,喜欢去刷微博,喜欢上去看那些心灵鸡汤和搞笑的博主,喜欢看看最近的陈伟霆是不是有帅了,偶尔碰到一条好玩的微博似乎会笑上一会儿,可能作为一个程序猿,真的比较枯燥叭,有时觉得孤单的像一条_好多好多话

简述计算机维修 维护的基本原则是什么,计算机硬件维护的四大原则-程序员宅基地

文章浏览阅读2.4k次。计算机硬件维护的四大原则近年来,随着社会经济的快速发展和计算机网络技术的广泛应用,如今计算机已经成为生产生活中不可或缺的必需品,随之而来就出现一些问题。下面是YJBYS小编为大家搜索整理的关于计算机硬件维护的四大原则,欢迎参考阅读,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生培训网!对于计算机而言,主要包括硬件系统与软件系统两部分,其中硬件系统是软件系统功能得以实现的重要基础和前..._硬件维修的基本原则?

java有趣的技术分享ppt,java面试数据结构与算法高频考-程序员宅基地

文章浏览阅读736次。前言本文涵盖了阿里巴巴、腾讯、字节跳动、京东、华为等大厂的Java面试真题,不管你是要面试大厂还是普通的互联网公司,这些面试题对你肯定是有帮助的,毕竟大厂一定是行业的发展方向标杆,很多公司的面试官同样会研究大厂的面试题。与此同时,今年算法面试一定是会被问的,而算法不是光靠背面试题就有用的,它是需要数学逻辑思维的,因此,小编会在文末为大家准备一份非常优质的算法学习手册,重点在于学习思维方法,话不多说,直接开始上精选的大厂面试真题!在校生如果你是在校生,你应该趁着在学校的时间夯实基础(比如计算机系统、_java有趣的技术分享

推荐文章

热门文章

相关标签