GithubHelp home page GithubHelp logo

imclab / threex.terrain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeromeetienne/threex.terrain

0.0 3.0 0.0 790 KB

three.js extension to generate perlin terrain

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

License: MIT License

threex.terrain's Introduction

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

threex.terrain's People

Contributors

jeromeetienne avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.