相机过滤实现 六角形花纹效果

 1.新建一个Unity3d 项目,新建一个CameraFilterPack_AAA_SuperHexagon.shader文件。

1.  Shader "CameraFilterPack/AAA_Super_Hexagon" {

2.         Properties

3.         {

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

5.                _TimeX ("Time", Range(0.0, 1.0)) = 1.0

6.                _Distortion ("_Distortion", Range(0.0, 1.0)) = 0.3

7.                _ScreenResolution ("_ScreenResolution", Vector) = (0.,0.,0.,0.)

8.                _Value ("_Value", Range(0.2, 10.0)) = 1

9.                _HexaColor ("_HexaColor", Color) = (1,1,1,1)

10.               _BorderSize ("_BorderSize", Range(-0.5, 0.5)) = 0.0

11.               _BorderColor ("_BorderColor", Color) = (1,1,1,1)

12.               _SpotSize ("_SpotSize", Range(0, 1.)) = 0.5

13.               _AlphaHexa ("_AlphaHexa", Range(0.2, 10.0)) = 1

14.               _PositionX ("_PositionX", Range(-0.5, 0.5)) = 0.0

15.               _PositionY ("_PositionY", Range(-0.5, 0.5)) = 0.0

16.               _Radius ("_Radius", Range(0, 1.)) = 0.5

17.        }

18.        SubShader

19.        {

20.               Pass

21.               {

22.                       ZTest Always

23.                       CGPROGRAM

24.                       #pragma vertex vert

25.                       #pragma fragment frag

26.                       #pragmafragmentoption ARB_precision_hint_fastest

27.                       #pragma target 3.0

28.                       #include"UnityCG.cginc"

29.                      

30.                      

31.                       uniform sampler2D_MainTex;

32.                       uniform float _TimeX;

33.                       uniform float _Distortion;

34.                       uniform float4_ScreenResolution;

35.                       uniform float _Value;

36.                       uniform float _BorderSize;

37.                       uniform float4_BorderColor;

38.                       uniform float4 _HexaColor;

39.       

40.                       uniform float _AlphaHexa;

41.                              

42.                       uniform float _PositionX;

43.                       uniform float _PositionY;

44.                       uniform float _Radius;                

45.                       uniform float _SpotSize;              

46.                      

47.                      struct appdata_t

48.             {

49.                 float4 vertex   : POSITION;

50.                 float4 color    : COLOR;

51.                 float2 texcoord : TEXCOORD0;

52.             };

53.  

54.             struct v2f

55.             {

56.                   half2 texcoord  : TEXCOORD0;

57.                   float4 vertex   : SV_POSITION;

58.                   fixed4 color    : COLOR;

59.            };  

60.              

61.                       v2fvert(appdata_t IN)

62.             {

63.                 v2f OUT;

64.                 OUT.vertex =mul(UNITY_MATRIX_MVP, IN.vertex);

65.                 OUT.texcoord = IN.texcoord;

66.                 OUT.color = IN.color;

67.                 

68.                 return OUT;

69.             }

70.             

71.  

72. floathexDist(float2 a, float2 b){

73.        float2 p = abs(b-a);

74.        float s = 0.5;

75.        float c = 0.8660254;

76.       

77.        float diagDist = s*p.x +c*p.y;

78.        return max(diagDist,p.x)/c;

79. }

80.  

81. float2 nearestHex(float s, float2 st){

82.        float h = 0.5*s;

83.        float r = 0.8660254*s;

84.        float b = s + 2.0*h;

85.        float a = 2.0*r;

86.        float m = h/r;

87.  

88.        float2 sect = st/float2(2.0*r, h+s);

89.        float2 sectPxl = fmod(st, float2(2.0*r, h+s));

90.       

91.        float aSection =fmod(floor(sect.y), 2.0);

92.       

93.        float2 coord = floor(sect);

94.        if(aSection > 0.0){

95.               if(sectPxl.y <(h-sectPxl.x*m)){

96.                       coord-= 1.0;

97.               }

98.               elseif(sectPxl.y < (-h +sectPxl.x*m)){

99.                       coord.y -= 1.0;

100.                        }

101.          

102.                 }

103.                 else{

104.                        if(sectPxl.x > r){

105.                                if(sectPxl.y < (2.0*h - sectPxl.x * m)){

106.                                        coord.y-= 1.0;

107.                                }

108.                        }

109.                        else{

110.                                if(sectPxl.y <(sectPxl.x*m)){

111.                                        coord.y-= 1.0;

112.                                }

113.                                else{

114.                                        coord.x-= 1.0;

115.                                }

116.                        }

117.                 }

118.                

119.                 float xoff = fmod(coord.y,2.0)*r;

120.                 return float2(coord.x*2.0*r-xoff,coord.y*(h+s))+float2(r*2.0, s);

121.         }

122.          

123.          

124.          

125.         inline float2 screenDistort(float2 uv)

126.         {

127.                 uv -=float2(.5,.5);

128.                 uv = uv*1.2*(1.0/1.2+2.*uv.x*uv.x*uv.y*uv.y);

129.                 uv +=float2(.5,.5);

130.                 return uv;

131.         }

132.          

133.         floatnoise2(float n)

134.         {

135.                 return lerp(0,0.5,smoothstep(0.0,0.5,+fmod(n+0.5,1.0)));

136.         }

137.          

138.          

139.         float4 frag (v2f i) : COLOR

140.         {

141.                 float2 uv               = i.texcoord.xy;

142.                 float   s               =_Value * _ScreenResolution.x/160.0;

143.                 float2nearest = nearestHex(s, i.texcoord.xy *_ScreenResolution.xy);

144.                 float4 texel   = tex2D(_MainTex,nearest/_ScreenResolution.xy);

145.                 float4texel2   = tex2D(_MainTex, uv);

146.                 uv =screenDistort(uv);

147.                 float2 uv2;

148.                 uv2 =uv.yy+float2(0.1*sin(_TimeX/3)*5,0.1*sin(_TimeX/5)*5);

149.                 float3 video= tex2D(_MainTex,uv2).rgb;

150.                 float vigAmt = 2.+.3*sin(15+5.*cos(5.));

151.                 float vignette = (1.-vigAmt*(uv.y-.5)*(uv.y-.5))*(1.-vigAmt*(uv.x-.5)*(uv.x-.5));

152.                 video += (12.+fmod(uv.y*10.+_TimeX,1.))/13.;

153.                 float2center = float2(_PositionX,_PositionY);

154.                 float dist2 = 1.0 - smoothstep(_Radius,_Radius+0.15*_SpotSize, length(center - uv));

155.                 float dist              = hexDist(i.texcoord.xy *_ScreenResolution.xy, nearest);

156.                 float luminance = (texel.r+ texel.g + texel.b)/3.0;

157.                 float interiorSize =s*_BorderSize;//*(1-dist2*2);

158.                 float interior = 1.0 -smoothstep(interiorSize-1.0, interiorSize, dist);

159.                 float4result;

160.                 result.rgb=lerp(_BorderColor.rgb,texel.rgb,interior);

161.                 float mem=1-noise2(_TimeX/4+uv.x);

162.                 result.rgb+=float3(mem,mem,mem);

163.                 result.rgb*=_HexaColor.rgb;

164.                 result.rgb/=video.rgb/2;

165.                 result /=vignette*2;

166.                 result.rgb=lerp(result.rgb,texel2.rgb,dist2);

167.                 result.rgb=lerp(result.rgb,texel2.rgb,1-_AlphaHexa);

168.                 result.a=1.0;

169.                 return float4(result); 

170.         }

171.                               

172.                                ENDCG

173.                        }

174.                       

175.                 }

176.         }





    2.我们新建一个CameraFilterPack_AAA_SuperHexagon.cs组件 ,并且直接附加在摄像机对象上。

1.  using UnityEngine;

2.   

3.  [ExecuteInEditMode]

4.  [AddComponentMenu ("Camera FilterPack/AAA/Super Hexagon")]

5.  publicclassCameraFilterPack_AAA_SuperHexagon : MonoBehaviour {

6.         #region Variables

7.         public Shader SCShader;

8.         [Range(0.0f, 1.0f)]

9.         publicfloat _AlphaHexa = 1.0f;

10.        privatefloat TimeX = 1.0f;

11.        private Vector4ScreenResolution;

12.        private Material SCMaterial;

13.        [Range(0.2f, 10.0f)]

14.        publicfloat HexaSize = 2.5f;

15.        publicfloat _BorderSize = 1.0f;

16.        public Color _BorderColor =new Color (0.75f, 0.75f, 1, 1);

17.        public Color _HexaColor = new Color (0, 0.5f, 1, 1);

18.        publicfloat _SpotSize = 2.5f;

19.  

20.        publicstaticfloat ChangeBorderSize = 1.0f;

21.        publicstatic ColorChangeBorderColor;

22.        publicstatic ColorChangeHexaColor;

23.        publicstaticfloat ChangeSpotSize = 1.0f;

24.        publicstaticfloat ChangeAlphaHexa = 1.0f;

25.        publicstaticfloat ChangeValue;

26.        public Vector2 center = new Vector2(0.5f,0.5f);

27.        publicfloat Radius = 0.25f;

28.  

29.        publicstatic Vector2Changecenter;

30.        publicstaticfloat ChangeRadius;

31.       

32.  

33.        #endregion

34.       

35.        #region Properties

36.        Material material

37.        {

38.               get

39.               {

40.                       if(SCMaterial == null)

41.                       {

42.                               SCMaterial = new Material(SCShader);

43.                               SCMaterial.hideFlags= HideFlags.HideAndDontSave;      

44.                       }

45.                       return SCMaterial;

46.               }

47.        }

48.        #endregion

49.        voidStart ()

50.        {

51.               Changecenter = center;

52.               ChangeRadius = Radius;

53.               ChangeValue = HexaSize;

54.               ChangeAlphaHexa = _AlphaHexa;

55.  

56.               ChangeBorderSize = _BorderSize;

57.               ChangeBorderColor = _BorderColor;

58.               ChangeHexaColor = _HexaColor;

59.               ChangeSpotSize = _SpotSize;

60.  

61.               SCShader = Shader.Find("CameraFilterPack/AAA_Super_Hexagon");

62.  

63.               if(!SystemInfo.supportsImageEffects)

64.               {

65.                       enabled = false;

66.                       return;

67.               }

68.        }

69.       

70.        voidOnRenderImage (RenderTexturesourceTexture, RenderTexture destTexture)

71.        {

72.               if(SCShader != null)

73.               {

74.                       TimeX+=Time.deltaTime;

75.                       if (TimeX>100)  TimeX=0;

76.                       material.SetFloat("_TimeX", TimeX);

77.                       material.SetFloat("_Value", HexaSize);

78.                       material.SetFloat("_PositionX", center.x);

79.                       material.SetFloat("_PositionY", center.y);

80.                       material.SetFloat("_Radius", Radius);

81.                       material.SetFloat("_BorderSize", _BorderSize);

82.                       material.SetColor("_BorderColor", _BorderColor);

83.                       material.SetColor("_HexaColor", _HexaColor);

84.                       material.SetFloat("_AlphaHexa", _AlphaHexa);

85.                       material.SetFloat("_SpotSize", _SpotSize);

86.                       material.SetVector("_ScreenResolution",newVector4(sourceTexture.width,sourceTexture.height,0.0f,0.0f));

87.                       Graphics.Blit(sourceTexture,destTexture, material);

88.               }

89.               else

90.               {

91.                       Graphics.Blit(sourceTexture,destTexture);     

92.               }

93.  

94.        }

95.        voidOnValidate()

96. {

97.               ChangeValue=HexaSize;

98.               Changecenter=center;

99.               ChangeRadius=Radius;

100.                        ChangeBorderSize=_BorderSize;

101.                        ChangeBorderColor=_BorderColor;

102.                        ChangeHexaColor=_HexaColor;

103.                        ChangeSpotSize=_SpotSize;

104.                        ChangeAlphaHexa=_AlphaHexa;

105.         }

106.                 // Update is calledonce per frame

107.                 voidUpdate ()

108.                 {

109.                        if(Application.isPlaying)

110.                        {

111.                                HexaSize= ChangeValue;

112.                                center= Changecenter;

113.                                Radius= ChangeRadius;

114.                                _BorderSize= ChangeBorderSize;

115.                                _BorderColor= ChangeBorderColor;

116.                                _HexaColor= ChangeHexaColor;

117.                                _SpotSize= ChangeSpotSize;

118.                                _AlphaHexa= ChangeAlphaHexa;

119.                        }

120.                        #if UNITY_EDITOR

121.                        if(Application.isPlaying!=true)

122.                        {

123.                                SCShader= Shader.Find("CameraFilterPack/AAA_Super_Hexagon");

124.                        }

125.                        #endif

126.          

127.                 }

128.                

129.                 voidOnDisable ()

130.                 {

131.                        if(SCMaterial)

132.                        {

133.                                DestroyImmediate(SCMaterial);  

134.                        }

135.                 }

136.          

137.         }



    

    3.因为操作比较简单,我们直奔终点看效果吧!



 

Work with Unity 5 and Unity 4.x Pro Watch the video demonstration NEW! Forum : https://forum.vetasoft.store/ Discuss with us about Camera Filter Pack and more ! Camera Filter Pack : More Than 310 Awesome Filters for your Camera ! Now Single Pass Stereo Support ! ( Unity 5.4 and more ) Camera Filter Pack offer you the best collection of high quality full screen post-processing effects to enhanced and improved the quality of your game. All the filters are optimized and adjustable. Add and turn on awesome next-gen filters to your camera! Camera Filter Pack require Unity 5.0+ (Personal or Pro) Online Documentation : http://www.vetasoft.store/camerafilterpack/ Follow us on Twitter Like us on Facebook 3.6.0 ——-Add AAA Rain FX 3.1.0 – Add 7 Glasses On FX : Classic, Vampire, Night, Futuristic Montain, Futuristic Desert and Spy – Add Fade parameter to TV_Arcade – Add Fade parameter to TV_Arcade_2 – Add Fade parameter to TV_Arcade_Fast – Add Fade parameter to TV_Artefact – ADD 25 new LUT textures – Add Fade, Intensity and Speed parameters to TV_Chromatical – Add Fade, Zoom Fade, Zoom Speed parameters to TV_Chromatical_2 – Add Remodeling Horror FX with Fade and Distortion parameters – Add Fade parameter on TV_Led – Add Fade on Planet Mars – Add Fade on TV Posterize – Add Fade on TV Tiles 3.0.5 – Add Sniper Score Filter – Improve VHS HQ parameters – Improve Gliths parameters – Improve Drunk parameters ( Distortion, Color, Wave and more ) – Improve TV Noise Parameters – Add Movie Noise – Improve Old Movie 2 Parameters – Improve TV 50′ and TV 80′ 3.0.1 – Fix Lut minor issue. v3.0.0 – Add more then 20 new filters ! – Better filter reorganisation. – Preview the filters on our website. – Documentations of all the filters online. – Add 3D Matrix – Add 3D Rain Drop System FX Pro – Add 3D Scene Scan – Add 3D Myst – Add 3D Binary – Add 3D Computer – Add 3D Snow – Add 3D Black Hole – Add 3D Ghost Light – Add 3D Anomaly – Add 3D Shield – Add Atmosphere Fog – Add Pixelisat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值