当前位置:网站首页>Unity Shader学习(三)试着绘制一个圆
Unity Shader学习(三)试着绘制一个圆
2022-07-04 12:50:00 【ToDoNothing】
先上代码
Shader "Unlit/shader4"
{
Properties
{
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD0;
};
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
return o;
}
float circle(float2 uv,float2 center){
float2 offset=uv-center;
float len=length(offset);
return step(len,0.2)-step(len,0.19);
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col=circle(i.uv,float2(0.5,0.5));
return col;
}
ENDCG
}
}
}
这里解释一下
根据第二个教程,我们在Pass块里定义了两个处理流程,分别为vertex和fragment,先执行vertex顶点着色器,处理顶点信息,再进行fragment片元着色器,处理颜色光照等信息。因此我们在最上面先定义了一个结构体,用来定义一个顶点的信息:
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD0;
};
可以看到,定义的结构体包含了两个Texcoord,即纹理信息,这里只读取了两组。除此之外还有SV_POSITION为顶点的位置,除了SV_POSITION,还有一个参数叫做POSITION,二者的区别不大,具体如下:
POSITION:用来存储,模型在本地坐标下,模型空间中(objcet space)的顶点坐标,转换为剪裁空间坐标前的坐标,unity告诉我们的模型顶点坐标,没经过转换的。可用作顶点着色器(vertex shader)的输入、输出;片元着色器(frag)的输入。
SV_POSITION:用来存储,模型在剪裁空间,投影空间中的位置信息,即把模型空间的定点坐标,转化为剪裁空间的坐标,可用作顶点着色器(vertex shader)的输出;片元着色器(frag)的输入。
接下来是顶点着色器的处理,即vert函数,要对应上面#pragma vertex的名字,如下所示,输入的appdata_base为已定义好的内置变量,不用管,整个函数返回一个结构体,内部的变量经过了重新赋值。
如o.vertex,通过内置的转换函数UnityObjectToClipPos,将模型空间,转换成裁切空间。关于模型空间、裁切空间的概念,参考渲染管线中几种基础的坐标空间(对象空间、世界空间、相机空间(观察空间)、NDC空间、裁剪空间、屏幕空间)
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
return o;
}
完成之后,进行片元着色器的处理
fixed4 frag (v2f i) : SV_Target
{
fixed4 col=circle(i.uv,float2(0.5,0.5));
return col;
}
片元着色器只是运行了一个函数,对当前的点进行颜色赋予,因此我们看一下这个函数
float circle(float2 uv,float2 center){
float2 offset=uv-center;
float len=length(offset);
return step(len,0.2)-step(len,0.19);
}
circle函数有参有返回值,输入了两个坐标信息,一个是顶点的坐标,一个是圆的中心点,首先,计算顶点坐标与中心点坐标的距离,再使用step函数进行处理。
step函数使用逻辑如下:
step (a, b)
{
if (a > b)
{
return 0;
}
else
{
return 1;
}
}
因此,它的原理就是判断当前点离中心点的距离,是否在合适的范围之内,如果在,返回1,表明可以填充颜色,这里我使用了两个step函数,使他形成一个圆环,在这个范围内的,就显示颜色,如下图所示。两个Step相减,就可以判断是否在这个范围内。
具体效果如下:
如果想改变颜色,半径和圆心,代码如下
Shader "Unlit/shader4"
{
Properties
{
_Radius("Radius",Float)=0.1
_Center("Center",Vector)=(0.5,0.5,0,0)
_LineColor("LineColor",Color)=(1,1,1,1)
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD0;
};
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
return o;
}
float _Radius;
float4 _Center;
fixed4 _LineColor;
float circle(float2 uv,float2 center){
float2 offset=uv-center;
float len=length(offset);
return step(len,_Radius+0.01)-step(len,_Radius);
}
fixed4 frag (v2f i) : SV_Target
{
float2 center=float2(_Center.x,_Center.y);
fixed4 col=circle(i.uv,center)*_LineColor;
return col;
}
ENDCG
}
}
}
效果如下
边栏推荐
- DGraph: 大规模动态图数据集
- Unittest中的TestSuite和TestRunner
- C language programming topic reference
- C language dormitory management query software
- Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
- remount of the / superblock failed: Permission denied
- C language staff management system
- C语言宿舍管理查询软件
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
- 国内酒店交易DDD应用与实践——代码篇
猜你喜欢
Fisher信息量检测对抗样本代码详解
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
JVM 内存布局详解,图文并茂,写得太好了!
近日小结(非技术文)
Five "potential errors" in embedded programming
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
Automatic filling of database public fields
Interviewer: what is the internal implementation of hash data type in redis?
Go 语言入门很简单:Go 实现凯撒密码
随机推荐
Fisher信息量检测对抗样本代码详解
IP 实验室月复盘 · 第 5 期
Read excel table data
sharding key type not supported
MySQL 5 installation and modification free
Getting started with microservices
Worried about "cutting off gas", Germany is revising the energy security law
美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一
小程序直播 + 电商,想做新零售电商就用它吧!
Introduction to reverse debugging PE structure resource table 07/07
Unittest框架之断言
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
自主工业软件的创新与发展
Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
结合案例:Flink框架中的最底层API(ProcessFunction)用法
C语言集合运算
做事的真正意义和目的,真正想得到什么
Introduction to XML II