您需要 登录 才可以下载或查看,没有账号?注册
x
今天为大家分享一下,如何写一个涡纹旋花效果的小Demo哈!
1.新建一个Unity3d 项目,新建一个CameraFilterPack_Vision_Psycho.shader文件。
- ///////////////////////////////////////////
- // CameraFilterPack v2.0 - by VETASOFT 2015 ///
- ///////////////////////////////////////////
- Shader "CameraFilterPack/Vision_Psycho" {
- Properties
- {
- _MainTex ("Base (RGB)", 2D) = "white" {}
- _TimeX ("Time", Range(0.0, 1.0)) = 1.0
- _ScreenResolution ("_ScreenResolution", Vector) = (0.,0.,0.,0.)
- }
- SubShader
- {
- Pass
- {
- ZTest Always
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma fragmentoption ARB_precision_hint_fastest
- #pragma target 3.0
- #pragma glsl
- #include "UnityCG.cginc"
- uniform sampler2D _MainTex;
- uniform float _TimeX;
- uniform float _Value;
- uniform float _Value2;
- uniform float _Value3;
- uniform float _Value4;
- uniform float4 _ScreenResolution;
- struct appdata_t
- {
- float4 vertex : POSITION;
- float4 color : COLOR;
- float2 texcoord : TEXCOORD0;
- };
- struct v2f
- {
- half2 texcoord : TEXCOORD0;
- float4 vertex : SV_POSITION;
- fixed4 color : COLOR;
- };
- v2f vert(appdata_t IN)
- {
- v2f OUT;
- OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
- OUT.texcoord = IN.texcoord;
- OUT.color = IN.color;
- return OUT;
- }
- float4 frag (v2f i) : COLOR
- {
- float k=0.;
- float2 uv=i.texcoord.xy;
- float3 d = float3(uv,1.0)/1.0-.5, o = d, c=k*d, p;
- d += float3(tex2D(_MainTex, float2(0.1, 0.5)).rgb) * 0.01;
- for( int i=0; i<12; i++ )
- {
- p = o+ cos(_TimeX);
- for (int j = 0; j < 10; j++)
- p = abs(p.zyx-.2) -.7,k += exp(-2. * abs(dot(p,o)));
- k/=3.;
- o += d *.5*k;
- c = .97*c + .1*k*float3(k*k*k*_Value3,k*k*_Value4,_Value4);
- }
- float dist2 = 1.0 - smoothstep(_Value,_Value-0.05-_Value2, length(float2(0.5,0.5) - uv));
- c = .4 *log(1.+c);
- float2 cc= float2(c.r/2*uv.x,c.r/2*uv.y);
- c +=tex2D(_MainTex, cc);
- c=lerp(tex2D(_MainTex,uv),c,dist2);
- return float4(c,1.0);
- }
- ENDCG
- }
- }
- }
点击此处复制文本
2.shader完成后,我们需要考虑可以更改shader的一些参数,所以我们新建一个CameraFilterPack_Vision_Psycho.cs 组件类,方便使用。
- ///////////////////////////////////////////
- // CameraFilterPack v2.0 - by VETASOFT 2015 ///
- ///////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- [ExecuteInEditMode]
- [AddComponentMenu("Camera Filter Pack/Vision/Psycho")]
- public class CameraFilterPack_Vision_Psycho : MonoBehaviour
- {
- #region Variables
- public Shader SCShader;
- private float TimeX = 1.0f;
- private Vector4 ScreenResolution;
- private Material SCMaterial;
- [Range(0.01f, 1f)]
- public float HoleSize = 0.6f;
- [Range(-1f, 1f)]
- public float HoleSmooth = 0.3f;
- [Range(-2f, 2f)]
- public float Color1 = 0.2f;
- [Range(-2f, 2f)]
- public float Color2 = 0.9f;
- public static float ChangeValue;
- public static float ChangeValue2;
- public static float ChangeValue3;
- public static float ChangeValue4;
- #endregion
- #region Properties
- Material material
- {
- get
- {
- if (SCMaterial == null)
- {
- SCMaterial = new Material(SCShader);
- SCMaterial.hideFlags = HideFlags.HideAndDontSave;
- }
- return SCMaterial;
- }
- }
- #endregion
- void Start()
- {
- ChangeValue = HoleSize;
- ChangeValue2 = HoleSmooth;
- ChangeValue3 = Color1;
- ChangeValue4 = Color2;
- SCShader = Shader.Find("CameraFilterPack/Vision_Psycho");
- if (!SystemInfo.supportsImageEffects)
- {
- enabled = false;
- return;
- }
- }
- void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
- {
- if (SCShader != null)
- {
- TimeX += Time.deltaTime;
- if (TimeX > 100) TimeX = 0;
- material.SetFloat("_TimeX", TimeX);
- material.SetFloat("_Value", HoleSize);
- material.SetFloat("_Value2", HoleSmooth);
- material.SetFloat("_Value3", Color1);
- material.SetFloat("_Value4", Color2);
- material.SetVector("_ScreenResolution", new Vector4(sourceTexture.width, sourceTexture.height, 0.0f, 0.0f));
- Graphics.Blit(sourceTexture, destTexture, material);
- }
- else
- {
- Graphics.Blit(sourceTexture, destTexture);
- }
- }
- void OnValidate()
- {
- ChangeValue = HoleSize;
- ChangeValue2 = HoleSmooth;
- ChangeValue3 = Color1;
- ChangeValue4 = Color2;
- }
- void Update()
- {
- if (Application.isPlaying)
- {
- HoleSize = ChangeValue;
- HoleSmooth = ChangeValue2;
- Color1 = ChangeValue3;
- Color2 = ChangeValue4;
- }
- #if UNITY_EDITOR
- if (Application.isPlaying != true)
- {
- SCShader = Shader.Find("CameraFilterPack/Vision_Psycho");
- }
- #endif
- }
- void OnDisable()
- {
- if (SCMaterial)
- {
- DestroyImmediate(SCMaterial);
- }
- }
- }
点击此处复制文本
3.我们通过点击屏幕按钮添加特效文件,所以在摄像机上附加AddCameraFilter.cs组件。
- using UnityEngine;
- using System.Collections;
- public class AddCameraFilter : MonoBehaviour {
- private CameraFilterPack_Vision_Psycho _cameraFilterPack;
- // Use this for initialization
- void Start () {
- _cameraFilterPack = GetComponent();
- }
-
- // Update is called once per frame
- void OnGUI()
- {
- if (GUI.Button(new Rect(50, 200, 200, 30), "添加涡纹旋花效果"))
- {
- if(_cameraFilterPack==null)
- _cameraFilterPack= gameObject.AddComponent();
- if (_cameraFilterPack != null)
- _cameraFilterPack.enabled = true;
- }
- if (GUI.Button(new Rect(50, 250, 200, 30), "移除涡纹旋花效果"))
- {
- if (_cameraFilterPack != null)
- _cameraFilterPack.enabled = false;
- }
- }
- }
点击此处复制文本
4.好吧!最后我们直接运行项目看效果啦!
学习交流群:575561285
|