Files
MinFt/Client/LocalPackages/zzwater/shaders/libs/WindAnim.hlsl
2026-04-27 12:07:32 +08:00

57 lines
1.6 KiB
HLSL

//UNITY_SHADER_NO_UPGRADE
#ifndef WIND_ANIM_INCLUDE
#define WIND_ANIM_INCLUDE
inline float4 SmoothCurve( float4 x ) {
return x * x *( 3.0 - 2.0 * x );
}
inline float4 TriangleWave( float4 x ) {
return abs( frac( x + 0.5 ) * 2.0 - 1.0 );
}
inline float4 SmoothTriangleWave( float4 x ) {
return SmoothCurve( TriangleWave( x ) );
}
inline float4 ClothWindAnim(float4 pos, float3 normal, float4 animParams,float4 wind,float2 time)
{
// animParams.x = branch phase
// animParams.y = edge flutter factor
// animParams.z = primary factor
// animParams.w = secondary factor
float fDetailAmp = 0.1f;
float fBranchAmp = 0.3f;
// Phases (object, vertex, branch)
float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
float fBranchPhase = fObjPhase + animParams.x;
float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
// x is used for edges; y is used for branches
float2 vWavesIn = time + float2(fVtxPhase, fBranchPhase );
// 1.975, 0.793, 0.375, 0.193 are good frequencies
float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
vWaves = SmoothTriangleWave( vWaves );
float2 vWavesSum = vWaves.xz + vWaves.yw;
// Edge (xz) and branch bending (y)
float3 bend = animParams.y * fDetailAmp * normal.xyz;
bend.y = animParams.w * fBranchAmp;
pos.xyz += ((vWavesSum.xyx * bend)*wind.w + (wind.xyz * vWavesSum.y * animParams.w)) * wind.w;
// Primary bending
// Displace position
pos.xyz += animParams.z * wind.xyz;
return pos;
}
#endif //WIND_ANIM_INCLUDE