LDraw.org Discussion Forums

Full Version: Heightmap to brick converter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
With many thanks to Roland, here is an example of a complete script to generate a kind of landscape with a "height map" from Excel.
Excel source and LDraw file attached.

Result:
[attachment=3198]

Script saved as generate.lua and put in folder C:\Users\[user]\AppData\Roaming\LDCad 1.6b\scripts\default\global



Code:
local genTools=require('genTools')

function heightGen()

 local sf=ldc.subfile()
 if not sf:isLinked() then
   ldc.dialog.runMessage('No active session')
   return
 end
 
 local sel=ldc.selection()
 sel:remove()

 local depthMap={
{1,1,2,2,3,2,2,1,1},
  {1,1,2,3,3,3,2,1,1},
  {2,2,3,4,4,4,3,2,2},
  {2,3,4,4,5,4,4,3,2},
  {3,3,4,5,6,5,4,3,3},
  {2,3,4,4,5,4,4,3,2},
  {2,2,3,4,4,4,3,2,2},
  {1,1,2,3,3,3,2,1,1},
  {1,1,2,2,3,2,2,1,1}
 }

 local colorMap={
{1,1,2,2,3,2,2,1,1},
  {1,1,2,3,3,3,2,1,1},
  {2,2,3,4,4,4,3,2,2},
  {2,3,4,4,5,4,4,3,2},
  {3,3,4,5,6,5,4,3,3},
  {2,3,4,4,5,4,4,3,2},
  {2,2,3,4,4,4,3,2,2},
  {1,1,2,3,3,3,2,1,1},
  {1,1,2,2,3,2,2,1,1}
 }

 local h=genTools.tableArrayCount(depthMap)
 local w=genTools.tableArrayCount(depthMap[1])

 for x=1,w do
   for z=1,h do
     local v=depthMap[z][x]
     local c=colorMap[z][x]

     local v1=v // 3
     local v2=v % 3
   
     local xx=(x-1)*20
     local zz=(z-1)*20
     local yy=0
     
     for b=1,v1  do
       yy=yy-24
       sel:add(sf:addNewRef('3005.dat', c, ldc.matrix(xx, yy, zz)))      
     end
     
     for p=1,v2 do
       yy=yy-8
       sel:add(sf:addNewRef('3024.dat', c, ldc.matrix(xx, yy, zz)))
     end
     
   end
 end
end

function register()

  local macro=ldc.macro('Gen Heightmap')
  macro:setHint('Generate terrain from heightmap')
  macro:setEvent('run', 'heightGen')

end

register()
(2018-06-19, 13:28)Jaco van der Molen Wrote: [ -> ]With many thanks to Roland, here is an example of a complete script to generate a kind of landscape with a "height map" from Excel.
Excel source and LDraw file attached.

Fun Smile

I was playing around with actual landscape generating last night using a "Diamond square algorithm" lua module I found somewhere.
Unfortunately it was too slow (due to the hard coded max script run time) to generate any real sized ones.
(2018-06-19, 18:19)Roland Melkert Wrote: [ -> ]
(2018-06-19, 13:28)Jaco van der Molen Wrote: [ -> ]With many thanks to Roland, here is an example of a complete script to generate a kind of landscape with a "height map" from Excel.
Excel source and LDraw file attached.

Fun Smile

I was playing around with actual landscape generating last night using a "Diamond square algorithm" lua module I found somewhere.
Unfortunately it was too slow (due to the hard coded max script run time) to generate any real sized ones.

Oh, I see. Very interesting. I know the Diamond square algorithm is used in games for example to create terrain.
I found a post on Stackoverflow how to make a Diamond square algorithm output to excel, but it doesn't work properly.
If we could somehow get a Diamond square algorithm to work in excel and it outputs numbers in cells, we could use your script.
Or better still straight in LDCad.

I have a few thoughts on this and what it should be able to do, but alas I am not a programmer so no idea how to make it.

My idea is as follows:
  • Modify the script to use only 1x1 plates
  • Be able to set user prefs like set up dimensions in baseplate format 16x16, 32x32 or 48x48
  • Set up colors corresponding to plate height for terrain
    Ie. very global 0 = blue water, 1 = tan sand, 2 = bright green gras, 3 = green trees, 4 = DBG hill, 5 = LBG mountain, 6 = whith snow top
Then
  • Using the Diamond square algorithm create output in Excel or straight in LDCad (?) to fill the height array
  • Generate the LDraw model
That would be awesome.
(2018-06-20, 10:22)Jaco van der Molen Wrote: [ -> ]Oh, I see. Very interesting. I know the Diamond square algorithm is used in games for example to create terrain.
I found a post on Stackoverflow how to make a Diamond square algorithm output to excel, but it doesn't work properly.
If we could somehow get a Diamond square algorithm to work in excel and it outputs numbers in cells, we could use your script.
Or better still straight in LDCad.

I have a few thoughts on this and what it should be able to do, but alas I am not a programmer so no idea how to make it.

My idea is as follows:
  • Modify the script to use only 1x1 plates
  • Be able to set user prefs like set up dimensions in baseplate format 16x16, 32x32 or 48x48
  • Set up colors corresponding to plate height for terrain
    Ie. very global 0 = blue water, 1 = tan sand, 2 = bright green gras, 3 = green trees, 4 = DBG hill, 5 = LBG mountain, 6 = whith snow top
