zoukankan      html  css  js  c++  java
  • [Shader]一个shader效果啦(那个lost soul aside里面看到的)

    <1>效果图和原图

    思路:初始扩散以()0.5,0)为圆心扩散,计算UV坐标到圆心距离,初始扩散完毕,从下到上改变alpha就好啦

    代码:

    float2 uv = IN.texcoord.xy;
    fixed4 col = IN.color;

    float timer = _Time.y;
    if (timer > 2.5) {
    timer = timer - 2.5;
    float h = timer/2 % 1;
    if (h - uv.y > 0.2 || h - uv.y < 0)//循环流动
    col.a = 0.4;
    else {
    float val = (1 - (h - uv.y));//0.8~1 
    col.a = val*val*val;//扩大0.8~1
    }

    }
    else {
    float dis = timer/2 % 1.5;//0-1 UV.Y//初始扩散
    float len = length(uv - float2(0.5, 0));
    dis = dis - len;
    if (dis< 0)
    col.a = 0;
    if (dis>0)
    col.a = 1 - dis>0.4?1-dis:0.4;
    }

    clip(col.a - 0.01);

    完整代码:

    // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

    Shader "Tang/UIFlashVertical"
    {
    Properties
    {
    [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    _Color("Tint", Color) = (1,1,1,1)

    _StencilComp("Stencil Comparison", Float) = 8
    _Stencil("Stencil ID", Float) = 0
    _StencilOp("Stencil Operation", Float) = 0
    _StencilWriteMask("Stencil Write Mask", Float) = 255
    _StencilReadMask("Stencil Read Mask", Float) = 255

    _ColorMask("Color Mask", Float) = 15

    //my member

    [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
    }

    SubShader
    {
    Tags
    {
    "Queue" = "Transparent"
    "IgnoreProjector" = "True"
    "RenderType" = "Transparent"
    "PreviewType" = "Plane"
    "CanUseSpriteAtlas" = "True"
    }

    Stencil
    {
    Ref[_Stencil]
    Comp[_StencilComp]
    Pass[_StencilOp]
    ReadMask[_StencilReadMask]
    WriteMask[_StencilWriteMask]
    }

    Cull Off
    Lighting Off
    ZWrite Off
    ZTest[unity_GUIZTestMode]
    Blend SrcAlpha OneMinusSrcAlpha
    ColorMask[_ColorMask]

    Pass
    {
    Name "Default"
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 2.0

    #include "UnityCG.cginc"
    #include "UnityUI.cginc"

    #pragma multi_compile __ UNITY_UI_ALPHACLIP
    #define PI 3.1415926

    struct appdata_t
    {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
    float4 vertex : SV_POSITION;
    fixed4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    float4 worldPosition : TEXCOORD1;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    fixed4 _Color;
    fixed4 _TextureSampleAdd;
    float4 _ClipRect;

    v2f vert(appdata_t IN)
    {
    v2f OUT;
    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    OUT.worldPosition = IN.vertex;
    OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

    OUT.texcoord = IN.texcoord;

    OUT.color = IN.color * _Color;
    return OUT;
    }

    sampler2D _MainTex;

    fixed4 frag(v2f IN) : SV_Target
    {
    float2 uv = IN.texcoord.xy;
    fixed4 col = IN.color;
    //从下到上 以0.5,0为圆心扩散
    float timer = _Time.y;
    if (timer > 2.5) {
    timer = timer - 2.5;
    float h = timer/2 % 1;
    if (h - uv.y > 0.2 || h - uv.y < 0)
    col.a = 0.4;
    else {
    float val = (1 - (h - uv.y));
    col.a = val*val*val;
    }

    }
    else {
    float dis = timer/2 % 1.5;//0-1 UV.Y
    float len = length(uv - float2(0.5, 0));
    dis = dis - len;
    if (dis< 0)
    col.a = 0;
    if (dis>0)
    col.a = 1 - dis>0.4?1-dis:0.4;
    }

    clip(col.a - 0.01);

    half4 color = (tex2D(_MainTex, uv) + _TextureSampleAdd) * col;
    color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);

    #ifdef UNITY_UI_ALPHACLIP
    clip(color.a - 0.001);
    #endif

    return color;
    }
    ENDCG
    }
    }
    }

     

     

    -------------------------------------分割线---------------------------------------

    有些效果写到github里面了,我的Github:https://github.com/tianjiuwan

  • 相关阅读:
    十大排序算法详解,基本思想+动画演示+C语言实现,太肝了!
    宛如一个未来穿越者,终年33岁的印度数学天才,大数学家哈代说“他发现并创造了数学”
    编程的相关概念
    android中ScrollView嵌套ListView或GridView显示位置问题
    炫酷MD风之dialog各种对话框
    (转载)new Thread的弊端及Java四种线程池的使用
    (转载)Android开发——Android中常见的4种线程池(保证你能看懂并理解)
    (转)android import library switch语句报错case expressions must be constant expressions
    eclipse中将一个项目作为library导入另一个项目中
    (转)Android四大组件——Activity跳转动画、淡出淡入、滑出滑入、自定义退出进入
  • 原文地址:https://www.cnblogs.com/cocotang/p/7339050.html
Copyright © 2011-2022 走看看