2023-10-05 18:17:48 +02:00
|
|
|
using System.Diagnostics;
|
2023-10-23 21:22:53 +02:00
|
|
|
using System.Linq;
|
2023-11-01 16:02:39 +01:00
|
|
|
using Godot;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
public class WorldChunk : Spatial
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
private Sprite _heightmapSprite;
|
|
|
|
private TextureRect _heightmapTextureRect;
|
|
|
|
private Sprite _noiseMask;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private Sprite _noiseSprite;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private readonly SpatialMaterial _rectMaterial = new();
|
|
|
|
private bool _showTextureOverlay;
|
|
|
|
[Export] public Vector2 ChunkIndex;
|
|
|
|
public Color DebugColor = Colors.White;
|
|
|
|
[Export] public Texture HeightMap;
|
|
|
|
public int HeightMapFrameCount;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
public Viewport HeightmapOffscreenViewport;
|
2023-10-05 18:17:48 +02:00
|
|
|
[Export] public Texture NavigationMap;
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
public bool NoiseTextureCheckerboardOverlay = true;
|
2023-10-27 22:56:49 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// signals
|
|
|
|
// delegate void OnCoordClicked(Vector2 world_pos);
|
|
|
|
|
|
|
|
// other members
|
|
|
|
public Rect2 PlaneRect;
|
2023-11-01 16:02:39 +01:00
|
|
|
// ui elements
|
|
|
|
|
|
|
|
// scene nodes
|
|
|
|
private MeshInstance PlaneRectMesh;
|
|
|
|
[Export] public int Size = 32;
|
|
|
|
|
|
|
|
// resources
|
|
|
|
|
|
|
|
// exports
|
|
|
|
[Export] public Texture TileTypeMap;
|
|
|
|
public int TileTypeMapFrameCount;
|
2023-10-23 21:22:53 +02:00
|
|
|
public Viewport TileTypeOffscreenViewport;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
public WorldChunk()
|
|
|
|
{
|
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
public WorldChunk(int size)
|
|
|
|
{
|
2023-10-23 21:22:53 +02:00
|
|
|
SetSize(size);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
[Export]
|
|
|
|
public bool ShowTextureOverlay
|
2023-10-23 21:22:53 +02:00
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
get => _showTextureOverlay;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
set
|
2023-10-23 21:22:53 +02:00
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
if (PlaneRectMesh != null) PlaneRectMesh.Visible = value;
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2023-10-23 21:22:53 +02:00
|
|
|
PlaneRectMesh = (MeshInstance)FindNode("PlaneRectMesh");
|
2023-10-05 18:17:48 +02:00
|
|
|
Debug.Assert(PlaneRectMesh != null);
|
2023-11-01 16:02:39 +01:00
|
|
|
if (PlaneRectMesh.Visible) _showTextureOverlay = true;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
var planeRectTransform = Transform.Identity;
|
2023-10-23 21:22:53 +02:00
|
|
|
planeRectTransform =
|
|
|
|
planeRectTransform.Scaled(new Vector3(PlaneRect.Size.x, 0.125f, PlaneRect.Size.y));
|
2023-10-05 18:17:48 +02:00
|
|
|
planeRectTransform.origin.x = PlaneRect.GetCenter().x;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
planeRectTransform.origin.z = PlaneRect.GetCenter().y;
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
PlaneRectMesh.Transform = planeRectTransform;
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
// PlaneRectMesh.MaterialOverride = new SpatialMaterial();
|
|
|
|
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).AlbedoColor = DebugColor;
|
|
|
|
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).FlagsTransparent = true;
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
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");
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
TileTypeOffscreenViewport = (Viewport)FindNode("TileTypeOffscreenViewport");
|
|
|
|
TileTypeOffscreenViewport.Size = Vector2.One * Size;
|
|
|
|
Debug.Assert(TileTypeOffscreenViewport != null);
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
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;
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetHeightmap(Texture texture)
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
_heightmapSprite.Texture = texture;
|
|
|
|
_heightmapSprite.Transform =
|
|
|
|
Transform2D.Identity.Scaled(TileTypeOffscreenViewport.Size / _heightmapSprite.Texture.GetSize());
|
|
|
|
|
|
|
|
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
|
|
|
|
TileTypeMapFrameCount = 1;
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
|
|
|
Texture tileTypeTexture = TileTypeOffscreenViewport.GetTexture();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
if (NoiseTextureCheckerboardOverlay)
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
var tileTypeImage = tileTypeTexture.GetData();
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeImage.Lock();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
foreach (var i in Enumerable.Range(0, Size))
|
|
|
|
foreach (var j in Enumerable.Range(0, Size))
|
2023-10-23 21:22:53 +02:00
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
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);
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeImage.Unlock();
|
2023-11-01 16:02:39 +01:00
|
|
|
var imageTexture = new ImageTexture();
|
|
|
|
imageTexture.CreateFromImage(tileTypeImage, 0);
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeTexture = imageTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
_rectMaterial.AlbedoTexture = tileTypeTexture;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
_rectMaterial.FlagsTransparent = true;
|
|
|
|
// _rectMaterial.AlbedoTexture = _heightmapRect.Texture;
|
|
|
|
_rectMaterial.Uv1Scale = new Vector3(-3, -2, 1);
|
|
|
|
_rectMaterial.Uv1Offset = Vector3.One * 2;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
//RectMaterial.Uv1Triplanar = true;
|
|
|
|
PlaneRectMesh.SetSurfaceMaterial(0, _rectMaterial);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
PlaneRectMesh.MaterialOverride = null;
|
|
|
|
}
|
|
|
|
}
|