Skip to content

Instantly share code, notes, and snippets.

@av
Created September 29, 2019 11:07
Show Gist options
  • Save av/d4c60bb0058f57aab79a84ab528d09bd to your computer and use it in GitHub Desktop.
Save av/d4c60bb0058f57aab79a84ab528d09bd to your computer and use it in GitHub Desktop.
Flutter: uvGrid with certain tiles count
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
/// Compute unified vector, values of its components
/// will be between 0 and 1
var uv = Vector2(x / size.width, y / size.height);
var xTiles = 5.0;
var yTiles = 10.0;
/// We can now use this [Vector2] as our unit coordinates
/// in same exact fashion as in examples below and it'll
/// allow us to compute color based on position of cell
/// within grid.
var gridUv = frac2(Vector2(uv.x * xTiles, uv.y * yTiles));
/// By creating new Vector3 we can control color
/// channels by an independent copy of x and y values.
return toColorInt(gridUv.xyx);
}
/// Returns a fractional part of given [Vector2]
/// Vector2(0.5, 1.5) => Vector2(.5, .5);
Vector2 frac2(Vector2 vec) {
return Vector2(
vec.x - vec.x.floor(),
vec.y - vec.y.floor(),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment