From d2cef63a126aa630e508ad8b79c194f38b217cc3 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 25 Aug 2022 15:33:44 +0200 Subject: [PATCH] Experimenting with 3D Hex world --- addons/gdhexgrid/demo_3d.tscn | 2 +- project.godot | 23 +++++++++++++ scenes/DebugCamera.gd | 28 ++++++++++++++++ scenes/HexGrid3DTest.gd | 26 +++++++++++++++ scenes/HexGrid3DTest.tscn | 61 +++++++++++++++++++++++++++++++++++ scenes/HexTile3D.gd | 17 ++++++++++ scenes/HexTile3D.tscn | 32 ++++++++++++++++++ 7 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 scenes/DebugCamera.gd create mode 100644 scenes/HexGrid3DTest.gd create mode 100644 scenes/HexGrid3DTest.tscn create mode 100644 scenes/HexTile3D.gd create mode 100644 scenes/HexTile3D.tscn diff --git a/addons/gdhexgrid/demo_3d.tscn b/addons/gdhexgrid/demo_3d.tscn index 8b2cc94..328039d 100644 --- a/addons/gdhexgrid/demo_3d.tscn +++ b/addons/gdhexgrid/demo_3d.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=13 format=2] -[ext_resource path="res://demo_3d.gd" type="Script" id=1] +[ext_resource path="res://addons/gdhexgrid/demo_3d.gd" type="Script" id=1] [sub_resource type="BoxShape" id=1] extents = Vector3( 4, 0.1, 4 ) diff --git a/project.godot b/project.godot index e6a2450..4290811 100644 --- a/project.godot +++ b/project.godot @@ -53,6 +53,29 @@ config/icon="res://icon.png" window/size/width=1280 window/size/height=768 +[input] + +Forward={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":87,"unicode":0,"echo":false,"script":null) + ] +} +Back={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":83,"unicode":0,"echo":false,"script":null) + ] +} +Left={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":65,"unicode":0,"echo":false,"script":null) + ] +} +Right={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":68,"unicode":0,"echo":false,"script":null) + ] +} + [physics] common/enable_pause_aware_picking=true diff --git a/scenes/DebugCamera.gd b/scenes/DebugCamera.gd new file mode 100644 index 0000000..d1db286 --- /dev/null +++ b/scenes/DebugCamera.gd @@ -0,0 +1,28 @@ +extends Camera + +var velocity = Vector3(0, 0, 0) +var camera_moving = false + +func _physics_process(delta): + self.transform.origin += velocity * delta + +func _unhandled_input(event): + if Input.get_mouse_button_mask() & 3: + camera_moving = true + else: + camera_moving = false + + if event is InputEventKey: + if Input.is_action_pressed("Forward"): + velocity.z = -5 + elif Input.is_action_pressed("Back"): + velocity.z = 5 + else: + velocity.z = 0 + + if Input.is_action_pressed("Left"): + velocity.x = -5 + elif Input.is_action_pressed("Right"): + velocity.x = 5 + else: + velocity.x = 0 diff --git a/scenes/HexGrid3DTest.gd b/scenes/HexGrid3DTest.gd new file mode 100644 index 0000000..c2572b6 --- /dev/null +++ b/scenes/HexGrid3DTest.gd @@ -0,0 +1,26 @@ +extends Spatial + +onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() +onready var HexTile3D = preload("res://scenes/HexTile3D.tscn") + +func _ready(): + print (hexgrid) + + var num_tiles_x = 50 + var num_tiles_z = 50 + + for i in range(-num_tiles_x / 2, num_tiles_x / 2): + for j in range (-num_tiles_z / 2, num_tiles_z / 2): + var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j)) + var tile = HexTile3D.instance() + var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 - 0.5 + tile.transform.origin = Vector3(pos.x, height, pos.y) + add_child(tile) + + var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0)) + print (pos) + + var hex = hexgrid.get_hex_at(pos) + print (hex) + + pass diff --git a/scenes/HexGrid3DTest.tscn b/scenes/HexGrid3DTest.tscn new file mode 100644 index 0000000..63fa44c --- /dev/null +++ b/scenes/HexGrid3DTest.tscn @@ -0,0 +1,61 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://scenes/HexTile3D.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/HexGrid3DTest.gd" type="Script" id=2] +[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3] + +[sub_resource type="PlaneMesh" id=1] +size = Vector2( 50, 50 ) + +[sub_resource type="Shader" id=2] +code = "shader_type spatial; + +uniform float beer_factor = 5.0; + +void fragment() { + // sample our depth buffer + float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r; + + // grab to values + //depth = depth * 50.0 - 49.0; + + // unproject depth + depth = depth * 2.0 - 1.0; + float z = -PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]); + // float x = (SCREEN_UV.x * 2.0 - 1.0) * z / PROJECTION_MATRIX[0][0]; + // float y = (SCREEN_UV.y * 2.0 - 1.0) * z / PROJECTION_MATRIX[1][1]; + float delta = -(z - VERTEX.z); // z is negative. + // delta *= 0.1; + + // beers law + float att = exp(-delta * beer_factor); + + ALPHA = clamp(1.0 - att, 0.0, 1.0); + + ALBEDO = vec3(0.0, 0.0, 0.05); +}" + +[sub_resource type="ShaderMaterial" id=3] +shader = SubResource( 2 ) +shader_param/beer_factor = 5.0 + +[node name="HexGrid3DTest" type="Spatial"] +script = ExtResource( 2 ) + +[node name="HexTile3D" parent="." instance=ExtResource( 1 )] +visible = false + +[node name="Camera" type="Camera" parent="."] +transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -1.05655, 3.66248, 10.3074 ) +fov = 60.0 +script = ExtResource( 3 ) + +[node name="DirectionalLight" type="DirectionalLight" parent="."] +transform = Transform( 0.83729, 0.174005, 0.518332, -0.54676, 0.266466, 0.793757, 0, -0.948008, 0.318248, 0, 8.03448, 0 ) +shadow_enabled = true +shadow_contact = 1.565 + +[node name="MeshInstance" type="MeshInstance" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.568377, 0.0356998 ) +mesh = SubResource( 1 ) +material/0 = SubResource( 3 ) diff --git a/scenes/HexTile3D.gd b/scenes/HexTile3D.gd new file mode 100644 index 0000000..0dcf609 --- /dev/null +++ b/scenes/HexTile3D.gd @@ -0,0 +1,17 @@ +extends MeshInstance + +onready var is_mouse_over = false +var old_material_override = Material.new() + +func _on_Area_mouse_entered(): + is_mouse_over = true + old_material_override = material_override + + var material = self.get_surface_material(0) + var cloned_material = material.duplicate() + cloned_material.albedo_color = Color (1, 0, 0) + self.material_override = cloned_material + +func _on_Area_mouse_exited(): + is_mouse_over = false + self.material_override = old_material_override diff --git a/scenes/HexTile3D.tscn b/scenes/HexTile3D.tscn new file mode 100644 index 0000000..db96182 --- /dev/null +++ b/scenes/HexTile3D.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://scenes/HexTile3D.gd" type="Script" id=1] + +[sub_resource type="CylinderMesh" id=6] +top_radius = 0.5 +bottom_radius = 0.5 +height = 1.0 +radial_segments = 6 +rings = 1 + +[sub_resource type="SpatialMaterial" id=4] +albedo_color = Color( 0.211765, 0.568627, 0.0666667, 1 ) +roughness = 0.0 + +[sub_resource type="CylinderShape" id=5] +radius = 0.5 +height = 1.0 + +[node name="HexTile3D" type="MeshInstance"] +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, -0.5, 0 ) +mesh = SubResource( 6 ) +material/0 = SubResource( 4 ) +script = ExtResource( 1 ) + +[node name="Area" type="Area" parent="."] + +[node name="CollisionShape" type="CollisionShape" parent="Area"] +shape = SubResource( 5 ) + +[connection signal="mouse_entered" from="Area" to="." method="_on_Area_mouse_entered"] +[connection signal="mouse_exited" from="Area" to="." method="_on_Area_mouse_exited"]