Unity Shader教程之 UGUI扫光效果的实现
来源:网络收集 点击: 时间:2024-03-13打开Unity,新建一个空工程,导入中一张图片,具体如下图


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

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



FlowLightShader 脚本的具体内容如下:
ShaderCustom/FlowLightShader{
Properties{
_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=TransparentRenderType=Transparent}
LOD200
BlendSrcAlphaOneMinusSrcAlpha
CGPROGRAM
#pragmasurfacesurfLambertalphaexclude_path:prepassnoforwardadd
sampler2D_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=(tanInverseInRad0);
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(xProjxBottomLeftBoundxProjxBottomRightBound)
{
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
}
FallBackDiffuse
}
5/7脚本编译正确,回到Unity,在工程中,新建一个 Material,然后 Shader 通道设置为 新建的 Shader,简单的设置参数,具体如下图

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


运行场景,


您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言
UNITYSHADER扫光效果UGUI扫光效果版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_336668.html