服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity3D使用Shader实现腐蚀消失

Unity3D使用Shader实现腐蚀消失

2022-07-08 09:28星空不语 C#

这篇文章主要为大家详细介绍了Unity3D使用Shader实现腐蚀消失,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本片shader实现的效果是模型腐蚀消失,且腐蚀的边缘大小可以调、颜色可调。效果图如下:

Unity3D使用Shader实现腐蚀消失

设置面板如下:

Unity3D使用Shader实现腐蚀消失

使用时需要给ClipMask参数给一张噪点图,设置合适的cliplinesize和cliplinecolor,然后调整clipalpha就可以了。

原理是通过获取噪点图上对应的颜色,转换成灰度,然后用灰度与clipalpha对比,如果大于则被剪裁掉。

shader实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Shader "XM/CorrosionEffect" {
 Properties {
 _Color ("Color", Color) = (1,1,1,1)
 _MainTex ("Albedo (RGB)", 2D) = "white" {}
 _Glossiness ("Smoothness", Range(0,1)) = 0.5
 _Metallic ("Metallic", Range(0,1)) = 0.0
 _ClipMaskTex ("Clip Mask", 2D) = "white" {}
 _ClipGray ("Clip Alpha", Range(0.0,1.0)) = 0.0
 _ClipLineSize ("Clip Line Size", Range(0,1)) = 0.0
 _ClipLineColor("Clip Line Color", Color) = (1,1,1,1)
 }
 SubShader {
 Tags { "RenderType"="Opaque" }
 LOD 200
 
 CGPROGRAM
 // Physically based Standard lighting model, and enable shadows on all light types
 #pragma surface surf Standard fullforwardshadows
 
 // Use shader model 3.0 target, to get nicer looking lighting
 #pragma target 3.0
 
 sampler2D _MainTex;
 sampler2D _ClipMaskTex;
 
 struct Input {
  float2 uv_MainTex;
 };
 
 half _Glossiness;
 half _Metallic;
 fixed4 _Color;
 fixed _ClipGray;
 fixed _ClipLineSize;
 fixed4 _ClipLineColor;
 
 void surf (Input IN, inout SurfaceOutputStandard o) {
  fixed4 m = tex2D (_ClipMaskTex, IN.uv_MainTex);
  fixed gray = Luminance(m.rgb);
  if(gray >= _ClipGray)
  {
  clip(-1);
  }
 
  fixed4 c;
  if(gray >= _ClipGray - _ClipLineSize)
  {
  c = _ClipLineColor;
  }
  else
  {
  // Albedo comes from a texture tinted by color
  c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  }
 
  o.Albedo = c.rgb;
 
  // Metallic and smoothness come from slider variables
  o.Metallic = _Metallic;
  o.Smoothness = _Glossiness;
  o.Alpha = c.a;
 }
 ENDCG
 }
 FallBack "Diffuse"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u012741077/article/details/53469215

延伸 · 阅读

精彩推荐