当前位置: 移动技术网 > IT编程>开发语言>c# > Unity shader实现消融效果

Unity shader实现消融效果

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

本文实例为大家分享了unity shader实现消融效果的具体代码,供大家参考,具体内容如下

效果图:

shader代码:

// upgrade note: replaced 'mul(unity_matrix_mvp,*)' with 'unityobjecttoclippos(*)'

shader "custom/edgecolo" {
properties
 {
 _maintex ("texture", 2d) = "white" {}
 _noisetex("noise", 2d) = "white" {}
 _threshold("threshold", range(0.0, 1.0)) = 0.5
 _edgelength("edge length", range(0.0, 0.2)) = 0.1
 _edgecolor("edge color", color) = (1,1,1,1)
 }
 subshader
 {
 tags { "queue"="geometry" "rendertype"="opaque" }

 pass
 {
 cull off //要渲染背面保证效果正确

 cgprogram
 #pragma vertex vert
 #pragma fragment frag
 
 #include "unitycg.cginc"

 struct appdata
 {
 float4 vertex : position;
 float2 uv : texcoord0;
 };

 struct v2f
 {
 float4 vertex : sv_position;
 float2 uvmaintex : texcoord0;
 float2 uvnoisetex : texcoord1;
 };

 sampler2d _maintex;
 float4 _maintex_st;
 sampler2d _noisetex;
 float4 _noisetex_st;
 float _threshold;
 float _edgelength;
 fixed4 _edgecolor;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = unityobjecttoclippos(v.vertex);
 o.uvmaintex = transform_tex(v.uv, _maintex);
 o.uvnoisetex = transform_tex(v.uv, _noisetex);
 return o;
 }
 
 fixed4 frag (v2f i) : sv_target
 {
 //镂空
 fixed cutout = tex2d(_noisetex, i.uvnoisetex).r;
 clip(cutout - _threshold);

 //边缘颜色
 if(cutout - _threshold < _edgelength)
 return _edgecolor;

 fixed4 col = tex2d(_maintex, i.uvmaintex);
 return col;
 }
 endcg
 }
}
}

使用方法:建一个材质球,选择此shader,然后选择一个噪声图(即:noise),最后修改threshold和edgelength参数即可看到效果。

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

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

相关文章:

验证码:
移动技术网