| 1 | = !WorldPainter Scripting Example 1 = |
| 2 | |
| 3 | A complete example which creates a world from a height map, sets the default terrain and applies the Frost layer according to altitude, and applies a River layer from a mask, saving the result as a new !WorldPainter world which you can edit and export in !WorldPainter. |
| 4 | |
| 5 | This example script expects three files to exist in the current directory: |
| 6 | |
| 7 | `heightmap.png` - a height map for creating the world. For a good demonstration the water level should be around 62 and there should be a mountain on it somewhere at least 120 high[[br]] |
| 8 | `River.layer` - a layer exported from !WorldPainter which will create a river where painted (for instance a Custom Ground Cover layer with a negative thickness and Water as the material)[[br]] |
| 9 | `rivermask.png` - a black and white mask with the same dimensions as heightmap.png, which is white where the river should be painted |
| 10 | |
| 11 | {{{ |
| 12 | #!js |
| 13 | // Load the height map from disk |
| 14 | var heightMap = wp.getHeightMap().fromFile('heightmap.png').go(); |
| 15 | |
| 16 | // Create a new WorldPainter world from the height map |
| 17 | var world = wp.createWorld().fromHeightMap(heightMap).go(); |
| 18 | |
| 19 | // Use the same height map to set the default terrain. For the terrain indices |
| 20 | // to use, see: http://www.worldpainter.net/trac/wiki/Scripting/TerrainTypeValues |
| 21 | wp.applyHeightMap(heightMap) |
| 22 | .toWorld(world) |
| 23 | .applyToTerrain() |
| 24 | .fromLevels(0, 64).toTerrain(36) // Beaches (also makes a good ocean floor) |
| 25 | .fromLevels(65, 96).toTerrain(0) // Grass |
| 26 | .fromLevels(97, 112).toTerrain(3) // Permadirt |
| 27 | .fromLevels(113, 255).toTerrain(29) // Rock |
| 28 | .go(); |
| 29 | |
| 30 | // Apply the Frost layer above 120, again using the same height map as input |
| 31 | var frostLayer = wp.getLayer().name('Frost').go(); |
| 32 | wp.applyHeightMap(heightMap) |
| 33 | .toWorld(world) |
| 34 | .applyToLayer(frostLayer) |
| 35 | .fromLevels(0, 119).toLevel(0) // Make sure to remove any Frost the |
| 36 | .fromLevels(120, 255).toLevel(1) // height map import might have added |
| 37 | .go(); |
| 38 | |
| 39 | // Load the mask for creating the rivers |
| 40 | var riverMask = wp.getHeightMap().fromFile('rivermask.png').go(); |
| 41 | |
| 42 | // Load the actual river layer |
| 43 | var riverLayer = wp.getLayer().fromFile('River.layer').go(); |
| 44 | |
| 45 | // Paint the river layer onto the world using the mask. Map anything that is |
| 46 | // not completely black to apply the layer |
| 47 | wp.applyHeightMap(riverMask) |
| 48 | .toWorld(world) |
| 49 | .applyToLayer(riverLayer) |
| 50 | .fromLevel(0).toLevel(0) |
| 51 | .fromLevels(1, 255).toLevel(1) |
| 52 | .go(); |
| 53 | |
| 54 | // Save the result to disk |
| 55 | wp.saveWorld(world) |
| 56 | .toFile('output.world') |
| 57 | .go(); |
| 58 | }}} |
| 59 | |
| 60 | Save this to a file called `example1.js` in the same directory that contains the height map, etc., and execute the script with the following command from inside that directory: |
| 61 | |
| 62 | {{{ |
| 63 | wpscript example1.js |
| 64 | }}} |