This is post is part of the ‘one threex a day’ challenge. This challenge is to publish every day one game extension for three.js! One per day, every day and that for 2month! In this post, we gonna talk about threex.terrain. threex.terrain is a three.js games extension which provides a procedural terrain generated from a simplex noise, the Perlin noise. As you can see you have different zones that make the terrain more varied, the blue zone represents water, the green one represents trees or grass and the white zone at the mountain top is snow. Imagine your video game character walking on these 3D mountains or flying over them, pretty cool eh? You can take him through river, forest, wind and snow if you want ;)
To see the other posts about one threex a day and forget our moto! “A THREEx extension a day, gets your game on its way!”
Show Don’t Tell
- examples/canvas.html [view source] : It shows a perlin terrain in a canvas 2d.
- examples/planegeometry.html [view source] : It displays the terrain in 3d with three.js.
- examples/height.html [view source] : It show how to test the height in a 3d terrain
- examples/minecraft.html [view source] : It show a minecraft character walking on perlin terrain
How To Install It
You can install it via script tag
Or you can install with bower, as you wish.
How To Use It
To allocate a heightMap with a width of 100 and a depth of 200, do
To generate some heights based on a simplex/perlin noise, do
If you want to display it in three.js, built a
THREE.PlaneGeometry for it
1 2 3 4 5 6 7
To get the ground height of this mesh, use the following
It is possible to enhance the rendering of this heightmap with some vertexColor, and a smoother shading if you want.
1 2 3 4 5 6 7 8 9 10 11 12
To get the height with heightMap coordinates, just use
If you want to display the result in a canvas 2d, just do