【内置渲染管线】Built_IN水面效果Shader

本文介绍两种实现水面波动效果的Shader技术。一种通过UV动画调整纹理映射,结合时间变量产生波动感;另一种则利用噪声纹理和动态参数,如水波速度和振幅,创建更真实的水波动效果。这两种技术适用于游戏开发和视觉效果制作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.水面波动UV动画

Shader "Hsj/Whater" {

		Properties
		{
		_MainTex("MainTex",2D) = "white"{}
		_MainColor("MainColor",COLOR) = (1,1,1,1)
		_AddTex("AddTex",2D) = "white"{}
		_Maxset("Max",Range(0.1,1.0)) = 0.1
		}
			SubShader
		{
		Tags{"RenderType" = "Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True"}
		LOD 200
		pass
		{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "UnityCG.cginc"

		float4 _MainColor;
		sampler2D _MainTex;
		sampler2D _AddTex;
		float4 _MainTex_ST;
		float _Maxset;
		struct appdata
		{
		float4 vertex:POSITION;
		float2 texcoord:TEXCOORD0;
		};
		struct v2f
		{
		float4 pos:POSITION;
		float2 uv:TEXCOORD0;
		};
		v2f vert(appdata v)
		{
		v2f o;
		o.pos = UnityObjectToClipPos(v.vertex);
		o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
		return o;
		}


		float4 frag(v2f o) :SV_Target
		{
		float2 c = (tex2D(_AddTex,o.uv.xy + float2(0,_Time.x)).gb + tex2D(_AddTex,o.uv.xy + float2(_Time.x,0)).gb) - 1;
		float2 ruv = o.uv.xy + c.xy*_Maxset;
		half4 h = tex2D(_MainTex,ruv)*_MainColor;
		return h;
		}
		ENDCG
		}
		}
			FallBack "Diffuse"

	}

2.简单的扰动

 

Shader "Custom/Water"
{
    Properties
    {

        _MainTint("Tint",Color) = (1,1,1,1)
        _MainTex("水纹贴图", 2D) = "white" {}
        _WaterAlpha("水不透明度",Range(0,1)) = 0.5
        _ScrollSpeed("贴图滚动速度", Range(0,10)) = 2

        _NoiseTex("水波噪声贴图",2D) = "white"{}
        _Amount("噪声影响因子", Range(0,2)) = 1
        _Frequency("水波频率",Range(0,1.4)) = 0.8
        _WaveSpeed("水波速度",Range(0.1, 80)) = 10
        _Amplitude("水波振幅", Range(-1, 1)) = 1

        _CWaveVal("水环振幅", Range(.01, 20)) = 1

    }
        SubShader
        {
            Tags
            {
                "Queue" = "Transparent"
                "RenderType" = "Transparent"
            }
            LOD 200

            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
            #pragma surface surf Standard alpha:fade 
            #pragma vertex vert
            #pragma target 4.0

            struct Input
            {
                float2 uv_MainTex;
                float2 uv_StoneTex;
                float2 uv_NoiseTex;
            };

            fixed4 _MainTint;
            fixed _ScrollSpeed;
            sampler2D _MainTex;
            sampler2D _NoiseTex;
            fixed _WaterAlpha;
            fixed _Amount;
            fixed _Frequency;
            fixed _WaveSpeed;
            fixed _Amplitude;
            float _CWaveVal;

            UNITY_INSTANCING_BUFFER_START(Props)
            UNITY_INSTANCING_BUFFER_END(Props)

            void surf(Input IN, inout SurfaceOutputStandard o)
            {
                //引入贴图
                fixed2 scrolledUV = IN.uv_MainTex;
                fixed2 noiseTex = IN.uv_NoiseTex;

                //流动
                fixed ScrollValue = _ScrollSpeed * _Time;//非交替定向流动
                half4 n = tex2D(_NoiseTex,noiseTex) * _Amount;//噪声因子
                scrolledUV += fixed2(ScrollValue + n.x, ScrollValue + n.y);//x,y方向各自偏移量

                //圆环旋转
                float2 center = (0.5, 0.5);//若要中点,注意与平铺值的一半对应;此处平铺为1,1
                float2 dt = IN.uv_MainTex - center;
                float len = length(dt);//偏移量
                float intensity = sin(_SinTime.x * 3) * _CWaveVal;//旋转强度因子
                float off = len * intensity;//对应uv旋转量
                float2x2 rot = float2x2(cos(off), -sin(off), sin(off), cos(off));//二维空间旋转矩阵
                dt = mul(rot, dt) + center;//乘旋转矩阵,加上需要偏移中心的量
                half4 c = tex2D(_MainTex, dt + scrolledUV);//uv偏移(圆环与流动)

                //赋值输出
                o.Albedo = c.rgb * _MainTint;
                o.Alpha = _WaterAlpha;

            }

            void vert(inout appdata_full v, out Input o)
            {
                UNITY_INITIALIZE_OUTPUT(Input,o);
                float time = _WaveSpeed * _Time;
                float waveValue = sin(time + v.vertex.x * _Frequency) * _Amplitude;//偏移量
                v.vertex.xyz = float3(v.vertex.x, v.vertex.y + waveValue, v.vertex.z);//在y轴附加偏移量
            }
            ENDCG
        }
            FallBack "Diffuse"
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值