| 1 | /**
|
|---|
| 2 | * Applies smoothing to the given area using Inverse-Distance-Weighting / Shepard's method.
|
|---|
| 3 | *
|
|---|
| 4 | * @param {Number} size - Determines the number of neighboring heights to interpolate. The area is a square with the length twice this size.
|
|---|
| 5 | * @param {Number} strength - Between 0 (no effect) and 1 (only neighbor heights count). This parameter has the lowest performance impact.
|
|---|
| 6 | * @param {Number} iterations - How often the process should be repeated. Typically 1. Can be used to gain even more smoothing.
|
|---|
| 7 | */
|
|---|
| 8 | function SmoothingPainter(size, strength, iterations)
|
|---|
| 9 | {
|
|---|
| 10 | if (size < 1)
|
|---|
| 11 | throw new Error("Invalid size: " + size);
|
|---|
| 12 |
|
|---|
| 13 | if (strength <= 0 || strength > 1)
|
|---|
| 14 | throw new Error("Invalid strength: " + strength);
|
|---|
| 15 |
|
|---|
| 16 | if (iterations <= 0)
|
|---|
| 17 | throw new Error("Invalid iterations: " + iterations);
|
|---|
| 18 |
|
|---|
| 19 | this.size = Math.floor(size);
|
|---|
| 20 | this.strength = strength;
|
|---|
| 21 | this.iterations = iterations;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | SmoothingPainter.prototype.paint = function(area)
|
|---|
| 25 | {
|
|---|
| 26 | const brushPoints = getPointsInBoundingBox(getBoundingBox(
|
|---|
| 27 | new Array(2).fill(0).map((zero, i) => new Vector2D(1, 1).mult(this.size).mult(i ? 1 : -1))));
|
|---|
| 28 |
|
|---|
| 29 | for (let i = 0; i < this.iterations; ++i)
|
|---|
| 30 | {
|
|---|
| 31 | const heightmap = clone(g_Map.height);
|
|---|
| 32 |
|
|---|
| 33 | // Additional complexity to process all 4 vertices of each tile, i.e the last row too
|
|---|
| 34 | const seen = new Array(heightmap.length).fill(0).map(zero => new Uint8Array(heightmap.length).fill(0));
|
|---|
| 35 |
|
|---|
| 36 | for (const point of area.getPoints())
|
|---|
| 37 | for (const tileVertex of g_TileVertices)
|
|---|
| 38 | {
|
|---|
| 39 | const vertex = Vector2D.add(point, tileVertex);
|
|---|
| 40 | if (!g_Map.validHeight(vertex) || seen[vertex.x][vertex.y])
|
|---|
| 41 | continue;
|
|---|
| 42 |
|
|---|
| 43 | seen[vertex.x][vertex.y] = 1;
|
|---|
| 44 |
|
|---|
| 45 | let sumWeightedHeights = 0;
|
|---|
| 46 | let sumWeights = 0;
|
|---|
| 47 |
|
|---|
| 48 | for (const brushPoint of brushPoints)
|
|---|
| 49 | {
|
|---|
| 50 | const position = Vector2D.add(vertex, brushPoint);
|
|---|
| 51 | const distance = Math.abs(brushPoint.x) + Math.abs(brushPoint.y);
|
|---|
| 52 | if (!distance || !g_Map.validHeight(position))
|
|---|
| 53 | continue;
|
|---|
| 54 |
|
|---|
| 55 | sumWeightedHeights += g_Map.getHeight(position) / distance;
|
|---|
| 56 | sumWeights += 1 / distance;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | g_Map.setHeight(
|
|---|
| 60 | vertex,
|
|---|
| 61 | this.strength * sumWeightedHeights / sumWeights +
|
|---|
| 62 | (1 - this.strength) * g_Map.getHeight(vertex));
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | };
|
|---|