Getting generated color texture works, now fix the height offset...

WorldChunkRefactoring
Martin Felis 2023-05-11 22:26:06 +02:00
parent a5ce4a235d
commit d3a36f438b
5 changed files with 26 additions and 36 deletions

View File

@ -73,6 +73,10 @@ public class GroundMotionComponent : Component
{ {
GD.Print("Jump!"); GD.Print("Jump!");
entityVelocity.y = 10; entityVelocity.y = 10;
Transform entityTransform = entity.GlobalTransform;
entityTransform.origin.y = nextHeight;
entity.GlobalTransform = entityTransform;
} }
} }

View File

@ -38,6 +38,7 @@ public class Game : Spatial
private HexCell _currentTile; private HexCell _currentTile;
private Vector2 _currentTileOffset; private Vector2 _currentTileOffset;
private Vector3 _cameraOffset; private Vector3 _cameraOffset;
private ImageTexture _blackWhitePatternTexture;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
@ -73,6 +74,11 @@ public class Game : Spatial
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres"); _tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
Debug.Assert(_tileMaterial != null); Debug.Assert(_tileMaterial != null);
_blackWhitePatternTexture = new ImageTexture();
Image image = new Image();
image.Load("assets/4x4checker.png");
_blackWhitePatternTexture.CreateFromImage(image, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
// other members // other members
_lastTile = new HexCell(); _lastTile = new HexCell();
_currentTile = new HexCell(); _currentTile = new HexCell();
@ -209,18 +215,6 @@ public class Game : Spatial
_player.TaskQueueComponent.Reset(); _player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask( _player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationComponent.NavigationPoint(tile.GlobalTranslation))); new NavigationComponent.NavigationPoint(tile.GlobalTranslation)));
// Vector3 direction = tile.GlobalTranslation - _player.GlobalTranslation;
// if (direction.LengthSquared() > 0.1)
// {
// direction = direction.Normalized();
//
// float angle_to_tile = _player.GlobalTransform.basis.z.SignedAngleTo(direction, Vector3.Up);
// GD.Print("Angle to tile: " + Mathf.Rad2Deg(angle_to_tile) + " Player basis z: " + _player.GlobalTransform.basis.z + " Direction: " + direction);
// Quat tile_direction = new Quat(Vector3.Up, angle_to_tile);
// _player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
// new NavigationComponent.NavigationPoint(_player.GlobalTransform.basis.Rotated(Vector3.Up, angle_to_tile).Quat())));
// }
} }
public void OnTileHovered(HexTile3D tile) public void OnTileHovered(HexTile3D tile)
@ -253,13 +247,11 @@ public class Game : Spatial
{ {
GD.Print("Using new map"); GD.Print("Using new map");
ImageTexture new_world_texture = new ImageTexture(); ImageTexture new_world_texture = new ImageTexture();
_tileWorld.Colormap.Unlock();
new_world_texture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); new_world_texture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileWorld.Colormap.Lock();
_worldTextureRect.Texture = new_world_texture; _worldTextureRect.Texture = new_world_texture;
_tileMaterial.Set("MapAlbedoTexture", new_world_texture); _tileMaterial.SetShaderParam("MapAlbedoTexture", new_world_texture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x);
_streamContainer.SetTileMaterial(_tileMaterial); _streamContainer.SetTileMaterial(_tileMaterial);
} }

View File

@ -79,6 +79,7 @@ margin_right = 100.0
margin_bottom = 134.0 margin_bottom = 134.0
rect_min_size = Vector2( 100, 100 ) rect_min_size = Vector2( 100, 100 )
stretch_mode = 1 stretch_mode = 1
flip_v = true
[node name="WorldGenerateButton" type="Button" parent="Control/HBoxContainer/VBoxContainer"] [node name="WorldGenerateButton" type="Button" parent="Control/HBoxContainer/VBoxContainer"]
margin_top = 138.0 margin_top = 138.0

View File

@ -15,7 +15,7 @@ public class TileWorld : Spatial
// public members // public members
public Vector2 Size = new Vector2(100, 100); public Vector2 Size = new Vector2(100, 100);
public float HeightScale = 10; public float HeightScale = 2;
public Image Heightmap; public Image Heightmap;
public Image Colormap; public Image Colormap;
public int Seed = 0; public int Seed = 0;
@ -103,10 +103,13 @@ public class TileWorld : Spatial
noise_generator.Lacunarity = 4; noise_generator.Lacunarity = 4;
ImageTexture imageTexture = new ImageTexture(); ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(noise_generator.GetImage((int)Size.x, (int)Size.y, null)); Heightmap.Unlock();
Heightmap = noise_generator.GetSeamlessImage((int)Size.x);
imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0; imageTexture.Flags = 0;
_offscreenTextureRect.Texture = imageTexture; _offscreenTextureRect.Texture = imageTexture;
Colormap.CopyFrom(_offscreenViewport.GetTexture().GetData()); Colormap.CopyFrom(_offscreenViewport.GetTexture().GetData());
Heightmap.Lock();
} }
@ -154,7 +157,7 @@ public class TileWorld : Spatial
Vector2 texture_coord = offset_coord + Size / 2; Vector2 texture_coord = offset_coord + Size / 2;
return Heightmap.GetPixel((int)texture_coord.x, (int)texture_coord.y).r * HeightScale - HeightScale * 0.5f ; return Heightmap.GetPixel((int)texture_coord.x, (int)(texture_coord.y)).r * HeightScale - HeightScale * 0.5f ;
} }

View File

@ -50,21 +50,14 @@ public class HexTile3DMaterialAssign : Spatial
public void OnBlackWhitePatternButton() public void OnBlackWhitePatternButton()
{ {
GD.Print("Apply Black White Pattern!"); GD.Print("Apply Black White Pattern!");
ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); _customTileMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture);
Debug.Assert(currentMaterial != null);
currentMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture);
// _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture);
// _hexTile.Mesh.SetSurfaceMaterial(0, _customTileMaterial);
} }
public void OnColorPatternButton() public void OnColorPatternButton()
{ {
GD.Print("Apply Collor Pattern!"); GD.Print("Apply Collor Pattern!");
ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); //currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
Debug.Assert(currentMaterial != null); _customTileMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
// _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture); // _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture);
@ -73,10 +66,7 @@ public class HexTile3DMaterialAssign : Spatial
public void OnTextureSizeChanged(float value) public void OnTextureSizeChanged(float value)
{ {
GD.Print("Texture size: " + value); _customTileMaterial.SetShaderParam("TextureSize", (int) value);
ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); GD.Print("Texture size: " + _customTileMaterial.GetShaderParam("TextureSize"));
Debug.Assert(currentMaterial != null);
currentMaterial.SetShaderParam("TextureSize", (int) value);
GD.Print("Texture size: " + currentMaterial.GetShaderParam("TextureSize"));
} }
} }