当前位置:网站首页>Unity shader learning (3) try to draw a circle
Unity shader learning (3) try to draw a circle
2022-07-04 14:06:00 【ToDoNothing】
On the first code
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
}
}
}
Here's an explanation
According to the second tutorial , We are Pass Two processing flows are defined in the block , Respectively vertex and fragment, Execute first vertex Vertex shader , Processing vertex information , Proceed again fragment Chip shader , Deal with color, illumination and other information . So we define a structure at the top , Information used to define a vertex :
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD0;
};
You can see , The defined structure contains two Texcoord, Texture information , Only two groups are read here . In addition to that SV_POSITION Is the position of the vertex , except SV_POSITION, There is another parameter called POSITION, There is not much difference between the two , As follows :
POSITION: Used to store , Model in local coordinates , In model space (objcet space) The coordinates of the vertices of , Coordinates before converting to clipping space coordinates ,unity Tell our model vertex coordinates , Not converted . Can be used as vertex shader (vertex shader) The input of 、 Output ; Chip shader (frag) The input of .
SV_POSITION: Used to store , The model is in the clipping space , Location information in projection space , That is, the fixed-point coordinates of the model space , Convert to the coordinates of the clipping space , Can be used as vertex shader (vertex shader) Output ; Chip shader (frag) The input of .
Next is the processing of vertex shaders , namely vert function , Corresponding to the above #pragma vertex Name , As shown below , Input appdata_base For defined built-in variables , Never mind , The whole function returns a structure , Internal variables have been re assigned .
Such as o.vertex, Through the built-in conversion function UnityObjectToClipPos, Model space , Convert to cutting space . About model space 、 The concept of cutting space , Reference resources Render several basic coordinate spaces in the pipeline ( Object space 、 World space 、 Camera space ( Observe the space )、NDC Space 、 Cutting space 、 Screen space )
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
return o;
}
When it's done , Handle the slice shader
fixed4 frag (v2f i) : SV_Target
{
fixed4 col=circle(i.uv,float2(0.5,0.5));
return col;
}
The slice shader just runs a function , Color the current point , So let's look at this function
float circle(float2 uv,float2 center){
float2 offset=uv-center;
float len=length(offset);
return step(len,0.2)-step(len,0.19);
}
circle Function has parameters and return values , Input two coordinate information , One is the coordinates of the vertex , One is the center point of the circle , First , Calculate the distance between the vertex coordinate and the center point coordinate , Reuse step Function to process .
step The logic of the function is as follows :
step (a, b)
{
if (a > b)
{
return 0;
}
else
{
return 1;
}
}
therefore , Its principle is to judge the distance between the current point and the central point , Whether it is within the appropriate range , If in , return 1, Indicates that colors can be filled , Here I use two step function , Make him form a circle , In this range , Just display the color , As shown in the figure below . Two Step Subtracting the , You can judge whether it is within this range .
The specific effect is as follows :
If you want to change the color , Radius and center , The code is as follows
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
}
}
}
The effect is as follows
边栏推荐
- 吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符
- Worried about "cutting off gas", Germany is revising the energy security law
- DGraph: 大规模动态图数据集
- 【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
- Go 语言入门很简单:Go 实现凯撒密码
- JVM memory layout detailed, illustrated, well written!
- 一次 Keepalived 高可用的事故,让我重学了一遍它
- MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
- C语言程序设计
- The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
猜你喜欢
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
逆向调试入门-PE结构-资源表07/07
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
小程序直播 + 电商,想做新零售电商就用它吧!
自主工业软件的创新与发展
. Net delay queue
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
基于YOLOv1的口罩佩戴检测
软件测试之测试评估
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
随机推荐
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
MySQL version 8 installation Free Tutorial
IP 实验室月复盘 · 第 5 期
锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
Install Trinity and solve error reporting
逆向调试入门-PE结构-资源表07/07
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
基于PaddleX的智能零售柜商品识别
软件测试之测试评估
Interviewer: what is the internal implementation of hash data type in redis?
C language programming topic reference
MySQL 5 installation and modification free
2022危险化学品经营单位主要负责人练习题及模拟考试
Understanding and difference between viewbinding and databinding
go vendor 项目迁移到 mod 项目
程序员转方向
Qt如何实现打包,实现EXE分享
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
C语言个人通讯录管理系统