Skip to content

Commit bd0f643

Browse files
committed
This produces a baseline result but fails to be useful
1 parent 1e77ad5 commit bd0f643

File tree

3 files changed

+91
-47
lines changed

3 files changed

+91
-47
lines changed

demo/Test.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[node name="Test" type="Node3D"]
1010

1111
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
12+
transform = Transform3D(1, 0, 0, 0, 0.00963406, 0.999954, 0, -0.999954, 0.00963406, 0, 2.1677, 0)
1213

1314
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
1415
environment = SubResource("Environment_njov2")

demo/terrain_gen_script.gd

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
extends TerrainGen
22

33
enum NoiseType {
4-
TYPE_VALUE = 5,
5-
TYPE_VALUE_CUBIC = 4,
6-
TYPE_PERLIN = 3,
7-
TYPE_CELLULAR = 2,
8-
TYPE_SIMPLEX = 0,
9-
TYPE_SIMPLEX_SMOOTH = 1,
4+
VALUE = 5,
5+
VALUE_CUBIC = 4,
6+
PERLIN = 3,
7+
CELLULAR = 2,
8+
SIMPLEX = 0,
9+
SIMPLEX_SMOOTH = 1,
1010
};
1111

1212

1313
func _ready():
1414
var grid_map = $"../GridMap"
1515
var seed_value = int(Time.get_unix_time_from_system()) % 1000000;
1616
grid_map.cell_size = Vector3(1, 0.25, 1);
17-
generate(grid_map, 100, 100, 3, seed_value, NoiseType.TYPE_SIMPLEX, 2, 0.79, 0.03);
17+
18+
# Value Noise Based
19+
generate(grid_map, 100, 100, 4, seed_value, NoiseType.VALUE, 2, 0.79, 0.05);
20+
21+
# Simplex Noise Based
22+
# generate(grid_map, 100, 100, 4, seed_value, NoiseType.SIMPLEX, 2, 0.79, 0.03);

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ TerrainGen::~TerrainGen() {
1010
}
1111

1212
void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth, int seed, int noiseType, int noiseOctaves, float jitter, float noiseFreq) {
13-
UtilityFunctions::print("Begin Terrain Generation!");
14-
1513
/*****************************************************
1614
1715
Setup the Noise Function
@@ -36,23 +34,26 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
3634
vector<vector<vector<int>>> gridMap(
3735
depth, vector<vector<int>>(width, vector<int>(height, 0)));
3836

39-
// Noise Map may be used later, not sure yet
40-
// vector<vector<float>> noiseMap(width, vector<float>(height, 0.0f));
41-
4237
vector<vector<int>> elevationMap(width, vector<int>(height, 0));
4338
vector<vector<int>> elevationMapSmooth(width, vector<int>(height, 0));
4439

40+
int dx[4] = { 1, -1, 0, 0 };
41+
int dy[4] = { 0, 0, 1, -1 };
42+
4543
/*****************************************************
4644
4745
Noise Map Generation & Smoothing
4846
4947
*****************************************************/
5048

49+
// Generate the Elevation Map
5150
for (int x = 0; x < width; x++) {
5251
for (int y = 0; y < height; y++) {
5352
float currentNoise = noise->get_noise_2d((float)x, (float)y);
54-
// noiseMap[x][y] = currentNoise;
55-
elevationMap[x][y] = round(((currentNoise + 1.0f) / 2.0f) * depth);
53+
54+
float bias = 0.1, stepSize = 4;
55+
int rawNoise = round(pow(((currentNoise + 1.0f) / 2.0f) + bias, 1.5) * depth);
56+
elevationMap[x][y] = round(rawNoise / stepSize) * stepSize;
5657
}
5758
}
5859

@@ -85,10 +86,35 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
8586

8687
/*****************************************************
8788
88-
Tile Placement
89+
Elevation Spacing
90+
91+
The elevation map generates elevation changes to close together
92+
create a buffer that smooths out double edges
8993
9094
*****************************************************/
9195

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+
}
111+
112+
if (hasElevationChange) {
113+
elevationMap[x][y] = elevationMap[x - 1][y]; // Force buffer using prior elevation
114+
}
115+
}
116+
}
117+
92118
for (int x = 0; x < width; ++x) {
93119
for (int y = 0; y < height; ++y) {
94120
int elevation = elevationMap[x][y];
@@ -116,8 +142,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
116142
*****************************************************/
117143

118144
int neighborElevations[4] = { elevation, elevation, elevation, elevation };
119-
int dx[4] = { 1, -1, 0, 0 };
120-
int dy[4] = { 0, 0, 1, -1 };
121145

122146
for (int d = 0; d < 4; ++d) {
123147
int nx = x + dx[d];
@@ -128,7 +152,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
128152
}
129153
}
130154

131-
// Elevation analysis
132155
TileType tileType = GROUND;
133156
int rotationOrientation = 0;
134157
bool needsRamp = false, needsCliff = false, needsCorner = false;
@@ -150,7 +173,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
150173
}
151174
}
152175

