Skip to content

Instantly share code, notes, and snippets.

@shama

shama/demo.js Secret

Last active December 18, 2015 20:39
Show Gist options
  • Save shama/92c94b257518dd9a5f63 to your computer and use it in GitHub Desktop.
Save shama/92c94b257518dd9a5f63 to your computer and use it in GitHub Desktop.
Mars Tiles - Work In Progress

generating tiles

  • Download with bin/download
  • Resize with bin/resize
  • Stitch with bin/stitch
  • Convert to jpg with convert output.png output.jpg
  • Convert to geotiff with python ./bin/togeo.py output.jpg
  • Use TileMill, add layer, set SRS to 900913, stylesheet to #layerid { raster-scaling: bilinear; } then export as a MBTiles.
  • Extract tiles with mb-util layerid.mbtiles tiles
var createGame = require('voxel-engine')
var walk = require('voxel-walk')
var game = createGame({
chunkDistance: 3,
generateChunks: false,
materialFlatColor: true,
materials: ['#E04C1B', '#ff0000'],
//materials: [['grass', 'dirt', 'grass_dirt'], 'brick', 'dirt'],
texturePath: 'textures/',
skyColor: 0x000000
})
var container = document.body
game.appendTo(container)
game.paused = false
game.view.renderer.setClearColorHex('#000000', 1)
// Generate terrain from tiles
var tile = require('voxel-tile')({
game: game,
tilepath: 'textures/mars/tiles/',
colortilepath: 'textures/marscolor/tiles/'
//flat: true
})
//tile.missingChunk([0, 0, 0])
game.once('tick', function() {
game.voxels.on('missingChunk', tile.missingChunk.bind(tile))
})
// create a player
var createPlayer = require('voxel-player')(game);
var player = createPlayer('textures/shama.png');
player.yaw.position.set(32, 32, -19 * 32);
player.possess();
player.toggle(); // switch to 3rd person
game.setInterval(function() {
console.log(Math.floor(player.yaw.position.x / 32), Math.floor(player.yaw.position.z / 32))
}, 1000)
// I can fly!
require('voxel-fly')(game)(game.controls.target())
game.on('tick', function() {
walk.render(player.playerSkin)
var vx = Math.abs(player.velocity.x)
var vz = Math.abs(player.velocity.z)
if (vx > 0.001 || vz > 0.001) walk.stopWalking()
else walk.startWalking()
// Set zoom level
tile.setZoom(player.yaw.position.y)
})
// toggle between first and third person modes
window.addEventListener('keydown', function (ev) {
if (ev.keyCode === 'R'.charCodeAt(0)) player.toggle()
})
#!/usr/bin/env node
var limit = 1
//var downloadFrom = 'http://www.mars.asu.edu/data/mola_color/'
var downloadFrom = 'http://www.mars.asu.edu/data/mdim_color/'
var downloadTo = 'textures/marscolor/raw/'
var fs = require('fs')
var path = require('path')
var request = require('request')
var cheerio = require('cheerio')
var async = require('async')
var mkdirp = require('mkdirp')
function findImages(uri, done) {
var imgs = []
request(uri, function(err, res, body) {
$ = cheerio.load(body)
$('a').each(function() {
var filename = this.attr('href')
if (!/\.png/g.test(filename)) return
imgs.push(filename)
})
done(imgs)
})
}
function download(uri, filename, done) {
console.log('Downloading ' + uri + ' -> ' + filename + '...')
request.head(uri, function(err, res, body) {
var s = fs.createWriteStream(filename)
s.on('close', done)
request(uri).pipe(s)
})
}
function search(uri, folder) {
var i = 0
findImages(uri, function(imgs) {
console.log('Found ' + imgs.length + ' images')
mkdirp.sync(folder);
async.eachLimit(imgs, limit, function(img, next) {
var filename = (i++) + '_' + path.basename(img)
if (fs.existsSync(folder + filename)) return next()
download(uri + img, folder + filename, next)
}, function(err) {
console.log('Finished downloading ' + imgs.length + ' images.')
})
})
}
search(downloadFrom, downloadTo)
//search(process.argv[2], process.argv[3])
#!/bin/bash
# Requires imagemagick: convert
FILES=textures/mars/raw/*
OUT=textures/mars/tmp/sized
AMT="25%"
mkdir -p $OUT
for f in $FILES
do
o=${f##*/}
cmd="convert -monitor $f -resize $AMT $OUT/$o"
echo "$cmd"
$cmd
done
#!/bin/bash
# Requires imagemagick: convert
DIR=textures/marscolor/raw
TMP=textures/marscolor/tmp/stitch
OUT=textures/marscolor/out
IM="convert -monitor"
mkdir -p $TMP
# convert top/bottom
CMD="$IM"
for x in {0..3}
do
for y in {0..1}
do
i=$(echo "$y * 4 + $x" | bc)
f=`ls $DIR/$i\_*`
CMD="$CMD $f"
done
CMD="$CMD -append $TMP/$x.png"
echo "$CMD"
$CMD
CMD="$IM"
done
echo "---"
mkdir -p $OUT
# convert left to right
CMD="$IM"
for i in {0..3}
do
f="$TMP/$i.png"
CMD="$CMD $f"
done
CMD="$CMD +append $OUT/output.png"
echo "$CMD"
$CMD
rm -rf $TMP
from subprocess import call, check_output
import sys, re
MERC = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs'
infile = sys.argv[1]
info_output = check_output(['gdalinfo', infile])
size_is_re = re.compile('Size is (?P<width>\d+), (?P<height>\d+)')
size_is = filter(lambda x: x, map(lambda x: size_is_re.match(x), info_output.split('\n')))
if (len(size_is) != 1):
raise 'Could not parse gdalinfo output for image size'
size = [float(size_is[0].group('width')), float(size_is[0].group('height'))]
aspect_ratio = size[1] / size[0]
# the full world dimension
dim = 20037508.34 * 2
if (aspect_ratio > 1):
h = dim
w = dim / aspect_ratio
else:
h = dim * aspect_ratio
w = dim
res = call(['gdal_translate', '-a_ullr', str(-w/2), str(-h/2), str(w/2), str(h/2), '-a_srs', MERC, infile, '%s.tif' % infile])
if res != 0:
raise 'An error occurred upon calling gdal_translate'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment