build/THREE.Terrain.js
function WhiteNoise(g, options, scale, segments, range, data) {
        if (scale > segments) return;
        var i = 0,
            j = 0,
            xl = segments,
            yl = segments,
            inc = Math.floor(segments / scale),
            lastX = -inc,
            lastY = -inc;
        // Walk over the target. For a target of size W and a resolution of N,
        // set every W/N points (in both directions).
        for (i = 0; i <= xl; i += inc) {
            for (j = 0; j <= yl; j += inc) {
                var k = j * xl + i;
                data[k] = Math.random() * range;
                if (lastX < 0 && lastY < 0) continue;
                // jscs:disable disallowSpacesInsideBrackets
                /* c b *
                 * l t */
                var t = data[k],
                    l = data[ j      * xl + (i-inc)] || t, // left
                    b = data[(j-inc) * xl +  i     ] || t, // bottom
                    c = data[(j-inc) * xl + (i-inc)] || t; // corner
                // jscs:enable disallowSpacesInsideBrackets
                // Interpolate between adjacent points to set the height of
                // higher-resolution target data.
                for (var x = lastX; x < i; x++) {
                    for (var y = lastY; y < j; y++) {
                        if (x === lastX && y === lastY) continue;
                        var z = y * xl + x;
                        if (z < 0) continue;
                        var px = ((x-lastX) / inc),
                            py = ((y-lastY) / inc),
                            r1 = px * b + (1-px) * c,
                            r2 = px * t + (1-px) * l;
                        data[z] = py * r2 + (1-py) * r1;
                    }
                }
                lastY = j;
            }
            lastX = i;
            lastY = -inc;
        }
        // Assign the temporary data back to the actual terrain heightmap.
        for (i = 0, xl = options.xSegments + 1; i < xl; i++) {
            for (j = 0, yl = options.ySegments + 1; j < yl; j++) {
                // http://stackoverflow.com/q/23708306/843621
                var kg = j * xl + i,
                    kd = j * segments + i;
                g[kg].z += data[kd];
            }
        }
    }
function WhiteNoise(g, options, scale, segments, range, data) {
        if (scale > segments) return;
        var i = 0,
            j = 0,
            xl = segments,
            yl = segments,
            inc = Math.floor(segments / scale),
            lastX = -inc,
            lastY = -inc;
        // Walk over the target. For a target of size W and a resolution of N,
        // set every W/N points (in both directions).
        for (i = 0; i <= xl; i += inc) {
            for (j = 0; j <= yl; j += inc) {
                var k = j * xl + i;
                data[k] = Math.random() * range;
                if (lastX < 0 && lastY < 0) continue;
                // jscs:disable disallowSpacesInsideBrackets
                /* c b *
                 * l t */
                var t = data[k],
                    l = data[ j      * xl + (i-inc)] || t, // left
                    b = data[(j-inc) * xl +  i     ] || t, // bottom
                    c = data[(j-inc) * xl + (i-inc)] || t; // corner
                // jscs:enable disallowSpacesInsideBrackets
                // Interpolate between adjacent points to set the height of
                // higher-resolution target data.
                for (var x = lastX; x < i; x++) {
                    for (var y = lastY; y < j; y++) {
                        if (x === lastX && y === lastY) continue;
                        var z = y * xl + x;
                        if (z < 0) continue;
                        var px = ((x-lastX) / inc),
                            py = ((y-lastY) / inc),
                            r1 = px * b + (1-px) * c,
                            r2 = px * t + (1-px) * l;
                        data[z] = py * r2 + (1-py) * r1;
                    }
                }
                lastY = j;
            }
            lastX = i;
            lastY = -inc;
        }
        // Assign the temporary data back to the actual terrain heightmap.
        for (i = 0, xl = options.xSegments + 1; i < xl; i++) {
            for (j = 0, yl = options.ySegments + 1; j < yl; j++) {
                // http://stackoverflow.com/q/23708306/843621
                var kg = j * xl + i,
                    kd = j * segments + i;
                g[kg].z += data[kd];
            }
        }
    }
function WhiteNoise ( g , options , scale , segments , range , data ) { if ( scale > segments ) return ; var i = 0 , j = 0 , xl = segments , yl = segments , inc = Math . floor ( segments / scale ) , lastX = - inc , lastY = - inc ; // Walk over the target. For a target of size W and a resolution of N, // set every W/N points (in both directions). for ( i = 0 ; i <= xl ; i += inc ) { for ( j = 0 ; j <= yl ; j += inc ) { var k = j * xl + i ; data [ k ] = Math . random ( ) * range ; if ( lastX < 0 && lastY < 0 ) continue ; // jscs:disable disallowSpacesInsideBrackets /* c b *
                 * l t */ var t = data [ k ] , l = data [ j * xl + ( i - inc ) ] || t , // left b = data [ ( j - inc ) * xl + i ] || t , // bottom c = data [ ( j - inc ) * xl + ( i - inc ) ] || t ; // corner // jscs:enable disallowSpacesInsideBrackets // Interpolate between adjacent points to set the height of // higher-resolution target data. for ( var x = lastX ; x < i ; x ++ ) { for ( var y = lastY ; y < j ; y ++ ) { if ( x === lastX && y === lastY ) continue ; var z = y * xl + x ; if ( z < 0 ) continue ; var px = ( ( x - lastX ) / inc ) , py = ( ( y - lastY ) / inc ) , r1 = px * b + ( 1 - px ) * c , r2 = px * t + ( 1 - px ) * l ; data [ z ] = py * r2 + ( 1 - py ) * r1 ; } } lastY = j ; } lastX = i ; lastY = - inc ; } // Assign the temporary data back to the actual terrain heightmap. for ( i = 0 , xl = options . xSegments + 1 ; i < xl ; i ++ ) { for ( j = 0 , yl = options . ySegments + 1 ; j < yl ; j ++ ) { // http://stackoverflow.com/q/23708306/843621 var kg = j * xl + i , kd = j * segments + i ; g [ kg ] . z += data [ kd ] ; } } }
Generate a heightmap using white noise.

@param {THREE.Vector3[]} g The terrain vertices.
@param {Object} options Settings
@param {Number} scale The resolution of the resulting heightmap.
@param {Number} segments The width of the target heightmap.
@param {Number} range The altitude of the noise.
@param {Number[]} data The target heightmap.
Generate a heightmap using white noise .