Skip to content

Commit 8cb20c9

Browse files
committed
A more usable format
Now make meaningful maps by first reducing resolution of random noise, posterize, & upscale with nearest neighbor
1 parent bd0f643 commit 8cb20c9

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

demo/Test.tscn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ metadata/_editor_floor_ = Vector3(0, 0, 0)
2828
[node name="Camera3D" type="Camera3D" parent="."]
2929
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 22.3218, 36.7455)
3030
script = ExtResource("3_2au82")
31+
32+
[node name="TextureRect" type="TextureRect" parent="."]
33+
offset_right = 40.0
34+
offset_bottom = 40.0

demo/bin/gaia_green.gdextension

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ android.arm64.double.release = "./android/GaiaGreen.android.template_release.arm
5353
web.wasm32.single.debug = "./web/GaiaGreen.web.template_debug.wasm32.nothreads.wasm"
5454
web.wasm32.double.debug = "./web/GaiaGreen.web.template_release.wasm32.double.nothreads.wasm"
5555
web.wasm32.single.release = "./web/GaiaGreen.web.template_release.wasm32.nothreads.wasm"
56-
web.wasm32.double.release = "./web/GaiaGreen.web.template_release.wasm32.double.nothreads.wasm"
56+
web.wasm32.double.release = "./web/GaiaGreen.web.template_release.wasm32.double.nothreads.wasm"

demo/terrain_gen_script.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func _ready():
1616
grid_map.cell_size = Vector3(1, 0.25, 1);
1717

1818
# Value Noise Based
19-
generate(grid_map, 100, 100, 4, seed_value, NoiseType.VALUE, 2, 0.79, 0.05);
19+
#generate(grid_map, 128, 128, 4, seed_value, NoiseType.VALUE, 2, 0.79, 0.1);
2020

2121
# Simplex Noise Based
22-
# generate(grid_map, 100, 100, 4, seed_value, NoiseType.SIMPLEX, 2, 0.79, 0.03);
22+
generate(grid_map, 128, 128, 4, seed_value, NoiseType.SIMPLEX, 2, 0.79, 0.12);

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
3535
depth, vector<vector<int>>(width, vector<int>(height, 0)));
3636

3737
vector<vector<int>> elevationMap(width, vector<int>(height, 0));
38-
vector<vector<int>> elevationMapSmooth(width, vector<int>(height, 0));
38+
39+
int blockSize = 4;
40+
int reducedX = floor(width / blockSize);
41+
int reducedY = floor(height / blockSize);
42+
vector<vector<int>> lowResMap(reducedX, vector<int>(reducedY, 0));
3943

4044
int dx[4] = { 1, -1, 0, 0 };
4145
int dy[4] = { 0, 0, 1, -1 };
@@ -57,61 +61,29 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
5761
}
5862
}
5963

60-
// Apply a Median Filter
61-
for (int x = 0; x < width; x++) {
62-
for (int y = 0; y < height; y++) {
63-
vector<int> neighbors;
64-
65-
for (int dx = -1; dx <= 1; dx++) {
66-
for (int dy = -1; dy <= 1; dy++) {
67-
int nx = x + dx;
68-
int ny = y + dy;
69-
70-
// Ensure indices are valid before adding to the list
71-
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
72-
neighbors.push_back(elevationMap[nx][ny]);
73-
}
74-
}
75-
}
64+
// Downsample by sampling one pixel per block.
65+
for (int x = 0; x < reducedX; x++) {
66+
for (int y = 0; y < reducedY; y++) {
67+
int sampleX = x * blockSize;
68+
int sampleY = y * blockSize;
7669

77-
if (!neighbors.empty()) {
78-
sort(neighbors.begin(), neighbors.end());
79-
elevationMapSmooth[x][y] = max(0, static_cast<int>(neighbors[neighbors.size() / 2]));
80-
}
70+
lowResMap[y][x] = (elevationMap[x][y] + 1.0f) / 2.0f;
8171
}
8272
}
8373

84-
// Apply the smoothed elevation map
85-
elevationMap = elevationMapSmooth;
86-
87-
/*****************************************************
88-
89-
Elevation Spacing
90-
91-
The elevation map generates elevation changes to close together
92-
create a buffer that smooths out double edges
93-
94-
*****************************************************/
95-
96-
for (int x = 1; x < width - 1; x++) {
97-
for (int y = 1; y < height - 1; y++) {
98-
int elevation = elevationMap[x][y];
99-
100-
bool hasElevationChange = false;
101-
for (int d = 0; d < 4; ++d) {
102-
int nx = x + dx[d];
103-
int ny = y + dy[d];
104-
105-
if (nx >= 0 && ny >= 0 && nx < width && ny < height) {
106-
if (abs(elevationMap[nx][ny] - elevation) > 1) {
107-
hasElevationChange = true;
108-
}
109-
}
110-
}
74+
//Posterize
75+
for (int x = 0; x < reducedX; x++) {
76+
for (int y = 0; y < reducedY; y++) {
77+
int quantizedLevel = min(depth - 1, static_cast<int>(lowResMap[x][y] * depth));
78+
lowResMap[x][x] = quantizedLevel / static_cast<float>(depth - 1);
79+
}
80+
}
11181

112-
if (hasElevationChange) {
113-
elevationMap[x][y] = elevationMap[x - 1][y]; // Force buffer using prior elevation
114-
}
82+
for (int x = 0; x < width; x++) {
83+
for (int y = 0; y < height; y++) {
84+
int srcX = x / blockSize;
85+
int srcY = y / blockSize;
86+
elevationMap[x][y] = lowResMap[srcX][srcY];
11587
}
11688
}
11789

src/TerrainGen/Terrain_Gen.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include <algorithm>
1010
#include <vector>
1111

12+
#include <godot_cpp/classes/image.hpp>
13+
#include <godot_cpp/classes/image_texture.hpp>
14+
#include <godot_cpp/classes/texture_rect.hpp>
15+
#include <godot_cpp/core/class_db.hpp>
16+
1217
using namespace godot;
1318
using namespace std;
1419

0 commit comments

Comments
 (0)