diff --git a/game-of-life-test/.editorconfig b/game-of-life-test/.editorconfig new file mode 100644 index 0000000..f28239b --- /dev/null +++ b/game-of-life-test/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +charset = utf-8 diff --git a/game-of-life-test/.gitattributes b/game-of-life-test/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/game-of-life-test/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/game-of-life-test/.gitignore b/game-of-life-test/.gitignore new file mode 100644 index 0000000..0af181c --- /dev/null +++ b/game-of-life-test/.gitignore @@ -0,0 +1,3 @@ +# Godot 4+ specific ignores +.godot/ +/android/ diff --git a/game-of-life-test/black_sq.png b/game-of-life-test/black_sq.png new file mode 100644 index 0000000..067d61d Binary files /dev/null and b/game-of-life-test/black_sq.png differ diff --git a/game-of-life-test/black_sq.png.import b/game-of-life-test/black_sq.png.import new file mode 100644 index 0000000..183b528 --- /dev/null +++ b/game-of-life-test/black_sq.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://c7assq3lqgw8x" +path="res://.godot/imported/black_sq.png-43fbf60d129e97f2df8400c8ac3589c6.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://black_sq.png" +dest_files=["res://.godot/imported/black_sq.png-43fbf60d129e97f2df8400c8ac3589c6.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/game-of-life-test/dot_matrix.gdshader b/game-of-life-test/dot_matrix.gdshader new file mode 100644 index 0000000..0bf43fb --- /dev/null +++ b/game-of-life-test/dot_matrix.gdshader @@ -0,0 +1,36 @@ +shader_type canvas_item; + +uniform sampler2D unlitTex; +uniform sampler2D litTex; +uniform sampler2D binDataTex; +uniform int n; + +const int cellSize = 8; + +void vertex() { + // Called for every vertex the material is visible on. +} + +void fragment() { + // Called for every pixel the material is visible on. + vec2 totalGridSize = vec2(float(n) * float(cellSize)); + + vec2 scaledUV = UV * float(n); + ivec2 cellIdx = ivec2(floor(scaledUV)); + vec2 cellUV = fract(scaledUV); + + float binVal = texelFetch(binDataTex, cellIdx, 0).r; + bool isWhite = binVal > 0.5; + + vec4 color = texture(unlitTex, cellUV); + if (isWhite) { + color = texture(litTex, cellUV); + } + + COLOR = color; +} + +//void light() { +// // Called for every pixel for every light affecting the material. +// // Uncomment to replace the default light processing function with this one. +//} diff --git a/game-of-life-test/dot_matrix.gdshader.uid b/game-of-life-test/dot_matrix.gdshader.uid new file mode 100644 index 0000000..3ed9ebf --- /dev/null +++ b/game-of-life-test/dot_matrix.gdshader.uid @@ -0,0 +1 @@ +uid://cidrd2kca02ko diff --git a/game-of-life-test/dot_matrix.tscn b/game-of-life-test/dot_matrix.tscn new file mode 100644 index 0000000..0946cf7 --- /dev/null +++ b/game-of-life-test/dot_matrix.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=7 format=3 uid="uid://bawvofeipkkmn"] + +[ext_resource type="Texture2D" uid="uid://dma7owg4valo7" path="res://icon.svg" id="1_4fmrq"] +[ext_resource type="Shader" uid="uid://cidrd2kca02ko" path="res://dot_matrix.gdshader" id="1_wjpme"] +[ext_resource type="Texture2D" uid="uid://csm64p7o7eyc6" path="res://white_sq.png" id="2_og22u"] +[ext_resource type="Texture2D" uid="uid://c7assq3lqgw8x" path="res://black_sq.png" id="3_b70w7"] +[ext_resource type="Script" uid="uid://cgwkfjisbqbwt" path="res://gol.gd" id="5_og22u"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_wvg0l"] +shader = ExtResource("1_wjpme") +shader_parameter/unlitTex = ExtResource("3_b70w7") +shader_parameter/litTex = ExtResource("2_og22u") +shader_parameter/n = 64 + +[node name="Dot Matrix" type="Node2D"] + +[node name="Renderer" type="Sprite2D" parent="."] +material = SubResource("ShaderMaterial_wvg0l") +position = Vector2(960, 960) +scale = Vector2(15, 15) +texture = ExtResource("1_4fmrq") +script = ExtResource("5_og22u") +metadata/_edit_lock_ = true diff --git a/game-of-life-test/gol.gd b/game-of-life-test/gol.gd new file mode 100644 index 0000000..5882f2b --- /dev/null +++ b/game-of-life-test/gol.gd @@ -0,0 +1,104 @@ +extends Sprite2D + +var n: int = 64 +var arr := [] + +var data_img: Image +var data_tex: ImageTexture + +@onready var mat: ShaderMaterial = material as ShaderMaterial + +# sim. consts +var T := 0.1 +var t := 0.0 + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + data_img = Image.create(n, n, false, Image.FORMAT_R8) + data_img.fill(Color8(0, 0, 0, 255)) + + data_tex = ImageTexture.create_from_image(data_img) + mat.set_shader_parameter("binDataTex", data_tex) + mat.set_shader_parameter("n", n) + + # init + _fill_example() + _upload_arr() + + +func _upload_arr() -> void: + for y in n: + for x in n: + var v := int(arr[y][x]) * 255 + data_img.set_pixel(x, y, Color8(v, 0, 0, 255)) + data_tex.update(data_img) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(_delta: float) -> void: + t += _delta + if t >= T: + t = 0.0 + _game_of_life_step() + _upload_arr() + +func _game_of_life_step() -> void: + var next := [] + for y in n: + var row := [] + for x in n: + var alive: bool = arr[y][x] == 1 + var cn := _count_neighs(x, y) + + var newv := 0 + if alive: + newv = int(cn == 2 or cn == 3) + else: + newv = int(cn == 3) + row.append(newv) + next.append(row) + arr = next + +func _count_neighs(x:int, y:int) -> int: + var ans := 0 + for _dy in 3: + var dy := _dy-1 + for _dx in 3: + var dx := _dx-1 + if dx == 0 and dy == 0: + continue + var nx := int((x + dx + n)%n) + var ny := int((y + dy + n)%n) + ans += arr[ny][nx] + return ans + + +func _fill_example() -> void: + arr.clear() + #_checkerboard() + _ship() + +# inits +func _checkerboard() -> void: + for y in n: + var row := [] + for x in n: + row.append((x + y) % 2) # checkerboard + arr.append(row) + +func _ship() -> void: + var i: int = 0 + var j: int = 0 + while (i < n): + var row := [] + j = 0 + while (j < n): + row.append(0) + j+=1 + arr.append(row) + i+=1 + + arr[0][1] = 1 + arr[1][2] = 1 + arr[2][0] = 1 + arr[2][1] = 1 + arr[2][2] = 1 diff --git a/game-of-life-test/gol.gd.uid b/game-of-life-test/gol.gd.uid new file mode 100644 index 0000000..02f9ed2 --- /dev/null +++ b/game-of-life-test/gol.gd.uid @@ -0,0 +1 @@ +uid://cgwkfjisbqbwt diff --git a/game-of-life-test/icon.svg b/game-of-life-test/icon.svg new file mode 100644 index 0000000..c6bbb7d --- /dev/null +++ b/game-of-life-test/icon.svg @@ -0,0 +1 @@ + diff --git a/game-of-life-test/icon.svg.import b/game-of-life-test/icon.svg.import new file mode 100644 index 0000000..6320143 --- /dev/null +++ b/game-of-life-test/icon.svg.import @@ -0,0 +1,43 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dma7owg4valo7" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/game-of-life-test/project.godot b/game-of-life-test/project.godot new file mode 100644 index 0000000..542da1c --- /dev/null +++ b/game-of-life-test/project.godot @@ -0,0 +1,21 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="GameOfLifeTest" +run/main_scene="uid://bawvofeipkkmn" +config/features=PackedStringArray("4.5", "Forward Plus") +config/icon="res://icon.svg" + +[display] + +window/size/viewport_width=1920 +window/size/viewport_height=1920 diff --git a/game-of-life-test/white_sq.png b/game-of-life-test/white_sq.png new file mode 100644 index 0000000..7f9d13d Binary files /dev/null and b/game-of-life-test/white_sq.png differ diff --git a/game-of-life-test/white_sq.png.import b/game-of-life-test/white_sq.png.import new file mode 100644 index 0000000..fb27e02 --- /dev/null +++ b/game-of-life-test/white_sq.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://csm64p7o7eyc6" +path="res://.godot/imported/white_sq.png-7baf4040d7b03bc76a02f41aa766a5a0.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://white_sq.png" +dest_files=["res://.godot/imported/white_sq.png-7baf4040d7b03bc76a02f41aa766a5a0.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1