Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /// Generates pseudo random double
  2. /// based on input [Vector2], try to
  3. /// change numbers within to see how it changes
  4. /// the pattern generated. Try some small integers
  5. /// to see more repetition in how tiles are placed.
  6. double random2(Vector2 vec) {
  7. // return vec.x % 2 + vec.y % 2;
  8. // return frac(vec.dot(vec.xy) * 0.28);
  9. // return frac(vec.x * vec.y * .99);
  10.  
  11. return frac(
  12. sin(vec.dot(Vector2(11.12312312, 21.31231231))) * 43243.42987234,
  13. );
  14. }
  15.  
  16. /// Main area of interest, this function will
  17. /// return color for each particular color on our [ui.Image]
  18. int generatePixel(int x, int y, Size size) {
  19. var tiles = 25.0;
  20. var uv = Vector2(x / size.width, y / size.height);
  21. uv.y /= size.aspectRatio;
  22.  
  23. // This is our x,y from 0 to tiles
  24. var tileCoordinate = uv * tiles;
  25.  
  26. // This vector would only have unique values
  27. // within each cell. Essentially,
  28. // it's an integer coordinate of the cell.
  29. // Vector2(0.1, 4.4) -> Vector2(0.0, 4.0);
  30. var tileIndex = tileCoordinate.clone()..floor();
  31.  
  32. // Compute uv position within given tile
  33. // Vector2(0.1, 4.4) -> Vector2(0.1, 0.4);
  34. var gridUv = frac2(tileCoordinate);
  35.  
  36. // 50-50% chance of generating either of two
  37. // tile fillings.
  38. // First is light vertical stripes, second
  39. // is slightly darker horizontal ones
  40. if (random2(tileIndex) > .5) {
  41. return (gridUv.x < .5)
  42. ? 0xffeeeeee
  43. : 0xff000000;
  44. } else {
  45. return gridUv.y < .5
  46. ? 0xff000000
  47. : 0xff999999;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement