当前位置:网站首页>Unity shader - Laser special effect shader[easy to understand]
Unity shader - Laser special effect shader[easy to understand]
2022-07-27 12:02:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Study Shader It has been several months ,Shader As a door GPU Programming languages are really hard to learn . The main reason after my thinking is that my own computer graphics and art foundation is not solid . So helpless , As a programmer who only wrote code by hand before, I really feel bad .
Learning period , I also want to Shader Great God asks for learning experience , Most of the responses I received were to let me study computer graphics well . ha-ha , So , I went to see computer graphics for more than a month . Then come back to study again Shader, But although it is better than before , But in most cases, his eyes are black .
So , I also thought of the famous saying of a great man : Practice is the only way to get truth . ha-ha , Don't hit me , I didn't mean to talk nonsense ( Actually, it's intentional ..... Come and hit me ). So how can we learn when the foundation is not very solid Shader Well ? I thought for a long time , After watching CSDN Shader The little girl who eats watermelon ( Don't hit me , The name of the great God is too long .....)” Mother said that girls should stand on their own ” After the blog of , I seem to have found a little inspiration . I decided to Unity The built-in Shader Start with , Introduce in detail some of the good and practical effects I have seen Shader. I also hope that all gods can criticize and guide what I write , Let us novices improve and make progress together , ha-ha .
Temporarily because of work , I'm going to update it at the rate of one article per week , It depends on the future arrangement , I will cheer up .....
This time, let's take a look at one I used when I was practicing in a project recently shader:
See the figure below for the effect :
This is a laser special effect I made before Shader, It's for hand training .
shader The main code of is as follows :
Shader "Particles/Additive" {
Properties {
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha One
ColorMask RGB
Cull Off Lighting Off ZWrite Off
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_particles
#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _TintColor;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_FOG_COORDS(1)
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD2;
#endif
};
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
sampler2D_float _CameraDepthTexture;
float _InvFade;
fixed4 frag (v2f i) : SV_Target
{
#ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif
fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
return col;
}
ENDCG
}
}
}
}This Shader You gods can see it at a glance , I won't pretend that I wrote it , Otherwise, I'm estimated to be beaten in the face by all kinds of slap slap slap mercilessly , Ha ha ha ha ha ha . This is a Unity Bring one with you Shader, The main functions are color mixing , Turn on the fog effect , Plus transparency . Have to say Unity Self contained shader Really amazing. , It's really not easy to write so many functions so succinctly .
I'll stop gossiping and talking more , Otherwise, you may be killed , Ha ha ha ha ha ha ha . Because I also want to write an article to consolidate the foundation , So don't dislike me for being verbose , Then I'll talk more .
_TintColor (“Tint Color”, Color) = (0.5,0.5,0.5,0.5) This sentence is in Shader The color set in the panel to mix , There is a RGBA The default value of .
_MainTex (“Particle Texture”, 2D) = “white” {} This is Shader Set the picture for color mixing in the panel .
_InvFade (“Soft Particles Factor”, Range(0.01,3.0)) = 1.0 This is the value of setting a range , It is used to let others adjust the final effect , Because it's better to let some professional people adjust some effects .
Tags { “Queue”=”Transparent” “IgnoreProjector”=”True” “RenderType”=”Transparent” } “Queue”=”Transparent” This sentence means to open the path of transparency ,”IgnoreProjector”=”True”
It means to turn on the effect of shadow ,”RenderType”=”Transparent” Is the category that enables rendering .
Blend SrcAlpha One Only this type alphaa Mixed mode , In fact, this is the sum of the color value you get and alpha Just a formula for mixing , It's just a shorthand . ColorMask RGB Color channel mask , It means that the last output color channel is RGB passageway Cull Off Lighting Off ZWrite Off Turn off culling Turn off the effect of light Turn off the depth cache ( This operation will actually cause some problems )
struct appdata_t { float4 vertex : POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; };
struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; UNITY_FOG_COORDS(1) #ifdef SOFTPARTICLES_ON float4 projPos : TEXCOORD2; #endif };
These are actually defined attribute values , I won't list them all , ha-ha .
float4 _MainTex_ST; Don't look at this, there is only this little sentence , In fact, there are many famous halls inside .
indeed for any texture property, Unity provides value for float4 with “_ST” suffix. The x,y contains texture scale, and z,w contains translation (offset).
_ST The variables are 4 It's worth ,xy yes texture scale,zw yes offset.
stay Material Can be set in Tiling Namely xy,Offset Namely zw.
From this we can see that , although Unity Self contained Shader Very concise code , It looks much simpler than those thousands of lines of code that can't move , But it actually secretly helps us package a lot of things , This also lets us learn unity Bring their own shader It's a lot more difficult . therefore , This is why I want to write this blog .
v2f vert (appdata_t v) This sentence means fixed-point shader , The returned value is what we defined above v2f attribute , Speaking of this, I can't help but roast . Used a while ago unity Self contained surface shader, I have defined an attribute , The result is not in the fixed-point shader , That is to say vert It gives him the initial value , therefore shader Has been reporting a mistake , My day, . Fortunately, in the end , Our brilliant God came to help me solve this problem .....
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); This is a MVP Transformation matrix , It means to change the fixed-point coordinates from the world coordinate system , Transform into screen coordinate system , This contains at least three transformation matrices , The code will be very long , Thank you almighty Unity It's sealed for us .( Although I'm not sure Unity Encapsulated or CG This language encapsulates , But let me pretend to be a little bit forced here ...... Think of the famous saying of a friend of mine : What's the difference between people who don't pretend to be forced and salted fish Ha ha ha ha ha ha ha ).
#ifdef SOFTPARTICLES_ON o.projPos = ComputeScreenPos (o.vertex); COMPUTE_EYEDEPTH(o.projPos.z); #endif o.color = v.color; o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex); UNITY_TRANSFER_FOG(o,o.vertex);
The meaning of this code is based on z To control the scaling and rotation of the special effects , This is a Unity 5.0 It contains new shader, There are many things in it , All we see is the parameters passed into the method by ourselves . See our super powerful Yusong for details MOMO The blog of , There are explanations http://www.xuanyusong.com/?p=3486( Mercilessly throw the pot Ha ha ha ha ha ha ha )
float _InvFade; Look directly at the code _InvFade, This is a parameter that controls transparency ( Match the difference between the depth of particles far and near the screen and the depth of the scene to present the effect ) It is similar to a professional explanation of the great God of Art unreal Of alpha bias Material nodes , Used to soften the interface between particle side and solid , Avoid intersecting lines with patches .
#ifdef SOFTPARTICLES_ON float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))); float partZ = i.projPos.z; float fade = saturate (_InvFade * (sceneZ-partZ)); i.color.a *= fade; #endif
This sentence is a way to show the effect of special effects on the depth of the scene and the distance from the screen . Everyone can see it from here , This Shader In fact, it is a special one for special effects Shader, Similar to laser or 2D The knife light effect in the game can be made using this method , Tell you a little secret , quite a lot Unity The above special effect plug-ins use this Shader To make the , Ha ha ha ha ha ha ha .
fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord); The meaning of this sentence is to deal with the previous series i.color For simple color blending , As for that 2.0f, In fact, that is to write this shader The great God defined it when he adjusted the color . Computer graphics is actually a subject that deceives people's eyes to a great extent , Ha ha ha ha ha ha ha .
UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); This sentence means setting the orientation and color of the fog , In fact, this is also Unity A self-contained method of encapsulating fog .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128219.html Link to the original text :https://javaforall.cn
边栏推荐
- 微博评论爬虫+可视化
- kazoo使用教程
- Finding the finite zero point of transfer function under different sampling periods
- How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
- Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
- JS parasitic combinatorial inheritance
- Could not load dynamic library ‘libcudnn.so.8‘;
- A possibility that ch340 module cannot be recognized / burned
- 查看系统下各个进程打开的文件描述符数量
- Sword finger offer note: T39. Numbers that appear more than half of the time in the array
猜你喜欢

Shell script text three swordsman awk
![[unity entry program] creator kitfps: first person shooting 3D game](/img/2b/78b535973b2898f53752ceeb25ef01.png)
[unity entry program] creator kitfps: first person shooting 3D game

MATLAB画带延时系统的伯德图

Shake quickly to rescue the "frustrated person"

阿里云云数据库RDS版Exception during pool initialization

I do live e-commerce in tiktok, UK

Regular expression of shell programming (grep of shell script text three swordsmen)

图像分割 vs Adobephotoshop(PS)

Shell编程之正则表达式(Shell脚本文本三剑客之grep)

Interaction free shell programming
随机推荐
Regular expression of shell programming (grep of shell script text three swordsmen)
微博评论爬虫+可视化
Principle of control system based on feedback rate
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
Lonely young people can't quit jellycat
Analysis of the use of JUC framework from runnable to callable to futuretask
How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
箱型图介绍
MySQL数据库主从复制集群原理概念以及搭建流程
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
【产品】关于微信产品分析
Firewall firewall
日本福岛废堆安全监视协议会认可排海计划“安全”
Leetcode 01: t1. sum of two numbers; T1108. IP address invalidation; T344. Reverse string
STS下载教程(include官网无法下载解决方案)
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
Database cli tool docker image