Experimenting with 3D Hex world

WorldChunkRefactoring
Martin Felis 2022-08-25 15:33:44 +02:00
parent d9f68fc02f
commit d2cef63a12
7 changed files with 188 additions and 1 deletions

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=13 format=2] [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] [sub_resource type="BoxShape" id=1]
extents = Vector3( 4, 0.1, 4 ) extents = Vector3( 4, 0.1, 4 )

View File

@ -53,6 +53,29 @@ config/icon="res://icon.png"
window/size/width=1280 window/size/width=1280
window/size/height=768 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] [physics]
common/enable_pause_aware_picking=true common/enable_pause_aware_picking=true

28
scenes/DebugCamera.gd Normal file
View File

@ -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

26
scenes/HexGrid3DTest.gd Normal file
View File

@ -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

61
scenes/HexGrid3DTest.tscn Normal file
View File

@ -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 )

17
scenes/HexTile3D.gd Normal file
View File

@ -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

32
scenes/HexTile3D.tscn Normal file
View File

@ -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"]