diff options
Diffstat (limited to 'src/Terrain.js')
-rw-r--r-- | src/Terrain.js | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/Terrain.js b/src/Terrain.js new file mode 100644 index 0000000..c287063 --- /dev/null +++ b/src/Terrain.js @@ -0,0 +1,111 @@ +'use strict'; + +import Voronoi from './3rdparty/rhill-voronoi-core.js'; + +import { useAverage } from './modules/Util.js'; +import { AABB, QuadTree } from './modules/Geometry.js'; + + +/* from here on up, we always assume that points live in the range [(0,0), (1,1)) */ + +function lloydRelax(point_set, density) { + /* setup quadtree and averages */ + let tree = new QuadTree(1,1); + let averages = {}; + for (let i=0; i<point_set.length; i++) { + const point = point_set[i]; + point.index = i; + tree.insert(point); + + let [avg, append] = useAverage(); + const cent_x = { avg, append }; + [avg, append] = useAverage(); + const cent_y = { avg, append }; + averages[i] = { cent_x, cent_y }; + } + + /* compute average centroids */ + for (let x=0; x<1; x += 1/density) { + for (let y=0; y<1; y += 1/density) { + const point = { x, y }; + const closest = tree.closest(point); + const { cent_x, cent_y } = averages[closest.index]; + cent_x.append(point.x); + cent_y.append(point.y); + } + } + + /* return centroid points */ + const result = []; + for (let i=0; i<point_set.length; i++) { + const point = { x: averages[i].cent_x.avg(), y: averages[i].cent_y.avg() }; + result.push(point); + } + return result; +} + + +class Terrain { + constructor() { + const N_SEED_POINTS = 2**12; + const N_RELAX_ITERATIONS = 1; + const RELAX_DENSITY = 400; + const randomPoint = () => ({x: Math.random(), y: Math.random()}); + + this.min_height = 0; + this.max_height = 0; + + let seed_points = []; + for (let i=0; i<N_SEED_POINTS; i++) seed_points.push(randomPoint()); + + for (let i=0; i<N_RELAX_ITERATIONS; i++) + lloydRelax(seed_points, RELAX_DENSITY); + + const v = new Voronoi(); + this.graph = v.compute(seed_points, {xl: 0, xr: 1, yt: 0, yb: 1}); + + this.tree = new QuadTree(1,1); + for (let v of this.graph.vertices) { + v.hue = 0; + this.tree.insert(v); + } + } + + + applyBrush(x, y, f, strength, radius) { + const region = new AABB(x-radius, y-radius, 2*radius, 2*radius); + const points = this.tree.root.getPointsInRegion(region); + + const dist2 = (a, b) => (a.x - b.x)**2 + (a.y - b.y)**2; + + const sigma = radius/3; + const beta = 1/(2*sigma*sigma); + const center = { x, y }; + const power = pt => Math.exp(-beta * dist2(pt, center)); + + for (let pt of points) f(pt, strength * power(pt)); + } + + + renderGrid(ct) { + ct.save(); + ct.lineWidth = 0.001; + for (let edge of this.graph.edges) { + ct.fillStyle = `hsl(${edge.va.hue}, 100%, 50%)`; + ct.beginPath(); + ct.arc(edge.va.x, edge.va.y, 0.005, 0, 2*Math.PI); + ct.closePath(); + ct.fill(); + + ct.beginPath(); + ct.moveTo(edge.va.x, edge.va.y); + ct.lineTo(edge.vb.x, edge.vb.y); + ct.closePath(); + ct.stroke(); + } + ct.restore(); + } +} + +export { lloydRelax }; +export default Terrain; |