jeromeetienne / threex.terrain

three.js extension to generate perlin terrain

Home Page:http://jeromeetienne.github.io/threex.terrain/examples/planegeometry.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 ;)

Show Don't Tell

A Screenshot

screenshot

How To Install It

You can install it via script tag

<script src='threex.terrain.js'></script>

Or you can install with bower, as you wish.

bower install threex.terrain

How To Use It

To allocate a heightMap with a width of 100 and a depth of 200, do

var heightMap	= THREEx.Terrain.allocateHeightMap(100, 200)

To generate some heights based on a simplex/perlin noise, do

THREEx.Terrain.simplexHeightMap(heightMap)

If you want to display it in three.js, built a THREE.PlaneGeometry for it

// build the geometry
var geometry	= THREEx.Terrain.heightMapToPlaneGeometry(heightMap)
// init the material
var material	= new THREE.MeshPhongMaterial();
// create the mesh and add it to the scene
var mesh	= new THREE.Mesh( geometry, material );
scene.add( mesh );

To get the ground height of this mesh, use the following

var y = THREEx.Terrain.planeToHeightMapCoords(heightMap, mesh, x, z)

It is possible to enhance the rendering of this heightmap with some vertexColor, and a smoother shading if you want.

// build the geometry
var geometry	= THREEx.Terrain.heightMapToPlaneGeometry(heightMap)
// set the vertexColor in the geometry
THREEx.Terrain.heightMapToVertexColor(heightMap, geometry)
// init the material
var material	= new THREE.MeshPhongMaterial({
	shading		: THREE.SmoothShading,
	vertexColors 	: THREE.VertexColors,
});
// create the mesh and add it to the scene
var mesh	= new THREE.Mesh( geometry, material );
scene.add( mesh );

To get the height with heightMap coordinates, just use

var y	= THREEx.Terrain.heightMapToHeight(heightMap, x, z)

If you want to display the result in a canvas 2d, just do

var canvas	= THREEx.Terrain.heightMapToCanvas(heightMap)
document.body.appendChild(canvas)

Possible optimisations

  • use shader material to build the perlin in shader
  • use THREE.BufferGeometry to boost the generation step

About

three.js extension to generate perlin terrain

http://jeromeetienne.github.io/threex.terrain/examples/planegeometry.html

License:MIT License


Languages

Language:JavaScript 100.0%