当前位置: 移动技术网 > IT编程>开发语言>c# > Unity3D使用Shader实现腐蚀消失

Unity3D使用Shader实现腐蚀消失

2019年07月18日  | 移动技术网IT编程  | 我要评论

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

设置面板如下:

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

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

shader实现如下:

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"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网