Unity中模拟2D草

2024/04 24 18:04

场景中大批量的草可以使用SkinnedMeshRender,也可以使用顶点动画来模拟

一、x轴受y的影响,模拟横向摇摆

float offset = sin(y);
input.y += offset;

二、防止所有草一起摇摆

使用干扰系数:

float wo = unity_ObjectToWorld[0][3]+unity_ObjectToWorld[1][3]+unity_ObjectToWorld[2][3];
即把xyz的坐标值作为干扰系数

三、完整代码

float4 input = v.vertex;
float wo = unity_ObjectToWorld[0][3]+unity_ObjectToWorld[1][3]+unity_ObjectToWorld[2][3];
float freq = pow( sin(_Time.x * wo * _Param1.x + input.y * _Param1.y), 2);
input.x += (input.y * freq * _Param1.z);
o.pos = UnityObjectToClipPos(input);