-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 929c178
Showing
14 changed files
with
647 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Godot-specific ignores | ||
.import/ | ||
export.cfg | ||
export_presets.cfg | ||
|
||
# Imported translations (automatically generated from CSV files) | ||
*.translation | ||
|
||
# Mono-specific ignores | ||
.mono/ | ||
data_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
[gd_scene load_steps=7 format=2] | ||
|
||
[ext_resource path="res://render.shader" type="Shader" id=1] | ||
[ext_resource path="res://cellular_automata.gd" type="Script" id=2] | ||
[ext_resource path="res://world.png" type="Texture" id=3] | ||
[ext_resource path="res://simulation.shader" type="Shader" id=4] | ||
|
||
[sub_resource type="ShaderMaterial" id=1] | ||
shader = ExtResource( 4 ) | ||
|
||
[sub_resource type="ShaderMaterial" id=2] | ||
shader = ExtResource( 1 ) | ||
|
||
[node name="CellularAutomata" type="Node"] | ||
script = ExtResource( 2 ) | ||
|
||
[node name="CanvasLayer" type="CanvasLayer" parent="."] | ||
|
||
[node name="GUI" type="MarginContainer" parent="CanvasLayer"] | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
custom_constants/margin_right = 20 | ||
custom_constants/margin_top = 20 | ||
custom_constants/margin_left = 20 | ||
custom_constants/margin_bottom = 20 | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Selection" type="PanelContainer" parent="CanvasLayer/GUI"] | ||
margin_left = 478.0 | ||
margin_top = 20.0 | ||
margin_right = 546.0 | ||
margin_bottom = 50.0 | ||
size_flags_horizontal = 4 | ||
size_flags_vertical = 0 | ||
|
||
[node name="Material" type="HBoxContainer" parent="CanvasLayer/GUI/Selection"] | ||
margin_left = 7.0 | ||
margin_top = 7.0 | ||
margin_right = 61.0 | ||
margin_bottom = 23.0 | ||
alignment = 1 | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Color" type="ColorRect" parent="CanvasLayer/GUI/Selection/Material"] | ||
margin_right = 16.0 | ||
margin_bottom = 16.0 | ||
rect_min_size = Vector2( 16, 16 ) | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Label" type="Label" parent="CanvasLayer/GUI/Selection/Material"] | ||
margin_left = 20.0 | ||
margin_top = 1.0 | ||
margin_right = 54.0 | ||
margin_bottom = 15.0 | ||
text = "Label" | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Simulation" type="ViewportContainer" parent="."] | ||
modulate = Color( 0, 0, 0, 0 ) | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Viewport" type="Viewport" parent="Simulation"] | ||
size = Vector2( 1024, 600 ) | ||
transparent_bg = true | ||
handle_input_locally = false | ||
disable_3d = true | ||
keep_3d_linear = true | ||
usage = 0 | ||
render_target_v_flip = true | ||
render_target_clear_mode = 1 | ||
render_target_update_mode = 3 | ||
gui_disable_input = true | ||
|
||
[node name="World" type="Sprite" parent="Simulation/Viewport"] | ||
material = SubResource( 1 ) | ||
texture = ExtResource( 3 ) | ||
centered = false | ||
|
||
[node name="Brush" type="Polygon2D" parent="Simulation/Viewport"] | ||
visible = false | ||
polygon = PoolVector2Array( -5, -5, 5, -5, 5, 5, -5, 5 ) | ||
|
||
[node name="Render" type="Node2D" parent="."] | ||
material = SubResource( 2 ) | ||
|
||
[node name="World" type="Sprite" parent="Render"] | ||
use_parent_material = true | ||
z_index = 1 | ||
centered = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Fabio Iotti | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
GPU Cellular Automata Template | ||
============================== | ||
|
||
GPU-accelerated cellular automata template in Godot. | ||
|
||
Uses GLSL3, but it should be possible to run it on GLSL2 with minor tweaks. | ||
|
||
![](screen/screen1.jpg) | ||
|
||
## Controls | ||
|
||
Scroll wheel = select material | ||
|
||
Click = place material | ||
|
||
Enter = pause/play | ||
|
||
Space = step once | ||
|
||
## How it works | ||
|
||
The current state of the simulation is stored in a texture, rendered in a special | ||
viewport named `Simulation/Viewport`. | ||
|
||
A [simulation.shader](simulation.shader) reads the viewport to compute the next | ||
frame and writes the result back to the same viewport. | ||
|
||
Finally, the result is shaded through the [render.shader](render.shader) and | ||
presented on the screen. | ||
|
||
Setup and input are handled from [cellular_automata.gd](cellular_automata.gd). | ||
|
||
## License | ||
|
||
Copyright (c) 2021 Fabio Iotti. Released under the [MIT License](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
extends Node | ||
|
||
const materials = [ | ||
{ "color": Color8( 0, 0, 0, 255), "name": "Wall" }, | ||
{ "color": Color8(156, 68, 0, 255), "name": "Dirt" }, | ||
{ "color": Color8(134, 134, 134, 255), "name": "Stone" }, | ||
{ "color": Color8( 32, 125, 253, 255), "name": "Water" }, | ||
{ "color": Color8(207, 156, 110, 255), "name": "Sand" }, | ||
{ "color": Color8( 95, 20, 0, 255), "name": "Wood" }, | ||
{ "color": Color8( 0, 95, 0, 255), "name": "Grass" }, | ||
{ "color": Color8(255, 78, 0, 255), "name": "Lava" }, | ||
{ "color": Color8(255, 0, 0, 255), "name": "Fire" }, | ||
{ "color": Color8(136, 164, 201, 255), "name": "Steam" }, | ||
]; | ||
|
||
var _first_frame_rendered = false | ||
var _material_opacity = 0 | ||
|
||
func _ready(): | ||
# The simulation computes the cellular automata, but doesn't draw anything | ||
# on the screen. The "Render" sprite renders a copy of the simulation. | ||
$Render/World.texture = $Simulation/Viewport.get_texture() | ||
|
||
# Initially hide material selection label. | ||
$CanvasLayer/GUI/Selection.modulate.a8 = 0 | ||
|
||
func _process(delta): | ||
if !_first_frame_rendered: | ||
_first_frame_rendered = true | ||
else: | ||
# After the first frame is rendered, assign viewport back to itself, | ||
# so that next frame can be computed from the previous. | ||
$Simulation/Viewport/World.texture = $Simulation/Viewport.get_texture() | ||
|
||
# Fade out material selection panel. | ||
if _material_opacity > 0: | ||
_material_opacity = _material_opacity - 3 | ||
$CanvasLayer/GUI/Selection.modulate.a8 = clamp(_material_opacity, 0, 255) | ||
|
||
func _input(event): | ||
# Brush follows mouse movements. | ||
if event is InputEventMouseMotion: | ||
$Simulation/Viewport/Brush.position = get_viewport().get_mouse_position() | ||
|
||
elif event is InputEventKey: | ||
if event.pressed: | ||
if event.scancode == KEY_SPACE: | ||
$Simulation/Viewport.render_target_update_mode = Viewport.UPDATE_ONCE | ||
elif event.scancode == KEY_ENTER: | ||
var is_paused = $Simulation/Viewport.render_target_update_mode != Viewport.UPDATE_ALWAYS | ||
var new_mode = Viewport.UPDATE_ALWAYS if is_paused else Viewport.UPDATE_DISABLED | ||
$Simulation/Viewport.render_target_update_mode = new_mode | ||
|
||
elif event is InputEventMouseButton: | ||
# Mouse wheel to change material. | ||
if event.button_index == BUTTON_WHEEL_UP || event.button_index == BUTTON_WHEEL_DOWN: | ||
if event.pressed: | ||
var current_material = 0 | ||
for i in range(materials.size()): | ||
if materials[i].color == $Simulation/Viewport/Brush.color: | ||
current_material = i | ||
break | ||
|
||
var offset = -1 if event.button_index == BUTTON_WHEEL_UP else +1 | ||
var set_material = (current_material + offset) % materials.size() | ||
|
||
$Simulation/Viewport/Brush.color = materials[set_material].color | ||
$CanvasLayer/GUI/Selection/Material/Color.color = materials[set_material].color | ||
$CanvasLayer/GUI/Selection/Material/Label.text = materials[set_material].name | ||
|
||
_material_opacity = 400 | ||
|
||
# Click to paint. | ||
elif event.button_index == BUTTON_LEFT: | ||
$Simulation/Viewport/Brush.visible = event.pressed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[remap] | ||
|
||
importer="texture" | ||
type="StreamTexture" | ||
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" | ||
metadata={ | ||
"vram_texture": false | ||
} | ||
|
||
[deps] | ||
|
||
source_file="res://icon.png" | ||
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] | ||
|
||
[params] | ||
|
||
compress/mode=0 | ||
compress/lossy_quality=0.7 | ||
compress/hdr_mode=0 | ||
compress/bptc_ldr=0 | ||
compress/normal_map=0 | ||
flags/repeat=0 | ||
flags/filter=true | ||
flags/mipmaps=false | ||
flags/anisotropic=false | ||
flags/srgb=2 | ||
process/fix_alpha_border=true | ||
process/premult_alpha=false | ||
process/HDR_as_SRGB=false | ||
process/invert_color=false | ||
stream=false | ||
size_limit=0 | ||
detect_3d=true | ||
svg/scale=1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
; 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=4 | ||
|
||
_global_script_classes=[ ] | ||
_global_script_class_icons={ | ||
|
||
} | ||
|
||
[application] | ||
|
||
config/name="GPU Cellular Automata" | ||
run/main_scene="res://CellularAutomata.tscn" | ||
config/icon="res://icon.png" | ||
|
||
[display] | ||
|
||
window/stretch/mode="2d" | ||
window/stretch/aspect="keep" | ||
|
||
[rendering] | ||
|
||
vram_compression/import_etc=true | ||
vram_compression/import_etc2=false | ||
environment/default_clear_color=Color( 0.709804, 0.94902, 1, 1 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
shader_type canvas_item; | ||
|
||
const vec4 black = vec4(0, 0, 0, 1); | ||
const vec4 white = vec4(1, 1, 1, 1); | ||
|
||
// Random number generator. | ||
float rand(vec2 coords) { | ||
return fract(sin(dot(coords, vec2(12.9898,78.233))) * 43758.5453); | ||
} | ||
|
||
void fragment() { | ||
// Get the color of this pixel and its neighbours. | ||
vec4 here = texture(TEXTURE, UV); | ||
vec4 up = texture(TEXTURE, UV + vec2(0, -1) * TEXTURE_PIXEL_SIZE); | ||
vec4 down = texture(TEXTURE, UV + vec2(0, +1) * TEXTURE_PIXEL_SIZE); | ||
|
||
// Use the color of this pixel. | ||
COLOR = here; | ||
|
||
// Randomize the color a bit, to make it more interesting. | ||
vec2 coords = UV - mod(UV, TEXTURE_PIXEL_SIZE); | ||
COLOR = mix(COLOR, black, rand(coords) * 0.1); | ||
|
||
// If the materia below is different, make it a little darker. | ||
if (here != down) | ||
COLOR = mix(COLOR, black, 0.2); | ||
|
||
// Or else, if the material above is different, make it a litte lighter. | ||
else if (here != up) | ||
COLOR = mix(COLOR, white, 0.2); | ||
} |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.