Reference Link: http://blog.csdn.net/stalendp/article/details/40690185
:
Here I divide it into three parts: 1. Color 2. Aperture 3. Animation
1. Color effect is achieved first. Analyze the color map, which is centered as the origin, then the color is divided into three parts, such as. When the angle is 90 degrees, the blue is the most; when the angle is-150 degrees, the red is the most; when the angle is-30 degrees, the green is the most. Then the other place is the tri-color mix.
Shader "Custom/colors" {properties{_anglerange ("Anglerange", Range ()) = 60}subshader{pass{cgprogram#pragma Vertex vert#pragma fragment Frag#include "Unitycg.cginc" #define PI 3.142struct appdata{float4 vertex:position;}; struct V2F{FLOAT4 vertex:sv_position;float4 scrpos:texcoord0;}; Half _anglerange;v2f vert (AppData v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); o.scrpos = Computescreenpos ( O.vertex); return o;} Fixed4 Frag (v2f i): sv_target{//range in (0, 1) float2 wcoord = I.SCRPOS.XY/I.SCRPOS.W; Map to (-1, 1), i.e. Screen Center for (0, 0) wcoord = Wcoord * 2-1; Atan2 (y, x): Anyway tangent, y/x's arc tangent range in [-π,π] float radian = atan2 (Wcoord.y, wcoord.x); 1 degrees (°) =0.017 radians (RAD)//1 radians (RAD) =57.29578 degrees (°) Float angle = Radian * 57.3;//mapped to (0,) if (angle < 0) Angle = + angle;f Ixed B = 1-saturate (ABS (ANGLE-90)/_anglerange), fixed g;if (angle >) G = 1-saturate (ABS (angle-330)/_angle Range); else G = 1-satUrate ((angle +)/_anglerange), Fixed r = 1-saturate (ABS (angle-210)/_anglerange), return Fixed4 (R, G, B, 1);} ENDCG}}}
2. First of all, 1/(XXX) This formula is powerful, it achieves the effect, often with a halo effect. The sixth one is the aperture effect we want to achieve.
Shader "Custom/test" {properties{_value ("Value", Range (1,)) = 1}subshader{pass{cgprogram#pragma Vertex vert#pragma Fragment Frag#include "Unitycg.cginc" struct appdata{float4 vertex:position;float2 uv:texcoord0;}; struct V2F{FLOAT4 vertex:sv_position;float2 uv:texcoord0;}; Half _value;v2f vert (AppData v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); o.uv = V.uv;return o;} Fixed4 Frag (v2f i): sv_target{//maps to (-1, 1) so that its center point is the origin float2 UV = I.uv * 2-FLOAT2 (1, 1); float v;//v = 1/abs (_value * UV . y);//1//v = 1/abs (_value * (Uv.y + uv.x))//2//v = 1/abs (_value * (UV.Y + 2 * uv.x));//3//v = 1/abs (_value * (ABS) (U V.Y) + ABS (uv.x)),//4//v = 1/abs (_value * Length (UV)),//5//v = 1/abs (_value * ABS (Length (UV)-0.5));//6v = 1/abs (_ Value * ABS (UV.X/UV.Y));//7 x the smaller y the greater the brighter the return fixed4 (V, V, V, 1);} ENDCG}}}
3. Animations. Here I do the effect is based on the angle of the light interval effect, the first of course is to calculate the angle, the implementation of the interval is the use of fmod and step.
Shader "Custom/test" {properties{_width ("Width", Range (+)) = 45}subshader{pass{cgprogram#pragma Vertex vert# pragma fragment frag#include "unitycg.cginc" struct appdata{float4 vertex:position;float2 uv:texcoord0;}; struct V2F{FLOAT4 vertex:sv_position;float2 uv:texcoord0;}; Half _width;v2f vert (AppData v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); o.uv = V.uv;return o;} Fixed4 Frag (v2f i): sv_target{//maps to (-1, 1), making its center point the origin float2 UV = I.uv * 2-FLOAT2 (1, 1); float a = atan2 (Uv.y, uv.x); a *= 57.3;if (A < 0) A + = 360;float B = fmod (A + _time.y *, _width); b = Step (0.5 * _width, b); return fixed4 (b, B, B, 1);} ENDCG}}}
4. In the end, of course, they are rubbed together.
Shader "Custom/colors" {properties{_anglerange ("Anglerange", range ()) = 60_width ("Width", Range (+)) =}su Bshader{pass{cgprogram#pragma vertex vert#pragma fragment frag#include "Unitycg.cginc" #define PI 3.142struct appdata{ Float4 vertex:position;float2 uv:texcoord0;}; struct V2F{FLOAT4 vertex:sv_position;float4 scrpos:texcoord0;float2 uv:texcoord1;}; Half _anglerange;half _width; v2f Vert (AppData v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); o.scrpos = Computescreenpos (O.vertex); o.uv = V.uv; r Eturn o;} Fixed4 Frag (v2f i): sv_target{//1. Color//range in (0, 1) float2 wcoord = I.SCRPOS.XY/I.SCRPOS.W; Map to (-1, 1), i.e. Screen Center for (0, 0) wcoord = Wcoord * 2-1; Atan2 (y, x): Anyway tangent, y/x's arc tangent range in [-π,π] float radian = atan2 (Wcoord.y, wcoord.x); 1 degrees (°) =0.017 radians (RAD)//1 radians (RAD) =57.29578 degrees (°) Float angle = Radian * 57.3;//mapped to (0,) if (angle < 0) Angle = + angle;f Ixed B = 1-saturate (ABS (angle-_anglerange), fixed g;if (angle >) G = 1-saturate (ABS (angle-330)/_anglerange); else G = 1-saturate ((angle +)/_anglerange), Fixed r = 1-saturate (ABS (angle-210)/_anglerange),//2. Aperture//Map to (-1, 1 ) so that its center point is the origin float2 UV = I.uv * 2-FLOAT2 (1, 1); Float v = 1/abs (* ABS (Length (UV)-0.3))//3. Turn float a = atan2 (Uv.y, uv.x); a *= 57.3; if (a < 0) A + = 360; float AA = fmod (A + _time.y *, _width); AA = Step (0.5 * _width, AA); if (length (UV) < 0.3) return Fixed4 (0, 0, 0, 1); return Fixed4 (R, G, B, 1) * AA + fixed4 (V, V, V, 1);} ENDCG}}}
[UnityShader3] Color light effect