More Tile caching and tweaked export settings for Android.

WorldChunkRefactoring
Martin Felis 2022-12-03 23:45:32 +01:00
parent d673b8c44f
commit 63a11c603a
4 changed files with 78 additions and 29 deletions

View File

@ -120,5 +120,5 @@ common/enable_pause_aware_picking=true
[rendering] [rendering]
quality/directional_shadow/size=512 quality/shadows/filter_mode.mobile=1
environment/default_environment="res://default_env.tres" environment/default_environment="res://default_env.tres"

View File

@ -55,6 +55,7 @@ script = ExtResource( 3 )
[node name="DirectionalLight" type="DirectionalLight" parent="World"] [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 ) 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 shadow_enabled = true
directional_shadow_mode = 0
[node name="Control" type="Control" parent="."] [node name="Control" type="Control" parent="."]
margin_right = 40.0 margin_right = 40.0
@ -190,7 +191,7 @@ text = "0"
[node name="StreamContainer" type="Spatial" parent="."] [node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 4 ) script = ExtResource( 4 )
Dimensions = Vector2( 18, 10 ) Dimensions = Vector2( 18, 17 )
[node name="ActiveTiles" type="Spatial" parent="StreamContainer"] [node name="ActiveTiles" type="Spatial" parent="StreamContainer"]

View File

@ -39,11 +39,11 @@ public class HexTile3D : Spatial
set set
{ {
Cell.OffsetCoords = value; Cell.OffsetCoords = value;
Transform tile3dTransform = this.Transform; Transform tile3dTransform = Transform;
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(Cell); Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(Cell);
tile3dTransform.origin.x = cellPlaneCoords.x; tile3dTransform.origin.x = cellPlaneCoords.x;
tile3dTransform.origin.z = cellPlaneCoords.y; tile3dTransform.origin.z = cellPlaneCoords.y;
this.Transform = tile3dTransform; Transform = tile3dTransform;
} }
} }

View File

@ -1,7 +1,9 @@
using System; using System;
using Godot; using Godot;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using GoDotLog;
//using Dictionary = Godot.Collections.Dictionary; //using Dictionary = Godot.Collections.Dictionary;
@ -10,6 +12,7 @@ public class StreamContainer : Spatial
// scene nodes // scene nodes
private MeshInstance _bounds; private MeshInstance _bounds;
private Spatial _activeTiles; private Spatial _activeTiles;
private Queue<HexTile3D> _unusedTiles;
// resources // resources
private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn"); private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
@ -36,6 +39,7 @@ public class StreamContainer : Spatial
// 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()
{ {
_unusedTiles = new Queue<HexTile3D>();
_bounds = GetNode<MeshInstance>("Bounds"); _bounds = GetNode<MeshInstance>("Bounds");
_activeTiles = GetNode<Spatial>("ActiveTiles"); _activeTiles = GetNode<Spatial>("ActiveTiles");
_tileTypeRandom = new Random(); _tileTypeRandom = new Random();
@ -80,58 +84,102 @@ public class StreamContainer : Spatial
} }
} }
public void UpdateTileCache() public void UpdateTileCache()
{ {
RemovedCoords.Clear(); RemovedCoords.Clear();
AddedCoords.Clear(); AddedCoords.Clear();
_unusedTiles.Clear();
Rect2 expandedRect = _currentOffsetCoordRect.Merge(_oldOffsetCoordRect);
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), RemovedCoords.Add(tile3D.OffsetCoords);
Mathf.CeilToInt(expandedRect.Size.y))) _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); Vector2 coord = new Vector2(coord_x, coord_y);
if (clippedRect.HasPoint(coord))
{
continue;
}
bool isInCurrent = _currentOffsetCoordRect.HasPoint(coord); bool isInCurrent = _currentOffsetCoordRect.HasPoint(coord);
bool isInOld = _oldOffsetCoordRect.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); AddedCoords.Add(coord);
GetTile3dAt(coord); HexTile3D tile3D = GetTile3dAt(coord);
} }
else if (isInOld && !isInCurrent) else
{ {
RemovedCoords.Add(coord); HexTile3D tile3D = _unusedTiles.Dequeue();
if (_coordToTile.Keys.Contains(coord)) tile3D.OffsetCoords = coord;
{ _coordToTile[coord] = tile3D;
Spatial tile3d = _coordToTile[coord];
_activeTiles.RemoveChild(tile3d);
tile3d.QueueFree();
_coordToTile.Remove(coord);
}
} }
} }
} }
} }
public Spatial GetTile3dAt(Vector2 offsetCoords) public HexTile3D GetTile3dAt(Vector2 offsetCoords)
{ {
if (!_coordToTile.Keys.Contains(offsetCoords)) if (!_coordToTile.Keys.Contains(offsetCoords))
{ {
HexTile3D tile3d = (HexTile3D)_hexTileScene.Instance(); HexTile3D tile3D = (HexTile3D)_hexTileScene.Instance();
tile3d.OffsetCoords = offsetCoords; tile3D.OffsetCoords = offsetCoords;
_activeTiles.AddChild(tile3d); _activeTiles.AddChild(tile3D);
Transform tileTransform = tile3d.Transform; Transform tileTransform = tile3D.Transform;
tileTransform.origin.y = GD.Randf() * 0.2f; tileTransform.origin.y = GD.Randf() * 0.2f;
tile3d.Transform = tileTransform; tile3D.Transform = tileTransform;
tile3d.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)]; tile3D.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)];
_coordToTile[offsetCoords] = tile3d; _coordToTile[offsetCoords] = tile3D;
} }
return _coordToTile[offsetCoords]; return _coordToTile[offsetCoords];