扰动

在unity中实现一个水面扰动的效果,这里用到了UV移动,和uv扰动这里我组合了这两个shader实现了一个水面扰动的效果,使用了一个波纹纹理,然后用一张2维噪声扰动uv。产生的效果还是比较乐观的

关键代码

v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv += float2(_XSpeed, _YSpeed)*_Time.y;//uv移动
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 bias = tex2D(_Noise, i.uv+_Time.xy*_distortFactorTime);
                half4 color = tex2D(_MainTex, i.uv+bias.xy*_distortFactor);//uv扰动
                return color*_Color;
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

素材

这里写图片描述这里写图片描述

效果图

这里写图片描述

完整代码

Shader "Custom/DistortSelf"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _Noise ("Noise", 2D) = "white" {}
        _distortFactorTime("FactorTime",Range(0,5)) = 0.5
        _distortFactor("factor",Range(0.04,1)) = 0

        _Color("Color",Color) = (1,1,1,1)
        _XSpeed("XSpeed",Range(-1,1)) = 0
        _YSpeed("YSpeed",Range(-1,1)) = 0
    }
    SubShader
    {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _Noise;
            fixed _distortFactorTime;
            fixed _distortFactor;
            fixed4 _Color;
            float _XSpeed;
            float _YSpeed;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv += float2(_XSpeed, _YSpeed)*_Time.y;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 bias = tex2D(_Noise, i.uv+_Time.xy*_distortFactorTime);
                half4 color = tex2D(_MainTex, i.uv+bias.xy*_distortFactor);
                return color*_Color;
            }
            ENDCG
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值