Skip to content

Instantly share code, notes, and snippets.

@memilian
Created August 30, 2015 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memilian/f7743ff5913ca11c4bc3 to your computer and use it in GitHub Desktop.
Save memilian/f7743ff5913ca11c4bc3 to your computer and use it in GitHub Desktop.
package;
import haxe.Timer;
import haxe.PosInfos;
import kha.Key;
import kha.Game;
import libnoise.ModuleBase;
import libnoise.generator.Perlin;
class TexGen extends Game {
public function new() {
super("TexGen", false);
}
override public function loadFinished() {
init();
}
override public function init() : Void {
#if js
haxe.Log.trace = function(o : Dynamic, ?pos : PosInfos){
untyped console.log(Std.string(o));
}
#end
var perlin = new Perlin(0.03, 1.0, 0.5, 8, 123, HIGH);
generateImage(perlin, 512, 512);
}
public override function keyUp(k : Key, c : String) {
var perlin = new Perlin(0.03, 1.0, 0.5, 8, 123, HIGH);
generateImage(perlin, 512, 512);
}
public function generateImage(module : ModuleBase, width : Int, height : Int) : Void {
var startTime = Timer.stamp();
var data = new Array<Float>();
for (y in 0...height) {
for (x in 0...width) {
var value = getGreyValue(module.getValue(x, y, 0));
data.push(value);
}
}
var totalTime = Timer.stamp()-startTime;
trace('generated values in : $totalTime');
}
public function getGreyValue(val : Float) {
var val = Std.int(128 * (val + 1));
return val > 255 ? 255 : val < 0 ? 0 : val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment