修改Shader让NGUI支持灰色滤镜 [Unity3D]
来源:互联网 发布:现在做淘宝还有前途吗 编辑:程序博客网 时间:2024/06/06 00:28
转载地址:http://chengduyi.com/blog/?post=43
之前有人做过,不过效率不高: http://blog.csdn.net/onerain88/article/details/12197277
他的代码:
fixed4 frag (v2f i) : COLOR{ fixed4 col; if (i.color.r < 0.001) { col = tex2D(_MainTex, i.texcoord); float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); col.rgb = float3(grey, grey, grey); } else { col = tex2D(_MainTex, i.texcoord) * i.color; } return col;}
通过修改后效率明显大幅提升:
struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; fixed4 color : COLOR; fixed gray : TEXCOORD1; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); o.color = v.color; o.gray = dot(v.color, fixed4(1,1,1,0)); return o; } fixed4 frag (v2f i) : COLOR { fixed4 col; if (i.gray == 0) { col = tex2D(_MainTex, i.texcoord); col.rgb = dot(col.rgb, fixed3(.222,.707,.071)); } else { col = tex2D(_MainTex, i.texcoord) * i.color; } return col; }
原理是在v2f中申请一个寄存器TEXCOORD1放置数据gray,并在顶点程序vert中计算是否灰色,在片段程序frag中用i.gray == 0做if判断。
第一段代码效率低的原因是if (i.color.r < 0.001)这里访问了color的分量r,但在官方文档中明确说了这样做效率低,除非必要情况时才使用。
另外在PC中(fixed gray : TEXCOORD1;)可以改成(bool gray : TEXCOORD1;),但在移动设备(android和ios)中都会有问题,求解。
0 0
- 修改Shader让NGUI支持灰色滤镜 [Unity3D]
- Unity3D NGUI中文支持
- Unity3D 灰度shader(改编自NGUI)
- Unity3d NGUI如何支持中文
- NGUI图片变灰Shader修改
- 灰色CSS滤镜
- 灰色CSS滤镜 grayscale
- houdini中让shader支持udim texture
- unity让shader支持UGUI Mask
- 让IjkPlayer支持插入自定义的GPU滤镜
- unity3d ngui右键菜单经常失效+无法修改坐标值
- Unity3d NGUI Button 简单地动态修改颜色
- unity3d NGUI满足不了需求,修改源代码记录
- [Unity3D] Dynamic Font for NGUI 2.5.0c,动态字体脚本支持NGUI最新版本
- Unity3d Shader
- unity3d shader
- Unity3D shader
- Unity3D Shader
- mybatis在xml文件中处理大于号小于号的方法
- Swift面向协议编程
- Oracle 查询优化器 -- 改写查询语句
- SpringMVC的各种参数绑定方式
- 列表排序
- 修改Shader让NGUI支持灰色滤镜 [Unity3D]
- 2012年5月SAT香港真题解析
- java list 交集 并集 差集 去重复并集
- binder驱动-------之内存映射篇
- Linux下获取硬盘空间的大小
- 偷懒代码块之保持屏幕常亮
- Android 获取手机中的图片信息的两种方法
- iOS 系统消息通知
- 关于 NSURL 的解析和编码