WIP: fixing heighmap and colormap mismatch.

WorldChunkRefactoring
Martin Felis 2023-05-20 12:27:30 +02:00
parent ba5b0ab242
commit e84fddf102
8 changed files with 119 additions and 69 deletions

View File

@ -4,8 +4,7 @@ using System;
public class WorldInfoComponent : Component public class WorldInfoComponent : Component
{ {
[Export] public NodePath World; [Export] public NodePath World;
private HexTile3D _currentHexTile3D;
public TileWorld TileWorld; public TileWorld TileWorld;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.

View File

@ -16,9 +16,7 @@ public class Chest : Entity
public LidState State = LidState.Closed; public LidState State = LidState.Closed;
public bool IsMouseOver = false; public bool IsMouseOver = false;
private MeshInstance _mesh; private MeshInstance _mesh;
private SpatialMaterial _previousMaterial;
private AnimationPlayer _animationPlayer; private AnimationPlayer _animationPlayer;
private Random _coinSpawnerRandom;
[Signal] [Signal]
delegate void EntityClicked(Entity entity); delegate void EntityClicked(Entity entity);

View File

@ -16,7 +16,6 @@ public class Player : Entity
} }
// private members // private members
private MovableComponent _movable;
private WorldInfoComponent _worldInfo; private WorldInfoComponent _worldInfo;
private GroundMotionComponent _groundMotion; private GroundMotionComponent _groundMotion;
private NavigationComponent _navigationComponent; private NavigationComponent _navigationComponent;

View File

@ -9,17 +9,17 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Reference", "base": "Node",
"class": "ClickableComponent", "class": "ClickableComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/ClickableComponent.gd" "path": "res://components/ClickableComponent.gd"
}, { }, {
"base": "Reference", "base": "KinematicBody2D",
"class": "CollisionLine", "class": "CollisionLine",
"language": "GDScript", "language": "GDScript",
"path": "res://utils/CollisionLine.gd" "path": "res://utils/CollisionLine.gd"
}, { }, {
"base": "Reference", "base": "Node",
"class": "ColorComponent", "class": "ColorComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/ColorComponent.gd" "path": "res://components/ColorComponent.gd"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://utils/SpringDamper.gd" "path": "res://utils/SpringDamper.gd"
}, { }, {
"base": "Reference", "base": "Sprite",
"class": "TintedSpriteComponent", "class": "TintedSpriteComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd" "path": "res://components/TintedSpriteComponent.gd"

View File

@ -36,7 +36,6 @@ public class Game : Spatial
private HexGrid _hexGrid; private HexGrid _hexGrid;
private HexCell _lastTile; private HexCell _lastTile;
private HexCell _currentTile; private HexCell _currentTile;
private Vector2 _currentTileOffset;
private Vector3 _cameraOffset; private Vector3 _cameraOffset;
private ImageTexture _blackWhitePatternTexture; private ImageTexture _blackWhitePatternTexture;
@ -82,11 +81,10 @@ public class Game : Spatial
// other members // other members
_lastTile = new HexCell(); _lastTile = new HexCell();
_currentTile = new HexCell(); _currentTile = new HexCell();
_currentTileOffset = new Vector2();
_hexGrid = new HexGrid(); _hexGrid = new HexGrid();
// update data // update data
_worldTextureRect.RectSize = _tileWorld.Size; _worldTextureRect.RectSize = Vector2.One * _tileWorld.Size;
// connect signals // connect signals
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent)); _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
@ -254,6 +252,11 @@ public class Game : Spatial
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x);
_streamContainer.OnWorldGenerated(); _streamContainer.OnWorldGenerated();
//_streamContainer.SetTileMaterial(_tileMaterial);
// Reset player transform to offset 0,0 and at current height
Transform playerStartTransform = Transform.Identity;
float height = _tileWorld.GetHeightAtOffset(new Vector2(0, 0));
playerStartTransform.origin.y = height;
_player.Transform = playerStartTransform;
} }
} }

