游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3870|回复: 0

Unity3D ?????????

[复制链接]

15

主题

16

帖子

102

积分

注册会员

Rank: 2

积分
102
发表于 2017-1-19 13:53:18 | 显示全部楼层 |阅读模式
   ????????????????????????Demo?
    ??????????? 575561285

    ??!??????????????

    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.                         #pragma fragmentoption 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.             struct v2f
  54.             {
  55.                   half2 texcoord  : TEXCOORD0;
  56.                   float4 vertex   : SV_POSITION;
  57.                   fixed4 color    : COLOR;
  58.            };   
  59.             
  60.                           v2f vert(appdata_t IN)
  61.             {
  62.                 v2f OUT;
  63.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
  64.                 OUT.texcoord = IN.texcoord;
  65.                 OUT.color = IN.color;
  66.                
  67.                 return OUT;
  68.             }
  69.             

  70. float hexDist(float2 a, float2 b){
  71.         float2 p = abs(b-a);
  72.         float s = 0.5;
  73.         float c = 0.8660254;
  74.        
  75.         float diagDist = s*p.x + c*p.y;
  76.         return max(diagDist, p.x)/c;
  77. }

  78. float2 nearestHex(float s, float2 st){
  79.         float h = 0.5*s;
  80.         float r = 0.8660254*s;
  81.         float b = s + 2.0*h;
  82.         float a = 2.0*r;
  83.         float m = h/r;

  84.         float2 sect = st/float2(2.0*r, h+s);
  85.         float2 sectPxl = fmod(st, float2(2.0*r, h+s));
  86.        
  87.         float aSection = fmod(floor(sect.y), 2.0);
  88.        
  89.         float2 coord = floor(sect);
  90.         if(aSection > 0.0){
  91.                 if(sectPxl.y < (h-sectPxl.x*m)){
  92.                         coord -= 1.0;
  93.                 }
  94.                 else if(sectPxl.y < (-h + sectPxl.x*m)){
  95.                         coord.y -= 1.0;
  96.                 }

  97.         }
  98.         else{
  99.                 if(sectPxl.x > r){
  100.                         if(sectPxl.y < (2.0*h - sectPxl.x * m)){
  101.                                 coord.y -= 1.0;
  102.                         }
  103.                 }
  104.                 else{
  105.                         if(sectPxl.y < (sectPxl.x*m)){
  106.                                 coord.y -= 1.0;
  107.                         }
  108.                         else{
  109.                                 coord.x -= 1.0;
  110.                         }
  111.                 }
  112.         }
  113.        
  114.         float xoff = fmod(coord.y, 2.0)*r;
  115.         return float2(coord.x*2.0*r-xoff, coord.y*(h+s))+float2(r*2.0, s);
  116. }



  117. inline float2 screenDistort(float2 uv)
  118. {
  119.         uv -= float2(.5,.5);
  120.         uv = uv*1.2*(1.0/1.2+2.*uv.x*uv.x*uv.y*uv.y);
  121.         uv += float2(.5,.5);
  122.         return uv;
  123. }

  124. float noise2(float n)
  125. {
  126.         return lerp(0,0.5,smoothstep(0.0,0.5,+fmod(n+0.5,1.0)));
  127. }


  128. float4 frag (v2f i) : COLOR
  129. {
  130.         float2 uv                 = i.texcoord.xy;
  131.         float   s                 = _Value * _ScreenResolution.x/160.0;
  132.         float2 nearest         = nearestHex(s, i.texcoord.xy * _ScreenResolution.xy);
  133.         float4 texel         = tex2D(_MainTex, nearest/_ScreenResolution.xy);
  134.         float4 texel2         = tex2D(_MainTex, uv);
  135.         uv = screenDistort(uv);
  136.         float2 uv2;
  137.         uv2 = uv.yy+float2(0.1*sin(_TimeX/3)*5,0.1*sin(_TimeX/5)*5);
  138.         float3 video = tex2D(_MainTex,uv2).rgb;
  139.         float vigAmt = 2.+.3*sin(15+5.*cos(5.));
  140.         float vignette = (1.-vigAmt*(uv.y-.5)*(uv.y-.5))*(1.-vigAmt*(uv.x-.5)*(uv.x-.5));
  141.         video += (12.+fmod(uv.y*10.+_TimeX,1.))/13.;
  142.         float2 center = float2(_PositionX,_PositionY);
  143.         float dist2 = 1.0 - smoothstep( _Radius,_Radius+0.15*_SpotSize, length(center - uv));
  144.         float dist                 = hexDist(i.texcoord.xy * _ScreenResolution.xy, nearest);
  145.         float luminance = (texel.r + texel.g + texel.b)/3.0;
  146.         float interiorSize = s*_BorderSize;//*(1-dist2*2);
  147.         float interior = 1.0 - smoothstep(interiorSize-1.0, interiorSize, dist);
  148.         float4 result;
  149.         result.rgb=lerp(_BorderColor.rgb,texel.rgb,interior);
  150.         float mem=1-noise2(_TimeX/4+uv.x);
  151.         result.rgb+=float3(mem,mem,mem);
  152.         result.rgb*=_HexaColor.rgb;
  153.         result.rgb/=video.rgb/2;
  154.         result /= vignette*2;
  155.         result.rgb=lerp(result.rgb,texel2.rgb,dist2);
  156.         result.rgb=lerp(result.rgb,texel2.rgb,1-_AlphaHexa);
  157.         result.a=1.0;
  158.         return float4(result);       
  159. }
  160.                        
  161.                         ENDCG
  162.                 }
  163.                
  164.         }
  165. }
复制代码


    2.??????CameraFilterPack_AAA_SuperHexagon.cs?? ???????????????

  1. using UnityEngine;

  2. [ExecuteInEditMode]
  3. [AddComponentMenu ("Camera Filter Pack/AAA/Super Hexagon")]
  4. public class CameraFilterPack_AAA_SuperHexagon : MonoBehaviour {
  5.         #region Variables
  6.         public Shader SCShader;
  7.         [Range(0.0f, 1.0f)]
  8.         public float _AlphaHexa = 1.0f;
  9.         private float TimeX = 1.0f;
  10.         private Vector4 ScreenResolution;
  11.         private Material SCMaterial;
  12.         [Range(0.2f, 10.0f)]
  13.         public float HexaSize = 2.5f;
  14.         public float _BorderSize = 1.0f;
  15.         public Color _BorderColor = new Color (0.75f, 0.75f, 1, 1);
  16.         public Color _HexaColor = new Color (0, 0.5f, 1, 1);
  17.         public float _SpotSize = 2.5f;

  18.         public static float ChangeBorderSize = 1.0f;
  19.         public static Color ChangeBorderColor;
  20.         public static Color ChangeHexaColor;
  21.         public static float ChangeSpotSize = 1.0f;
  22.         public static float ChangeAlphaHexa = 1.0f;
  23.         public static float ChangeValue;
  24.         public Vector2 center = new Vector2(0.5f,0.5f);
  25.         public float Radius = 0.25f;

  26.         public static Vector2 Changecenter;
  27.         public static float ChangeRadius;
  28.        

  29.         #endregion
  30.        
  31.         #region Properties
  32.         Material material
  33.         {
  34.                 get
  35.                 {
  36.                         if(SCMaterial == null)
  37.                         {
  38.                                 SCMaterial = new Material(SCShader);
  39.                                 SCMaterial.hideFlags = HideFlags.HideAndDontSave;       
  40.                         }
  41.                         return SCMaterial;
  42.                 }
  43.         }
  44.         #endregion
  45.         void Start ()
  46.         {
  47.                 Changecenter = center;
  48.                 ChangeRadius = Radius;
  49.                 ChangeValue = HexaSize;
  50.                 ChangeAlphaHexa = _AlphaHexa;

  51.                 ChangeBorderSize = _BorderSize;
  52.                 ChangeBorderColor = _BorderColor;
  53.                 ChangeHexaColor = _HexaColor;
  54.                 ChangeSpotSize = _SpotSize;

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

  56.                 if(!SystemInfo.supportsImageEffects)
  57.                 {
  58.                         enabled = false;
  59.                         return;
  60.                 }
  61.         }
  62.        
  63.         void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
  64.         {
  65.                 if(SCShader != null)
  66.                 {
  67.                         TimeX+=Time.deltaTime;
  68.                         if (TimeX>100)  TimeX=0;
  69.                         material.SetFloat("_TimeX", TimeX);
  70.                         material.SetFloat("_Value", HexaSize);
  71.                         material.SetFloat("_PositionX", center.x);
  72.                         material.SetFloat("_PositionY", center.y);
  73.                         material.SetFloat("_Radius", Radius);
  74.                         material.SetFloat("_BorderSize", _BorderSize);
  75.                         material.SetColor("_BorderColor", _BorderColor);
  76.                         material.SetColor("_HexaColor", _HexaColor);
  77.                         material.SetFloat("_AlphaHexa", _AlphaHexa);
  78.                         material.SetFloat("_SpotSize", _SpotSize);
  79.                         material.SetVector("_ScreenResolution",new Vector4(sourceTexture.width,sourceTexture.height,0.0f,0.0f));
  80.                         Graphics.Blit(sourceTexture, destTexture, material);
  81.                 }
  82.                 else
  83.                 {
  84.                         Graphics.Blit(sourceTexture, destTexture);       
  85.                 }

  86.         }
  87.         void OnValidate()
  88. {
  89.                 ChangeValue=HexaSize;
  90.                 Changecenter=center;
  91.                 ChangeRadius=Radius;
  92.                 ChangeBorderSize=_BorderSize;
  93.                 ChangeBorderColor=_BorderColor;
  94.                 ChangeHexaColor=_HexaColor;
  95.                 ChangeSpotSize=_SpotSize;
  96.                 ChangeAlphaHexa=_AlphaHexa;
  97. }
  98.         // Update is called once per frame
  99.         void Update ()
  100.         {
  101.                 if (Application.isPlaying)
  102.                 {
  103.                         HexaSize = ChangeValue;
  104.                         center = Changecenter;
  105.                         Radius = ChangeRadius;
  106.                         _BorderSize = ChangeBorderSize;
  107.                         _BorderColor = ChangeBorderColor;
  108.                         _HexaColor = ChangeHexaColor;
  109.                         _SpotSize = ChangeSpotSize;
  110.                         _AlphaHexa = ChangeAlphaHexa;
  111.                 }
  112.                 #if UNITY_EDITOR
  113.                 if (Application.isPlaying!=true)
  114.                 {
  115.                         SCShader = Shader.Find("CameraFilterPack/AAA_Super_Hexagon");
  116.                 }
  117.                 #endif

  118.         }
  119.        
  120.         void OnDisable ()
  121.         {
  122.                 if(SCMaterial)
  123.                 {
  124.                         DestroyImmediate(SCMaterial);       
  125.                 }
  126.         }

  127. }
复制代码


    3.????????????????????
3_1.png    
3_0.png
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-5-15 14:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表