Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Generates pseudo random double
- /// based on input [Vector2], try to
- /// change numbers within to see how it changes
- /// the pattern generated. Try some small integers
- /// to see more repetition in how tiles are placed.
- double random2(Vector2 vec) {
- // return vec.x % 2 + vec.y % 2;
- // return frac(vec.dot(vec.xy) * 0.28);
- // return frac(vec.x * vec.y * .99);
- return frac(
- sin(vec.dot(Vector2(11.12312312, 21.31231231))) * 43243.42987234,
- );
- }
- /// 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) {
- var tiles = 25.0;
- var uv = Vector2(x / size.width, y / size.height);
- uv.y /= size.aspectRatio;
- // This is our x,y from 0 to tiles
- var tileCoordinate = uv * tiles;
- // This vector would only have unique values
- // within each cell. Essentially,
- // it's an integer coordinate of the cell.
- // Vector2(0.1, 4.4) -> Vector2(0.0, 4.0);
- var tileIndex = tileCoordinate.clone()..floor();
- // Compute uv position within given tile
- // Vector2(0.1, 4.4) -> Vector2(0.1, 0.4);
- var gridUv = frac2(tileCoordinate);
- // 50-50% chance of generating either of two
- // tile fillings.
- // First is light vertical stripes, second
- // is slightly darker horizontal ones
- if (random2(tileIndex) > .5) {
- return (gridUv.x < .5)
- ? 0xffeeeeee
- : 0xff000000;
- } else {
- return gridUv.y < .5
- ? 0xff000000
- : 0xff999999;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement