Color Mask解释,见unity文档:

ColorMask

ColorMask RGB | A | 0 | any combination of R, G, B, A

Set color channel writing mask. Writing ColorMask 0 turns off rendering to all color channels. Default mode is writing to all channels (RGBA), but for some special effects you might want to leave certain channels unmodified, or disable color writes completely.

When using multiple render target (MRT) rendering, it is possible to set up different color masks for each render target, by adding index (0–7) at the end. For example, ColorMask RGB 3 would make render target #3 write only to RGB channels.

如下面问题,这是通过RenderTexture渲染了一个粒子,挂在NGUI的UITexture中。

希望去掉黑色区域而完全展现背景颜色,可通过ColorMask解决。

摄像机颜色为:

粒子shader为:

 Shader "effect/distortadd Lv3"
{
Properties
{
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_NoiseTex ("Distort Texture (RG)", 2D) = "white" {}
_MainTex ("Alpha (A)", 2D) = "white" {}
_HeatTime ("Heat Time", range (-,)) =
_ForceX ("Strength X", range (,)) = 0.1
_ForceY ("Strength Y", range (,)) = 0.1
} Category
{
Tags { "Queue"="Transparent+400" "RenderType"="Transparent" }
Blend SrcAlpha One
Cull Off Lighting Off ZWrite Off Fog { Color (,,,) }
BindChannels
{
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
} SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_particles
#include "UnityCG.cginc" struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord: TEXCOORD0;
}; struct v2f
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 uvmain : TEXCOORD1;
}; fixed4 _TintColor;
fixed _ForceX;
fixed _ForceY;
fixed _HeatTime;
float4 _MainTex_ST;
float4 _NoiseTex_ST;
sampler2D _NoiseTex;
sampler2D _MainTex; v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
return o;
} fixed4 frag( v2f i ) : COLOR
{
//noise effect
fixed4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz*_HeatTime);
fixed4 offsetColor2 = tex2D(_NoiseTex, i.uvmain + _Time.yx*_HeatTime);
i.uvmain.x += ((offsetColor1.r + offsetColor2.r) - ) * _ForceX;
i.uvmain.y += ((offsetColor1.r + offsetColor2.r) - ) * _ForceY;
return 2.0f * i.color * _TintColor * tex2D( _MainTex, i.uvmain);
}
ENDCG
}
}
// ------------------------------------------------------------------
// Fallback for older cards and Unity non-Pro SubShader
{
Blend DstColor Zero
Pass
{
Name "BASE"
SetTexture [_MainTex] { combine texture }
}
}
}
}

shader使用图片为:

NGUI中 UITexture使用的shader为:

 Shader "Unlit/Premultiplied Colored"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
AlphaTest Off
Fog { Mode Off }
Offset -, -
ColorMask RGB
Blend One OneMinusSrcAlpha CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
float4 _MainTex_ST; struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
}; struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
}; v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
return o;
} half4 frag (v2f IN) : COLOR
{
half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
return col;
}
ENDCG
}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
AlphaTest Off
Fog { Mode Off }
Offset -, -
ColorMask RGB
Blend One OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse SetTexture [_MainTex]
{
Combine Texture * Primary
}
}
}
}

只所以出现黑色,这里有两层混合,一层是摄像机中此粒子与摄像机默认颜色,因为此shader渲染出来的颜色就是黑色(见图片颜色),alpha也全为1,再通过Blend SrcAlpha One命令混合,而目标颜色为黑色,alpha为0(见摄像机颜色),因此这一层混合后只剩下图片的颜色。

第二层混合在NGUI的UITexture中,此UITexture使用shader的混合命令为:Blend One OneMinusSrcAlpha,由于srcAlpha一直为1,所以UITexture后面的背景颜色在混合中消失了,渲染出来的结果只剩下图片的颜色。

要想去除黑色,可以通过 ColorMask RGB来屏蔽此通道输出的alpha,而完全取摄像机的alpha(即为0)。见shader代码:

此时效果如下:

转载请注明出处:https://www.cnblogs.com/jietian331/p/10675265.html

之所以ColorMask会解决这个问题,是因为ColorMask和Blend命令的执行先后顺序,先Blend,再ColorMask,这样Blend时使用frag shader中输出的alpha,保持了rgb颜色的正常,再color mask,屏蔽alpha通道,此时会去取摄像机的alpha,即为0。

而UITexture使用shader的混合命令为:Blend One OneMinusSrcAlpha,所以黑色在混合时消失了,其它颜色保留下来了,且能与背景很好地融合。

资源如下:

https://files.cnblogs.com/files/jietian331/ColorMask.zip

