Compare commits

..

5 Commits

Author SHA1 Message Date
Cat Flynn 7333b14593 add basic heat transfer #4 2022-12-07 00:08:38 +00:00
Cat Flynn 500fa6ddb8 generalise shader #4 2022-12-07 00:08:38 +00:00
Cat Flynn d044beb3d5 add tick clock and tick counter UI #4 2022-12-07 00:08:38 +00:00
Cat Flynn 4090ac732e extract struct to file 2022-12-07 00:08:38 +00:00
ktyl 424d6e4b98 grid (#9)
Reviewed-on: #9
2022-12-07 01:06:04 +01:00
6 changed files with 90 additions and 25 deletions

View File

@ -4,6 +4,6 @@
[resource] [resource]
shader = ExtResource( 1 ) shader = ExtResource( 1 )
shader_param/baseColor = Color( 0.992157, 1, 0, 1 ) shader_param/lowColor = Color( 1, 0.960784, 0, 1 )
shader_param/highlightColor = Color( 1, 0, 0.984314, 1 ) shader_param/highColor = Color( 0.921569, 0, 1, 1 )
shader_param/isHighlighted = 0 shader_param/t = null

View File

@ -5,6 +5,6 @@
[resource] [resource]
resource_local_to_scene = true resource_local_to_scene = true
shader = ExtResource( 1 ) shader = ExtResource( 1 )
shader_param/baseColor = Color( 0.0823529, 0, 1, 1 ) shader_param/lowColor = Color( 0, 0, 0, 1 )
shader_param/highlightColor = Color( 1, 0, 0, 1 ) shader_param/highColor = Color( 1, 0.560784, 0, 1 )
shader_param/isHighlighted = null shader_param/t = null

View File

@ -16,4 +16,5 @@ Grid = NodePath("../Grid")
[node name="Clock" parent="." instance=ExtResource( 4 )] [node name="Clock" parent="." instance=ExtResource( 4 )]
[connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"]
[connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"] [connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"]

View File

@ -8,18 +8,18 @@ public class GridCursor : Sprite
private WorldGrid _grid; private WorldGrid _grid;
private ShaderMaterial _material; private ShaderMaterial _material;
private const string IS_HIGHLIGHTED = "isHighlighted"; private const string T = "t";
public override void _Ready() public override void _Ready()
{ {
_grid = GetNode<WorldGrid>(Grid); _grid = GetNode<WorldGrid>(Grid);
_material = (ShaderMaterial)Material; _material = (ShaderMaterial)Material;
} }
public override void _Input(InputEvent @event) public override void _Input(InputEvent @event)
{ {
base._Input(@event); base._Input(@event);
if (@event is InputEventMouseMotion mouseMoveEvent) if (@event is InputEventMouseMotion mouseMoveEvent)
{ {
var pos = mouseMoveEvent.Position; var pos = mouseMoveEvent.Position;
@ -37,12 +37,12 @@ public class GridCursor : Sprite
{ {
var pos = mouseButtonEvent.Position; var pos = mouseButtonEvent.Position;
_grid.GetGridPos(pos, out var x, out var y); _grid.GetGridPos(pos, out var x, out var y);
_grid.ToggleTileHighlight(x, y); _grid.SetTileValue(x, y, 1.0f);
_material.SetShaderParam(IS_HIGHLIGHTED, 1); _material.SetShaderParam(T, 1f);
} }
else else
{ {
_material.SetShaderParam(IS_HIGHLIGHTED, 0); _material.SetShaderParam(T, 0f);
} }
break; break;
} }

View File

@ -1,5 +1,6 @@
using Godot; using Godot;
using System; using System;
using System.Collections.Generic;
public class WorldGrid : Node2D public class WorldGrid : Node2D
{ {
@ -8,11 +9,15 @@ public class WorldGrid : Node2D
[Export] [Export]
public int Size { get; set; } public int Size { get; set; }
[Export] [Export]
public int CellSize { get; set; } public int CellSize { get; set; }
[Export]
public float DiffusionCoefficient { get; set; } = 0.01f;
private Tile[,] _tiles; private Tile[,] _tiles;
private float[,] _nextValues = null;
// 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()
@ -23,18 +28,18 @@ public class WorldGrid : Node2D
public override void _Process(float delta) public override void _Process(float delta)
{ {
base._Process(delta); base._Process(delta);
for (int x = 0; x < Size; x++) for (int x = 0; x < Size; x++)
{ {
for (int y = 0; y < Size; y++) for (int y = 0; y < Size; y++)
{ {
var tile = _tiles[x, y]; var tile = _tiles[x, y];
var material = tile.material; var material = tile.material;
material.SetShaderParam("isHighlighted", tile.isHighlighted ? 1 : 0); material.SetShaderParam("t", tile.value);
} }
} }
} }
public bool IsInBounds(int x, int y) public bool IsInBounds(int x, int y)
{ {
return x >= 0 && x < Size && y >= 0 && y < Size; return x >= 0 && x < Size && y >= 0 && y < Size;
@ -47,7 +52,7 @@ public class WorldGrid : Node2D
_tiles[x, y].isHighlighted ^= true; _tiles[x, y].isHighlighted ^= true;
} }
public void SetTileValue(int x, int y, float value) public void SetTileValue(int x, int y, float value)
{ {
_tiles[x, y].value = value; _tiles[x, y].value = value;
@ -75,8 +80,9 @@ public class WorldGrid : Node2D
private void GenerateGrid(int size) private void GenerateGrid(int size)
{ {
this.Clear(); this.Clear();
_tiles = new Tile[size, size]; _tiles = new Tile[size, size];
_nextValues = new float[size, size];
for (int x = 0; x < size; x++) for (int x = 0; x < size; x++)
{ {
@ -85,7 +91,7 @@ public class WorldGrid : Node2D
var tile = new Tile(); var tile = new Tile();
tile.isHighlighted = false; tile.isHighlighted = false;
tile.value = 0.0f; tile.value = 0.0f;
var node = TileScene.Instance<Node2D>(); var node = TileScene.Instance<Node2D>();
this.AddChild(node); this.AddChild(node);
var position = new Vector2 var position = new Vector2
@ -96,9 +102,67 @@ public class WorldGrid : Node2D
node.Position = position; node.Position = position;
var canvasItem = (CanvasItem)node; var canvasItem = (CanvasItem)node;
tile.material = (ShaderMaterial)canvasItem.Material; tile.material = (ShaderMaterial)canvasItem.Material;
_tiles[x, y] = tile; _tiles[x, y] = tile;
} }
} }
} }
public void _on_Clock_OnTick(int ticks)
{
Diffuse();
}
private float TransferHeat(float t0, float alpha, float nx, float ny, float px, float py)
{
float d2tdx2 = nx - 2 * t0 + px;
float d2tdy2 = ny - 2 * t0 + py;
return t0 + alpha * (d2tdx2 + d2tdy2);
}
private void GetNeighbourTemperatures(int x, int y, out float nx, out float ny, out float px, out float py)
{
// default value
var t = _tiles[x, y].value;
nx = x > 0 ? _tiles[x - 1, y].value : t;
px = x < Size - 1 ? _tiles[x + 1, y].value : t;
ny = y > 0 ? _tiles[x, y - 1].value : t;
py = y < Size - 1 ? _tiles[x, y + 1].value : t;
}
private void Diffuse()
{
for (int x = 0; x < Size; x++)
{
for (int y = 0; y < Size; y++)
{
float t = _tiles[x, y].value;
var D = DiffusionCoefficient;
GetNeighbourTemperatures(
x, y,
out var nx,
out var ny,
out var px,
out var py);
// current value
_nextValues[x, y] = TransferHeat(t, D, nx, ny, px, py);
}
}
float sum = 0f;
for (int x = 0; x < Size; x++)
{
for (int y = 0; y < Size; y++)
{
_tiles[x, y].value = _nextValues[x, y];
sum += _tiles[x, y].value;
}
}
GD.Print(sum);
}
} }

View File

@ -1,9 +1,9 @@
shader_type canvas_item; shader_type canvas_item;
uniform vec4 baseColor : hint_color; uniform vec4 lowColor : hint_color;
uniform vec4 highlightColor : hint_color; uniform vec4 highColor : hint_color;
uniform int isHighlighted; uniform float t;
void fragment() { void fragment() {
COLOR = mix(baseColor, highlightColor, float(isHighlighted)); COLOR = mix(lowColor, highColor, t);
} }