Camera Shader
Shader "Effect/WS_Camera" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { Tags{"LightMode" = "Always"} ZTest Always Cull off ZWrite off Fog {Mode off} CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #pragma target 3.0 #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform fixed4 _MainTex_TexelSize; uniform fixed _CenterX, _CenterY; uniform fixed _Strength; uniform fixed _RadiusX, _RadiusY; uniform fixed _Hue; uniform fixed _Saturation; uniform fixed _Value; float3 RGB2HSV( float3 RGB ) { float4 k = float4(0.0, -1.0/3.0, 2.0/3.0, -1.0); float4 p = RGB.g < RGB.b ? float4(RGB.b, RGB.g, k.w, k.z) : float4(RGB.gb, k.xy); float4 q = RGB.r < p.x ? float4(p.x, p.y, p.w, RGB.r) : float4(RGB.r , p.yzx); float d = q.x - min(q.w, q.y); float e = 1.0e-10; return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); } float3 HSV2RGB( float3 HSV ) { float4 k = float4(1.0, 2.0/3.0, 1.0/3.0, 3.0); float3 p = abs(frac(HSV.xxx + k.xyz) * 6.0 - k.www); return HSV.z * lerp(k.xxx, clamp(p - k.xxx, 0.0, 1.0), HSV.y); } v2f_img vert(appdata_img v) { v2f_img o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); #ifdef UNITY_HALF_TEXEL_OFFSET v.texcoord.y += _MainTex_TexelSize.y; #endif #if SHADER_API_D3D9 if (_MainTex_TexelSize.y < 0) v.texcoord.y = 1.0 - v.texcoord.y; #endif o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord); return o; } fixed4 frag (v2f_img i) : COLOR { fixed4 main = tex2D(_MainTex, i.uv); half2 center = fixed2(_CenterX, _CenterY); i.uv -= center; //Radial Blur for(int j = 1; j < 10; j++) { float scale = 1.0 + (-_Strength * j / 10.0); main.rgb += tex2D(_MainTex, i.uv * scale + center); } main.rgb /= 9; //LensCircle fixed dist = distance(i.uv + center, fixed2(_CenterX, _CenterY)); main.rgb *= smoothstep(_RadiusX, _RadiusY, dist); //HSV fixed3 colorTemp = RGB2HSV( main.rgb ).rgb; fixed3 finalColor = HSV2RGB( float3(float2((_Hue+colorTemp.r),(_Saturation+colorTemp.g)),(_Value+colorTemp.b)) ); main.rgb *= finalColor; return main; } ENDCG } } FallBack off }
Unity调用
using UnityEngine; [ExecuteInEditMode] public class WS_Camera : MonoBehaviour { private Material _material; public Shader _shader; //Variables// public float centerX = 0.5f; public float centerY = 0.5f; public float radiusX = 0.8f; public float radiusY = 0.4f; public float strength = 0.1f; public float _Hue = 0.0f; public float _Saturation = 0.0f; public float _Value = 0.0f; //Properties Material material { get { if (_material == null) { _material = new Material(_shader); _material.hideFlags = HideFlags.HideAndDontSave; } return _material; } } void Start() { //_shader = Shader.Find("DreamFaction/Effect/WS_Camera"); if (!SystemInfo.supportsImageEffects) { enabled = false; return; } if (!_shader && !_shader.isSupported) { enabled = false; } } void OnRenderImage(RenderTexture source, RenderTexture destination) { if (_shader != null) { material.SetFloat("_CenterX", centerX); material.SetFloat("_CenterY", centerY); material.SetFloat("_RadiusX", radiusX); material.SetFloat("_RadiusY", radiusY); material.SetFloat("_Strength", strength); material.SetFloat("_Hue", _Hue); material.SetFloat("_Saturation", _Saturation); material.SetFloat("_Value", _Value); Graphics.Blit(source, destination, _material); } } void OnDisable() { if (_material) { DestroyImmediate(_material); } } }
运行测试
时间: 2024-10-03 14:45:09