Creating a rectangular map

Hi everybody.

I’m desesperately trying to create some rectangular map for my program. It requires the user to give an image (so the image can have an arbitrary size). Then, I want to create a terrain with the same dimensions that the image (ie, if the image is 700x683 pixels, create a terrain of 700x683), to add the image as a texture for the terrain, and to specify the height of the points with a HeightMap array.

I’ve seen the class TerrainQuad which has a lot of advantages, but it just doesn’t allow me to have a rectangular size.

One solution I thought about would be to create a TerrainQuad object with a square dimension just superior to the image I go (in my example 1025x1025) and to partially apply the “Unshaded.j3md” material only where there are pixel (ie on a surface 700x683), and to leave the rest of the TerrainQuad object empty. But I don’t know if the idea is good, and even not more how to do it.

Any help would be clearly appreciated.
Many thanks by advance !


Just thinking out loud here and don’t ask me for code cuz I got none on this at all but… Could you not take the users image, then through image manipulation add it to an appropriately sized “flat” image that is square then use that for your TerrainQuad? Just a thought.

Alternatively, you could generate your own mesh.
The terrain system needs the mesh to be a power of 2 so that it can be split easily. Thus, parts of the mesh can be culled out to save CPU/GPU power.
Similarly, the LOD can be different for various parts of the mesh.

Hi and thanks for your quick replies.

@thecyberbob : I don’t understand what you mean by “an appropriately sized “flat” image”. I’m sorry but I’m a real beginner in JMonkey…

@yang71 : The thing is that I don’t know how to use a mesh to create a terrain… What I really loved in the TerrainQuad is that things were interpolated automatically and I had just to give the image as a texture, and an array as HeightMap. Do you have an idea ?


Well… You could still use the whole grid, fill missing heights with zeros of something lile that, and play with shading to make the non-desired part disappear.

I did some “fog of war”, making parts of the terrain disappear based on exploration, using a simple “lightMap” unshaded texture. Thus only the white parts are visible. The remaining is deep black. But still here.

You may also simply hide the undesired parts using some kind of borders… Something like big boxes all around the map, a kind of “frame”.

Ok. What I meant was that TerrainQuads require (as you noticed) a perfectly square image to render from, so like 256 pixels by 256 pixels, 512x512, 1024x1024 etc. To make a “flat” terrain quad you’d simply create an image that is square and all 1 colour (I’d go black so that it’s the lowest possible elevation). When a user imports an grayscale image to be a new height map you’d take the image that the user imported, and add it to the “flat” (completely black) image to essentially make your rectangle a square. What this does is essentially pads the space around the rectangle to make it a square.

What @yang71 suggested isn’t actually all that hard either as a side note. I’ve made meshes for sphere’s and the like and it’s not that bad. Really! For your case you’d need to first detect the dimensions of your image, then loop over every pixel to determine the “height” at that point and store it in a point array. After that you essentially build quads from the points. Seriously it’s not crazy difficult. I followed this and it was straight fairly straight forward. :slight_smile:

1 Like

@thecyberbob : Right. but computing normals is not that fun :(. And the choice on how to span triangles not that evident yet.

Last thing : It may become very large. If you take 700×683, this makes 699 lines of 682 quads… 953K triangles.
This is obviously possible, but it will become a burden fast if you do place objects on the map…

1 Like

@yang71 Good point. :stuck_out_tongue:


JMonkey is not something easy to understand at first, but I’m happy to see this helpful community behind it, ready to help beginners.

Again, many thanks for your help !