(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o elø1 ? (function () { return minø1 = elø1; })() : void 0; }.call(this)); return loop[0] = inc(index1ø1), loop; })() : void 0; } while (index1ø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this); }.call(this)); return function () { var spanø1 = maxø1 - minø1; return function () { var array4ø1 = heightmap; return function () { var G__5ø1 = array4ø1.shape[0]; return function loop() { var recur = loop; var xø1 = 0; do { recur = xø1 < G__5ø1 ? (function () { (function () { var G__6ø1 = array4ø1.shape[1]; return function loop() { var recur = loop; var yø1 = 0; do { recur = yø1 < G__6ø1 ? (function () { (function () { return heightmapSet(heightmap, xø1, yø1, (heightmapGet(heightmap, xø1, yø1) - minø1) / spanø1); })(); return loop[0] = inc(yø1), loop; })() : void 0; } while (yø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this)); return loop[0] = inc(xø1), loop; })() : void 0; } while (xø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this); }.call(this); }.call(this); }.call(this); }; var makeHeightmap = exports.makeHeightmap = function makeHeightmap(exponent) { return function () { var resolutionø1 = Math.pow(2, exponent) + 1; return function () { var heightmapø1 = ndarray(new Float64Array(resolutionø1 * resolutionø1), [ resolutionø1, resolutionø1 ]); heightmapø1.exponent = exponent; heightmapø1.resolution = resolutionø1; heightmapø1.last = dec(resolutionø1); return heightmapø1; }.call(this); }.call(this); }; var topLeftCorner = exports.topLeftCorner = function topLeftCorner(heightmap) { return function () { var centerø1 = heightmapCenterIndex(heightmap); return heightmap.lo(0, 0).hi(inc(centerø1), inc(centerø1)); }.call(this); }; var topRightCorner = exports.topRightCorner = function topRightCorner(heightmap) { return function () { var centerø1 = heightmapCenterIndex(heightmap); return heightmap.lo(centerø1, 0).hi(inc(centerø1), inc(centerø1)); }.call(this); }; var bottomLeftCorner = exports.bottomLeftCorner = function bottomLeftCorner(heightmap) { return function () { var centerø1 = heightmapCenterIndex(heightmap); return heightmap.lo(0, centerø1).hi(inc(centerø1), inc(centerø1)); }.call(this); }; var bottomRightCorner = exports.bottomRightCorner = function bottomRightCorner(heightmap) { return function () { var centerø1 = heightmapCenterIndex(heightmap); return heightmap.lo(centerø1, centerø1).hi(inc(centerø1), inc(centerø1)); }.call(this); }; var dsInitCorners = exports.dsInitCorners = function dsInitCorners(heightmap) { return function () { var lastø1 = heightmapLastIndex(heightmap); heightmapSet(heightmap, 0, 0, rand()); heightmapSet(heightmap, 0, lastø1, rand()); heightmapSet(heightmap, lastø1, 0, rand()); return heightmapSet(heightmap, lastø1, lastø1, rand()); }.call(this); }; var dsSquare = exports.dsSquare = function dsSquare(heightmap, x, y, radius, spread) { return function () { var newHeightø1 = jitter(average4(heightmapGet(heightmap, x - radius, y - radius), heightmapGet(heightmap, x - radius, y + radius), heightmapGet(heightmap, x + radius, y - radius), heightmapGet(heightmap, x + radius, y + radius)), spread); return heightmapSet(heightmap, x, y, newHeightø1); }.call(this); }; var dsDiamond = exports.dsDiamond = function dsDiamond(heightmap, x, y, radius, spread) { return function () { var newHeightø1 = jitter(safeAverage(heightmapGetSafe(heightmap, x - radius, y), heightmapGetSafe(heightmap, x + radius, y), heightmapGetSafe(heightmap, x, y - radius), heightmapGetSafe(heightmap, x, y + radius)), spread); return heightmapSet(heightmap, x, y, newHeightø1); }.call(this); }; var dsSquares = exports.dsSquares = function dsSquares(heightmap, radius, spread) { return function () { var start8ø1 = radius; var end9ø1 = heightmapResolution(heightmap); var stride7ø1 = 2 * radius; return function loop() { var recur = loop; var xø1 = start8ø1; do { recur = xø1 < end9ø1 ? (function () { (function loop() { var recur = loop; var yø1 = start8ø1; do { recur = yø1 < end9ø1 ? (function () { (function () { return dsSquare(heightmap, xø1, yø1, radius, spread); })(); return loop[0] = yø1 + stride7ø1, loop; })() : void 0; } while (yø1 = loop[0], recur === loop); return recur; }.call(this)); return loop[0] = xø1 + stride7ø1, loop; })() : void 0; } while (xø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this); }; var dsDiamonds = exports.dsDiamonds = function dsDiamonds(heightmap, radius, spread) { return function () { var sizeø1 = heightmapResolution(heightmap); return function () { var start11ø1 = 0; var end12ø1 = sizeø1; var stride10ø1 = radius; return function loop() { var recur = loop; var yø1 = start11ø1; do { recur = yø1 < end12ø1 ? (function () { (function () { return function () { var shiftø1 = isEven(yø1 / radius) ? radius : 0; return function () { var start14ø1 = shiftø1; var end15ø1 = sizeø1; var stride13ø1 = 2 * radius; return function loop() { var recur = loop; var xø1 = start14ø1; do { recur = xø1 < end15ø1 ? (function () { (function () { return dsDiamond(heightmap, xø1, yø1, radius, spread); })(); return loop[0] = xø1 + stride13ø1, loop; })() : void 0; } while (xø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this); }.call(this); })(); return loop[0] = yø1 + stride10ø1, loop; })() : void 0; } while (yø1 = loop[0], recur === loop); return recur; }.call(this); }.call(this); }.call(this); }; var diamondSquare = exports.diamondSquare = function diamondSquare(heightmap) { return function () { var initialSpreadø1 = 0.3; var spreadReductionø1 = 0.5; var centerø1 = heightmapCenterIndex(heightmap); var sizeø1 = heightmap.shape[0]; dsInitCorners(heightmap); (function loop() { var recur = loop; var radiusø1 = centerø1; var spreadø1 = initialSpreadø1; do { recur = radiusø1 >= 1 ? (function () { dsSquares(heightmap, radiusø1, spreadø1); dsDiamonds(heightmap, radiusø1, spreadø1); return loop[0] = radiusø1 / 2, loop[1] = spreadø1 * spreadReductionø1, loop; })() : void 0; } while (radiusø1 = loop[0], spreadø1 = loop[1], recur === loop); return recur; }.call(this)); return normalize(heightmap); }.call(this); }; var diamondSquare1 = exports.diamondSquare1 = function diamondSquare1(heightmap) { dsInitCorners(heightmap); return normalize(heightmap); }; var diamondSquare2 = exports.diamondSquare2 = function diamondSquare2(heightmap) { return function () { var initialSpreadø1 = 0.3; var spreadReductionø1 = 0.5; var centerø1 = heightmapCenterIndex(heightmap); var sizeø1 = heightmap.shape[0]; dsInitCorners(heightmap); dsSquares(heightmap, centerø1, initialSpreadø1); return normalize(heightmap); }.call(this); }; var diamondSquare3 = exports.diamondSquare3 = function diamondSquare3(heightmap) { return function () { var initialSpreadø1 = 0.3; var spreadReductionø1 = 0.5; var centerø1 = heightmapCenterIndex(heightmap); var sizeø1 = heightmap.shape[0]; dsInitCorners(heightmap); dsSquares(heightmap, centerø1, initialSpreadø1); dsDiamonds(heightmap, centerø1, initialSpreadø1); dsSquares(heightmap, centerø1 / 2, spreadReductionø1 * initialSpreadø1); dsDiamonds(heightmap, centerø1 / 2, spreadReductionø1 * initialSpreadø1); return normalize(heightmap); }.call(this); }; var makeDirectionalLight = exports.makeDirectionalLight = function makeDirectionalLight() { return function () { var lightø1 = new THREE.DirectionalLight(16777215, 1); lightø1.position.set(100, 0, 150); return lightø1; }.call(this); }; var makeCamera = exports.makeCamera = function makeCamera() { return function () { var cameraø1 = new THREE.PerspectiveCamera(55, width / height, 0.1, 1000); cameraø1.position.set(0, -100, 150); return cameraø1; }.call(this); }; var makeRenderer = exports.makeRenderer = function makeRenderer() { return function () { var rendererø1 = new THREE.WebGLRenderer({ 'antialias': false }); rendererø1.setClearColor(16777215); rendererø1.setSize(width, height); rendererø1.setPixelRatio(2); return rendererø1; }.call(this); }; var makeGeometry = exports.makeGeometry = function makeGeometry(heightmap) { return function () { var resolutionø1 = heightmap.shape[0]; var geometryø1 = new THREE.PlaneGeometry(terrainSize, terrainSize, resolutionø1 - 1, resolutionø1 - 1); return geometryø1; }.call(this); }; var makeControls = exports.makeControls = function makeControls(camera, renderer) { return function () { var controlsø1 = new THREE.TrackballControls(camera, renderer.domElement); controlsø1.rotateSpeed = 1.4; controlsø1.zoomSpeed = 0.5; controlsø1.staticMoving = true; controlsø1.dynamicDampingFactor = 0.3; return controlsø1; }.call(this); }; var makePlane = exports.makePlane = function makePlane(geometry) { return function () { var materialø1 = new THREE.MeshLambertMaterial({ 'wireframe': wireframe, 'wireframeLinewidth': wireframeWidth, 'color': 47872 }); return new THREE.Mesh(geometry, materialø1); }.call(this); }; var attachToDom = exports.attachToDom = function attachToDom(renderer, elName, refreshFn) { return function () { var containerø1 = document.getElementById(elName); var settingsø1 = document.createElement('div'); var refreshButtonø1 = document.createElement('button'); var buttonTextø1 = document.createTextNode('Refresh'); var cancelScrollø1 = function (e) { return e.preventDefault(); }; refreshButtonø1.onclick = refreshFn; renderer.domElement.onmousewheel = cancelScrollø1; renderer.domElement.addEventListener('MozMousePixelScroll', cancelScrollø1, false); refreshButtonø1.appendChild(buttonTextø1); containerø1.appendChild(renderer.domElement); containerø1.appendChild(settingsø1); return settingsø1.appendChild(refreshButtonø1); }.call(this); }; var updateGeometry = exports.updateGeometry = function updateGeometry(geometry, heightmap) { (function loop() { var recur = loop; var iø1 = 0; do { recur = iø1 < geometry.vertices.length ? (function () { geometry.vertices[iø1].z = terrainHeight * heightmap.data[iø1]; return loop[0] = iø1 + 1, loop; })() : void 0; } while (iø1 = loop[0], recur === loop); return recur; }.call(this)); geometry.computeVertexNormals(); return geometry; }; var makeDemo = exports.makeDemo = function makeDemo(elementId, algorithm, size) { var scene = new THREE.Scene(); scene.add(new THREE.AxisHelper(100)); var clock = new THREE.Clock(); var camera = makeCamera(); var renderer = makeRenderer(); var geometry = void 0; var plane = void 0; scene.add(makeDirectionalLight()); scene.add(new THREE.AmbientLight(16777215, 0.05)); var refresh = function refresh() { return function () { var heightmapø1 = makeHeightmap(size); console.log('Generating terrain...'); (function () { var G__16ø1 = new Date().getTime(); var G__18ø1 = (function () { return algorithm(heightmapø1); })(); var G__17ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__17ø1 - G__16ø1) + 'ms.'); return G__18ø1; }.call(this)); console.log('Rebuilding geometry...'); (function () { var G__19ø1 = new Date().getTime(); var G__21ø1 = (function () { geometry = makeGeometry(heightmapø1); return updateGeometry(geometry, heightmapø1); })(); var G__20ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__20ø1 - G__19ø1) + 'ms.'); return G__21ø1; }.call(this)); console.log('Rebuilding plane...'); return function () { var G__22ø1 = new Date().getTime(); var G__24ø1 = (function () { scene.remove(plane); plane = makePlane(geometry); return scene.add(plane); })(); var G__23ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__23ø1 - G__22ø1) + 'ms.'); return G__24ø1; }.call(this); }.call(this); }; attachToDom(renderer, elementId, refresh); var controls = makeControls(camera, renderer); var render = function render() { return function () { var deltaø1 = clock.getDelta(); requestAnimationFrame(render); controls.update(deltaø1); return renderer.render(scene, camera); }.call(this); }; refresh(); render(); return void 0; }; var makeFinal = exports.makeFinal = function makeFinal(elementId) { var scene = new THREE.Scene(); scene.add(new THREE.AxisHelper(100)); var clock = new THREE.Clock(); var camera = makeCamera(); var renderer = makeRenderer(); var geometry = void 0; var plane = void 0; scene.add(makeDirectionalLight()); scene.add(new THREE.AmbientLight(16777215, 0.05)); var refresh = function refresh() { return function () { var heightmapø1 = makeHeightmap(6); console.log('Generating terrain...'); (function () { var G__25ø1 = new Date().getTime(); var G__27ø1 = (function () { return diamondSquare(heightmapø1); })(); var G__26ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__26ø1 - G__25ø1) + 'ms.'); return G__27ø1; }.call(this)); console.log('Rebuilding geometry...'); (function () { var G__28ø1 = new Date().getTime(); var G__30ø1 = (function () { geometry = makeGeometry(heightmapø1); return updateGeometry(geometry, heightmapø1); })(); var G__29ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__29ø1 - G__28ø1) + 'ms.'); return G__30ø1; }.call(this)); console.log('Rebuilding plane...'); return function () { var G__31ø1 = new Date().getTime(); var G__33ø1 = (function () { scene.remove(plane); plane = makePlane(geometry); return scene.add(plane); })(); var G__32ø1 = new Date().getTime(); console.log('Elapsed time: ' + (G__32ø1 - G__31ø1) + 'ms.'); return G__33ø1; }.call(this); }.call(this); }; attachToDom(renderer, elementId, refresh); var controls = makeControls(camera, renderer); var render = function render() { return function () { var deltaø1 = clock.getDelta(); requestAnimationFrame(render); controls.update(deltaø1); return renderer.render(scene, camera); }.call(this); }; refresh(); render(); return void 0; }; var run = exports.run = function run() { makeDemo('demo-1', diamondSquare1, 2); makeDemo('demo-2', diamondSquare2, 4); makeDemo('demo-3', diamondSquare3, 4); return makeFinal('demo-final'); }; $(run); },{"ndarray":4}],2:[function(require,module,exports){ "use strict" function iota(n) { var result = new Array(n) for(var i=0; i * License: MIT * * `npm install is-buffer` */ module.exports = function (obj) { return !!(obj != null && (obj._isBuffer || // For Safari 5-7 (missing Object.prototype.constructor) (obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)) )) } },{}],4:[function(require,module,exports){ var iota = require("iota-array") var isBuffer = require("is-buffer") var hasTypedArrays = ((typeof Float64Array) !== "undefined") function compare1st(a, b) { return a[0] - b[0] } function order() { var stride = this.stride var terms = new Array(stride.length) var i for(i=0; iMath.abs(this.stride[1]))?[1,0]:[0,1]}})") } else if(dimension === 3) { code.push( "var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);\ if(s0>s1){\ if(s1>s2){\ return [2,1,0];\ }else if(s0>s2){\ return [1,2,0];\ }else{\ return [1,0,2];\ }\ }else if(s0>s2){\ return [2,0,1];\ }else if(s2>s1){\ return [0,1,2];\ }else{\ return [0,2,1];\ }}})") } } else { code.push("ORDER})") } } //view.set(i0, ..., v): code.push( "proto.set=function "+className+"_set("+args.join(",")+",v){") if(useGetters) { code.push("return this.data.set("+index_str+",v)}") } else { code.push("return this.data["+index_str+"]=v}") } //view.get(i0, ...): code.push("proto.get=function "+className+"_get("+args.join(",")+"){") if(useGetters) { code.push("return this.data.get("+index_str+")}") } else { code.push("return this.data["+index_str+"]}") } //view.index: code.push( "proto.index=function "+className+"_index(", args.join(), "){return "+index_str+"}") //view.hi(): code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+ indices.map(function(i) { return ["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[", i, "]:i", i,"|0"].join("") }).join(",")+","+ indices.map(function(i) { return "this.stride["+i + "]" }).join(",")+",this.offset)}") //view.lo(): var a_vars = indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" }) var c_vars = indices.map(function(i) { return "c"+i+"=this.stride["+i+"]" }) code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(",")) for(var i=0; i=0){\ d=i"+i+"|0;\ b+=c"+i+"*d;\ a"+i+"-=d}") } code.push("return new "+className+"(this.data,"+ indices.map(function(i) { return "a"+i }).join(",")+","+ indices.map(function(i) { return "c"+i }).join(",")+",b)}") //view.step(): code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+ indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" }).join(",")+","+ indices.map(function(i) { return "b"+i+"=this.stride["+i+"]" }).join(",")+",c=this.offset,d=0,ceil=Math.ceil") for(var i=0; i=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}") } code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}") //Add return statement code.push("return function construct_"+className+"(data,shape,stride,offset){return new "+className+"(data,"+ indices.map(function(i) { return "shape["+i+"]" }).join(",")+","+ indices.map(function(i) { return "stride["+i+"]" }).join(",")+",offset)}") //Compile procedure var procedure = new Function("CTOR_LIST", "ORDER", code.join("\n")) return procedure(CACHED_CONSTRUCTORS[dtype], order) } function arrayDType(data) { if(isBuffer(data)) { return "buffer" } if(hasTypedArrays) { switch(Object.prototype.toString.call(data)) { case "[object Float64Array]": return "float64" case "[object Float32Array]": return "float32" case "[object Int8Array]": return "int8" case "[object Int16Array]": return "int16" case "[object Int32Array]": return "int32" case "[object Uint8Array]": return "uint8" case "[object Uint16Array]": return "uint16" case "[object Uint32Array]": return "uint32" case "[object Uint8ClampedArray]": return "uint8_clamped" } } if(Array.isArray(data)) { return "array" } return "generic" } var CACHED_CONSTRUCTORS = { "float32":[], "float64":[], "int8":[], "int16":[], "int32":[], "uint8":[], "uint16":[], "uint32":[], "array":[], "uint8_clamped":[], "buffer":[], "generic":[] } ;(function() { for(var id in CACHED_CONSTRUCTORS) { CACHED_CONSTRUCTORS[id].push(compileConstructor(id, -1)) } }); function wrappedNDArrayCtor(data, shape, stride, offset) { if(data === undefined) { var ctor = CACHED_CONSTRUCTORS.array[0] return ctor([]) } else if(typeof data === "number") { data = [data] } if(shape === undefined) { shape = [ data.length ] } var d = shape.length if(stride === undefined) { stride = new Array(d) for(var i=d-1, sz=1; i>=0; --i) { stride[i] = sz sz *= shape[i] } } if(offset === undefined) { offset = 0 for(var i=0; i