Unity Shader实现描边OutLine效果
作者:elegentbeauty 时间:2022-01-13 03:11:13
本文实例为大家分享了Unity Shader实现描边OutLine效果的具体代码,供大家参考,具体内容如下
Shader实现描边流程大致为:对模型进行2遍(2个pass)绘制,第一遍(描边pass)在vertex shader中对模型沿顶点法线方向放大,fragment shader设置输出颜色为描边颜色;第二遍正常绘制模型,除被放大的部分外,其余被覆盖,这样就有了描边的效果。
实现代码如下:
Shader "Custom/OutlineShader" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_OutLineWidth("width", float) = 1.2//定义一个变量
}
SubShader {
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex:POSITION;
float2 uv:TEXCOORD0;
};
struct v2f
{
float2 uv :TEXCOORD0;
float4 vertex:SV_POSITION;
};
float _OutLineWidth;//设置变量
v2f vert(appdata v)
{
v2f o;
//设置一下xy
//v.vertex.xy *= 1.1;
v.vertex.xy *= _OutLineWidth;//乘上变量
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f i) :SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
//return col;
return fixed4(0, 0, 1, 1);
}
ENDCG
}
Pass
{
ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex:POSITION;
float2 uv:TEXCOORD0;
};
struct v2f
{
float2 uv :TEXCOORD0;
float4 vertex:SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f i) :SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
//return fixed4(0, 0, 1, 1);//返回蓝色,因为再次渲染会把第一个颜色覆盖掉
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
物体被遮挡,仍然需要显示,需要设置描边pass的语句ZTest为Always,深度检测一直通过,这样描边pass的片段不会被深度剔除。
运行效果图:
来源:https://blog.csdn.net/elegentbeauty/article/details/82915299
标签:Unity,Shader,描边
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
实例详解Java调用第三方接口方法
2023-10-15 06:34:32
![](https://img.aspxhome.com/file/2023/9/67469_0s.png)
一文搞懂Java创建线程的五种方法
2023-10-30 18:35:04
![](https://img.aspxhome.com/file/2023/3/58693_0s.png)
浅谈java中守护线程与用户线程
2023-11-26 20:46:41
![](https://img.aspxhome.com/file/2023/3/60173_0s.png)
Shiro 控制并发登录人数限制及登录踢出的实现代码
2023-09-18 02:03:56
![](https://img.aspxhome.com/file/2023/5/68515_0s.jpg)
java.lang.Runtime.exec() Payload知识点详解
2023-11-30 09:56:10
SpringBoot创建maven多模块项目实战代码
2023-11-11 04:05:32
![](https://img.aspxhome.com/file/2023/1/59391_0s.png)
java网络编程之socket网络编程示例(服务器端/客户端)
2022-05-02 00:18:59
![](https://img.aspxhome.com/file/2023/7/72587_0s.jpg)
Java实现LeetCode(54.螺旋矩阵)
2023-01-26 20:54:00
Mybatis通过Spring完成代理类注入的流程分析
2023-10-25 00:55:55
![](https://img.aspxhome.com/file/2023/5/70575_0s.png)
JavaWeb工程中集成YMP框架快速上手
2023-11-24 12:15:12
![](https://img.aspxhome.com/file/2023/9/58709_0s.png)
Java实现简易生产者消费者模型过程解析
2023-12-03 01:33:26
![](https://img.aspxhome.com/file/2023/1/76261_0s.png)
详解Java如何进行Base64的编码(Encode)与解码(Decode)
2023-01-31 18:53:34
Java实现几种序列化方式总结
2023-02-13 06:18:27
Java实现经典游戏复杂迷宫
2021-05-29 22:33:34
![](https://img.aspxhome.com/file/2023/1/63301_0s.png)
详解Java中Period类的使用方法
2023-11-28 21:04:44
Java语言求解完美数代码分析
2023-01-28 10:17:58
![](https://img.aspxhome.com/file/2023/1/64791_0s.png)
SpringBoot2整合Ehcache组件实现轻量级缓存管理
2022-06-18 18:11:16
SpringBoot 整合 ElasticSearch操作各种高级查询搜索
2023-03-25 17:12:40
DUCC配置平台实现一个动态化线程池示例代码
2023-11-28 12:07:39
![](https://img.aspxhome.com/file/2023/4/60354_0s.png)
SpringBoot 嵌入式web容器的启动原理详解
2021-12-29 23:23:14
![](https://img.aspxhome.com/file/2023/8/64908_0s.png)