Unity3D Shader实现扫描显示效果
程序员文章站
2022-10-15 14:42:54
本文实例为大家分享了unity3d shader实现扫描显示的具体代码,供大家参考,具体内容如下
通过shader实现,从左向右的扫描显示,可自定义扫描颜色、宽度、速度。...
本文实例为大家分享了unity3d shader实现扫描显示的具体代码,供大家参考,具体内容如下
通过shader实现,从左向右的扫描显示,可自定义扫描颜色、宽度、速度。
效果图如下
编辑器界面如下
shader源码如下
shader "xm/scaneffect" { properties { _maintex("main tex", 2d) = "white"{} _linecolor("line color", color) = (0,0,0,0) _linewidth("line width", range(0, 1.0)) = 0.1 _rangex("range x", range(0,1.0)) = 1.0 } subshader { tags { "queue" = "transparent" } zwrite off blend srcalpha oneminussrcalpha cull back pass { cgprogram #pragma vertex vert #pragma fragment frag #include "lighting.cginc" sampler2d _maintex; float4 _maintex_st; float4 _linecolor; float _linewidth; float _rangex; struct a2v { float4 vertex : position; float4 texcoord : texcoord0; }; struct v2f { float4 pos : sv_position; float2 uv : texcoord0; }; v2f vert(a2v v) { v2f o; o.pos = mul(unity_matrix_mvp, v.vertex); o.uv = transform_tex(v.texcoord, _maintex); return o; } fixed4 frag(v2f i) : sv_target { fixed4 col = tex2d(_maintex, i.uv); if(i.uv.x > _rangex) { clip(-1); } else if (i.uv.x > _rangex - _linewidth) { float offsetx = i.uv.x - _rangex +_linewidth; fixed xalpha = offsetx / _linewidth; col = col * (1 - xalpha) + _linecolor * xalpha; } return col; } endcg } } fallback "diffuse" }
代码调用如下
using unityengine; using system.collections; public class scaneffect : monobehaviour { //默认扫描线的宽 [range(0,1)] public float _defaultlinew = 0.2f; //扫描的速度 [range(0, 1)] public float _showspeed = 0.02f; private meshrenderer _render; private void awake() { _render = getcomponent<meshrenderer>(); setx(0); setlinewidth(0); } public void setlinewidth(float val) { _render.material.setfloat("_linewidth", val); } public void setx(float val) { _render.material.setfloat("_rangex", val); } public void show() { stopcoroutine("showing"); startcoroutine("showing"); } public void hide() { stopcoroutine("showing"); setx(0); setlinewidth(0); } private ienumerator showing() { float deltax = 0; float deltawidth = _defaultlinew; setx(deltax); setlinewidth(deltawidth); while (true) { if (deltax != 1) { deltax = mathf.clamp01(deltax + _showspeed); setx(deltax); } else { if (deltawidth != 0) { deltawidth = mathf.clamp01(deltawidth - _showspeed); setlinewidth(deltawidth); } else { break; } } yield return new waitforendofframe(); } } public void ongui() { if (guilayout.button("show")) { show(); } if (guilayout.button("hide")) { hide(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。