[Closed] HeightMap/Terrain creation problem
Hi,
i’m trying to write a little script wich creates a heightmap/terrain geometry using the same amount of verts as pixels in the heightmap.
So far so good… i can create it and all and it kinda works, except one mayor flaw… the height doesn’t match.
Where it should be a smooth gradient from min to max height i get a bumpy surface wich increases in bumpyness the higher the heightmap&mesh resolution. And i dont have the slightest clue why :surprised
the code
(
with undo false
(
local hmFilePath = @"C:\Users\Insanto\Desktop\heightmap.jpg"
local heightmapBM = openBitmap hmFilePath
local width = heightmapBM.width
local length = heightmapBM.height-1
local maxHeight = 5000
local terra = plane width:10000 length:10000 widthsegs:(width-1) lengthsegs:(length) density:1 renderScale:1 mapCoords:true realWorldMapSize:false wirecolor:orange
convertToMesh terra
for y = 0 to length do
(
local row = getPixels heightmapBM [0,y] width
local verts = #{}
local offsets = for x = 1 to row.count collect
(
local vertI = (width*(length-y)+x)
verts[vertI] = true
local col = (row[x].r + row[x].g + row[x].b)/3.0
local height = maxHeight * (col /255.0)
[0,0,height]
)
meshop.moveVert terra verts offsets useSoftSel:false
)
)
)
i know i should use the UVCoords instead of my current method but that took even longer to calculate, any help on this would be greatly appreciated
haven’t gone thoroughly over the code, just taking a guess… does it change anything if you put in 5000.0 for max height instead of 5000 ?
edit: no, it doesn’t.
yeah its multiplied with a float so wont matter
im atm trying to get some kinda mapped approach for the UV method going :banghead:
see if that helps
edit: nope even with the uv method its still the same problem :shrug:
heres the quick test:
(
with undo false
(
local hmFilePath = @"C:\Users\Insanto\Desktop\heightmap.jpg"
local heightmapBM = openBitmap hmFilePath
local width = heightmapBM.width
local length = heightmapBM.height
local maxHeight = 5000
-- width /= 20
-- length /= 20
local terra = plane width:10000 length:10000 widthsegs:(width-1) lengthsegs:(length-1) density:1 renderScale:1 mapCoords:true realWorldMapSize:false wirecolor:orange
convertToMesh terra
nVerts = terra.numVerts
for v = 1 to nVerts do
(
local uvPos = getTVert terra v
local bmPos = uvPos * [width,length,0] -- [1,1,0]
try(
local col = (getPixels heightmapBM bmPos 1)[1]
col = (col.r + col.g + col.b)/3.0
local height = maxHeight * (col /255.0)
meshop.moveVert terra v [0,0,height] useSoftSel:false
)catch()
)
)
)
hmm might be the floating point thing
lets see
There are two reasons why it’s bumpy:
-
For a high amount of precision, you’ll need to save your images with a floating point format. A 255 color scale isn’t enough to give your plane a completely smooth gradient, given a 100 pixel squared bitmap.
-
Your bitmap is saved as a JPG. The JPG format is lossy, and even though your eyes can’t detect it just by viewing the raw image, the lossy format is affecting the grayscale values very minutely, causing extra bumpiness.
However, given the 2nd reason, if you create a new bitmap and save it in a non-lossy format, you’ll notice that the number of non-uniform bumps on the generated plane is greatly decreased.
hmm looks like your right about that.
was testing with a tif before, only upped a jpg here because tifs wont work but with a raw it giving a nice result