Skip to content

Instantly share code, notes, and snippets.

@memilian memilian/TexGen.hx Secret
Created Aug 30, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.