From 63a11c603a53cd259f09a1f5c44f2123bbfd03f7 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 3 Dec 2022 23:45:32 +0100 Subject: [PATCH] More Tile caching and tweaked export settings for Android. --- project.godot | 2 +- scenes/AdaptiveWorldStream.tscn | 3 +- scenes/HexTile3D.cs | 4 +- scenes/StreamContainer.cs | 98 ++++++++++++++++++++++++--------- 4 files changed, 78 insertions(+), 29 deletions(-) diff --git a/project.godot b/project.godot index c6a9cdb..eb2ae4a 100644 --- a/project.godot +++ b/project.godot @@ -120,5 +120,5 @@ common/enable_pause_aware_picking=true [rendering] -quality/directional_shadow/size=512 +quality/shadows/filter_mode.mobile=1 environment/default_environment="res://default_env.tres" diff --git a/scenes/AdaptiveWorldStream.tscn b/scenes/AdaptiveWorldStream.tscn index dda4c69..949ab6b 100644 --- a/scenes/AdaptiveWorldStream.tscn +++ b/scenes/AdaptiveWorldStream.tscn @@ -55,6 +55,7 @@ script = ExtResource( 3 ) [node name="DirectionalLight" type="DirectionalLight" parent="World"] transform = Transform( 0.328059, -0.878387, 0.347583, 0, 0.367946, 0.929847, -0.944657, -0.305045, 0.120708, 0, 6.59293, 1.20265 ) shadow_enabled = true +directional_shadow_mode = 0 [node name="Control" type="Control" parent="."] margin_right = 40.0 @@ -190,7 +191,7 @@ text = "0" [node name="StreamContainer" type="Spatial" parent="."] script = ExtResource( 4 ) -Dimensions = Vector2( 18, 10 ) +Dimensions = Vector2( 18, 17 ) [node name="ActiveTiles" type="Spatial" parent="StreamContainer"] diff --git a/scenes/HexTile3D.cs b/scenes/HexTile3D.cs index e3fcc8e..9a9b516 100644 --- a/scenes/HexTile3D.cs +++ b/scenes/HexTile3D.cs @@ -39,11 +39,11 @@ public class HexTile3D : Spatial set { Cell.OffsetCoords = value; - Transform tile3dTransform = this.Transform; + Transform tile3dTransform = Transform; Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(Cell); tile3dTransform.origin.x = cellPlaneCoords.x; tile3dTransform.origin.z = cellPlaneCoords.y; - this.Transform = tile3dTransform; + Transform = tile3dTransform; } } diff --git a/scenes/StreamContainer.cs b/scenes/StreamContainer.cs index 40bcffc..73dbb13 100644 --- a/scenes/StreamContainer.cs +++ b/scenes/StreamContainer.cs @@ -1,7 +1,9 @@ using System; using Godot; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using GoDotLog; //using Dictionary = Godot.Collections.Dictionary; @@ -10,6 +12,7 @@ public class StreamContainer : Spatial // scene nodes private MeshInstance _bounds; private Spatial _activeTiles; + private Queue _unusedTiles; // resources private PackedScene _hexTileScene = GD.Load("res://scenes/HexTile3D.tscn"); @@ -36,6 +39,7 @@ public class StreamContainer : Spatial // Called when the node enters the scene tree for the first time. public override void _Ready() { + _unusedTiles = new Queue(); _bounds = GetNode("Bounds"); _activeTiles = GetNode("ActiveTiles"); _tileTypeRandom = new Random(); @@ -80,58 +84,102 @@ public class StreamContainer : Spatial } } - + public void UpdateTileCache() { RemovedCoords.Clear(); AddedCoords.Clear(); - - Rect2 expandedRect = _currentOffsetCoordRect.Merge(_oldOffsetCoordRect); + _unusedTiles.Clear(); + + Rect2 expandedRect = _currentOffsetCoordRect.Merge(_oldOffsetCoordRect).Grow(2); + Rect2 clippedRect = _currentOffsetCoordRect.Clip(_oldOffsetCoordRect); + + MarkUnusedTiles(expandedRect, clippedRect); + AddNewTiles(expandedRect, clippedRect); - foreach (int coord_x in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.x), Mathf.CeilToInt(expandedRect.Size.x))) + foreach (HexTile3D tile3D in _unusedTiles.ToArray()) { - foreach (int coord_y in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.y), - Mathf.CeilToInt(expandedRect.Size.y))) + RemovedCoords.Add(tile3D.OffsetCoords); + _activeTiles.RemoveChild(tile3D); + tile3D.QueueFree(); + } + } + + + public void MarkUnusedTiles(Rect2 expandedRect, Rect2 clippedRect) + { + foreach (int coord_x in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.x) - 5, + Mathf.CeilToInt(expandedRect.Size.x) + 20)) + { + foreach (int coord_y in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.y) - 5, + Mathf.CeilToInt(expandedRect.Size.y) + 20)) { Vector2 coord = new Vector2(coord_x, coord_y); + + if (clippedRect.HasPoint(coord)) + { + continue; + } + bool isInCurrent = _currentOffsetCoordRect.HasPoint(coord); bool isInOld = _oldOffsetCoordRect.HasPoint(coord); - if (isInCurrent && !isInOld) + if (isInOld && !isInCurrent && _coordToTile.Keys.Contains(coord)) + { + HexTile3D tile3D = _coordToTile[coord]; + _unusedTiles.Enqueue(tile3D); + _coordToTile.Remove(coord); + RemovedCoords.Add(coord); + } + } + } + } + + public void AddNewTiles(Rect2 expandedRect, Rect2 clippedRect) + { + foreach (int coord_x in Enumerable.Range(Mathf.FloorToInt(_currentOffsetCoordRect.Position.x), + Mathf.CeilToInt(_currentOffsetCoordRect.Size.x))) + { + foreach (int coord_y in Enumerable.Range(Mathf.FloorToInt(_currentOffsetCoordRect.Position.y), + Mathf.CeilToInt(_currentOffsetCoordRect.Size.y))) + { + Vector2 coord = new Vector2(coord_x, coord_y); + + if (clippedRect.HasPoint(coord)) + { + continue; + } + + if (_unusedTiles.Count == 0) { AddedCoords.Add(coord); - GetTile3dAt(coord); + HexTile3D tile3D = GetTile3dAt(coord); } - else if (isInOld && !isInCurrent) + else { - RemovedCoords.Add(coord); - if (_coordToTile.Keys.Contains(coord)) - { - Spatial tile3d = _coordToTile[coord]; - _activeTiles.RemoveChild(tile3d); - tile3d.QueueFree(); - _coordToTile.Remove(coord); - } + HexTile3D tile3D = _unusedTiles.Dequeue(); + tile3D.OffsetCoords = coord; + _coordToTile[coord] = tile3D; } } } } - public Spatial GetTile3dAt(Vector2 offsetCoords) + public HexTile3D GetTile3dAt(Vector2 offsetCoords) { if (!_coordToTile.Keys.Contains(offsetCoords)) { - HexTile3D tile3d = (HexTile3D)_hexTileScene.Instance(); - tile3d.OffsetCoords = offsetCoords; - _activeTiles.AddChild(tile3d); + HexTile3D tile3D = (HexTile3D)_hexTileScene.Instance(); + tile3D.OffsetCoords = offsetCoords; + _activeTiles.AddChild(tile3D); - Transform tileTransform = tile3d.Transform; + Transform tileTransform = tile3D.Transform; tileTransform.origin.y = GD.Randf() * 0.2f; - tile3d.Transform = tileTransform; + tile3D.Transform = tileTransform; - tile3d.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)]; - _coordToTile[offsetCoords] = tile3d; + tile3D.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)]; + _coordToTile[offsetCoords] = tile3D; } return _coordToTile[offsetCoords];