Refactored map generation and masking.

World now has textures for height and tile types.
WorldChunkRefactoring
Martin Felis 2023-11-01 16:02:39 +01:00
parent a1f02a8820
commit 36be4bc4c8
6 changed files with 464 additions and 403 deletions

View File

@ -1,55 +1,55 @@
using Godot;
using System.Diagnostics;
using System.Linq;
using Array = Godot.Collections.Array;
using Godot;
public class Game : Spatial
{
private ImageTexture _blackWhitePatternTexture;
private Camera _camera;
private Vector3 _cameraOffset;
private Label _centerLabel;
private HexCell _currentTile;
// ui elements
private Label _framesPerSecondLabel;
private Label _centerLabel;
private Label _tileOffsetLabel;
private Label _numTilesLabel;
private Label _mouseWorldLabel;
private Label _mouseTileOffsetLabel;
private Label _mouseTileCubeLabel;
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private TextureRect _worldTextureRect;
private TextureRect _heightTextureRect;
private Button _generateWorldButton;
private Control _gameUi;
private Button _generateWorldButton;
private Label _goldCountLabel;
// scene nodes
private Spatial _tileHighlight;
private Spatial _mouseTileHighlight;
private StreamContainer _streamContainer;
private Area _streamContainerArea;
private Spatial _streamContainerActiveTiles;
private Player _player;
private TileWorld _tileWorld;
private Camera _camera;
private World _world;
private WorldView _worldView;
// Resources
private PackedScene _tileHighlightScene;
private ShaderMaterial _tileMaterial;
private TextureRect _heightTextureRect;
// other members
private HexGrid _hexGrid;
private HexCell _lastTile;
private HexCell _currentTile;
private Vector3 _cameraOffset;
private ImageTexture _blackWhitePatternTexture;
private InteractionSystem _interactionSystem;
private HexCell _lastTile;
private Label _mouseTileCubeLabel;
private Spatial _mouseTileHighlight;
private Label _mouseTileOffsetLabel;
private Label _mouseWorldLabel;
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private Label _numTilesLabel;
private Player _player;
private StreamContainer _streamContainer;
private Spatial _streamContainerActiveTiles;
private Area _streamContainerArea;
// scene nodes
private Spatial _tileHighlight;
// Resources
private PackedScene _tileHighlightScene;
private TileInstanceManager _tileInstanceManager;
private ShaderMaterial _tileMaterial;
private Label _tileOffsetLabel;
private TileWorld _tileWorld;
private World _world;
private TextureRect _worldTextureRect;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
// debugStatsContainer
Container debugStatsContainer = (Container)FindNode("DebugStatsContainer");
var debugStatsContainer = (Container)FindNode("DebugStatsContainer");
_framesPerSecondLabel = debugStatsContainer.GetNode<Label>("fps_label");
_centerLabel = debugStatsContainer.GetNode<Label>("center_label");
@ -62,7 +62,7 @@ public class Game : Spatial
_numCoordsRemovedLabel = debugStatsContainer.GetNode<Label>("num_coords_removed_label");
// UI elements
Container worldGeneratorContainer = (Container)FindNode("WorldGeneratorContainer");
var worldGeneratorContainer = (Container)FindNode("WorldGeneratorContainer");
_worldTextureRect = worldGeneratorContainer.GetNode<TextureRect>("WorldTextureRect");
_heightTextureRect = worldGeneratorContainer.GetNode<TextureRect>("HeightTextureRect");
_generateWorldButton = worldGeneratorContainer.GetNode<Button>("WorldGenerateButton");
@ -84,10 +84,10 @@ public class Game : Spatial
_cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation;
_world = (World)FindNode("World");
_worldView = (WorldView)FindNode("WorldView");
_tileInstanceManager = (TileInstanceManager)FindNode("TileInstanceManager");
// populate UI values
Slider generatorWorldSizeSlider = worldGeneratorContainer.GetNode<Slider>("HBoxContainer/WorldSizeSlider");
var generatorWorldSizeSlider = worldGeneratorContainer.GetNode<Slider>("HBoxContainer/WorldSizeSlider");
generatorWorldSizeSlider.Value = _tileWorld.Size;
Debug.Assert(_tileWorld != null);
@ -98,7 +98,7 @@ public class Game : Spatial
Debug.Assert(_tileMaterial != null);
_blackWhitePatternTexture = new ImageTexture();
Image image = new Image();
var image = new Image();
image.Load("assets/4x4checker.png");
_blackWhitePatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
@ -122,26 +122,20 @@ public class Game : Spatial
_player.TaskQueueComponent.Connect("StartInteraction", _interactionSystem,
nameof(_interactionSystem.OnStartInteraction));
_player.Connect("GoldCountChanged", this, nameof(OnGoldCountChanged));
_worldView.Connect("TileClicked", this, nameof(OnTileClicked));
_worldView.Connect("TileHovered", this, nameof(OnTileHovered));
_worldView.Connect("OnWorldViewTileTypeImageChanged", this, nameof(OnWorldViewTileTypeImageChanged));
_tileInstanceManager.Connect("TileClicked", this, nameof(OnTileClicked));
_tileInstanceManager.Connect("TileHovered", this, nameof(OnTileHovered));
_world.Connect("OnWorldViewTileTypeImageChanged", this, nameof(OnWorldViewTileTypeImageChanged));
_world.Connect("OnHeightmapImageChanged", this, nameof(OnHeightmapImageChanged));
// register entity events
foreach (Node node in GetNode("Entities").GetChildren())
{
if (node.HasSignal("EntityClicked"))
{
node.Connect("EntityClicked", this, nameof(OnEntityClicked));
}
}
// perform dependency injection
//_streamContainer.SetWorld(_tileWorld);Clicked
WorldInfoComponent worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");
if (worldInfoComponent != null)
{
worldInfoComponent.SetWorld(_tileWorld);
}
var worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");
if (worldInfoComponent != null) worldInfoComponent.SetWorld(_tileWorld);
_tileWorld.Generate(_tileWorld.Size);
UpdateCurrentTile();
@ -151,23 +145,17 @@ public class Game : Spatial
public override void _Input(InputEvent inputEvent)
{
if (inputEvent.IsAction("Forward"))
{
GD.Print("Forward");
}
if (inputEvent.IsAction("Forward")) GD.Print("Forward");
if (inputEvent.IsAction("Back"))
{
GD.Print("Back");
}
if (inputEvent.IsAction("Back")) GD.Print("Back");
}
public void UpdateCurrentTile()
{
// cast a ray from the camera to center
Vector3 cameraNormal = _camera.ProjectRayNormal(_camera.GetViewport().Size * 0.5f);
Vector3 cameraPosition = _camera.ProjectRayOrigin(_camera.GetViewport().Size * 0.5f);
Vector3 cameraDir = cameraNormal - cameraPosition;
var cameraNormal = _camera.ProjectRayNormal(_camera.GetViewport().Size * 0.5f);
var cameraPosition = _camera.ProjectRayOrigin(_camera.GetViewport().Size * 0.5f);
var cameraDir = cameraNormal - cameraPosition;
Vector3 centerCoord;
@ -197,8 +185,8 @@ public class Game : Spatial
UpdateCurrentTile();
Transform tileHighlightTransform = Transform.Identity;
Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
var tileHighlightTransform = Transform.Identity;
var currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
tileHighlightTransform.origin.x = currentTileCenter.x;
tileHighlightTransform.origin.z = currentTileCenter.y;
_tileHighlight.Transform = tileHighlightTransform;
@ -212,7 +200,7 @@ public class Game : Spatial
_numCoordsRemovedLabel.Text = _streamContainer.RemovedCoords.Count.ToString();
}
Transform cameraTransform = _camera.Transform;
var cameraTransform = _camera.Transform;
cameraTransform.origin = _player.GlobalTranslation + _cameraOffset;
_camera.Transform = cameraTransform;
}
@ -221,7 +209,7 @@ public class Game : Spatial
public void OnGenerateButton()
{
GD.Print("Generating");
Slider worldSizeSlider = (Slider)FindNode("WorldSizeSlider");
var worldSizeSlider = (Slider)FindNode("WorldSizeSlider");
if (worldSizeSlider == null)
{
GD.PrintErr("Could not find WorldSizeSlider!");
@ -236,8 +224,8 @@ public class Game : Spatial
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
int shapeIndex)
{
HexCell cellAtCursor = _hexGrid.GetHexAt(new Vector2(position.x, position.z));
Transform highlightTransform = Transform.Identity;
var cellAtCursor = _hexGrid.GetHexAt(new Vector2(position.x, position.z));
var highlightTransform = Transform.Identity;
_mouseWorldLabel.Text = position.ToString("F3");
_mouseTileOffsetLabel.Text = cellAtCursor.OffsetCoords.ToString("N");
@ -245,23 +233,15 @@ public class Game : Spatial
_mouseTileHighlight.Transform = highlightTransform;
if (inputEvent is InputEventMouseButton && ((InputEventMouseButton)inputEvent).Pressed)
{
_streamContainer.EmitSignal("TileClicked", _streamContainer.GetTile3dAt(cellAtCursor.OffsetCoords));
}
}
public void OnTileClicked(HexTile3D tile)
{
if (_player == null)
{
return;
}
if (_player == null) return;
if (_player.InteractionComponent != null)
{
_player.InteractionComponent.EmitSignal("InteractionEnd");
}
if (_player.InteractionComponent != null) _player.InteractionComponent.EmitSignal("InteractionEnd");
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
@ -270,7 +250,7 @@ public class Game : Spatial
public void OnTileHovered(HexTile3D tile)
{
Transform highlightTransform = tile.GlobalTransform;
var highlightTransform = tile.GlobalTransform;
_mouseTileHighlight.Transform = highlightTransform;
_mouseWorldLabel.Text = highlightTransform.origin.ToString("F3");
_mouseTileOffsetLabel.Text = tile.OffsetCoords.ToString("N");
@ -283,7 +263,7 @@ public class Game : Spatial
{
GD.Print("Clicked on entity at " + entity.GlobalTranslation);
Spatial mountPoint = (Spatial)entity.FindNode("MountPoint");
var mountPoint = (Spatial)entity.FindNode("MountPoint");
if (mountPoint != null)
{
_player.TaskQueueComponent.Reset();
@ -296,8 +276,8 @@ public class Game : Spatial
public void ResetGameState()
{
Transform playerStartTransform = Transform.Identity;
float height = _tileWorld.GetHeightAtOffset(new Vector2(0, 0));
var playerStartTransform = Transform.Identity;
var height = _tileWorld.GetHeightAtOffset(new Vector2(0, 0));
playerStartTransform.origin.y = height;
_player.Transform = playerStartTransform;
_player.TaskQueueComponent.Reset();
@ -308,10 +288,10 @@ public class Game : Spatial
foreach (Spatial entity in GetNode("Entities").GetChildren())
{
Transform entityTransform = entity.Transform;
Vector2 entityPlanePos = new Vector2(entityTransform.origin.x, entityTransform.origin.z);
Vector2 entityOffsetCoordinates = _hexGrid.GetHexAt(entityPlanePos).OffsetCoords;
float entityHeight = _tileWorld.GetHeightAtOffset(entityOffsetCoordinates);
var entityTransform = entity.Transform;
var entityPlanePos = new Vector2(entityTransform.origin.x, entityTransform.origin.z);
var entityOffsetCoordinates = _hexGrid.GetHexAt(entityPlanePos).OffsetCoords;
var entityHeight = _tileWorld.GetHeightAtOffset(entityOffsetCoordinates);
entityTransform.origin.y = entityHeight;
entity.Transform = entityTransform;
}
@ -332,34 +312,39 @@ public class Game : Spatial
// Connect all signals of the generated world
foreach (Node node in _tileWorld.Entities.GetChildren())
{
if (node.HasSignal("EntityClicked"))
{
node.Connect("EntityClicked", this, nameof(OnEntityClicked));
}
}
}
private void UpdateWorldTextures()
{
GD.Print("Updating World textures");
ImageTexture newWorldTexture = new ImageTexture();
var newWorldTexture = new ImageTexture();
newWorldTexture.CreateFromImage(_tileWorld.ColormapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_worldTextureRect.Texture = newWorldTexture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x);
ImageTexture newHeightTexture = new ImageTexture();
var newHeightTexture = new ImageTexture();
newHeightTexture.CreateFromImage(_tileWorld.HeightmapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_heightTextureRect.Texture = newHeightTexture;
}
private void OnHeightmapImageChanged(Image heightmapImage)
{
var newHeightmapTexture = new ImageTexture();
newHeightmapTexture.CreateFromImage(heightmapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_heightTextureRect.Texture = newHeightmapTexture;
}
private void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage)
{
ImageTexture newWorldTexture = new ImageTexture();
var newWorldTexture = new ImageTexture();
newWorldTexture.CreateFromImage(viewTileTypeImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
@ -367,13 +352,13 @@ public class Game : Spatial
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)newWorldTexture.GetSize().x);
_tileMaterial.SetShaderParam("CoordinateOffsetU", (int) _worldView.WorldTextureCoordinateOffset.x);
_tileMaterial.SetShaderParam("CoordinateOffsetV", (int) _worldView.WorldTextureCoordinateOffset.y);
_tileMaterial.SetShaderParam("CoordinateOffsetU", (int)_tileInstanceManager.WorldTextureCoordinateOffset.x);
_tileMaterial.SetShaderParam("CoordinateOffsetV", (int)_tileInstanceManager.WorldTextureCoordinateOffset.y);
}
public void OnGoldCountChanged(int goldCount)
{
AnimationPlayer animationPlayer = _gameUi.GetNode<AnimationPlayer>("AnimationPlayer");
var animationPlayer = _gameUi.GetNode<AnimationPlayer>("AnimationPlayer");
_goldCountLabel.Text = goldCount.ToString();
animationPlayer.CurrentAnimation = "FlashLabel";
animationPlayer.Seek(0);

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@ using Godot.Collections;
using Vector2 = Godot.Vector2;
using Vector3 = Godot.Vector3;
public class WorldView : Spatial
public class TileInstanceManager : Spatial
{
// ui elements
@ -22,8 +22,6 @@ public class WorldView : Spatial
delegate void TileClicked(HexTile3D tile3d);
[Signal]
delegate void TileHovered(HexTile3D tile3d);
[Signal]
delegate void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage);
// other members
public Vector2 WorldTextureCoordinateOffset = Vector2.Zero;
@ -128,41 +126,6 @@ public class WorldView : Spatial
return null;
}
private void UpdateWorldViewTexture()
{
int worldChunkSize = global::World.ChunkSize;
int numWorldChunkRows = global::World.NumChunkRows;
int numWorldChunkColumns = global::World.NumChunkColumns;
_viewTileTypeImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false, Image.Format.Rgba8);
Vector2 chunkIndexSouthWest = Vector2.Inf;
Vector2 chunkIndexNorthEast = -Vector2.Inf;
foreach (SceneTileChunk chunk in _sceneTileChunks)
{
WorldChunk worldChunk = _world.GetOrCreateWorldChunk((int) chunk.ChunkIndex.x, (int)chunk.ChunkIndex.y, Colors.White);
if (chunk.ChunkIndex.x <= chunkIndexSouthWest.x && chunk.ChunkIndex.y <= chunkIndexSouthWest.y)
{
chunkIndexSouthWest = chunk.ChunkIndex;
} else if (chunk.ChunkIndex.x >= chunkIndexNorthEast.x && chunk.ChunkIndex.y >= chunkIndexNorthEast.y)
{
chunkIndexNorthEast = chunk.ChunkIndex;
}
_viewTileTypeImage.BlendRect(
worldChunk.TileTypeOffscreenViewport.GetTexture().GetData(),
new Rect2(Vector2.Zero, Vector2.One * worldChunkSize),
(chunk.ChunkIndex - _world.CenterChunkIndex + Vector2.One) * worldChunkSize);
}
_viewTileTypeTexture = new ImageTexture();
_viewTileTypeTexture.CreateFromImage(_viewTileTypeImage);
WorldTextureCoordinateOffset = chunkIndexSouthWest * worldChunkSize;
EmitSignal("OnWorldViewTileTypeImageChanged", _viewTileTypeImage);
}
private void HandleWorldTileChange(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices)
{
@ -206,8 +169,6 @@ public class WorldView : Spatial
_sceneTileChunks.Add(sceneTileChunk);
}
UpdateWorldViewTexture();
GD.Print("Removed Chunks " + removedChunkIndices.Count);
GD.Print("Added Chunks " + addedChunkIndices.Count);
GD.Print("Removed chunk count: " + removedChunks.Count);

View File

@ -1,8 +1,8 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Godot;
using Godot.Collections;
public class World : Spatial
@ -15,45 +15,40 @@ public class World : Spatial
Objects,
Done
}
public GenerationState State = GenerationState.Done;
public Vector2 CenterChunkIndex = Vector2.Zero;
// referenced scenes
private PackedScene _worldChunkScene = GD.Load<PackedScene>("res://scenes/WorldChunk.tscn");
// constants
public const int ChunkSize = 16;
public const int NumChunkRows = 3;
public const int NumChunkColumns = NumChunkRows;
public int Seed = 0;
public HexGrid HexGrid = new HexGrid();
public Spatial Chunks;
public Color DebugColor;
// ui elements
// scene nodes
// resources
// exports
// [Export] public Vector2 Size = new Vector2(1, 1);
// signals
[Signal]
delegate void OnTilesChanged(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices);
private List<Vector2> _activeChunkIndices = new();
private readonly List<Vector2> _addedChunkIndices = new();
private readonly Godot.Collections.Dictionary<Vector2, WorldChunk> _cachedWorldChunks;
private Rect2 _centerChunkRect;
// delegate void OnCoordClicked(Vector2 world_pos);
// other members
private Vector2 _centerPlaneCoord;
private Rect2 _centerChunkRect = new Rect2();
private Godot.Collections.Dictionary<Vector2, WorldChunk> _cachedWorldChunks;
private List<Vector2> _activeChunkIndices = new();
private List<Vector2> _addedChunkIndices = new();
private List<Vector2> _removedChunkIndices = new();
private readonly Image _heightmapImage = new();
private ImageTexture _heightmapTexture;
private readonly List<Vector2> _removedChunkIndices = new();
private TileInstanceManager _tileInstanceManager;
private readonly Image _tileTypeMapImage = new();
private ImageTexture _viewTileTypeTexture;
// referenced scenes
private readonly PackedScene _worldChunkScene = GD.Load<PackedScene>("res://scenes/WorldChunk.tscn");
public Vector2 CenterChunkIndex = Vector2.Zero;
public Spatial Chunks;
public Color DebugColor;
public HexGrid HexGrid = new();
private OpenSimplexNoise noiseGenerator = new();
public int Seed = 0;
public GenerationState State = GenerationState.Done;
public World()
{
@ -68,6 +63,9 @@ public class World : Spatial
Chunks = (Spatial)FindNode("Chunks");
Debug.Assert(Chunks != null);
_tileInstanceManager = (TileInstanceManager)FindNode("TileInstanceManager");
Debug.Assert(_tileInstanceManager != null);
InitNoiseGenerator();
SetCenterPlaneCoord(Vector2.Zero);
@ -88,7 +86,7 @@ public class World : Spatial
{
if (IsTileCached(xIndex, yIndex))
{
WorldChunk cachedChunk = _cachedWorldChunks[new Vector2(xIndex, yIndex)];
var cachedChunk = _cachedWorldChunks[new Vector2(xIndex, yIndex)];
return cachedChunk;
}
@ -102,18 +100,18 @@ public class World : Spatial
private WorldChunk CreateWorldChunk(int xIndex, int yIndex, Color debugColor)
{
WorldChunk result = (WorldChunk)_worldChunkScene.Instance();
var result = (WorldChunk)_worldChunkScene.Instance();
result.SetSize(ChunkSize);
Vector2 offsetCoordSouthWest = new Vector2(xIndex, yIndex) * ChunkSize;
Vector2 offsetCoordNorthEast = offsetCoordSouthWest + new Vector2(1, 1) * (ChunkSize - 1);
var offsetCoordSouthWest = new Vector2(xIndex, yIndex) * ChunkSize;
var offsetCoordNorthEast = offsetCoordSouthWest + new Vector2(1, 1) * (ChunkSize - 1);
Vector2 planeCoordSouthWest = HexGrid.GetHexCenterFromOffset(offsetCoordSouthWest) +
var planeCoordSouthWest = HexGrid.GetHexCenterFromOffset(offsetCoordSouthWest) +
new Vector2(-HexGrid.HexSize.x, HexGrid.HexSize.y) * 0.5f;
Vector2 planeCoordNorthEast = HexGrid.GetHexCenterFromOffset(offsetCoordNorthEast) +
var planeCoordNorthEast = HexGrid.GetHexCenterFromOffset(offsetCoordNorthEast) +
new Vector2(HexGrid.HexSize.x, -HexGrid.HexSize.y) * 0.5f;
result.ChunkAddress = new Vector2(xIndex, yIndex);
result.ChunkIndex = new Vector2(xIndex, yIndex);
result.PlaneRect = new Rect2(
new Vector2(planeCoordSouthWest.x, planeCoordNorthEast.y),
new Vector2(planeCoordNorthEast.x - planeCoordSouthWest.x, planeCoordSouthWest.y - planeCoordNorthEast.y));
@ -122,7 +120,7 @@ public class World : Spatial
result.DebugColor.a = 0.6f;
Chunks.AddChild(result);
Vector2 chunkIndex = new Vector2(xIndex, yIndex);
var chunkIndex = new Vector2(xIndex, yIndex);
_cachedWorldChunks.Add(chunkIndex, result);
return result;
@ -143,7 +141,7 @@ public class World : Spatial
var chunkIndex = GetChunkTupleFromPlaneCoord(planeCoord);
CenterChunkIndex = new Vector2(chunkIndex.Item1, chunkIndex.Item2);
WorldChunk currentChunk = GetOrCreateWorldChunk(chunkIndex.Item1, chunkIndex.Item2,
var currentChunk = GetOrCreateWorldChunk(chunkIndex.Item1, chunkIndex.Item2,
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
_centerChunkRect = currentChunk.PlaneRect;
@ -163,35 +161,57 @@ public class World : Spatial
Debug.Assert(_activeChunkIndices.Count == NumChunkRows * NumChunkColumns);
foreach(Vector2 activeChunkIndex in _activeChunkIndices)
{
GetOrCreateWorldChunk((int) activeChunkIndex.x, (int) activeChunkIndex.y, new Color(GD.Randf(), GD.Randf(), GD.Randf()));
}
foreach (var activeChunkIndex in _activeChunkIndices)
GetOrCreateWorldChunk((int)activeChunkIndex.x, (int)activeChunkIndex.y,
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
// unload retired chunks
_removedChunkIndices.Clear();
_addedChunkIndices.Clear();
foreach (var cachedChunkKey in oldCachedChunks.Keys)
{
if (!_activeChunkIndices.Contains(cachedChunkKey))
{
RemoveChunk(cachedChunkKey);
}
}
foreach (var chunkKey in _activeChunkIndices)
{
if (!oldCachedChunks.ContainsKey(chunkKey))
{
_addedChunkIndices.Add(chunkKey);
var chunk = _cachedWorldChunks[chunkKey];
GenerateChunkNoiseMap(chunk);
State = GenerationState.Heightmap;
}
}
if (_addedChunkIndices.Count > 0)
private void GenerateChunkNoiseMap(WorldChunk chunk)
{
State = GenerationState.Heightmap;
}
var chunkIndex = chunk.ChunkIndex;
var debugChunkColor = new Color(Mathf.Abs(chunkIndex.x) / 5, Mathf.Abs(chunkIndex.y) / 5,
Mathf.RoundToInt(Mathf.Abs(chunkIndex.x + chunkIndex.y)) % 2);
var noiseImageTexture = new ImageTexture();
noiseImageTexture.CreateFromImage(noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize),
0);
// Debug Texture
var simpleImage = new Image();
simpleImage.Create(ChunkSize, ChunkSize, false, Image.Format.Rgb8);
simpleImage.Lock();
foreach (var i in Enumerable.Range(0, ChunkSize))
foreach (var j in Enumerable.Range(0, ChunkSize))
if ((i + j) % 2 == 0)
simpleImage.SetPixelv(new Vector2(i, j), Colors.Aqua);
else
simpleImage.SetPixelv(new Vector2(i, j), debugChunkColor);
simpleImage.Unlock();
// noiseImageTexture.CreateFromImage(simpleImage, 0);
chunk.SetNoisemap(noiseImageTexture);
}
private void RemoveChunk(Vector2 cachedChunkKey)
@ -200,82 +220,118 @@ public class World : Spatial
_removedChunkIndices.Add(cachedChunkKey);
foreach (WorldChunk chunk in Chunks.GetChildren())
{
if (chunk.ChunkAddress == new Vector2(cachedChunkKey.x, cachedChunkKey.y))
{
if (chunk.ChunkIndex == new Vector2(cachedChunkKey.x, cachedChunkKey.y))
chunk.QueueFree();
}
}
}
private Tuple<int, int> GetChunkTupleFromPlaneCoord(Vector2 planeCoord)
{
HexCell centerOffsetCoord = HexGrid.GetHexAt(planeCoord);
Vector2 chunkIndexFloat = (centerOffsetCoord.OffsetCoords / (float)ChunkSize).Floor();
Tuple<int, int> chunkIndex = new Tuple<int, int>((int)chunkIndexFloat.x, (int)chunkIndexFloat.y);
var centerOffsetCoord = HexGrid.GetHexAt(planeCoord);
var chunkIndexFloat = (centerOffsetCoord.OffsetCoords / ChunkSize).Floor();
var chunkIndex = new Tuple<int, int>((int)chunkIndexFloat.x, (int)chunkIndexFloat.y);
return chunkIndex;
}
public void SetCenterPlaneCoord(Vector2 centerPlaneCoord)
{
if (!_centerChunkRect.HasPoint(centerPlaneCoord))
{
UpdateCenterChunkFromPlaneCoord(centerPlaneCoord);
if (!_centerChunkRect.HasPoint(centerPlaneCoord)) UpdateCenterChunkFromPlaneCoord(centerPlaneCoord);
}
private void UpdateWorldViewTexture()
{
var worldChunkSize = ChunkSize;
var numWorldChunkRows = NumChunkRows;
var numWorldChunkColumns = NumChunkColumns;
_heightmapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
Image.Format.Rgba8);
_tileTypeMapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
Image.Format.Rgba8);
var chunkIndexSouthWest = Vector2.Inf;
var chunkIndexNorthEast = -Vector2.Inf;
foreach (var chunkIndex in _activeChunkIndices)
{
var worldChunk = GetOrCreateWorldChunk((int)chunkIndex.x, (int)chunkIndex.y, Colors.White);
if (chunkIndex.x <= chunkIndexSouthWest.x && chunkIndex.y <= chunkIndexSouthWest.y)
chunkIndexSouthWest = chunkIndex;
else if (chunkIndex.x >= chunkIndexNorthEast.x && chunkIndex.y >= chunkIndexNorthEast.y)
chunkIndexNorthEast = chunkIndex;
_heightmapImage.BlendRect(
worldChunk.HeightmapOffscreenViewport.GetTexture().GetData(),
new Rect2(Vector2.Zero, Vector2.One * worldChunkSize),
(chunkIndex - CenterChunkIndex + Vector2.One) * worldChunkSize);
_tileTypeMapImage.BlendRect(
worldChunk.TileTypeOffscreenViewport.GetTexture().GetData(),
new Rect2(Vector2.Zero, Vector2.One * worldChunkSize),
(chunkIndex - CenterChunkIndex + Vector2.One) * worldChunkSize);
}
_heightmapTexture = new ImageTexture();
_heightmapTexture.CreateFromImage(_heightmapImage);
_viewTileTypeTexture = new ImageTexture();
_viewTileTypeTexture.CreateFromImage(_tileTypeMapImage);
_tileInstanceManager.WorldTextureCoordinateOffset = chunkIndexSouthWest * worldChunkSize;
EmitSignal("OnWorldViewTileTypeImageChanged", _tileTypeMapImage);
EmitSignal("OnHeightmapImageChanged", _heightmapImage);
}
public override void _Process(float delta)
{
GenerationState oldState = State;
if (State == GenerationState.Heightmap)
{
// generate heightmap for all new chunks
foreach (Vector2 chunkIndex in _addedChunkIndices)
foreach (var chunkIndex in _addedChunkIndices)
{
WorldChunk chunk = _cachedWorldChunks[chunkIndex];
Color debugChunkColor = new Color(Mathf.Abs(chunkIndex.x) / 5, Mathf.Abs(chunkIndex.y) / 5, Mathf.RoundToInt(Mathf.Abs(chunkIndex.x + chunkIndex.y)) % 2);
GD.Print("Generating for offset " + chunkIndex + " chunk: " + chunk + " debugChunkColor: " + debugChunkColor);
ImageTexture noiseImageTexture = new ImageTexture();
noiseImageTexture.CreateFromImage(noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize), (uint) 0);
// Debug Texture
Image simpleImage = new Image();
simpleImage.Create(ChunkSize, ChunkSize, false, Image.Format.Rgb8);
simpleImage.Lock();
foreach (int i in Enumerable.Range(0, ChunkSize))
{
foreach (int j in Enumerable.Range(0, ChunkSize))
{
if ((i + j) % 2 == 0)
{
simpleImage.SetPixelv(new Vector2(i, j), Colors.Aqua);
}
else
{
simpleImage.SetPixelv(new Vector2(i, j), debugChunkColor);
}
}
}
simpleImage.Unlock();
// noiseImageTexture.CreateFromImage(simpleImage, 0);
chunk.SetHeightmap(noiseImageTexture);
State = GenerationState.Heightmap;
}
var oldState = State;
if (State == GenerationState.Heightmap)
{
var numChunksGeneratingHeightmap = 0;
foreach (var chunkIndex in _addedChunkIndices)
{
var chunk = _cachedWorldChunks[chunkIndex];
if (chunk.HeightMapFrameCount > 0) numChunksGeneratingHeightmap++;
}
if (numChunksGeneratingHeightmap == 0)
{
// assign height map images
State = GenerationState.TileType;
} else if (State == GenerationState.TileType)
foreach (var chunkIndex in _addedChunkIndices)
{
var chunk = _cachedWorldChunks[chunkIndex];
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
}
}
}
else if (State == GenerationState.TileType)
{
// assign tile type images
// generate heightmap for all new chunks
foreach (var chunkIndex in _addedChunkIndices)
{
var chunk = _cachedWorldChunks[chunkIndex];
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
}
State = GenerationState.Objects;
} else if (State == GenerationState.Objects)
}
else if (State == GenerationState.Objects)
{
// generate objects
@ -284,7 +340,27 @@ public class World : Spatial
if (oldState != GenerationState.Done && State == GenerationState.Done)
{
UpdateWorldViewTexture();
EmitSignal("OnTilesChanged", _removedChunkIndices.ToArray(), _addedChunkIndices.ToArray());
}
}
// ui elements
// scene nodes
// resources
// exports
// [Export] public Vector2 Size = new Vector2(1, 1);
// signals
[Signal]
private delegate void OnTilesChanged(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices);
[Signal]
private delegate void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage);
[Signal]
private delegate void OnHeightmapImageChanged(Image heightmapImage);
}

View File

@ -1,55 +1,44 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Godot;
public class WorldChunk : Spatial
{
// ui elements
private Sprite _heightmapSprite;
private TextureRect _heightmapTextureRect;
private Sprite _noiseMask;
// scene nodes
private MeshInstance PlaneRectMesh;
private Sprite _noiseSprite;
// resources
// exports
[Export] public Texture TileTypeMap;
[Export] public Texture NavigationMap;
private readonly SpatialMaterial _rectMaterial = new();
private bool _showTextureOverlay;
[Export] public Vector2 ChunkIndex;
public Color DebugColor = Colors.White;
[Export] public Texture HeightMap;
[Export] public int Size = 32;
[Export] public Vector2 ChunkAddress;
public int HeightMapFrameCount;
[Export]
public bool ShowTextureOverlay
{
get
{
return _showTextureOverlay;
}
public Viewport HeightmapOffscreenViewport;
[Export] public Texture NavigationMap;
set
{
if (PlaneRectMesh != null)
{
PlaneRectMesh.Visible = value;
}
}
}
public bool NoiseTextureCheckerboardOverlay = true;
// signals
// delegate void OnCoordClicked(Vector2 world_pos);
// other members
public Rect2 PlaneRect;
public Color DebugColor = Colors.White;
public Viewport TileTypeOffscreenViewport;
public bool NoiseTextureCheckerboardOverlay = true;
// ui elements
private TextureRect _heightmapRect;
private SpatialMaterial _rectMaterial = new SpatialMaterial();
private bool _showTextureOverlay = false;
// scene nodes
private MeshInstance PlaneRectMesh;
[Export] public int Size = 32;
// resources
// exports
[Export] public Texture TileTypeMap;
public int TileTypeMapFrameCount;
public Viewport TileTypeOffscreenViewport;
public WorldChunk()
{
@ -60,46 +49,25 @@ public class WorldChunk : Spatial
SetSize(size);
}
public void SetSize(int size)
[Export]
public bool ShowTextureOverlay
{
Size = size;
get => _showTextureOverlay;
if (TileTypeOffscreenViewport != null)
set
{
TileTypeOffscreenViewport.Size = Vector2.One * size;
if (PlaneRectMesh != null) PlaneRectMesh.Visible = value;
}
}
// other members
public void SaveToFile(String chunkName)
{
Image image = new Image();
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, TileTypeMap.GetData().GetData());
image.SavePng(chunkName + "_tileType.png");
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, NavigationMap.GetData().GetData());
image.SavePng(chunkName + "_navigationMap.png");
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, HeightMap.GetData().GetData());
image.SavePng(chunkName + "_heightMap.png");
}
public void LoadFromFile(String chunkName)
{
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
PlaneRectMesh = (MeshInstance)FindNode("PlaneRectMesh");
Debug.Assert(PlaneRectMesh != null);
if (PlaneRectMesh.Visible)
{
_showTextureOverlay = true;
}
if (PlaneRectMesh.Visible) _showTextureOverlay = true;
Transform planeRectTransform = Transform.Identity;
var planeRectTransform = Transform.Identity;
planeRectTransform =
planeRectTransform.Scaled(new Vector3(PlaneRect.Size.x, 0.125f, PlaneRect.Size.y));
planeRectTransform.origin.x = PlaneRect.GetCenter().x;
@ -112,50 +80,98 @@ public class WorldChunk : Spatial
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).AlbedoColor = DebugColor;
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).FlagsTransparent = true;
HeightmapOffscreenViewport = (Viewport)FindNode("HeightmapOffscreenViewport");
HeightmapOffscreenViewport.Size = Vector2.One * Size;
Debug.Assert(HeightmapOffscreenViewport != null);
_noiseSprite = (Sprite)FindNode("NoiseSprite");
_noiseMask = (Sprite)FindNode("NoiseMask");
_heightmapSprite = (Sprite)FindNode("HeightmapSprite");
TileTypeOffscreenViewport = (Viewport)FindNode("TileTypeOffscreenViewport");
TileTypeOffscreenViewport.Size = Vector2.One * Size;
Debug.Assert(TileTypeOffscreenViewport != null);
_heightmapRect = (TextureRect)FindNode("HeightmapTexture");
SetSize(World.ChunkSize);
}
public void SetSize(int size)
{
Size = size;
if (TileTypeOffscreenViewport != null)
{
TileTypeOffscreenViewport.Size = Vector2.One * size;
HeightmapOffscreenViewport.Size = Vector2.One * size;
_noiseMask.Transform = Transform2D.Identity.Scaled(Vector2.One * size / _noiseMask.Texture.GetSize().x);
_noiseSprite.Transform = Transform2D.Identity.Scaled(Vector2.One * size / _noiseSprite.Texture.GetSize().x);
_heightmapSprite.Transform =
Transform2D.Identity.Scaled(Vector2.One * size / _heightmapSprite.Texture.GetSize().x);
}
}
// other members
public void SaveToFile(string chunkName)
{
var image = new Image();
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, TileTypeMap.GetData().GetData());
image.SavePng(chunkName + "_tileType.png");
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, NavigationMap.GetData().GetData());
image.SavePng(chunkName + "_navigationMap.png");
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, HeightMap.GetData().GetData());
image.SavePng(chunkName + "_heightMap.png");
}
public void LoadFromFile(string chunkName)
{
}
public void SetNoisemap(Texture texture)
{
_noiseSprite.Texture = texture;
_noiseSprite.Transform =
Transform2D.Identity.Scaled(HeightmapOffscreenViewport.Size / _noiseSprite.Texture.GetSize().x);
HeightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
HeightMapFrameCount = 1;
}
public void SetHeightmap(Texture texture)
{
GD.Print("Setting HeightmapRect Texture: " + _heightmapRect + " with size " + _heightmapRect.GetSize());
_heightmapRect.Texture = texture;
_heightmapSprite.Texture = texture;
_heightmapSprite.Transform =
Transform2D.Identity.Scaled(TileTypeOffscreenViewport.Size / _heightmapSprite.Texture.GetSize());
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
TileTypeMapFrameCount = 1;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
Texture tileTypeTexture = TileTypeOffscreenViewport.GetTexture();
if (NoiseTextureCheckerboardOverlay)
{
Image tileTypeImage = tileTypeTexture.GetData();
var tileTypeImage = tileTypeTexture.GetData();
tileTypeImage.Lock();
foreach (int i in Enumerable.Range(0, Size))
foreach (var i in Enumerable.Range(0, Size))
foreach (var j in Enumerable.Range(0, Size))
{
foreach (int j in Enumerable.Range(0, Size))
{
Vector2 textureCoord = new Vector2(i, j);
Color baseColor = tileTypeImage.GetPixelv(textureCoord);
var textureCoord = new Vector2(i, j);
var baseColor = tileTypeImage.GetPixelv(textureCoord);
if ((i + j) % 2 == 0)
{
tileTypeImage.SetPixelv(textureCoord, baseColor);
}
else
{
tileTypeImage.SetPixelv(textureCoord, baseColor * 0.6f);
}
}
}
tileTypeImage.Unlock();
ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(tileTypeImage, (uint) 0);
var imageTexture = new ImageTexture();
imageTexture.CreateFromImage(tileTypeImage, 0);
tileTypeTexture = imageTexture;
}
@ -169,7 +185,14 @@ public class WorldChunk : Spatial
//RectMaterial.Uv1Triplanar = true;
PlaneRectMesh.SetSurfaceMaterial(0, _rectMaterial);
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
if (HeightMapFrameCount == 0) HeightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
HeightMapFrameCount = HeightMapFrameCount > 0 ? HeightMapFrameCount - 1 : 0;
if (TileTypeMapFrameCount == 0) TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
TileTypeMapFrameCount = TileTypeMapFrameCount > 0 ? TileTypeMapFrameCount - 1 : 0;
PlaneRectMesh.MaterialOverride = null;
}
}

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://scenes/WorldChunk.cs" type="Script" id=1]
[ext_resource path="res://materials/IslandColorRampShader.tres" type="Material" id=2]
[ext_resource path="res://assets/TestHeightmap.tres" type="Texture" id=3]
[ext_resource path="res://assets/4x4checkerColor.png" type="Texture" id=4]
[ext_resource path="res://addons/gdhexgrid/icon.png" type="Texture" id=6]
[sub_resource type="CubeMesh" id=27]
size = Vector3( 1, 1, 1 )
@ -15,15 +16,10 @@ albedo_texture = ExtResource( 4 )
uv1_scale = Vector3( -3, -2, 0 )
uv1_offset = Vector3( 2, 2, 0 )
[sub_resource type="OpenSimplexNoise" id=29]
octaves = 1
period = 9.0
persistence = 0.0
[sub_resource type="CanvasItemMaterial" id=29]
blend_mode = 3
[sub_resource type="NoiseTexture" id=30]
width = 8
height = 8
noise = SubResource( 29 )
[sub_resource type="ImageTexture" id=30]
[node name="WorldChunk" type="Spatial"]
script = ExtResource( 1 )
@ -36,8 +32,8 @@ visible = false
mesh = SubResource( 27 )
material/0 = SubResource( 28 )
[node name="TileTypeOffscreenViewport" type="Viewport" parent="."]
size = Vector2( 8, 8 )
[node name="HeightmapOffscreenViewport" type="Viewport" parent="."]
size = Vector2( 50, 50 )
handle_input_locally = false
hdr = false
disable_3d = true
@ -49,19 +45,29 @@ shadow_atlas_quad_1 = 0
shadow_atlas_quad_2 = 0
shadow_atlas_quad_3 = 0
[node name="NoiseTexture" type="TextureRect" parent="TileTypeOffscreenViewport"]
visible = false
margin_right = 40.0
margin_bottom = 40.0
[node name="NoiseSprite" type="Sprite" parent="HeightmapOffscreenViewport"]
texture = ExtResource( 6 )
centered = false
[node name="NoiseMask" type="Sprite" parent="HeightmapOffscreenViewport"]
material = SubResource( 29 )
texture = SubResource( 30 )
centered = false
[node name="IslandShader" type="TextureRect" parent="TileTypeOffscreenViewport"]
[node name="TileTypeOffscreenViewport" type="Viewport" parent="."]
size = Vector2( 100, 100 )
handle_input_locally = false
hdr = false
disable_3d = true
usage = 0
render_target_v_flip = true
render_target_update_mode = 3
shadow_atlas_quad_0 = 0
shadow_atlas_quad_1 = 0
shadow_atlas_quad_2 = 0
shadow_atlas_quad_3 = 0
[node name="HeightmapSprite" type="Sprite" parent="TileTypeOffscreenViewport"]
material = ExtResource( 2 )
margin_right = 100.0
margin_bottom = 100.0
[node name="HeightmapTexture" type="TextureRect" parent="TileTypeOffscreenViewport/IslandShader"]
use_parent_material = true
margin_right = 100.0
margin_bottom = 100.0
texture = ExtResource( 3 )
centered = false