Skip to content

Instantly share code, notes, and snippets.

@Uradamus
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Uradamus/e5434b1ea859559a1097 to your computer and use it in GitHub Desktop.
Save Uradamus/e5434b1ea859559a1097 to your computer and use it in GitHub Desktop.
Lua script that takes a file of elevation values and uses them to recreate an InWorldz / Second Life style ground mesh as an OBJ file.
--[[
Call this file with the height input file and the desired output file as arguments.
example: lua ground_obj.lua height.txt ground.obj
]]--
-- Read the input height values.
input = io.open(arg[1], "r")
heights = {}
for line in input:lines() do
table.insert(heights, tonumber(line))
end
io.close(input)
-- Write the output obj file.
output = io.open(arg[2], "w")
for x = 0, 256, 1 do
for y = 0, 256, 1 do
output:write("v ", x, " ", y, " ", heights[x*256+y+1], "\n")
end
end
for i = 0, 256, 1 do
for j = 1, 256, 1 do
output:write("f ", (i*257)+j, " ", (i*257)+j+1, " ", (257*(i+1))+j+1, " ", (257*(i+1))+j, "\n")
end
end
io.close(output)
-- This generates a file full of random height values that can be used to test the above script.
output = io.open("height.txt", "w")
for i = 1, 66049, 1 do
output:write(math.random(3), "\n")
end
io.close(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment