Reading a flowmap with script vs shadergraph
Hi,
I'm currently working on a project for the University. I'm working with a flowmap texture to achieve a the "flowing" effect of a river. The logic that i want to achieve is to use the flowmap as a vector field to move an object (using a script) according to the flow of the material on the river mesh (using the shader).
Here the code snippet of the C# scripts that describes how I read the pixel and how i use the vector field data:
Texture2D flowmap;
private void OnDrawGizmos()
{
int pixelCount = flowmap.width * flowmap.height;
for (int i = 0; i < pixelCount; ++i)
{
int row = (i - 1) % flowmap.height + 1;
int col = i / flowmap.height;
if (row % 16 != 0 || col % 16 != 0)
continue;
Vector3 pos = new Vector3(row / (float)flowmap.width, 0f, col / (float)flowmap.height);
pos *= 10f;
pos -= new Vector3(5f, 0, 5f);
Color color = flowmap.GetPixel(row, col);
Vector3 flowDirection = new Vector3(color.r, 0, color.g);
flowDirection.x = Remap(flowDirection.x, 0f, 1f, -1f, 1f);
flowDirection.z = Remap(flowDirection.z, 0f, 1f, -1f, 1f);
Gizmos.color = color;
DrawArrow.ForGizmo(pos, flowDirection / 2f, 0.1f, 10f);
}
}
public static float Remap(float value, float from1, float to1, float from2, float to2)
{
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
The script result seems correct (as you can see in the following image):
The error seems to be inside the shader (made with the shadergraph). The scrolling texture is moving correctly in the corners, but in the middle of the quad it's moving in the opposite direction. This flowmap shader is based on the one present on the shader forge wiki: shader forge wiki flowmap
Anyone can help me?