当前位置:网站首页>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
边栏推荐
- golang fmt.printf()(转)
- 动画与过渡效果
- 392. 判断子序列
- Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
- 吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
- #yyds干货盘点# 解决名企真题:连续最大和
- go语言中的文件创建,写入,读取,删除(转)
- Getting started with microservices
- OpenHarmony应用开发之如何创建DAYU200预览器
- 基于链表管理的单片机轮询程序框架
猜你喜欢
1200. 最小绝对差
嵌入式编程中五个必探的“潜在错误”
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
字节面试算法题
Unittest框架中引入TestFixture
吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
392. 判断子序列
随机推荐
Lick the dog until the last one has nothing (state machine)
WS2811 M是三通道LED驱动控制专用电路彩灯带方案开发
find命令报错: paths must precede expression(转)
一次 Keepalived 高可用的事故,让我重学了一遍它
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
Five "potential errors" in embedded programming
How to choose a technology stack for web applications in 2022
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
C语言图书租赁管理系统
OPPO Find N2产品形态首曝:补齐各项短板
面试官:Redis中哈希数据类型的内部实现方式是什么?
如何在 2022 年为 Web 应用程序选择技术堆栈
美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一
基于链表管理的单片机轮询程序框架
国内酒店交易DDD应用与实践——代码篇
担心“断气” 德国正修改《能源安全法》
Node の MongoDB安装
php 日志调试
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect