From d3a36f438b85644c3d57381e4d969e5c20608686 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 11 May 2023 22:26:06 +0200 Subject: [PATCH] Getting generated color texture works, now fix the height offset... --- components/GroundMotionComponent.cs | 4 ++++ scenes/Game.cs | 26 +++++++++---------------- scenes/Game.tscn | 1 + scenes/TileWorld.cs | 11 +++++++---- scenes/tests/HexTile3DMaterialAssign.cs | 20 +++++-------------- 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/components/GroundMotionComponent.cs b/components/GroundMotionComponent.cs index 0a21453..81dc0e7 100644 --- a/components/GroundMotionComponent.cs +++ b/components/GroundMotionComponent.cs @@ -73,6 +73,10 @@ public class GroundMotionComponent : Component { GD.Print("Jump!"); entityVelocity.y = 10; + + Transform entityTransform = entity.GlobalTransform; + entityTransform.origin.y = nextHeight; + entity.GlobalTransform = entityTransform; } } diff --git a/scenes/Game.cs b/scenes/Game.cs index 994d680..6d1bb51 100644 --- a/scenes/Game.cs +++ b/scenes/Game.cs @@ -38,6 +38,7 @@ public class Game : Spatial private HexCell _currentTile; private Vector2 _currentTileOffset; private Vector3 _cameraOffset; + private ImageTexture _blackWhitePatternTexture; // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -73,6 +74,11 @@ public class Game : Spatial _tileMaterial = GD.Load("materials/HexTileTextureLookup.tres"); 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 _lastTile = new HexCell(); _currentTile = new HexCell(); @@ -209,18 +215,6 @@ public class Game : Spatial _player.TaskQueueComponent.Reset(); _player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask( 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) @@ -253,14 +247,12 @@ public class Game : Spatial { GD.Print("Using new map"); ImageTexture new_world_texture = new ImageTexture(); - - _tileWorld.Colormap.Unlock(); new_world_texture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); - _tileWorld.Colormap.Lock(); _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); } } \ No newline at end of file diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 0aac377..fe18618 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -79,6 +79,7 @@ margin_right = 100.0 margin_bottom = 134.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 diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 70f19b2..5f9b4b2 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -15,7 +15,7 @@ public class TileWorld : Spatial // public members public Vector2 Size = new Vector2(100, 100); - public float HeightScale = 10; + public float HeightScale = 2; public Image Heightmap; public Image Colormap; public int Seed = 0; @@ -101,12 +101,15 @@ public class TileWorld : Spatial noise_generator.Period = 20; noise_generator.Persistence = 0.2f; noise_generator.Lacunarity = 4; - + 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; _offscreenTextureRect.Texture = imageTexture; Colormap.CopyFrom(_offscreenViewport.GetTexture().GetData()); + Heightmap.Lock(); } @@ -154,7 +157,7 @@ public class TileWorld : Spatial 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 ; } diff --git a/scenes/tests/HexTile3DMaterialAssign.cs b/scenes/tests/HexTile3DMaterialAssign.cs index a071dfa..7b3a7f2 100644 --- a/scenes/tests/HexTile3DMaterialAssign.cs +++ b/scenes/tests/HexTile3DMaterialAssign.cs @@ -50,21 +50,14 @@ public class HexTile3DMaterialAssign : Spatial public void OnBlackWhitePatternButton() { GD.Print("Apply Black White Pattern!"); - ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); - Debug.Assert(currentMaterial != null); - currentMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture); - -// _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture); - -// _hexTile.Mesh.SetSurfaceMaterial(0, _customTileMaterial); + _customTileMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture); } public void OnColorPatternButton() { GD.Print("Apply Collor Pattern!"); - ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); - Debug.Assert(currentMaterial != null); - currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture); + //currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture); + _customTileMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture); // _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture); @@ -73,10 +66,7 @@ public class HexTile3DMaterialAssign : Spatial public void OnTextureSizeChanged(float value) { - GD.Print("Texture size: " + value); - ShaderMaterial currentMaterial = (ShaderMaterial) _hexTile.Mesh.GetSurfaceMaterial(0); - Debug.Assert(currentMaterial != null); - currentMaterial.SetShaderParam("TextureSize", (int) value); - GD.Print("Texture size: " + currentMaterial.GetShaderParam("TextureSize")); + _customTileMaterial.SetShaderParam("TextureSize", (int) value); + GD.Print("Texture size: " + _customTileMaterial.GetShaderParam("TextureSize")); } }