View File

@ -303,7 +303,7 @@ shape = SubResource( 23 )
[node name="Entities" type="Spatial" parent="."] [node name="Entities" type="Spatial" parent="."]
[node name="Chest" parent="Entities" instance=ExtResource( 7 )] [node name="Chest" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( -0.510903, 0, 0.859639, 0, 1, 0, -0.859639, 0, -0.510903, 0.0770743, 0, 2.60865 ) transform = Transform( -0.534471, 0, -0.845187, 0, 1, 0, 0.845187, 0, -0.534471, 2.68689, 0, 2.60865 )
[node name="Chest3" parent="Entities" instance=ExtResource( 7 )] [node name="Chest3" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( 0.550568, 0, -0.83479, 0, 1, 0, 0.83479, 0, 0.550568, 4.88275, 0, -1.70504 ) transform = Transform( 0.550568, 0, -0.83479, 0, 1, 0, 0.83479, 0, 0.550568, 4.88275, 0, -1.70504 )

View File

@ -14,8 +14,8 @@ public class TileWorld : Spatial
delegate void WorldGenerated(); delegate void WorldGenerated();
// public members // public members
public Vector2 Size = new Vector2(10, 10); public float Size = 125;
public float HeightScale = 2; public float HeightScale = 10;
public Image Heightmap; public Image Heightmap;
public Image Colormap; public Image Colormap;
public int Seed = 0; public int Seed = 0;
@ -34,10 +34,10 @@ public class TileWorld : Spatial
_offscreenViewport = (Viewport)GetNode("OffscreenViewport"); _offscreenViewport = (Viewport)GetNode("OffscreenViewport");
Debug.Assert(_offscreenViewport != null); Debug.Assert(_offscreenViewport != null);
_offscreenViewport.Size = Size; _offscreenViewport.Size = new Vector2(Size, Size);
_offscreenTextureRect = (TextureRect)GetNode("OffscreenViewport/TextureRect"); _offscreenTextureRect = (TextureRect)GetNode("OffscreenViewport/TextureRect");
Debug.Assert(_offscreenTextureRect != null); Debug.Assert(_offscreenTextureRect != null);
_offscreenTextureRect.SetSize(Size); _offscreenTextureRect.SetSize(new Vector2(Size, Size));
//VisualServer.Singleton.Connect("frame_post_draw", this, nameof(GenerateNoiseColorMap)); //VisualServer.Singleton.Connect("frame_post_draw", this, nameof(GenerateNoiseColorMap));
} }
@ -46,23 +46,55 @@ public class TileWorld : Spatial
public void Generate() public void Generate()
{ {
//GenerateSimpleMap(); //GenerateSimpleMap();
// GenerateNoiseMap(); //GenerateNoiseMap();
GenerateNoiseColorMap(); GenerateNoiseColorMap();
//GenerateDebugMap();
EmitSignal("WorldGenerated"); EmitSignal("WorldGenerated");
} }
private void GenerateDebugMap()
{
Colormap = new Image();
Colormap.Create((int)Size, (int)Size, false, Image.Format.Rgba8);
Heightmap = new Image();
Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rf);
Heightmap.Lock();
Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, (int)Size))
{
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));
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)
{
coord_to_height = 1;
}
SetHeightAtOffset(new Vector2(coord_x, coord_y), coord_to_height);
}
}
Colormap.Unlock();
}
private void GenerateSimpleMap() private void GenerateSimpleMap()
{ {
Heightmap = new Image(); Heightmap = new Image();
Heightmap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rf); Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rf);
Heightmap.Lock(); Heightmap.Lock();
foreach (int coord_x in Enumerable.Range(-(int) Size.x / 2, (int)Size.x)) foreach (int coord_x in Enumerable.Range(-(int) Size / 2, (int)Size))
{ {
foreach (int coord_y in Enumerable.Range(-(int) Size.y / 2, (int)Size.y)) foreach (int coord_y in Enumerable.Range(-(int) Size / 2, (int)Size))
{ {
SetHeightAtOffset(new Vector2(coord_x, coord_y), 5f); SetHeightAtOffset(new Vector2(coord_x, coord_y), 5f);
} }
@ -73,38 +105,38 @@ public class TileWorld : Spatial
private void GenerateNoiseMap() private void GenerateNoiseMap()
{ {
Heightmap = new Image(); Heightmap = new Image();
Heightmap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rf); Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rf);
NoiseTexture noise_texture = new NoiseTexture(); NoiseTexture noiseTexture = new NoiseTexture();
OpenSimplexNoise noise_generator = new OpenSimplexNoise(); OpenSimplexNoise noiseGenerator = new OpenSimplexNoise();
noise_generator.Seed = Seed; noiseGenerator.Seed = Seed;
noise_generator.Octaves = 4; noiseGenerator.Octaves = 4;
noise_generator.Period = 20; noiseGenerator.Period = 20;
noise_generator.Persistence = 0.1f; noiseGenerator.Persistence = 0.1f;
noise_generator.Lacunarity = 2; noiseGenerator.Lacunarity = 2;
Heightmap.CopyFrom(noise_generator.GetImage((int)Size.x, (int)Size.y, null)); Heightmap.CopyFrom(noiseGenerator.GetImage((int)Size, (int)Size, null));
} }
private void GenerateNoiseColorMap() private void GenerateNoiseColorMap()
{ {
Colormap = new Image(); Colormap = new Image();
Colormap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rgba8); Colormap.Create((int)Size, (int)Size, false, Image.Format.Rgba8);
NoiseTexture noise_texture = new NoiseTexture(); NoiseTexture noiseTexture = new NoiseTexture();
OpenSimplexNoise noise_generator = new OpenSimplexNoise(); OpenSimplexNoise noiseGenerator = new OpenSimplexNoise();
noise_generator.Seed = Seed; noiseGenerator.Seed = Seed;
noise_generator.Octaves = 4; noiseGenerator.Octaves = 4;
noise_generator.Period = 20; noiseGenerator.Period = 20;
noise_generator.Persistence = 0.2f; noiseGenerator.Persistence = 0.2f;
noise_generator.Lacunarity = 4; noiseGenerator.Lacunarity = 4;
ImageTexture imageTexture = new ImageTexture(); ImageTexture imageTexture = new ImageTexture();
//Heightmap.Unlock(); //Heightmap.Unlock();
Heightmap = noise_generator.GetSeamlessImage((int)Size.x); Heightmap = noiseGenerator.GetSeamlessImage((int)Size);
imageTexture.CreateFromImage(Heightmap); imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0; imageTexture.Flags = 0;
_offscreenTextureRect.Texture = imageTexture; _offscreenTextureRect.Texture = imageTexture;
@ -112,17 +144,31 @@ public class TileWorld : Spatial
Heightmap.Lock(); Heightmap.Lock();
} }
public bool IsOffsetCoordValid(Vector2 offset_coord) public void ApplyHeightmap(Image heightmap)
{ {
return ((int)Math.Clamp(offset_coord.x, -Size.x / 2, Size.x / 2 - 1) == (int)offset_coord.x foreach (int coord_x in Enumerable.Range(-(int) Size / 2, (int)Size))
&& (int)Math.Clamp(offset_coord.y, -Size.y / 2, Size.y / 2 - 1) == (int)offset_coord.y); {
foreach (int coord_y in Enumerable.Range(-(int) Size / 2, (int)Size))
{
Vector2 textureCoord = OffsetToTextureCoord(new Vector2(coord_x, coord_y));
float height = heightmap.GetPixel((int) textureCoord.x, (int) textureCoord.y).r;
SetHeightAtOffset(new Vector2(coord_x, coord_y), height);
}
}
}
public bool IsOffsetCoordValid(Vector2 offsetCoord)
{
return ((int)Math.Clamp(offsetCoord.x, -Size / 2, Size / 2 - 1) == (int)offsetCoord.x
&& (int)Math.Clamp(offsetCoord.y, -Size / 2, Size / 2 - 1) == (int)offsetCoord.y);
} }
public HexTile3D.TileType GetTileTypeAtOffset(Vector2 offset_coord) public HexTile3D.TileType GetTileTypeAtOffset(Vector2 offsetCoord)
{ {
if (!IsOffsetCoordValid(offset_coord)) if (!IsOffsetCoordValid(offsetCoord))
{ {
return HexTile3D.TileType.Undefined; return HexTile3D.TileType.Undefined;
} }
@ -131,44 +177,50 @@ public class TileWorld : Spatial
} }
public void SetHeightAtOffset(Vector2 offset_coord, float height) public Vector2 OffsetToTextureCoord(Vector2 offsetCoord)
{ {
if (!IsOffsetCoordValid(offset_coord)) Vector2 textureCoord = (offsetCoord - Vector2.One * Mathf.Floor(Size / 2)) % (Vector2.One * Size);
if (textureCoord[0] < 0)
{ {
return; textureCoord[0] += Size;
}
Vector2 texture_coord = offset_coord + Size / 2;
Heightmap.SetPixel((int) texture_coord.x, (int) texture_coord.y, new Color(height / HeightScale, 0f, 0f));
}
public float GetHeightAtOffset(Vector2 offset_coord)
{
if (!IsOffsetCoordValid(offset_coord))
{
return 0;
} }
Vector2 texture_coord = offset_coord + Size / 2; if (textureCoord[1] < 0)
{
textureCoord[1] += Size;
}
return textureCoord;
}
public void SetHeightAtOffset(Vector2 offsetCoord, float height)
{
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
return Heightmap.GetPixel((int)texture_coord.x, (int)(texture_coord.y)).r * HeightScale - HeightScale * 0.5f ; Heightmap.SetPixel((int) textureCoord.x, (int) textureCoord.y, new Color(height, 0f, 0f));
}
public float GetHeightAtOffset(Vector2 offsetCoord)
{
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
return Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r * HeightScale;
} }
public Vector2 WorldToOffsetCoords(Vector3 world_coord) public Vector2 WorldToOffsetCoords(Vector3 worldCoord)
{ {
return _hexGrid.GetHexAt(new Vector2(world_coord.x, world_coord.z)).OffsetCoords; return _hexGrid.GetHexAt(new Vector2(worldCoord.x, worldCoord.z)).OffsetCoords;
} }
public Vector3 GetTileWorldCenterFromOffset(Vector2 offset_coord) public Vector3 GetTileWorldCenterFromOffset(Vector2 offsetCoord)
{ {
Vector2 tileCenter = _hexGrid.GetHexCenterFromOffset(offset_coord); Vector2 tileCenter = _hexGrid.GetHexCenterFromOffset(offsetCoord);
return new Vector3( return new Vector3(
tileCenter.x, tileCenter.x,
GetHeightAtOffset(offset_coord), GetHeightAtOffset(offsetCoord),
tileCenter.y); tileCenter.y);
} }
} }

View File

@ -10,7 +10,6 @@ public class HexTile3DMaterialAssign : Spatial
private Button _blackWhitePatternButton; private Button _blackWhitePatternButton;
private Button _colorPatternButton; private Button _colorPatternButton;
private Button _changeMaterialButton;
private SpinBox _textureSizeSpinBox; private SpinBox _textureSizeSpinBox;
private HexTile3D _hexTile; private HexTile3D _hexTile;
private ShaderMaterial _customTileMaterial; private ShaderMaterial _customTileMaterial;