Unity shader之ColorMask的更多相关文章

  1. Unity Shader 知识点总结(二)

    紧接着上一篇文章的shader入门知识的总结,本文主要总结shader中的纹理贴图.透明度混合.顶点动画.后期特效处理等操作.如果有什么地方有错,请指出更正,谢谢.本文的代码主要来自开源书:unity ...

  2. 【原】Unity Shader VS UDK Material Editor

    UDK 的材质编辑器十分好用,毕竟是所见即所得的.虽然unity也有类似第三方插件,但易用性还是差很多,下面主要是,把一些常见表达式概念对应起来. 1. UDK CameraVector (相机位向量 ...

  3. Unity3D学习(八):《Unity Shader入门精要》——透明效果

    前言 在实时渲染中要实现透明效果,通常会在渲染模型时控制它的透明通道. Unity中通常使用两种方法来实现透明 :(1)透明度测试(AlphaTest)(2)透明度混合(AlphaBlend).前者往 ...

  4. Unity Shader 入门精要学习 (冯乐乐 著)

    第1篇 基础篇 第1章 欢迎来到Shader的世界 第2章 渲染流水线 第3章 Unity Shader 基础 第4章 学习Shader所需的数学基础 第2篇 初级篇 第5章 开始Unity Shad ...

  5. Unity Shader入门精要学习笔记 - 第12章 屏幕后处理效果

    建立一个基本的屏幕后处理脚本系统 屏幕后处理,顾名思义,通常指的是在渲染完整个场景得到屏幕图像后,再对这个图像进行一系列操作,实现各种屏幕特效.使用这种技术,可以为游戏画面添加更多艺术效果,例如景深. ...

  6. Unity Shader入门精要学习笔记 - 第8章 透明效果

    转载自 冯乐乐的 <Unity Shader入门精要> 透明是游戏中经常要使用的一种效果.在实时渲染中要实现透明效果,通常会在渲染模型时控制它的透明通道.当开启透明混合后,当一个物体被渲染 ...

  7. Unity Shader之模板测试

    Unity Shader之模板测试 一沙一世界,一花一天堂 一.Stencil testing 渲染管线     当片段着色器处理完一个片段之后,模板测试(Stencil Test)会开始执行,和深度 ...

  8. Unity Shader入门

    Unity Shader入门 http://www.cnblogs.com/lixiang-share/p/5025662.html http://www.manew.com/blog-30559-1 ...

  9. Unity Shader IDE — Sublime Text2

    使用MonoDevelop写了一段时间的Shader代码,发现效率太低了,所以换用Sublime Text. 安装Sublime Text 1.下载 sublime Text2 官网:http://w ...

随机推荐

  1. 消息队列——ActiceMQ

    1.下载apache-activemq-5.xx.x,\bin\win64目录下运行activemq.bat.之后可进入管理员界面http://localhost:8161/admin,账号密码均为a ...

  2. 微信小程序--家庭记账本开发--03

    组件.标签以及模板的使用 在一个微信小程序中,需要用到大量的组件,一些页面的设计也需要模板,在自己所学课程中,对于一些组件.标签模板的使用有了初步的了解. 1.组件 组件是数据和方法的简单封装,对于微 ...

  3. Centos6.5升级openssh、OpenSSL和wget

    1.OpenSSL 1.1.查看版本 使用如下命令查看版本: openssl version 1.2.安装gcc依赖 yum -y install gcc gcc-c++ 1.3.安装配置 ./con ...

  4. 解决sql_mode=only_full_group_by的问题

    1.mysql查询报错: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_ ...

  5. 移动端滑动效果 swiper 4.0.7

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  6. springboot 启动排除某些bean 的注入

    问题: 最近做项目的时候,需要引入其他的jar.然后还需要扫描这些jar里的某些bean.于是使用注解:@ComponentScan这个注解直接指定包名就可以,它会去扫描这个包下所有的class,然后 ...

  7. Yarn任务提交流程(源码分析)

    关键词:yarn rm mapreduce 提交 Based on Hadoop 2.7.1 JobSubmitter addMRFrameworkToDistributedCache(Configu ...

  8. (50)Wangdao.com第七天_JavaScript 发展与简介

    一个完整的JavaScript 应该由以下三部分组成: ECMAScript DOM,全称Browser Object Model,即浏览器对象模型,主要处理浏览器窗口和框架 BOM,全称Docume ...

  9. vue_过滤器: 对要显示的数据进行特定格式化后再显示

    过滤器 对要显示的数据进行特定格式化后再显示 并未改变原本的数据,可是产生新的对应的数据 <!DOCTYPE html> <html lang="en"> ...

  10. export和export default

    在  vue中 export  变量名不能识别,只能export default 变量,import 随便取名,不需要{}