Hello.
I’m trying to make a procedurally generated river and a shader for it. I created a simple GetDirection() function that I call for each vertex of the river mesh. In it, I return the direction of the river for the given point. I’m passing the values returned by this function to the mesh’s uv array.
public Vector2 GetDirection(Vector2 position)
{
float minv = 0.0f;
Vector2 direction = Vector2.zero;
for (int i = 0; i < Resolution; i++)
{
Vector2 P = _spline.GetPoint((float)i / (float)(Resolution - 1));
Vector2 E = position;
float reqAns = Vector2.Distance(E, P);
if (i == 0)
{
minv = reqAns;
Vector2 uv;
Vector3 dir = _spline.GetDirection((float)i / (float)(Resolution - 1));
uv = new Vector2(dir.x, dir.z);
direction = uv;
}
else
{
if (reqAns <= minv)
{
minv = reqAns;
Vector2 uv;
Vector3 dir = _spline.GetDirection((float)i / (float)(Resolution - 1));
uv = new Vector2(dir.x, dir.z);
direction = uv;
}
}
}
return direction ;
}
My shader looks like this:
struct appdata {
float4 vertex : POSITION;
float2 direction : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 direction : TEXCOORD0;
float2 worlduv : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
float4 s;
o.pos = UnityObjectToClipPos(v.vertex);
float4 wpos = mul (unity_ObjectToWorld, v.vertex);
o.direction = v.direction;
o.worlduv = wpos.xz;
return o;
}
half4 frag( v2f i ) : COLOR
{
return tex2D(_MainTex, i.worlduv + direction * _Time.y);
}
I am getting wrong result:
With a high resolution mesh, it looks like this:
I get weird zigzag stitches.
How can this be fixed? I tried to pass UV from 0 to 1 instead of directions, but I had the same problem.