153-
// Assign terrain type
154176
if (numDirectionChanges == 0) {
155177
tileType = GROUND;
156178
} else if (needsCliff) {
@@ -159,7 +181,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
159181
tileType = RAMP;
160182
}
161183

162-
// Corner detection: Two direction elevation change
163184
if (numDirectionChanges >= 2) {
164185
needsCorner = true;
165186
if (tileType == RAMP)
@@ -168,7 +189,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
168189
tileType = CLIFF_CORNER;
169190
}
170191

171-
// Water Edge & Corner detection
172192
if (tileType == WATER) {
173193
if (numDirectionChanges == 1) {
174194
tileType = WATER_EDGE;
@@ -177,40 +197,58 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
177197
}
178198
}
179199

180-
// Assign rotation using **0, 90, 180, 270** only
181200
if (tileType == RAMP || tileType == CLIFF || tileType == RAMP_CORNER || tileType == CLIFF_CORNER) {
182-
if (!needsCorner) {
183-
switch (dominantDirection) {
184-
case 0:
185-
rotationOrientation = 0; // No Rotation
186-
break; // North
187-
case 1:
188-
rotationOrientation = 1; // 90 Degrees Clockwise
189-
break; // East
190-
case 2:
191-
rotationOrientation = 2; // 180 Degrees Clockwise
192-
break; // South
193-
case 3:
194-
rotationOrientation = 3; // 270 Degrees Clockwise
195-
break; // West
201+
int highestNeighborIndex = -1;
202+
int highestNeighborElevation = elevation;
203+
int secondaryNeighborIndex = -1;
204+
205+
for (int d = 0; d < 4; ++d) {
206+
if (neighborElevations[d] > highestNeighborElevation) {
207+
secondaryNeighborIndex = highestNeighborIndex;
208+
highestNeighborElevation = neighborElevations[d];
209+
highestNeighborIndex = d;
196210
}
197-
} else {
198-
// Corner cases use the same **0, 90, 180, 270** logic.
199-
if (neighborElevations[0] != elevation && neighborElevations[2] != elevation) {
200-
rotationOrientation = 1; // Vertical corner (North-South elevation change)
201-
} else if (neighborElevations[1] != elevation && neighborElevations[3] != elevation) {
202-
rotationOrientation = 2; // Horizontal corner (East-West elevation change)
203-
} else if (neighborElevations[0] != elevation && neighborElevations[1] != elevation) {
204-
rotationOrientation = 0; // Corner facing North-East
205-
} else if (neighborElevations[2] != elevation && neighborElevations[3] != elevation) {
206-
rotationOrientation = 3; // Corner facing South-West
211+
}
212+
213+
bool isCorner = (secondaryNeighborIndex != -1 && abs(neighborElevations[secondaryNeighborIndex] - elevation) > 0);
214+
215+
if (highestNeighborIndex != -1) {
216+
if (!isCorner) {
217+
switch (highestNeighborIndex) {
218+
case 0:
219+
rotationOrientation = 2;
220+
break; // North
221+
case 1:
222+
rotationOrientation = 3;
223+
break; // East
224+
case 2:
225+
rotationOrientation = 0;
226+
break; // South
227+
case 3:
228+
rotationOrientation = 1;
229+
break; // West
230+
}
231+
} else {
232+
if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 1) ||
233+
(highestNeighborIndex == 1 && secondaryNeighborIndex == 0)) {
234+
rotationOrientation = 0;
235+
} else if ((highestNeighborIndex == 2 && secondaryNeighborIndex == 3) ||
236+
(highestNeighborIndex == 3 && secondaryNeighborIndex == 2)) {
237+
rotationOrientation = 3;
238+
} else if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 3) ||
239+
(highestNeighborIndex == 3 && secondaryNeighborIndex == 0)) {
240+
rotationOrientation = 1;
241+
} else if ((highestNeighborIndex == 1 && secondaryNeighborIndex == 2) ||
242+
(highestNeighborIndex == 2 && secondaryNeighborIndex == 1)) {
243+
rotationOrientation = 2;
244+
}
207245
}
208246
}
209247
}
210248

211249
/*****************************************************
212250
213-
Grid Cell Setter
251+
Grid Map Cell Setter
214252
215253
*****************************************************/
216254

0 commit comments

Comments
 (0)