2023-10-05 18:17:48 +02:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2023-10-23 21:22:53 +02:00
|
|
|
using System.Linq;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
public class WorldChunk : Spatial
|
|
|
|
{
|
|
|
|
// ui elements
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// scene nodes
|
|
|
|
private MeshInstance PlaneRectMesh;
|
|
|
|
|
|
|
|
// resources
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// exports
|
|
|
|
[Export] public Texture TileTypeMap;
|
|
|
|
[Export] public Texture NavigationMap;
|
|
|
|
[Export] public Texture HeightMap;
|
2023-10-23 21:22:53 +02:00
|
|
|
[Export] public int Size = 32;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
[Export] public Vector2 ChunkAddress;
|
|
|
|
// [Export] public Vector2 Size = new Vector2(1, 1);
|
|
|
|
|
|
|
|
// signals
|
|
|
|
// delegate void OnCoordClicked(Vector2 world_pos);
|
|
|
|
|
|
|
|
// other members
|
|
|
|
public Rect2 PlaneRect;
|
|
|
|
public Color DebugColor = Colors.White;
|
2023-10-23 21:22:53 +02:00
|
|
|
public Viewport TileTypeOffscreenViewport;
|
|
|
|
public bool NoiseTextureCheckerboardOverlay = true;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
private TextureRect _heightmapRect;
|
|
|
|
private SpatialMaterial _rectMaterial = new SpatialMaterial();
|
|
|
|
|
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
|
|
|
|
|
|
|
public void SetSize(int size)
|
|
|
|
{
|
|
|
|
Size = size;
|
|
|
|
|
|
|
|
if (TileTypeOffscreenViewport != null)
|
|
|
|
{
|
|
|
|
TileTypeOffscreenViewport.Size = Vector2.One * size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// other members
|
|
|
|
public void SaveToFile(String chunkName)
|
|
|
|
{
|
|
|
|
Image image = new Image();
|
2023-10-23 21:22:53 +02:00
|
|
|
|
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, TileTypeMap.GetData().GetData());
|
2023-10-05 18:17:48 +02:00
|
|
|
image.SavePng(chunkName + "_tileType.png");
|
2023-10-23 21:22:53 +02:00
|
|
|
|
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, NavigationMap.GetData().GetData());
|
2023-10-05 18:17:48 +02:00
|
|
|
image.SavePng(chunkName + "_navigationMap.png");
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, HeightMap.GetData().GetData());
|
2023-10-05 18:17:48 +02:00
|
|
|
image.SavePng(chunkName + "_heightMap.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadFromFile(String chunkName)
|
|
|
|
{
|
|
|
|
}
|
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-10-23 21:22:53 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
Transform 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-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;
|
|
|
|
|
|
|
|
TileTypeOffscreenViewport = (Viewport)FindNode("TileTypeOffscreenViewport");
|
|
|
|
TileTypeOffscreenViewport.Size = Vector2.One * Size;
|
|
|
|
Debug.Assert(TileTypeOffscreenViewport != null);
|
|
|
|
|
|
|
|
_heightmapRect = (TextureRect)FindNode("HeightmapTexture");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetHeightmap(Texture texture)
|
|
|
|
{
|
|
|
|
GD.Print("Setting HeightmapRect Texture: " + _heightmapRect + " with size " + _heightmapRect.GetSize());
|
|
|
|
_heightmapRect.Texture = texture;
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
// 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();
|
|
|
|
tileTypeImage.Lock();
|
|
|
|
|
|
|
|
foreach (int i in Enumerable.Range(0, Size))
|
|
|
|
{
|
|
|
|
foreach (int j in Enumerable.Range(0, Size))
|
|
|
|
{
|
|
|
|
Vector2 textureCoord = new Vector2(i, j);
|
|
|
|
Color 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);
|
|
|
|
tileTypeTexture = imageTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
_rectMaterial.AlbedoTexture = tileTypeTexture;
|
|
|
|
|
|
|
|
_rectMaterial.FlagsTransparent = true;
|
|
|
|
// _rectMaterial.AlbedoTexture = _heightmapRect.Texture;
|
|
|
|
_rectMaterial.Uv1Scale = new Vector3(-3, -2, 1);
|
|
|
|
_rectMaterial.Uv1Offset = Vector3.One * 2;
|
|
|
|
|
|
|
|
//RectMaterial.Uv1Triplanar = true;
|
|
|
|
PlaneRectMesh.SetSurfaceMaterial(0, _rectMaterial);
|
|
|
|
|
|
|
|
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
|
|
|
|
PlaneRectMesh.MaterialOverride = null;
|
|
|
|
}
|
|
|
|
}
|