unity shader —— LOGO闪烁

总算找到了一个实用的效果,就拿出来给大家分享一下。

效果截图:

 

unity shader —— LOGO闪烁

unity shader —— LOGO闪烁

unity shader —— LOGO闪烁

 

代码:

 1 Shader "Custom/LOGOblink" {

 2     Properties {

 3         _MainTex ("Base (RGB)", 2D) = "white" {}

 4         

 5         _BlinkColor ( "Blink Color", color ) = (1,1,1,1)

 6         _BlinkWidth ( "Blink Width", range(0, 1)  ) = 0.2

 7         _LoopTime ( "Looping Time", range(0,10)) = 1

 8         _Interval ( "Time Interval", range(0,10)) = 2

 9     }

10     SubShader {

11         Tags { "Queue"="Transparent" "RenderType"="Transparent" }

12         

13         LOD 200

14         

15         CGPROGRAM

16         #pragma surface surf Lambert alpha exclude_path:prepass noforwardadd

17 

18         sampler2D _MainTex;

19         float4 _BlinkColor;

20         float _BlinkWidth;

21         float _LoopTime;

22         float _Interval;

23         

24 

25         struct Input {

26             float2 uv_MainTex;

27         };

28         

29         float Blink( float2 uv )

30         {

31         

32             float curTime = _Time.y;    // 当前时间

33             float totalTime = _LoopTime + _Interval;

34             float curInitTime = (int)(_Time.y / totalTime) * totalTime;    // 当前起始时间

35             

36             float passedTime = _Time.y - curInitTime - _Interval;

37             

38             

39             if(passedTime <= 0.0)

40                 return 0.0;

41             else

42             {

43             float passedTimePercent = (passedTime) / _LoopTime;

44             

45             

46             float bottomPoint1 = passedTimePercent*(_BlinkWidth + 2);    // 底点1

47             // 默认是45度角,所以不需要太多计算

48             

49             float temp1 = bottomPoint1 - uv.y;

50             float temp2 = _BlinkWidth*0.5;

51             

52             

53             if( uv.x > temp1 || uv.x < temp1-_BlinkWidth)

54                 return 0.0;

55             else

56                 return  1.0 - abs( ( temp1 - uv.x - temp2 ) / temp2 );

57             }

58         }

59         

60 

61         void surf (Input IN, inout SurfaceOutput o) {

62             half4 c = tex2D (_MainTex, IN.uv_MainTex);

63             float brightness = Blink(IN.uv_MainTex);

64             

65             o.Albedo = c.rgb + ( brightness * _BlinkColor.rgb );

66             o.Alpha = c.a;

67         }

68         ENDCG

69         

70     } 

71     FallBack "Diffuse"

72 }

 

参考教程地址: 

http://blog.csdn.net/candycat1992/article/details/25720243

作者写的很好,赞一个。

我参照她的代码稍微改了一些地方。

角度固定成了45度,省去许多计算,提高实用性。

并没有太多难度,有一个难以想到的部分,就是用除法归整实现计数。(这是被逼出来的招么?>_<)

注意事项:

1.UV坐标原点是左下角。

2.区别于C语言,分支分部必须显式地写明if else;

else不能省略。

 *************************

原作者所说的使用闪光贴图实现闪烁,美术水平所限,没有实际操作过,不敢妄谈。

但是总体上我的感觉:用数学公式控制总会比贴图的效果看起来更真实;贴图未必速度就更快。

我的感觉是:如果有这种选择,应该做两套,实地测试。

效率、美感综合衡量,进行取舍。

(也不知道游戏公司是不是都这样的制作流程)

你可能感兴趣的:(unity)