Created
September 29, 2019 13:41
-
-
Save av/83e51968677f4d11d181c3a1fe442fec to your computer and use it in GitHub Desktop.
Flutter: perlin snake
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
/// Generates a [ui.Image] with certain pixel data | |
Future<ui.Image> generateImage(Size size) async { | |
int width = size.width.ceil(); | |
int height = size.height.ceil(); | |
/// PerlinNoise generator, there're lots of parameters | |
/// to tweak and lots of effects possible to produce | |
var noise = PerlinNoise( | |
octaves: 1, frequency: 0.03, fractalType: FractalType.RigidMulti); | |
var completer = Completer<ui.Image>(); | |
Int32List pixels = Int32List(width * height); | |
for (var x = 0; x < width; x++) { | |
for (var y = 0; y < height; y++) { | |
int index = y * width + x; | |
// Compute pixel luminance | |
var luminance = noise.getPerlinFractal2(x.toDouble(), y.toDouble()); | |
// Turn luminance into actual color | |
pixels[index] = luminance > .93 ? Colors.white.value : Colors.black.value; | |
// pixels[index] = generatePixel(x, y, size); | |
} | |
} | |
ui.decodeImageFromPixels( | |
pixels.buffer.asUint8List(), | |
width, | |
height, | |
ui.PixelFormat.bgra8888, | |
(ui.Image img) { | |
completer.complete(img); | |
}, | |
); | |
return completer.future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment