当前位置:网站首页>[unity shader stroke effect _ case sharing first]
[unity shader stroke effect _ case sharing first]
2022-07-01 06:25:00 【The king of scrolls is coming】
1. Implementation logic
Stroke effect Shader There are several ways to do this , Can pass post-processing and MatCap Realization .
What I want to show this time is through two Pass Realization .
When Shader There are multiple Pass when , The rendering process is executed in the order of installation , So the back Pass The effect will cover the front Pass The drawn image .
The logic of implementation is :
first floor , Expand the vertices of the model along the normal direction for a distance , The expanded distance is the width of the stroke , The expanded distance can be opened to the material panel as an attribute , Convenient to call . Then assign a solid color to the expanded model for coloring , Shading does not need to be involved in any lighting interaction , The solid color after expansion is the color of stroke . Open the color to the material panel as an attribute , Easy to change color later .
The second floor , The model is displayed in normal effect . That is to say Surface Shader, And use Standard Specular Illumination model .
Under normal circumstances , The expanded model of the first layer will completely cover the model of the second layer , The model of the second layer cannot pass the in-depth test , Finally, only the effect of the first layer expansion will be displayed , To solve this problem, we need to make the second model pass the in-depth test .
The way is to turn off the depth writing of the first layer , In this way, there is no depth value of the first layer model , The second layer model can naturally pass the depth test .
So there's a situation , When the object is closed after depth writing , Objects behind an expanding layer do not know that they are covered , Should have been covered , Because there is no depth write , Now I have passed the speed test , Therefore, it will cover the expansion of this layer of model .
Avoid this situation , The object needs to be drawn after all opaque objects are drawn , So change Shader The rendering queue for is Transparent, In this way, the drawn image will not be covered by the objects behind .

