11.3.1波浪效果

本文介绍了如何在Unity中使用定制的Shader实现具有波浪效果的顶点动画,通过调整频率、振幅和时间参数,模拟水流的真实流动。
摘要由CSDN通过智能技术生成

11.3.1波浪效果

Shader "Custom/chap11.3.1/vertex Animation"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}  // 河流纹理
        _Color("Main Color", Color) = (1,1,1,1) // 颜色
        _Magnitude("Distortion Magnitude", Float) = 1 // 控制水流波动的幅度
        _Frequency("Distortion Frequency", Float) = 1 // 控制水流波动的频率
        _InvWaveLength("Distortion Inverse Wave Length", Float)  = 10 // 控制波长的倒数(越小波长越大。 反比)
        _Speed("animation Speed1", Range(0,100)) = 0.5 // 动画速度
    }
    SubShader
    {
        // 序列帧动画通常带有透明度, 因此这里的 renderType 和 Queue 均设置为 Transparent
        // DisableBatching = true, 批处理会合并相关模型,而顶点动画会修改模型的顶点偏移。所以要取消批处理
        Tags { "RenderPipeLine"="UniversalRenderPipeline" "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True" "DisableBatching"="True" }

        // 透明模式下通常关闭深度写入
        ZWrite Off

        // 指定混合因子
        Blend SrcAlpha OneMinusSrcAlpha

        HLSLINCLUDE
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"

            CBUFFER_START(UnityPerMaterial)
            float4 _MainTex_ST;
            half4 _Color;
            float _Speed;
            float  _Magnitude;
            float  _Frequency;
            float  _InvWaveLength;
            CBUFFER_END
        ENDHLSL


        Pass
        {
            Tags { "LightMode"="UniversalForward" }
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            TEXTURE2D (_MainTex);
            SAMPLER(sampler_MainTex);

            struct appdata
            {
                float4 vertex : POSITION;
                float4 uv : TEXCOORD0;
                float3 normal: NORMAL;
            };

            struct v2f 
            {
                float2 uv : TEXCOORD0;
                // UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 worldNormal: TEXCOORD1;
                float3 worldPos: TEXCOORD2;
            };

            v2f vert (appdata v) 
            { 
                float4 offset;
                offset = float4(0.0, 0.0, 0.0, 0.0); 
                // 原理: sin (aX + b) * c 中
                // c 就是这里的   _Magnitude 控制振幅
                // b 就是代指 v.vertex.z * _InvWaveLength 控制初始偏移量 -> 出现波浪的条件
                // a 就是代指 _Frequency -> 控制频率
                // x 就是代指 _Time.y 出现波浪流动的条件。(变量)

                offset.x = sin(_Frequency * _Time.y +  v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.x = sin(v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength  + v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.z = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength  + v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.y = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength  + v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.z = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength  + v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength  + v.vertex.z * _InvWaveLength) * _Magnitude;
                // offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;

                v2f o;
                // 裁剪空间坐标
                o.vertex = TransformObjectToHClip(v.vertex + offset);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                // 修改纹理的 y 坐标,可以让纹理中的白线流动使得水流更加真实
                o.uv += float2(0.0, _Time.y * _Speed);
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float4 c = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
                c.rgb *= _Color.rgb;
                return c;
            }
            ENDHLSL
        }
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值