Created
September 29, 2019 11:07
-
-
Save av/d4c60bb0058f57aab79a84ab528d09bd to your computer and use it in GitHub Desktop.
Flutter: uvGrid with certain tiles count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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