首页 技术 正文
技术 2022年11月13日
0 收藏 750 点赞 2,659 浏览 1120 个字

哎!竭力想说清楚这个实现原理,并解释清楚shader里面的算法,结果发现越解释越不好理解,见谅!

一、实现目标:矩形四角是圆弧效果

二、实现的原理:通过每个角绘制1/4圆弧,剔除掉圆弧以外的部分。

原理图:[原]shader实现矩形圆角 [原]shader实现矩形圆角[原]shader实现矩形圆角

实现代码:

Shader "Custom/Test" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD
Blend SrcAlpha OneMinusSrcAlpha
pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex; struct Input {
fixed4 vertex:POSITION;
fixed2 texcoord :TEXCOORD0;
}; struct Output {
fixed4 pos:POSITION;
fixed2 uv :TEXCOORD0;
};
Output vert(Input i)
{
Output o;
o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
o.uv = i.texcoord;
return o;
} fixed4 frag(Output o):COLOR
{
fixed4 c = tex2D(_MainTex,o.uv);
float2 uv = o.uv.xy- float2(0.5);//UV 坐标默认是(0 0)左下角,减0.5 是把UV的中心点移到(0 0) 这样纹理坐标的范围就是 (-0.5,-0.5)~(0.5,0.5)
float rx = fmod(uv.x, 0.4); //fmod 求余函数,从原点开始 矩形的顶点坐标为0.4
float ry = fmod(uv.y, 0.4);
float mx = step(0.4, abs(uv.x)); // if 0.4>abs(uv.x)?0:1
float my = step(0.4, abs(uv.y));
float filletAlpha = - mx * my * step(0.1, length(half2(rx,ry))); //如果在圆角矩形内,alpha值为1 否则为0。 和0.1做比较 因为圆角半径为0.1
c = fixed4(c.r,c.g,c.b,filletAlpha);
return c;
}
ENDCG
}
}
}

注意:直接理解上面的计算圆角部分 有点难以理解,可以套到具体图里面 看效果推理就可以。

实现效果:

[原]shader实现矩形圆角

参考:

http://imgtec.eetrend.com/blog/3608

http://blog.sina.com.cn/s/blog_89d90b7c0102vayz.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852