forked from arcanis/js.perlin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90295e9
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
function Perlin( table ) { | ||
this._table = table || Perlin.makeTable( 255 ); | ||
|
||
this.octaves = 10; | ||
this.frequency = .1; | ||
this.persistence = .5; | ||
} | ||
|
||
Perlin.random = function ( ) { | ||
return Math.random( ); | ||
}; | ||
|
||
Perlin.makeTable = function ( size ) { | ||
var result = [ ]; | ||
|
||
for ( var n = 0; n < size; ++ n ) | ||
result[ n ] = Perlin.random( ); | ||
|
||
return result; | ||
}; | ||
|
||
Perlin.cosineInterpolate = function ( a, b, t ) { | ||
var c = ( 1 - Math.cos( t * Math.PI ) ) * .5; | ||
|
||
return ( 1 - c ) * a + c * b; | ||
}; | ||
|
||
Perlin.prototype = { | ||
|
||
_randify : function ( n ) { | ||
return this._table[ Math.floor( n % this._table.length ) ]; | ||
}, | ||
|
||
_noise : function ( point ) { | ||
var value = 0; | ||
var dimensions = point.length; | ||
|
||
for (var dimension = 0; dimension < dimensions; ++ dimension ) | ||
value = this._randify( value * 85000 + point[ dimension ] ); | ||
|
||
return value; | ||
}, | ||
|
||
_smooth : function ( point, dimension ) { | ||
if ( dimension < 0 ) | ||
return this._noise( point ); | ||
|
||
var value = point[ dimension ]; | ||
var integer = Math.floor( value ); | ||
var fractional = value - integer; | ||
|
||
point[ dimension ] = integer; | ||
var a = this._smooth( point, dimension - 1 ); | ||
|
||
point[ dimension ] = integer + 1; | ||
var b = this._smooth( point, dimension - 1 ); | ||
|
||
point[ dimension ] = value; | ||
|
||
return Perlin.cosineInterpolate( a, b, fractional ); | ||
}, | ||
|
||
_perlin : function ( point ) { | ||
var value = 0; | ||
var amplitude = 1; | ||
|
||
var octaves = this.octaves; | ||
var frequency = this.frequency; | ||
var persistence = this.persistence; | ||
|
||
var copy = point.slice( ); | ||
var dimensions = copy.length; | ||
|
||
for ( var i = 0; i < octaves; ++ i ) | ||
{ | ||
for ( var dimension = 0; dimension < dimensions; ++ dimension ) | ||
copy[ dimension ] = point[ dimension ] * frequency; | ||
|
||
value += this._noise( copy, dimensions - 1 ) * amplitude; | ||
|
||
amplitude *= persistence; | ||
frequency *= 2; | ||
} | ||
|
||
var limiter = ( 1 - persistence ) / ( 1 - amplitude ); | ||
return value * limiter; | ||
}, | ||
|
||
_generate : function ( start, size, callback, dimension ) { | ||
if ( dimension < 0 ) | ||
return callback( start, this._perlin( start ) ); | ||
|
||
for ( var end = start[ dimension ] + size[ dimension ]; start[ dimension ] < end; ++ start[ dimension ] ) | ||
this._generate( start, size, callback, dimension - 1 ); | ||
|
||
start[ dimension ] -= size[ dimension ]; | ||
return null; | ||
}, | ||
|
||
generate : function ( start, size, callback ) { | ||
this._generate( start, size, callback, start.length - 1 ); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
window.addEventListener( 'load', function ( ) { | ||
var map = new Perlin( ); | ||
|
||
var canvas = document.getElementById( 'canvas' ); | ||
var ctx = canvas.getContext( '2d' ); | ||
|
||
var width = canvas.width; | ||
var height = canvas.height; | ||
|
||
var buffer = ctx.createImageData( width, height ); | ||
|
||
function update( ) { | ||
map.generate( [ 0, 0 ], [ width, height ], function ( point, value ) { | ||
var idx = ( point[ 1 ] * width + point[ 0 ] ) * 4; | ||
var clr = Math.floor( ( value + 1 ) / 2 * 255 ); | ||
console.log(clr); | ||
|
||
buffer.data[ idx + 0 ] = clr; | ||
buffer.data[ idx + 1 ] = clr; | ||
buffer.data[ idx + 2 ] = clr; | ||
buffer.data[ idx + 3 ] = 255; | ||
}); | ||
|
||
ctx.putImageData( buffer, 0, 0 ); | ||
} | ||
|
||
update( ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<canvas id="canvas" width="500" height="500"></canvas> | ||
<script src="../lib/perlin.js"></script> | ||
<script src="demo.js"></script> |