Unity简易字符雨材质

发表于2018-06-20
评论0 3k浏览
本篇文章主要给大家介绍下实现字符雨特效的方法。

效果图:

代码如下:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'  
Shader "Shadertoy/Template" {  
    Properties{  
        iMouse("Mouse Pos", Vector) = (100, 100, 0, 0)  
        iChannel0("iChannel0", 2D) = "white" {}  
    iChannelResolution0("iChannelResolution0", Vector) = (100, 100, 0, 0)  
    }  
        CGINCLUDE  
#include "UnityCG.cginc"     
#pragma target 3.0        
#define vec2 float2  
#define vec3 float3  
#define vec4 float4  
#define mat2 float2x2  
#define mat3 float3x3  
#define mat4 float4x4  
#define iGlobalTime _Time.y  
#define mod fmod  
#define mix lerp  
#define fract frac  
#define texture2D tex2D  
#define iResolution _ScreenParams  
#define gl_FragCoord ((_iParam.scrPos.xy/_iParam.scrPos.w) * _ScreenParams.xy)  
#define PI2 6.28318530718  
#define pi 3.14159265358979  
#define halfpi (pi * 0.5)  
#define oneoverpi (1.0 / pi)  
        fixed4 iMouse;  
    sampler2D iChannel0;  
    fixed4 iChannelResolution0;  
    struct v2f {  
        float4 pos : SV_POSITION;  
        float4 scrPos : TEXCOORD0;  
    };  
    v2f vert(appdata_base v) {  
        v2f o;  
        o.pos = UnityObjectToClipPos(v.vertex);  
        o.scrPos = ComputeScreenPos(o.pos);  
        return o;  
    }  
    //https://www.shadertoy.com/view/4d2fWV  
    vec4 main(vec2 fragCoord);  
    fixed4 frag(v2f _iParam) : COLOR0{  
        vec2 fragCoord = gl_FragCoord;  
    return main(gl_FragCoord);  
    }  
        vec4 main(vec2 fragCoord) {  
        fragCoord.y /= iResolution.y;  
        fragCoord.y += tan(_Time.y + tan(fragCoord.x) + sin(.2 * fragCoord.x));  
        //return vec4(1, 1, 1, 1);  
        return vec4(0., .3 + (fragCoord.y < 0. ? 0. : 1. - fragCoord.y * 3.), 0., 1.);  
    }  
    ENDCG  
        SubShader{  
        Pass{  
        CGPROGRAM  
#pragma vertex vert      
#pragma fragment frag      
#pragma fragmentoption ARB_precision_hint_fastest       
        ENDCG  
    }  
    }  
        FallBack Off  
}  


// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'  
Shader "Custom/fontRain" {  
    Properties{  
        _MainTex("Base (RGB)", 2D) = "white" {}  
    }  
        SubShader{  
        Tags{ "RenderType" = "Opaque" }  
        LOD 300  
        pass {  
        Cull Back  
            //Lighting On  
            CGPROGRAM  
#pragma vertex vert  
#pragma fragment frag  
#include "UnityCG.cginc"  
#define R frac(43.*sin(p.x*73.+p.y*8.))  
#pragma vertex vert      
#pragma fragment frag      
#pragma fragmentoption ARB_precision_hint_fastest       
            sampler2D _MainTex;  
        float4 _MainTex_ST;  
        float uvOffset;  
        struct a2v {  
            float4 vertex : POSITION;  
            float3 normal : NORMAL;  
            float4 texcoord : TEXCOORD0;  
        };  
        struct v2f {  
            float4 pos : POSITION;  
            float2 uv : TEXCOORD0;  
            float3 color : TEXCOORD1;  
        };  
        v2f vert(a2v v) {  
            v2f o; o.pos = UnityObjectToClipPos(v.vertex);  
            o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);  
            o.color = ShadeVertexLights(v.vertex, v.normal);  
            return o;  
        }  
#define PIXELATION 50.  
#define PIXELATE(uv) floor((uv)*PIXELATION)/PIXELATION  
        //uv.y = uv.y / fragCoord.y;  
        //uv.y += tan(iTime + tan(uv.x*50.1) + sin(0.2 * uv.x));  
        //fragColor = vec4(0., .3 + (uv.y < 0. ? 0. : 1. - uv.y * 3.), 0., 1.);  
        //p.y /= iResolution.y;  
        //p.y += tan(iTime + tan(p.x) + sin(.2 * p.x));  
        //o = vec4(0., .3 + (p.y < 0. ? 0. : 1. - p.y * 3.), 0., 1.);  
        float4 frag(v2f i) : COLOR{  
            float4 c;  
        float a = i.uv.y;  
        float2 uv = i.uv;  
        //这个是和屏幕面积相关的 尤其注意必须在2d模式下光看 还有物体的缩放之类都对这个shader产生很大影响  
        //i.uv.y = i.uv.y /_ScreenParams.y*1000;  
        i.uv.y = i.uv.y /1000;  
        //i.uv.y += tan(_Time.y+tan(i.uv.x*50.1) + sin(0.2 * i.uv.x*50.1));  
        i.uv.y += tan(_Time.y+tan(i.uv.x*1000) + sin(0.2 * i.uv.x));  
        c = float4(0., .3 + (i.uv.y < 0. ? 0. : 1. - i.uv.y * 3.), 0., 1.);  
        //floor((col.x + vd / (lvlsR))*(lvlsR)+0.5) / (lvlsR),  
        //c.y = float4(floor(c.y +5)/10, 0, 0,1);  
        /*c.xy = _ScreenParams.xy/500;  
        c.xy = floor(20.*(uv + uv - (c.xy)) / c.y);  
        c.y -= c.x*cos(c.x);*/  
        //float pixelBorder = step(pixelNumber.x, borderSize) * step(pixelNumber.y, borderSize) * step(borderSize * 2.0 + 1.0, tileSize);  
        //c = float4(smoothstep(0.5,0.51,i.uv.x), 0,0.,1.);  
        return float4(0,c.y,0,1);  
        }  
        ENDCG  
    }  
    /*Pass{  
        Blend One Zero  
        CGPROGRAM  
#pragma vertex vert_img    
#pragma fragment frag    
#pragma fragmentoption ARB_precision_hint_fastest    
#include "UnityCG.cginc"    
        fixed4 frag(v2f_img i) : COLOR  
    {  
        return fixed4(1,0,0,1);  
    }  
        ENDCG  
    }*/  
    }FallBack "Diffuse"  
}  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引