WIP: still fixing heighmap and colormap mismatch.

WorldChunkRefactoring
Martin Felis 2023-05-20 21:36:32 +02:00
parent e84fddf102
commit 55da2346d3
3 changed files with 36 additions and 18 deletions

View File

@ -15,6 +15,7 @@ public class Game : Spatial
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private TextureRect _worldTextureRect;
private TextureRect _heightTextureRect;
private Button _generateWorldButton;
// scene nodes
@ -52,6 +53,7 @@ public class Game : Spatial
_numCoordsAddedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_added_label");
_numCoordsRemovedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_removed_label");
_worldTextureRect = (TextureRect)FindNode("WorldTextureRect");
_heightTextureRect = (TextureRect)FindNode("HeightTextureRect");
_generateWorldButton = (Button)FindNode("WorldGenerateButton");
// scene nodes
@ -85,6 +87,7 @@ public class Game : Spatial
// update data
_worldTextureRect.RectSize = Vector2.One * _tileWorld.Size;
_heightTextureRect.RectSize = Vector2.One * _tileWorld.Size;
// connect signals
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
@ -244,13 +247,17 @@ public class Game : Spatial
public void OnWorldGenerated()
{
GD.Print("Using new map");
ImageTexture new_world_texture = new ImageTexture();
new_world_texture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
ImageTexture newWorldTexture = new ImageTexture();
newWorldTexture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
ImageTexture newHeightTexture = new ImageTexture();
newHeightTexture.CreateFromImage(_tileWorld.Heightmap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_worldTextureRect.Texture = new_world_texture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", new_world_texture);
_worldTextureRect.Texture = newWorldTexture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x);
_heightTextureRect.Texture = newHeightTexture;
_streamContainer.OnWorldGenerated();
// Reset player transform to offset 0,0 and at current height

View File

@ -70,27 +70,34 @@ alignment = 2
[node name="VBoxContainer" type="VBoxContainer" parent="Control/HBoxContainer"]
margin_right = 100.0
margin_bottom = 158.0
margin_bottom = 228.0
alignment = 2
[node name="WorldTextureRect" type="TextureRect" parent="Control/HBoxContainer/VBoxContainer"]
margin_top = 34.0
margin_right = 100.0
margin_bottom = 134.0
margin_bottom = 100.0
rect_min_size = Vector2( 100, 100 )
stretch_mode = 1
flip_v = true
[node name="HeightTextureRect" type="TextureRect" parent="Control/HBoxContainer/VBoxContainer"]
margin_top = 104.0
margin_right = 100.0
margin_bottom = 204.0
rect_min_size = Vector2( 100, 100 )
stretch_mode = 1
flip_v = true
[node name="WorldGenerateButton" type="Button" parent="Control/HBoxContainer/VBoxContainer"]
margin_top = 138.0
margin_top = 208.0
margin_right = 100.0
margin_bottom = 158.0
margin_bottom = 228.0
text = "Generate"
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
margin_left = 104.0
margin_right = 231.0
margin_bottom = 158.0
margin_bottom = 228.0
mouse_filter = 2
columns = 2

View File

@ -14,8 +14,8 @@ public class TileWorld : Spatial
delegate void WorldGenerated();
// public members
public float Size = 125;
public float HeightScale = 10;
public float Size = 5;
public float HeightScale = 10.0f;
public Image Heightmap;
public Image Colormap;
public int Seed = 0;
@ -70,12 +70,11 @@ public class TileWorld : Spatial
{
foreach (int coord_y in Enumerable.Range(0, (int)Size))
{
Colormap.SetPixel(coord_x, coord_y, new Color((float) coord_x, (float) coord_y, 0, 1));
// Colormap.SetPixel(coord_x, coord_y, new Color((float) coord_x, (float) coord_y, 0, 1));
float coord_to_height =
(float)coord_y / Size * 0f;
// Heightmap.SetPixel(coord_x, coord_y, new Color(coord_to_height, 0, 0, 1));
if (coord_x == 3 && coord_y == 3)
if (coord_x == 1 && coord_y == 4)
{
coord_to_height = 1;
}
@ -84,6 +83,12 @@ public class TileWorld : Spatial
}
Colormap.Unlock();
ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0;
_offscreenTextureRect.Texture = imageTexture;
Colormap.CopyFrom(imageTexture.GetData());
}
private void GenerateSimpleMap()
@ -135,7 +140,6 @@ public class TileWorld : Spatial
noiseGenerator.Lacunarity = 4;
ImageTexture imageTexture = new ImageTexture();
//Heightmap.Unlock();
Heightmap = noiseGenerator.GetSeamlessImage((int)Size);
imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0;
@ -179,7 +183,7 @@ public class TileWorld : Spatial
public Vector2 OffsetToTextureCoord(Vector2 offsetCoord)
{
Vector2 textureCoord = (offsetCoord - Vector2.One * Mathf.Floor(Size / 2)) % (Vector2.One * Size);
Vector2 textureCoord = (offsetCoord - Vector2.One * (Mathf.Floor(Size / 2) + 1)) % (Vector2.One * Size);
if (textureCoord[0] < 0)
{
textureCoord[0] += Size;