2. To write Shader
Understand the implementation logic , Then start writing according to the logic Shader.
2.1. Open attribute
Normal effect related attributes :
[Header(Texture Group)]
[Space(10)]
_Albedo ("Albedo" , 2D) = "white" {
}
[NoScaleOffset]_Specular ("Specular (RGB - A)" , 2D) = "black" {
}
[NoScaleOffset]_Normal("Normal" , 2D) = "bump" {
}
[NoScaleOffset]_AO ("Ambient Occlusion" , 2D) = "white" {
}
Explain :
Properties Code blocks open up routines Specular The maps required by the workflow are Albedo、Specular、Normal and AO, as for Smoothness attribute , This case refers to Unity Built in Standard Specular Shader, Merge it into Specular Mapped alpha In the passage .
adopt Albedo Texture mapped Tiling and Offset Control all texture maps , That is to use Albedo The texture coordinates of all texture maps are sampled , So in addition to Albedo, Of all other texture maps Tiling and Offset It's going to fail , If you add... Before these attributes [NoScaleOffset] Command to hide Tiling and Offset.
To categorize attributes , Add... Before these attributes [Header(Texture Group)] Instructions , Thus, it is displayed on the material panel “Texture Group” Text plays the role of grouping reminder .
Stroke related properties :
[Header(Outline Properties)]
[Space(10)]
_OutlineColor (“OutlineColor” , Color) = (1,0,1,1)
_OutlineWidth (“OutlineWidth” , Range(0,0.1)) = 0.01
Explain :
In addition to opening the mapping attributes required for normal effects , It also opens two properties for controlling the stroke effect , Respectively : Paint the color _Outlinecolor Stroke width _OutlineWidth.
To categorize attributes , Also add before these attributes [Header(Outline Properties)] Instructions , Thus, it is displayed on the material panel “Outline Properties” Text plays the role of grouping reminder .
2.2.SubShader label
Code :
Tags {
"RenderType"="Opaque" "Queue"="Transparent" }
Explain :
It belongs to opaque effect , So the render type is set to “Opaque”. In order to draw objects after all opaque objects , You also need to set the rendering queue to “Transparent”.
2.3. Stroke Pass
Code :
Pass
{
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 vertex : SV_POSITION;
};
fixed4 _OutlineColor;
fixed _OutlineWidth;
v2f vert (appdata_base v)
{
v2f o;
v.vertex.xyz += v.normal * _OutlineWidth;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return _OutlineColor;
}
ENDCG
}
Explain :
In the stroke effect Pass in , In order not to cover the back Pass, Depth writing needs to be turned off .
In the vertex shader , You need to multiply the normal vector of the vertex by _OutlineWidth attribute , Used to control the length of normal vector , The product is added to the vertex , Offset vertices along normal , So as to produce expansion effect .
No calculations are required in the clip shader , Go straight back to _OutlineColor Attribute is enough .
2.3. Stroke Pass
Code :
CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows
struct Input
{
float2 uv_Albedo;
};
sampler2D _Albedo;
sampler2D _Specular;
sampler2D _Normal;
sampler2D _AO;
void surf (Input IN , inout SurfaceOutputStandardSpecular o)
{
fixed4 c = tex2D (_Albedo , IN.uv_Albedo);
o.Albedo = c.rgb;
fixed4 specular = tex2D(_Specular , IN.uv_Albedo);
o.Specular = specular.rgb;
o.Smoothness = specular.a;
o.Normal = UnpackNormal(tex2D (_Normal, IN.uv_Albedo));
o.Occlusion = tex2D(_AO, IN.uv_Albedo);
}
Explain :
Use Surface Shader Write the normal effect of the model . In the compile instruction , Define the lighting model as StandarSpecular, Then add fullforwardshadows Instructions , Make the object support the projection of all light types .
Use... In surface shaders Albedo Texture coordinates of uv_Albedo Sample all maps , Then output to the corresponding surface attributes . among Smoothness Merged in Specular Textured Alpha In the passage , So it will specular.a Output to Smoothness.
Here is the complete code :
Shader "Sample/Outline"
{
Properties
{
[Header(Texture Group)]
[Space(10)]
_Albedo ("Albedo", 2D) = "white" {
}
[NoScaleOffset]_Specular ("Specular (RGB - A)" , 2D) = "black" {
}
[NoScaleOffset]_Normal("Normal" , 2D) = "bump" {
}
[NoScaleOffset]_AO ("Ambient Occlusion" , 2D) = "white" {
}
[Header(Outline Properties)]
[Space(10)]
_OutlineColor ("OutlineColor" , Color) = (1,0,1,1)
_OutlineWidth ("OutlineWidth" , Range(0,1)) = 0.01
}
SubShader
{
Tags {
"RenderType"="Opaque" "Queue"="Transparent" }
LOD 100
Pass
{
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 vertex : SV_POSITION;
};
fixed4 _OutlineColor;
fixed _OutlineWidth;
v2f vert (appdata_base v)
{
v2f o;
v.vertex.xyz += v.normal * _OutlineWidth;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return _OutlineColor;
}
ENDCG
}
//----------Regular layer----------
CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows
struct Input
{
float2 uv_Albedo;
};
sampler2D _Albedo;
sampler2D _Specular;
sampler2D _Normal;
sampler2D _AO;
void surf (Input IN , inout SurfaceOutputStandardSpecular o)
{
fixed4 c = tex2D (_Albedo , IN.uv_Albedo);
o.Albedo = c.rgb;
fixed4 specular = tex2D(_Specular , IN.uv_Albedo);
o.Specular = specular.rgb;
o.Smoothness = specular.a;
o.Normal = UnpackNormal(tex2D (_Normal, IN.uv_Albedo));
o.Occlusion = tex2D(_AO, IN.uv_Albedo);
}
ENDCG
}
}
边栏推荐
- IT服务管理(ITSM)在高等教育领域的应用
- Application of IT service management (ITSM) in Higher Education
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- 请求模块(requests)
- Projects and dependencies in ABP learning solutions
- [ManageEngine Zhuohao] mobile terminal management solution, helping the digital transformation of Zhongzhou aviation industry
- ABP 学习解决方案中的项目以及依赖关系
- 69 cesium code datasource loading geojson
- SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
- Minio error correction code, construction and startup of distributed Minio cluster
猜你喜欢
![[summary of knowledge points] chi square distribution, t distribution, F distribution](/img/a6/bb5cabbfffb0edc9449c4c251354ae.png)
[summary of knowledge points] chi square distribution, t distribution, F distribution

High order binary search tree

Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)

Movable mechanical wall clock
![[ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security](/img/fb/0a9f0ea72efc4785cc21fd0d4830c2.png)
[ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security

Solve the problem of garbled files uploaded by Kirin v10

Promise
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

Understanding of C manualresetevent class

端口扫描工具对企业有什么帮助?
随机推荐
make: g++:命令未找到
High order binary balanced tree
子类调用父类的同名方法和属性
ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)
kubeadm搭建kubenetes 集群(个人学习版)
How does MySQL store Emoji?
C语言课设销售管理系统设计(大作业)
图片服务器项目测试
Dongle data collection
C语言课设职工信息管理系统(大作业)
pycharm 配置jupyter
连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
【ManageEngine卓豪】局域网监控的作用
Record currency in MySQL
Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
三分钟带你快速了解网站开发的整个流程
C语言课设学生考勤系统(大作业)
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
【KV260】利用XADC生成芯片温度曲线图
HDU - 1501 Zipper(记忆化深搜)