Unity Shader案例02--------流光效果

使用编辑工具Unity2021

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Shader "CLF/SetFlowingLight"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        //流动控制
        _Progress("Progress",Range(0, 1)) = 0.2
        //光色
        _ScanColor("Scan Color", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 100
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
 
            #include "UnityCG.cginc"
 
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
 
            struct v2f
            {
                float3 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };
 
            sampler2D _MainTex;
            float4 _MainTex_ST;        
 
            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                //获取除了流光方向的UV坐标
                o.uv.xz = TRANSFORM_TEX(v.uv, _MainTex);
                //设置流光方向
                o.uv.y = v.vertex.y;
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
 
            float _Progress;
            float4 _ScanColor;
 
            fixed4 frag(v2f i) : SV_Target
            {
                //流光速度
                float halfWidth = 0.25;
                fixed4 col = tex2D(_MainTex, i.uv);
                //判断流光是否在物体范围内
                if (i.uv.z > _Progress - halfWidth && i.uv.z < _Progress + halfWidth)
                {
                    //流光范围和控制
                    float dis = (halfWidth - abs(i.uv.z - _Progress)) / halfWidth;                 
                    //上色
                    col.rgb = col.rgb + _ScanColor * dis;
                }
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

 

 

posted @   打工仔-也想飞  阅读(395)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示