Then
  • Using the Diamond square algorithm create output in Excel or straight in LDCad (?) to fill the height array
  • Generate the LDraw model
That would be awesome.

Should all be possible directly in LDCad, but with 1.6 the main problem is the 'slowness' of the addNewRef function as it takes ~1ms and currently the maximum runtime of a script is limited to 2000ms. I will remove that limitation in 1.6c though (if ever released, so 2.0 otherwise).
(2018-06-20, 17:18)Roland Melkert Wrote: [ -> ]Should all be possible directly in LDCad, but with 1.6 the main problem is the 'slowness' of the addNewRef function as it takes ~1ms and currently the maximum runtime of a script is limited to 2000ms. I will remove that limitation in 1.6c though (if ever released, so 2.0 otherwise).

OK, so that means the current limit is about 2000 bricks or a bit less?
(2018-06-21, 5:50)Jaco van der Molen Wrote: [ -> ]
(2018-06-20, 17:18)Roland Melkert Wrote: [ -> ]Should all be possible directly in LDCad, but with 1.6 the main problem is the 'slowness' of the addNewRef function as it takes ~1ms and currently the maximum runtime of a script is limited to 2000ms. I will remove that limitation in 1.6c though (if ever released, so 2.0 otherwise).

OK, so that means the current limit is about 2000 bricks or a bit less?

It depends on multiple things, but the bottle neck is clearly the addNewRef.

I compiled a version without the max execution time, and played around with diamond square.

[attachment=3203]

This consists of 36000 bricks and, the script took about 35 seconds.


2.0's scripting environment will be much more suited for these kinds of things as the one in 1.6 is mostly optimized for animations.
(2018-06-21, 22:17)Roland Melkert Wrote: [ -> ]
(2018-06-21, 5:50)Jaco van der Molen Wrote: [ -> ]OK, so that means the current limit is about 2000 bricks or a bit less?

It depends on multiple things, but the bottle neck is clearly the addNewRef.

I compiled a version without the max execution time, and played around with diamond square.



This consists of 36000 bricks and, the script took about 35 seconds.


2.0's scripting environment will be much more suited for these kinds of things as the one in 1.6 is mostly optimized for animations.

Oh wow! That looks stunning already.

I think we can conclude that Michael Horvath's original question is answered:
"Does anyone know of any heightmap to LDraw brick converters?"

The answer is yes, though in development :-)

Do you mind sharing this script? Can I run some tests too on smaller scale?
(2018-06-22, 7:14)Jaco van der Molen Wrote: [ -> ]Do you mind sharing this script? Can I run some tests too on smaller scale?

Attached zip contains two .lua files, put landscape.lua in the global folder and heightmap.lua in the modules folder of %appdata%/LDCad/scripts/default

Also change the
maxExecTime
value to 2000 in main.cfg for the maximum time allowed in 1.6b (instead of the default 250ms)

Using a freshly started new file might be slightly faster as deleted type 1 lines are still kept in memory and thus processed during addNewRef stuff.

On my system it runs at max 64x64 (most of the time) within the 2s limit. It's an older system though, so you might go higher if you have a more recent one.

Look for hMap.create to change the dimensions, (should be powers of 2).
(2018-06-22, 17:39)Roland Melkert Wrote: [ -> ]
(2018-06-22, 7:14)Jaco van der Molen Wrote: [ -> ]Do you mind sharing this script? Can I run some tests too on smaller scale?

Attached zip contains two .lua files, put landscape.lua in the global folder and heightmap.lua in the modules folder of %appdata%/LDCad/scripts/default

Also change the
maxExecTime
value to 2000 in main.cfg for the maximum time allowed in 1.6b (instead of the default 250ms)

Using a freshly started new file might be slightly faster as deleted type 1 lines are still kept in memory and thus processed during addNewRef stuff.

On my system it runs at max 64x64 (most of the time) within the 2s limit. It's an older system though, so you might go higher if you have a more recent one.

Look for hMap.create to change the dimensions, (should be powers of 2).

I get this error:

[string "landscape.lua"]:11: module 'heightmap' not found:
    no field package.preload['heightmap']
    no file 'C:\Users\mj\AppData\Roaming\LDCad 1.6b\scripts\default\global\heightmap.lua'
    no file 'C:\Users\mj\AppData\Roaming\LDCad 1.6b\scripts\default\modules\heightmap.lua'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\heightmap.dll'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\..\lib\lua\5.3\heightmap.dll'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\loadall.dll'
    no file '.\heightmap.dll'
Script main run failed.
(2018-06-25, 12:34)Jaco van der Molen Wrote: [ -> ]I get this error:

[string "landscape.lua"]:11: module 'heightmap' not found:
    no field package.preload['heightmap']
    no file 'C:\Users\mj\AppData\Roaming\LDCad 1.6b\scripts\default\global\heightmap.lua'
    no file 'C:\Users\mj\AppData\Roaming\LDCad 1.6b\scripts\default\modules\heightmap.lua'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\heightmap.dll'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\..\lib\lua\5.3\heightmap.dll'
    no file 'C:\Program Files (x86)\LDraw\LDCad 1.6b\loadall.dll'
    no file '.\heightmap.dll'
Script main run failed.
Did you put the loose heightmap.lua file in the global folder and not its whole containing folder.
Pages: 1 2 3