Unity水效果

由于Unity自带的水用起来实在不方便,而且效果一般,没办法只好自己重新写了一个,最终出来的效果感觉还想,如果觉得效果不好请勿喷。先上图,预览一下效果!
Unity水效果 - Util - Util

Shader:

Shader "Util's Shader/Water/Water Real"
{
Properties
{
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (1,1,1,1)
_Shininess("Shininess", Range(0.01,5) ) = 0.01
_MainTex("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap("Normalmap", 2D) = "bump" {}
_RefractionColor("Refraction Color", Color ) = (1,1,1,1)
_RefractionDis("Refraction Distortion", Range(0,1) ) = 1
_ReflectColor("Reflection Color", Color) = (1,1,1,1)
_ReflectionDis("Reflection Distortion", Range(0,1) ) = 1
_ReflectionTex("Reflection Texture", 2D) = "black" {}
_Fresnel("Fresnel", Range(0,1) ) = 0
_WaveSpeed("Wave speed (map1 x,y; map2 x,y)", Vector) = (0.2,0.2,1,0)
}

SubShader
{
GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
}
Tags { "Queue"="Transparent" "IgnoreProjector"="False" "RenderType"="Opaque" }
Cull Back
Lighting Off
CGPROGRAM
#pragma surface surf BlinnPhongGlass dualforward
#pragma target 3.0

inline fixed4 LightingBlinnPhongGlass ( inout SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
half3 h = normalize (lightDir + viewDir);
fixed diff = max (0, dot (s.Normal*2, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
s.Alpha = s.Alpha + c.a + c.rgb;
return c;
}

sampler2D _GrabTexture;
fixed4 _Color;
half _Shininess;
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _ReflectColor;
sampler2D _ReflectionTex;
half _ReflectionDis;
fixed4 _RefractionColor;
half _RefractionDis;
half _Fresnel;
float4 _WaveSpeed;

struct Input {
float2 uv_MainTex

### 如何在 Unity 中实现流效果Unity 中实现流效果通常涉及编写自定义 Shader 来模拟流的动态特性。以下是关于如何实现这一目标的具体方法: #### 使用 Surface Shader 或 Vertex Fragment Shader 为了创建流效果,可以选择使用 **Surface Shader** 或者更灵活的 **Vertex and Fragment Shader**。对于复杂的视觉效果(如面波动、反射和折射),推荐采用后者。 - 表面着色器适合快速原型设计,而顶点片段着色器则提供了更高的自由度以处理复杂场景[^4]。 #### 创建基本的 Water Shader 下面是一个简单的顶点片段着色器示例,用于展示基础的流动画效果: ```csharp Shader "Custom/WaterFlow" { Properties { _MainTex ("Texture", 2D) = "white" {} _SpeedX ("Wave Speed X", Float) = 0.1 _SpeedY ("Wave Speed Y", Float) = 0.1 _ColorA ("Water Color A", Color) = (1,1,1,1) _ColorB ("Water Color B", Color) = (0,0,1,1) } SubShader { Tags { "RenderType"="Opaque" } 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; float _SpeedX; float _SpeedY; fixed4 _ColorA; fixed4 _ColorB; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; // 动态偏移 UV 坐标 o.uv.x += sin(_Time.y * _SpeedX + v.vertex.z * 0.1); o.uv.y -= cos(_Time.y * _SpeedY + v.vertex.x * 0.1); return o; } fixed4 frag(v2f i) : SV_Target { // 结合两种颜色并应用纹理采样 fixed4 col = tex2D(_MainTex, i.uv); col *= lerp(_ColorA, _ColorB, frac(sin(i.uv.x + i.uv.y))); return col; } ENDCG } } } ``` 此代码实现了以下功能: - 利用 `_Time` 变量驱动波浪运动。 - 应用了两个方向上的正弦与余弦函数来制造平和垂直方向的流动感[^3]。 - 颜色渐变由 `lerp()` 函数完成,在不同位置之间平滑过渡。 #### 进一步优化建议 如果希望进一步增强真实感,可以考虑加入更多细节,比如法线贴图(Normal Map)、泡沫边缘以及环境映射等技术。这些都可以显著提升最终呈现的质量。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值