Created
January 19, 2013 07:13
-
-
Save danfinlay/4571184 to your computer and use it in GitHub Desktop.
My png-loading height map voxel generator for voxel.js, which would work except the pngjs module I chose to depend on won't build with browserify: throw moduleError('No wrapper for core module'); It's the kind of thing that makes me helplessly wonder about the depths of the system.
This file contains 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
//Sample usage: | |
//var pngGen = require('./png-gen').loadImage('heightmap.png'); | |
//Assign this as your voxel.js generate function. | |
var fs = require('fs'), | |
PNG = require('pngjs').PNG, | |
theImage, | |
heightMod = 32 | |
function loadImage(url, cb){ | |
fs.createReadStream('url') | |
.pipe(new PNG({ | |
filterType: 4 | |
})) | |
.on('parsed', function() { | |
console.log("Image parsed with width: "+this.width+" and height: "+this.height); | |
theImage = this | |
return generate | |
//Sample image inversion: | |
// for (var y = 0; y < this.height; y++) { | |
// for (var x = 0; x < this.width; x++) { | |
// var idx = (this.width * y + x) << 2; | |
// // invert color | |
// this.data[idx] = 255 - this.data[idx]; | |
// this.data[idx+1] = 255 - this.data[idx+1]; | |
// this.data[idx+2] = 255 - this.data[idx+2]; | |
// // and reduce opacity | |
// this.data[idx+3] = this.data[idx+3] >> 1; | |
// } | |
// } | |
//Sample image saving: | |
//this.pack().pipe(fs.createWriteStream('out.png')); | |
}); | |
} | |
function modulateHeight(mod){ | |
heightMod = mod | |
} | |
function generate(x,y,z){ | |
var idx = (this.width * y + x) << 2; | |
var pitch = this.data[idx]+this.data[idx+1]+this.data[idx+2] | |
return pitch / heightMod | |
} | |
exports.loadImage = loadImage; | |
exports.modulateHeight = modulateHeight; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment