当前位置:网站首页>Shadertoy basic teaching 02. Drawing smiling faces
Shadertoy basic teaching 02. Drawing smiling faces
2022-06-23 04:57:00 【Zonghao duolao】
1、Shadertoy Link to the original teaching video
2、Shadertoy Website links
List of articles
1 Explain
First, let's encapsulate the function of drawing a circle
// Shape parameter ( Texture coordinates , center of a circle , radius , Boundary blur length )
float Circle(vec2 uv, vec2 p, float r, float blur)
{
float d = length(uv - p);
float c = smoothstep(r, r - blur, d); // Give Way t1 > t2, The pixels in the circle are 1, Outer circle is 0
return c;
}
First draw a yellow head
vec3 col = vec3(1.0, 1.0, 0.0); // Background color
float mask = Circle(uv, vec2(0.0), 0.4, 0.05); // Mask , A very vivid name , Draw a circle here , Inside the circle is 1, Outer circle is 0
col *= mask; // Pixels inside the circle col * 1 It's yellow , Outer circle is col * 0 For the black
fragColor = vec4(vec3(col), 1.0);
Draw eyes , By addition and subtraction , We define the second circle as the left eye , It's still the same , The pixels in the circle are 1, The pixels outside the circle are 0, On the basis of the head , Subtract this circle , So both are 1 The part of is subtracted to be 0 了 , Right eye empathy .( This is actually a union of graphs 、 The problem of intersection )
float mask = Circle(uv, vec2(0.0) , 0.4, 0.05); // head
mask -= Circle(uv, vec2(-0.15, 0.1), 0.07, 0.05); // The left eye
mask -= Circle(uv, vec2( 0.15, 0.1), 0.07, 0.05); // Right eye

Make the mouth part : The first circle is the head , The second circle is offset slightly upwards 0.1, Subtract .
float mouth = Circle(uv, vec2(0.0), 0.3, 0.01);
mouth -= Circle(uv, vec2(0.0, 0.1), 0.3, 0.01);

Then put the above results subtract The mouth part can make a smiling face
2 Complete code
float Circle(vec2 uv, vec2 p, float r, float blur)
{
float d = length(uv - p);
float c = smoothstep(r, r - blur, d);
return c;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
uv.x *= iResolution.x / iResolution.y;
vec3 col = vec3(1.0, 1.0, 0.0);
float mask = Circle(uv, vec2(0.0), 0.4, 0.05);
mask -= Circle(uv, vec2(-0.15, 0.1), 0.07, 0.01);
mask -= Circle(uv, vec2(0.15, 0.1), 0.07, 0.01);
float mouth = Circle(uv, vec2(0.0), 0.3, 0.01);
mouth -= Circle(uv, vec2(0.0, 0.1), 0.3, 0.01);
if (mouth < 0.0) // The two circles of the mouth are subtracted , It will make the color value of some pixels on the top of the head as -1, prevent bug, Set them to zero
mouth = 0.0;
mask -= mouth;
col *= mask;
fragColor = vec4(col, 1.0);
}
effect 
边栏推荐
- How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
- Laravel customizes the paging style through the service provider
- 独立站聊天机器人有哪些类型?如何快速创建属于自己的免费聊天机器人?只需3秒钟就能搞定!
- Can bus Basics
- win10下安装、运行MongoDB
- Ad9 tips
- notepad++ 查找替换之分组替换保留
- Receive incoming files and download (simple usage) a tag
- Abnova blood total nucleic acid purification kit protocol
- Abnova 荧光染料 555-C3 马来酰亚胺方案
猜你喜欢
随机推荐
Current relay hdl-a/1-110vdc-1
Abnova 荧光染料 510-M 链霉亲和素方案
Altium designer 09 screen printing displays a green warning near the pad. How to prevent it from alarming?
The solution to prompt "this dictionary creation could be rewritten as a dictionary literal" when updating the dictionary key value in pychart
使用Live Chat促进业务销售的惊人技巧
A mvc5+easyui enterprise rapid development framework source code BS framework source code
396. 矿场搭建
free( )的一个理解(《C Primer Plus》的一个错误)
静态双位置继电器 XJLS-84/440/DC220V
数据科学家是不是特有前途的职业?
PaddlePaddle模型服务化部署,重新启动pipeline后出现报错,trt报错
Second assignment notes
开关磁阻电机悬浮驱动IR2128小结
Openjudge noi 1.13 51: ancient password
win10下安装、运行MongoDB
Abnova liquidcell negative enrichment cell separation and recovery system
Abnova abcb10 (human) recombinant protein specification
Usage of apipost interface test ------ get
Thinkphp6 linked table query without associated ID (2 tables) is mainly the application of select
Openjudge noi 1.13 50: several









