Differential D4687 Diff 20413 binaries/data/mods/public/maps/random/rmgen/painter/SmoothElevationPainter.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/maps/random/rmgen/painter/SmoothElevationPainter.js
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | for (let point of area.getPoints()) | ||||
for (let dx = -1; dx < 1 + brushSize; ++dx) | for (let dx = -1; dx < 1 + brushSize; ++dx) | ||||
{ | { | ||||
let nx = point.x + dx; | let nx = point.x + dx; | ||||
for (let dz = -1; dz < 1 + brushSize; ++dz) | for (let dz = -1; dz < 1 + brushSize; ++dz) | ||||
{ | { | ||||
let nz = point.y + dz; | let nz = point.y + dz; | ||||
let position = new Vector2D(nx, nz); | let position = new Vector2D(nx, nz); | ||||
if (g_Map.validHeight(position) && !gotHeightPt[nx][nz]) | if (g_Map.validVertex(position) && !gotHeightPt[nx][nz]) | ||||
{ | { | ||||
newHeight[nx][nz] = g_Map.getHeight(position); | newHeight[nx][nz] = g_Map.getVertexHeight(position); | ||||
gotHeightPt[nx][nz] = 1; | gotHeightPt[nx][nz] = 1; | ||||
heightPoints.push(position); | heightPoints.push(position); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
let withinArea = (area, position) => g_TileVertices.some(vertexPos => area.contains(Vector2D.sub(position, vertexPos))); | let withinArea = (area, position) => g_TileVertices.some(vertexPos => area.contains(Vector2D.sub(position, vertexPos))); | ||||
// Change height inside the area depending on the distance to the border | // Change height inside the area depending on the distance to the border | ||||
breadthFirstSearchPaint({ | breadthFirstSearchPaint({ | ||||
"area": area, | "area": area, | ||||
"brushSize": brushSize, | "brushSize": brushSize, | ||||
"gridSize": heightmapSize, | "gridSize": heightmapSize, | ||||
"withinArea": withinArea, | "withinArea": withinArea, | ||||
"paintTile": (point, distance) => { | "paintTile": (point, distance) => { | ||||
let a = 1; | let a = 1; | ||||
if (distance <= this.blendRadius) | if (distance <= this.blendRadius) | ||||
a = (distance - 1) / this.blendRadius; | a = (distance - 1) / this.blendRadius; | ||||
if (this.type == ELEVATION_SET) | if (this.type == ELEVATION_SET) | ||||
newHeight[point.x][point.y] = (1 - a) * g_Map.getHeight(point); | newHeight[point.x][point.y] = (1 - a) * g_Map.getVertexHeight(point); | ||||
newHeight[point.x][point.y] += a * this.elevation + randFloat(-0.5, 0.5) * this.randomElevation; | newHeight[point.x][point.y] += a * this.elevation + randFloat(-0.5, 0.5) * this.randomElevation; | ||||
} | } | ||||
}); | }); | ||||
// Smooth everything out | // Smooth everything out | ||||
for (let point of heightPoints) | for (let point of heightPoints) | ||||
{ | { | ||||
if (!withinArea(area, point)) | if (!withinArea(area, point)) | ||||
continue; | continue; | ||||
let count = 0; | let count = 0; | ||||
let sum = 0; | let sum = 0; | ||||
for (let dx = -1; dx <= 1; ++dx) | for (let dx = -1; dx <= 1; ++dx) | ||||
{ | { | ||||
let nx = point.x + dx; | let nx = point.x + dx; | ||||
for (let dz = -1; dz <= 1; ++dz) | for (let dz = -1; dz <= 1; ++dz) | ||||
{ | { | ||||
let nz = point.y + dz; | let nz = point.y + dz; | ||||
if (g_Map.validHeight(new Vector2D(nx, nz))) | if (g_Map.validVertex(new Vector2D(nx, nz))) | ||||
{ | { | ||||
sum += newHeight[nx][nz]; | sum += newHeight[nx][nz]; | ||||
++count; | ++count; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
g_Map.setHeight(point, (newHeight[point.x][point.y] + sum / count) / 2); | g_Map.setVertexHeight(point, (newHeight[point.x][point.y] + sum / count) / 2); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Calls the given paintTile function on all points within the given Area, | * Calls the given paintTile function on all points within the given Area, | ||||
* providing the distance to the border of the area (1 for points on the border). | * providing the distance to the border of the area (1 for points on the border). | ||||
* This function can traverse any grid, for instance the tile grid or the larger heightmap grid. | * This function can traverse any grid, for instance the tile grid or the larger heightmap grid. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator