美文网首页Unity教程合集Unity Shader分享
unity 随时间变化的shader(20170301)

unity 随时间变化的shader(20170301)

作者: 洪福齐天999 | 来源:发表于2017-03-01 08:22 被阅读0次

    参照【风宇冲】Unity3D教程宝典之Shader篇:第四讲制作一个美丽的地球Unity3D教程宝典之Shader篇:第五讲LOGO闪光效果

    这两篇文章都是引入时间这一因素,进行shader的移动达到的效果

    一、【风宇冲】Unity3D教程宝典之Shader篇:第四讲制作一个美丽的地球

    Shader "Custom/earth" {

    Properties {

    _MainTex ("Texture", 2D) = "white" { }

    _Cloud ("_Cloud", 2D) = "white" { }

    }

    SubShader {

    Tags{"Queue" = "Transparent" "RenderType"="Transparent"}

    Pass {

    CGPROGRAM

    #pragma vertex vert

    #pragma fragment frag

    #include "UnityCG.cginc"

    float4 _Color;

    sampler2D _MainTex;

    sampler2D _Cloud;

    struct v2f {

    float4  pos : SV_POSITION;

    float2  uv : TEXCOORD0;

    } ;

    float4 _MainTex_ST;

    v2f vert (appdata_base v)

    {

    //和之前一样

    v2f o;

    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

    o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);

    return o;

    }

    half4 frag (v2f i) : COLOR

    {

    //地球的贴图uv, x即横向在动

    float u_x = i.uv.x + -0.1*_Time;

    float2 uv_earth=float2( u_x , i.uv.y);

    half4 texcolor_earth = tex2D (_MainTex, uv_earth);

    //云层的贴图uv的x也在动,但是动的更快一些

    float2 uv_cloud;

    u_x = i.uv.x + -0.2*_Time;

    uv_cloud=float2( u_x , i.uv.y);

    half4 tex_cloudDepth = tex2D (_Cloud, uv_cloud);

    //纯白 x 深度值= 该点的云颜色

    half4 texcolor_cloud = float4(1,1,1,0) * (tex_cloudDepth.x);

    //地球云彩颜色混合

    return lerp(texcolor_earth,texcolor_cloud,0.5f);

    }

    ENDCG}}}

    二、Unity3D教程宝典之Shader篇:第五讲LOGO闪光效果

    Shader "Custom/logo" {

    Properties {

    //_Color ("Color", Color) = (1,1,1,1)

    _MainTex ("Albedo (RGB)", 2D) = "white" {}

    //_Glossiness ("Smoothness", Range(0,1)) = 0.5

    //_Metallic ("Metallic", Range(0,1)) = 0.0

    }

    SubShader {

    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

    Blend SrcAlpha OneMinusSrcAlpha

    AlphaTest Greater 0.1

    pass

    {

    CGPROGRAM

    #pragma vertex vert

    #pragma fragment frag

    #include "UnityCG.cginc"

    sampler2D _MainTex;

    float4 _MainTex_ST;

    struct v2f {

    float4  pos : SV_POSITION;

    float2  uv : TEXCOORD0;

    };

    //顶点函数没什么特别的,和常规一样

    v2f vert (appdata_base v)

    {

    v2f o;

    o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

    o.uv =    TRANSFORM_TEX(v.texcoord,_MainTex);

    return o;

    }

    //必须放在使用其的 frag函数之前,否则无法识别。

    //核心:计算函数,角度,uv,光带的x长度,间隔,开始时间,偏移,单次循环时间

    float inFlash(float angle,float2 uv,float xLength,int interval,int beginTime, float offX, float loopTime )

    {

    //亮度值

    float brightness =0;

    //倾斜角

    float angleInRad = 0.0174444 * angle;

    //当前时间

    float currentTime = _Time.y;

    //获取本次光照的起始时间

    int currentTimeInt = _Time.y/interval;

    currentTimeInt *=interval;

    //获取本次光照的流逝时间 = 当前时间 - 起始时间

    float currentTimePassed = currentTime -currentTimeInt;

    if(currentTimePassed >beginTime)

    {

    //底部左边界和右边界

    float xBottomLeftBound;

    float xBottomRightBound;

    //此点边界

    float xPointLeftBound;

    float xPointRightBound;

    float x0 = currentTimePassed-beginTime;

    x0 /= loopTime;

    //设置右边界

    xBottomRightBound = x0;

    //设置左边界

    xBottomLeftBound = x0 - xLength;

    //投影至x的长度 = y/ tan(angle)

    float xProjL;

    xProjL= (uv.y)/tan(angleInRad);

    //此点的左边界 = 底部左边界 - 投影至x的长度

    xPointLeftBound = xBottomLeftBound - xProjL;

    //此点的右边界 = 底部右边界 - 投影至x的长度

    xPointRightBound = xBottomRightBound - xProjL;

    //边界加上一个偏移

    xPointLeftBound += offX;

    xPointRightBound += offX;

    //如果该点在区域内

    if(uv.x > xPointLeftBound && uv.x < xPointRightBound)

    {

    //得到发光区域的中心点

    float midness = (xPointLeftBound + xPointRightBound)/2;

    //趋近中心点的程度,0表示位于边缘,1表示位于中心点

    float rate= (xLength -2*abs(uv.x - midness))/ (xLength);

    brightness = rate;

    }

    }

    brightness= max(brightness,0);

    //返回颜色 = 纯白色 * 亮度

    float4 col = float4(1,1,1,1) *brightness;

    return brightness;

    }

    float4 frag (v2f i) : COLOR

    {

    float4 outp;

    //根据uv取得纹理颜色,和常规一样

    float4 texCol = tex2D(_MainTex,i.uv);

    //传进i.uv等参数,得到亮度值

    float tmpBrightness;

    tmpBrightness =inFlash(75,i.uv,0.25,5,2,0.15,0.7);

    //图像区域,判定设置为 颜色的A > 0.5,输出为材质颜色+光亮值

    if(texCol.w >0.5)

    outp  =texCol+float4(1,1,1,1)*tmpBrightness;

    //空白区域,判定设置为 颜色的A <=0.5,输出空白

    else

    outp =float4(0,0,0,0);

    return outp;

    }

    ENDCG

    }

    }

    }

    相关文章

      网友评论

        本文标题:unity 随时间变化的shader(20170301)

        本文链接:https://www.haomeiwen.com/subject/mdxqgttx.html