Unity Shader教程之 UGUI扫光效果的实现

 时间:2024-10-15 14:08:24

1、打开Unity,新建一个空工程,导入中一张图片,具体如下图

Unity Shader教程之 UGUI扫光效果的实现
Unity Shader教程之 UGUI扫光效果的实现

2、在场景中,新建一个Image,适当合理布局,具体如下

Unity Shader教程之 UGUI扫光效果的实现

3、在工程中新建Shader,命名为 FlowLightShader,双击打开进行编辑,具体如下图

Unity Shader教程之 UGUI扫光效果的实现
Unity Shader教程之 UGUI扫光效果的实现
Unity Shader教程之 UGUI扫光效果的实现

4、FlowLightShader 脚本的具体内容如下:Shader"Custom/FlowLightShader"{霸烹钟爷Properties{ [HideInInspector] _MainTex("Base(RGB)",2D)="white"{} _FlashColor("FlashColor",Color)=(1,1,1,1) _Angle("FlashAngle",Range(0,180))=45 _Width("FlashWidth",Range(0,1))=0.2 _LoopTime("LoopTime",Float)=0.5 _Interval("TimeInterval",Float)=1.5 } SubShader { Tags{"Queue"="Transparent""RenderType"="Transparent"} LOD200 BlendSrcAlphaOneMinusSrcAlpha CGPROGRAM #pragmasurfacesurfLambertalphaexclude_path:prepassnoforwardaddsampler2D_MainTex; float4_FlashColor; float_Angle; float_Width; float_LoopTime; float_Interval;structInput { half2uv_MainTex; };floatinFlash(half2uv) { floatbrightness=0;floatangleInRad=0.0174444*_Angle; floattanInverseInRad=1.0/tan(angleInRad);floatcurrentTime=_Time.y; floattotalTime=_Interval+_LoopTime; floatcurrentTurnStartTime=(int)((currentTime/totalTime))*totalTime; floatcurrentTurnTimePassed=currentTime-currentTurnStartTime-_Interval;boolonLeft=(tanInverseInRad>0); floatxBottomFarLeft=onLeft?0.0:tanInverseInRad; floatxBottomFarRight=onLeft?(1.0+tanInverseInRad):1.0;floatpercent=currentTurnTimePassed/_LoopTime; floatxBottomRightBound=xBottomFarLeft+percent*(xBottomFarRight-xBottomFarLeft); floatxBottomLeftBound=xBottomRightBound-_Width;floatxProj=uv.x+uv.y*tanInverseInRad;if(xProj>xBottomLeftBound&&xProj<xBottomRightBound) { brightness=1.0-abs(2.0*xProj-(xBottomLeftBound+xBottomRightBound))/_Width; }returnbrightness; }voidsurf(InputIN,inoutSurfaceOutputo) { half4texCol=tex2D(_MainTex,IN.uv_MainTex); floatbrightness=inFlash(IN.uv_MainTex);o.Emission=texCol.rgb+_FlashColor.rgb*brightness;//改为输出Emission,不受光影响 o.Alpha=texCol.a; }ENDCG } FallBack"Diffuse"}

5、脚本编译正确,回到Unity,在工程中,新建一个 Material,然后 Shader 通道设置为 新建的 Shader,简单的设置参数,具体如下图

Unity Shader教程之 UGUI扫光效果的实现

6、把材质赋给Image,然后把贴图设置为 sprite,也赋给 Image,具体如下图

Unity Shader教程之 UGUI扫光效果的实现
Unity Shader教程之 UGUI扫光效果的实现

7、运行场景,

Unity Shader教程之 UGUI扫光效果的实现
Unity Shader教程之 UGUI扫光效果的实现
  • Win10设备管理器窗口变成空白页面怎么办
  • 星际战甲近战武器高压电棒怎么获得?
  • 鬼谷八荒残破锁灵阵怎么保护乌龟
  • 笔记本电脑独显不能正常工作的解决方案
  • 怎么对火绒安全联网控制添加自定义规则
  • 热门搜索
    防火手抄报资料 初中法制手抄报 生活中的语文手抄报 手抄报的格式 汉字王国手抄报 安全消防手抄报 关于圣诞节的手抄报 孝德手抄报内容 运动会手抄报内容 垃圾分类的手抄报