//UNITY_SHADER_NO_UPGRADE #ifndef WIND_ANIM #define WIND_ANIM // SmoothTriangleWave function inline float SmoothTriangleWave(float x) { // 将输入值限制在 [0, 1] 范围内,使用 frac 函数保证值是周期性的 x = frac(x); // 使用三角函数生成平滑的波形,注意:frac(x) 生成的是 [0, 1] 之间的值 return 1.0 - abs(2.0 * x - 1.0); // 生成平滑的三角波 } // SmoothTriangleWave function for a 4-component vector inline float4 SmoothTriangleWave(float4 x) { return float4(SmoothTriangleWave(x.x), SmoothTriangleWave(x.y), SmoothTriangleWave(x.z), SmoothTriangleWave(x.w)); } inline void WindVert_float(float4 pos, float3 normal, float3 vertColor, float4 wind, float speed, float amplitude, out float4 aniPos) { // animParams.x = branch phase // animParams.y = edge flutter factor // animParams.z = primary factor // animParams.w = secondary factor float bendingFact = 1 - vertColor.b; //wind.xyz = TransformWorldToObject(wind.xyz); //wind.z = wind.z * bendingFact; float4 animParams = float4(0, amplitude, bendingFact.xx); float time = _Time.y * float2(speed, 1); 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; aniPos = pos; } #endif