Shader-能量槽-MyMobile Energy Sink

2017年09月05日

声明:版权所有,禁止匿名转载;禁止商业使用。

效果和参数

参数功能

MainTex 主贴图
MainU 主贴图U动画
MainV 主贴图V动画
ShapeTex 形状贴图。和遮罩同功能
MaskTex 遮罩图,一条直线,考虑改成UV算法。少一张贴图
MaskDistance 遮罩比例。0是完全遮住,1是完全显示。
FlowTex 流光贴图,让水更逼真。使用该贴图的UV流动叠加在主贴图上的效果。
FlowU 流光贴图的U动画
FlowV 流光贴图的V动画
LineTex 波浪线贴图
LineColor 波浪线颜色
LineMask 波浪线遮罩偏移
LineU 波浪线U动画
LineV 波浪线V动画

源码

Shader "MyMobile/Energy Sink" {
    Properties {
        _MainTex ("MainTex", 2D) = "white" {}
        _MainU ("MainU", Range(-0.5, 0.5)) = 0
        _MainV ("MainV", Range(-0.5, 0.5)) = 0
        _ShapeTex ("ShapeTex", 2D) = "white" {}
        _MaskTex ("MaskTex", 2D) = "white" {}
        _MaskDistance ("MaskDistance", Range(0, 1)) = 0
        _FlowTex ("FlowTex", 2D) = "white" {}
        _FlowU ("FlowU", Range(-0.5, 0.5)) = 0
        _FlowV ("FlowV", Range(-0.5, 0.5)) = 0
        _LineTex ("LineTex", 2D) = "white" {}
        _LineColor ("LineColor", Color) = (0.5,0.5,0.5,1)
        _LineMask ("LineMask", Range(-0.2, 0.2)) = 0
        _LineU ("LineU", Range(-1, 1)) = -0.13
        _LineV ("LineV", Range(-1, 1)) = 0
        [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase

            sampler2D _MainTex; float4 _MainTex_ST;
            sampler2D _FlowTex; float4 _FlowTex_ST;
            sampler2D _LineTex; float4 _LineTex_ST;
            sampler2D _MaskTex; float4 _MaskTex_ST;
            sampler2D _ShapeTex; float4 _ShapeTex_ST;
            float _MaskDistance,_LineMask;
            float _FlowU,_FlowV,_MainU,_MainV,_LineU,_LineV;
            float4 _LineColor;

            struct a2v {
                float4 vertex : POSITION;
                float2 texcoord0 : TEXCOORD0;
                float4 vertexColor : COLOR;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                float4 vertexColor : COLOR;
            };

            v2f vert (a2v v) {
                v2f o ;
                o.uv0 = v.texcoord0;
                o.vertexColor = v.vertexColor;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                return o;
            }
            float4 frag(v2f i) : SV_Target {
                float2 FlowUV = (i.uv0+float2((_FlowU*_Time.g),(_Time.g*_FlowV)));
                float4 _FlowTex_var = tex2D(_FlowTex,TRANSFORM_TEX(FlowUV, _FlowTex));
                float2 MainUV00 = (i.uv0+_FlowTex_var.r+(i.uv0+float2((_MainU*_Time.g),(_Time.g*_MainV))));
                float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(MainUV00, _MainTex));
                float LineMaskU = 0.0;
                float2 LineUV = (i.uv0+float2((_LineU*_Time.g),(_Time.g*_LineV)));
                float4 _LineTex_var = tex2D(_LineTex,TRANSFORM_TEX(LineUV, _LineTex));
                float2 MaskTexUV = (i.uv0+float2(LineMaskU,(1.0 - (_MaskDistance+0.5)))+float2(LineMaskU,lerp(0.01,0,_MaskDistance))+(_LineTex_var.r*_LineMask));
                float4 _MaskTex_var = tex2D(_MaskTex,TRANSFORM_TEX(MaskTexUV, _MaskTex));
                float3 finalColor = lerp(lerp((_MainTex_var.rgb+_LineColor.rgb),_MainTex_var.rgb,_MaskTex_var.r),float3(1,1,1),0.0);
                float4 _ShapeTex_var = tex2D(_ShapeTex,TRANSFORM_TEX(i.uv0, _ShapeTex));
                return fixed4(finalColor,(i.vertexColor.a*(_MaskTex_var.r+_MaskTex_var.r)*_ShapeTex_var.r));
            }
            ENDCG
        }
    }
    FallBack Off
}