Login

Seamless heightmap generator

A newly generated and smoothed heightmap.
Kohaku's picture
sync on
sync rate 60
backdrop on
autocam off
set camera range 10, 10000
 
position camera 300,300, 0
xrotate camera 45
move camera -500
 
global displayData : displayData = 1
 
//size of map, must be 2,4,8,16 etc
global size : size = 64
 
dim map(size,size)
 
if displayData = 1
	make matrix 1, 400,400, size+1, size+1
endif
 
 
//noise
for x = 1 to size
	for y = 1 to size
		map(x,y) = rnd(255)
	next y
next x
	remstart
	//make the edges the same
	for x = 1 to size
			map(x,size) = map(x,1)
	next x
	for y = 1 to size
			map(size,y) = map(1,y)
	next y
	remend
	if displayData = 1
		for x = 1 to size
			for y = 1 to size
				set matrix height 1, x, y, map(x,y)
			next y
		next x
		update matrix 1
	endif
//display
do
 
	//stretch map
	if shiftkey()
		stretchMap(size*2)
		repeat
		until shiftkey() = 0
	endif
 
	//noise
	if controlkey()
		for x = 1 to size
			for y = 1 to size
				map(x,y) = rnd(255)
				if displayData = 1
					set matrix height 1, x, y, map(x,y)
				endif
			next y
		next x
		if displayData = 1
			update matrix 1
		endif
		repeat
		until controlkey() = 0
	endif
 
	//smooth
	if spacekey()
		for goes = 1 to 5
		for x = 1 to size
			for y = 1 to size
				smoothCell(x, y)
				if displayData = 1
					set matrix height 1, x, y, map(x,y)
				endif
			next y
		next x
		if displayData = 1
			update matrix 1
		endif
		next goes
		repeat	
		until spacekey() = 0
 
	endif
 
 
	//save heightmap
	if keystate(31)=1
 
		h#=-10000
		l#=10000
		`find high and low points
		for a = 1 to size
			for b = 1 to size
				if map(a,b) > h#
					h# = map(a,b)
				endif
				if map(a,b) < l#
					l# = map(a,b)
				endif
			next b
		next a
 
		for a = 1 to size
			for b = 1 to size
				null = int( (map(a,b)-l#)/(h#-l#) * 255 )
				dot a,b, rgb(null, null, null)
			next b
		next a
 
		if image exist(10) then delete image 10
		get image 10, 1,1,size+1,size+1,3
		if file exist("h.bmp") then delete file "h.bmp"
		save image "h.bmp", 10
 
		repeat
		until keystate(31)=0
	endif
 
	//show data
	if size < 33
		for x = 1 to size
			for y = 1 to size
				text x*40, y*15, str$(map(x,y))
			next y
		next x
	endif
 
	MouseControl(10)
 
	text 1, 1, "Space: Smoothen    Ctrl: Noise    S: Save heightmap    Shift: Stretch*2    size="+str$(size)
 
 
	sync
loop
 
 
end
 
function smoothCell(x, y)
		if y-1 < 1
			top = size
		else
			top = y-1
		endif
 
 
		if y+1 > size 
			bottom = 1
		else
			bottom = y+1
		endif
 
		if x-1 < 1
			left = size
		else
			left = x-1
		endif
 
		if x+1 > size
			right = 1
		else
			right = x+1
		endif
 
		map(x,y)= (map(x,top) + map(x, bottom) + map(left, y) + map(right, y))/4
 
endfunction
 
function minMax(v, minv, maxv)
	if v < minv then v = minv
	if v > maxv then v = maxv
endfunction v
 
 
 
 
function MouseControl(Speed as float)
 
	if mouseclick()>0
		xrotate camera camera angle x()+mousemovey()
		yrotate camera camera angle y()+mousemovex()
		position mouse screen width()/2, screen height()/2
		if mouseclick()=1 then move camera Speed
		if mouseclick()=2 then move camera (0-Speed)
	endif
 
	r=mousemovex()
	r=mousemovey()
endfunction
 
function stretchMap(toSize)
 
	ratio = toSize/size
 
	dim tempMap(toSize, toSize)
 
	for x = 1 to toSize
		for y = 1 to toSize
			tempMap(x, y) = map(int(x/ratio), int(y/ratio))
		next y
	next x
 
	dim map(toSize, toSize)
 
	for x = 1 to toSize
		for y = 1 to toSize
			map(x, y) = tempMap(x, y)
		next y
	next x
 
	size = toSize
 
	if displayData = 1
		delete matrix 1
		make matrix 1, 400,400, size+1, size+1
 
		for x = 1 to size
			for y = 1 to size
				set matrix height 1, x, y, map(x,y)
			next y
		next x
		update matrix 1
 
	endif
 
endfunction

DownloadSize
Seamless heightmap generator.exe2.19 MB
Average: 5 (1 vote)

Comments

ooh,
Merlin's picture

ooh, hypnotic..........

*donates all money to devaug*

You got it to run?!
Kohaku's picture

You got it to run?! Shocked