Tests with level generator and water

WorldChunkRefactoring
Martin Felis 2022-09-15 12:32:45 +02:00
parent a04ff9bd2f
commit ff73d6a543
8 changed files with 224 additions and 63 deletions

BIN
assets/water_diffuse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 KiB

View File

@ -0,0 +1,37 @@
[remap]
importer="texture"
type="StreamTexture"
path.s3tc="res://.import/water_diffuse.png-a44afe07b3d35e565d86feee627a1afb.s3tc.stex"
path.etc2="res://.import/water_diffuse.png-a44afe07b3d35e565d86feee627a1afb.etc2.stex"
metadata={
"imported_formats": [ "s3tc", "etc2" ],
"vram_texture": true
}
[deps]
source_file="res://assets/water_diffuse.png"
dest_files=[ "res://.import/water_diffuse.png-a44afe07b3d35e565d86feee627a1afb.s3tc.stex", "res://.import/water_diffuse.png-a44afe07b3d35e565d86feee627a1afb.etc2.stex" ]
[params]
compress/mode=2
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=true
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=1
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=false
svg/scale=1.0

View File

@ -24,6 +24,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://components/ColorComponent.gd" "path": "res://components/ColorComponent.gd"
}, { }, {
"base": "Spatial",
"class": "Island",
"language": "GDScript",
"path": "res://scenes/Island.gd"
}, {
"base": "Object", "base": "Object",
"class": "SpringDamper", "class": "SpringDamper",
"language": "GDScript", "language": "GDScript",
@ -38,6 +43,7 @@ _global_script_class_icons={
"ClickableComponent": "", "ClickableComponent": "",
"CollisionLine": "", "CollisionLine": "",
"ColorComponent": "", "ColorComponent": "",
"Island": "",
"SpringDamper": "", "SpringDamper": "",
"TintedSpriteComponent": "" "TintedSpriteComponent": ""
} }

View File

@ -1,42 +1,22 @@
extends Spatial extends Spatial
onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
onready var HexTile3D = preload("res://scenes/HexTile3D.tscn")
onready var tiles = $Tiles
onready var fps_label = $CanvasLayer/HBoxContainer/FPSLabel onready var fps_label = $CanvasLayer/HBoxContainer/FPSLabel
onready var player_pos_label = $CanvasLayer/HBoxContainer/PlayerPos onready var player_pos_label = $CanvasLayer/HBoxContainer/PlayerPos
onready var tile_pos_label = $CanvasLayer/HBoxContainer/TilePos onready var tile_pos_label = $CanvasLayer/HBoxContainer/TilePos
onready var Island = preload("res://scenes/Island.gd")
onready var player = $Player onready var player = $Player
onready var world = $World
var player_velocity = Vector3.ZERO var player_velocity = Vector3.ZERO
var player_speed = 5 var player_speed = 5
var target_coordinate = Vector3.ZERO var target_coordinate = Vector3.ZERO
var target_tile = null
var current_tile = null
func _ready(): func _ready():
print (hexgrid) for node in world.get_children():
if node is Island:
var num_tiles_x = 50 node.generate()
var num_tiles_z = 50 node.connect("island_tile_selected", self, "on_island_tile_selected")
node.connect("island_tile_hover", self, "on_island_tile_hover")
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.4 + rand_range(-0.3, 0.3)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
tiles.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
func _process(delta): func _process(delta):
@ -48,7 +28,7 @@ func _physics_process(delta):
var position_error = target_coordinate - player.transform.origin var position_error = target_coordinate - player.transform.origin
var error_length = position_error.length() var error_length = position_error.length()
if error_length > 0.1: if error_length > 0.05:
player_velocity = position_error / error_length * player_speed player_velocity = position_error / error_length * player_speed
else: else:
player_velocity = Vector3.ZERO player_velocity = Vector3.ZERO
@ -56,13 +36,11 @@ func _physics_process(delta):
player.transform.origin += player_velocity * delta player.transform.origin += player_velocity * delta
func on_tile_selected(tile): func on_island_tile_selected(island, tile):
target_tile = tile target_coordinate = tile.global_transform.origin
target_coordinate = Vector3(tile.transform.origin.x, tile.transform.origin.y, tile.transform.origin.z)
var position_error = target_coordinate - player.transform.origin var position_error = target_coordinate - player.transform.origin
print ("clicked on tile " + str(tile.transform.origin) + " error: " + str(position_error)) print ("clicked on tile " + str(target_coordinate) + " error: " + str(position_error))
func on_tile_hover(tile): func on_island_tile_hover(island, tile):
current_tile = tile tile_pos_label.text = "TilePos: " + str(tile.global_transform.origin)
tile_pos_label.text = "Tile: " + str(current_tile.transform.origin)

View File

@ -1,11 +1,21 @@
[gd_scene load_steps=9 format=2] [gd_scene load_steps=11 format=2]
[ext_resource path="res://scenes/HexTile3D.tscn" type="PackedScene" id=1] [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/HexGrid3DTest.gd" type="Script" id=2]
[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3] [ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3]
[ext_resource path="res://scenes/Island.tscn" type="PackedScene" id=4]
[ext_resource path="res://assets/water_diffuse.png" type="Texture" id=5]
[sub_resource type="CapsuleMesh" id=4]
radius = 0.2
mid_height = 0.5
radial_segments = 16
[sub_resource type="NoiseTexture" id=5]
[sub_resource type="PlaneMesh" id=1] [sub_resource type="PlaneMesh" id=1]
size = Vector2( 50, 50 ) subdivide_width = 24
subdivide_depth = 24
[sub_resource type="Shader" id=2] [sub_resource type="Shader" id=2]
code = "shader_type spatial; code = "shader_type spatial;
@ -17,19 +27,44 @@ uniform vec2 time_factor = vec2(2.0, 3.0);
uniform float beer_factor = 100.01; uniform float beer_factor = 100.01;
uniform float refraction = 0.05; uniform float refraction = 0.05;
uniform sampler2D uv_offset_texture : hint_black;
uniform vec2 uv_offset_scale = vec2(0.2, 0.2);
uniform float uv_offset_time_scale = 0.01;
uniform float uv_offset_amplitude = 0.2;
uniform sampler2D texturemap : hint_albedo;
uniform vec2 texture_scale = vec2(8.0, 4.0);
float height(vec2 pos, float time) { float height(vec2 pos, float time) {
return (amplitude.x * sin(pos.x * frequency.x + time * time_factor.x)) + (amplitude.y * sin(pos.y * frequency.y + time * time_factor.y)); return (amplitude.x * sin(pos.x * frequency.x + time * time_factor.x)) + (amplitude.y * sin(pos.y * frequency.y + time * time_factor.y));
} }
void vertex() { void vertex() {
//VERTEX.y += height(VERTEX.xz, TIME); // sample the height at the location of our vertex VERTEX.y += height(VERTEX.xz, TIME); // sample the height at the location of our vertex
TANGENT = normalize(vec3(0.0, height(VERTEX.xz + vec2(0.0, 0.2), TIME) - height(VERTEX.xz + vec2(0.0, -0.2), TIME), 0.4)); TANGENT = normalize(vec3(0.0, height(VERTEX.xz + vec2(0.0, 0.2), TIME) - height(VERTEX.xz + vec2(0.0, -0.2), TIME), 0.4));
BINORMAL = normalize(vec3(0.4, height(VERTEX.xz + vec2(0.2, 0.0), TIME) - height(VERTEX.xz + vec2(-0.2, 0.0), TIME ), 0.0)); BINORMAL = normalize(vec3(0.4, height(VERTEX.xz + vec2(0.2, 0.0), TIME) - height(VERTEX.xz + vec2(-0.2, 0.0), TIME ), 0.0));
NORMAL = cross(TANGENT, BINORMAL); NORMAL = cross(TANGENT, BINORMAL);
} }
void fragment() { void fragment() {
// sample our depth buffer vec2 base_uv_offset = UV * uv_offset_scale; // Determine the UV that we use to look up our DuDv
base_uv_offset += TIME * uv_offset_time_scale;
vec2 texture_based_offset = texture(uv_offset_texture, base_uv_offset).rg; // Get our offset
texture_based_offset = texture_based_offset * 2.0 - 1.0; // Convert from 0.0 <=> 1.0 to -1.0 <=> 1.0
vec2 texture_uv = UV * texture_scale;
texture_uv += uv_offset_amplitude * texture_based_offset;
ALBEDO = texture(texturemap, texture_uv).rgb * 0.5;
METALLIC = 0.0;
ROUGHNESS = 0.5;
//NORMALMAP = texture(normalmap, base_uv_offset).rgb;
//NORMALMAP_DEPTH = 0.2;
if (ALBEDO.r > 0.9 && ALBEDO.g > 0.9 && ALBEDO.b > 0.9) {
ALPHA = 0.9;
} else {
// sample our depth buffer
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r; float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
// grab to values // grab to values
@ -47,32 +82,28 @@ void fragment() {
float att = exp(-delta * beer_factor); float att = exp(-delta * beer_factor);
ALPHA = clamp(1.0 - att, 0.0, 1.0); ALPHA = clamp(1.0 - att, 0.0, 1.0);
}
vec3 ref_normal = normalize( mix(NORMAL,TANGENT * NORMALMAP.x + BINORMAL * NORMALMAP.y + NORMAL * NORMALMAP.z,NORMALMAP_DEPTH) ); vec3 ref_normal = normalize( mix(NORMAL,TANGENT * NORMALMAP.x + BINORMAL * NORMALMAP.y + NORMAL * NORMALMAP.z,NORMALMAP_DEPTH) );
vec2 ref_ofs = SCREEN_UV - ref_normal.xy * refraction; vec2 ref_ofs = SCREEN_UV - ref_normal.xy * refraction;
EMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 2.0).rgb * (1.0 - ALPHA); EMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 2.0).rgb * (1.0 - ALPHA);
ALBEDO = vec3(0.0, 0.0, 0.05);
ALBEDO *= ALPHA; ALBEDO *= ALPHA;
//ALPHA = 1.0; ALPHA = 1.0;
}" }"
[sub_resource type="ShaderMaterial" id=3] [sub_resource type="ShaderMaterial" id=3]
shader = SubResource( 2 ) shader = SubResource( 2 )
shader_param/amplitude = Vector2( 0.01, 0.05 ) shader_param/amplitude = Vector2( 0.01, 0.011 )
shader_param/frequency = Vector2( 3, 2.5 ) shader_param/frequency = Vector2( 50, 48 )
shader_param/time_factor = Vector2( 2, 3 ) shader_param/time_factor = Vector2( 7, 8 )
shader_param/beer_factor = 5.0 shader_param/beer_factor = 0.5
shader_param/refraction = 0.05 shader_param/refraction = 0.01
shader_param/uv_offset_scale = Vector2( 0.2, 0.2 )
[sub_resource type="CapsuleMesh" id=4] shader_param/uv_offset_time_scale = 0.1
radius = 0.2 shader_param/uv_offset_amplitude = 1.2
mid_height = 0.5 shader_param/texture_scale = Vector2( 8, 8 )
radial_segments = 16 shader_param/texturemap = ExtResource( 5 )
[sub_resource type="NoiseTexture" id=5]
[node name="HexGrid3DTest" type="Spatial"] [node name="HexGrid3DTest" type="Spatial"]
script = ExtResource( 2 ) script = ExtResource( 2 )
@ -87,11 +118,6 @@ shadow_contact = 1.565
directional_shadow_mode = 0 directional_shadow_mode = 0
directional_shadow_depth_range = 1 directional_shadow_depth_range = 1
[node name="Water" type="MeshInstance" parent="."]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0.568377, 0.0356998 )
mesh = SubResource( 1 )
material/0 = SubResource( 3 )
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="HBoxContainer" type="HBoxContainer" parent="CanvasLayer"] [node name="HBoxContainer" type="HBoxContainer" parent="CanvasLayer"]
@ -130,8 +156,6 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="Tiles" type="Spatial" parent="."]
[node name="Player" type="Spatial" parent="."] [node name="Player" type="Spatial" parent="."]
[node name="Camera" type="Camera" parent="Player"] [node name="Camera" type="Camera" parent="Player"]
@ -153,3 +177,23 @@ expand = true
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="World" type="Spatial" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Island" parent="World" instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Water" type="Spatial" parent="."]
[node name="Water" type="MeshInstance" parent="Water"]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="Water2" type="MeshInstance" parent="Water"]
transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, -20, 0, -0.01 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )

43
scenes/Island.gd Normal file
View File

@ -0,0 +1,43 @@
tool
extends Spatial
class_name Island
export var generate: bool = false setget do_generate
export var level_size: int = 10
signal island_tile_selected
signal island_tile_hover
func generate():
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tiles = $Tiles
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
for i in range(-level_size / 2, level_size / 2):
for j in range (-level_size / 2, level_size / 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.4 + rand_range(-0.3, 0.3)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
tiles.add_child(tile)
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")
func do_generate(flag: bool):
print ("Generating Level")
generate()
func on_tile_selected(tile):
emit_signal("island_tile_selected", self, tile)
func on_tile_hover(tile):
emit_signal("island_tile_hover", self, tile)

9
scenes/Island.tscn Normal file
View File

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scenes/Island.gd" type="Script" id=1]
[node name="Island" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5.43195, 0, 0 )
script = ExtResource( 1 )
[node name="Tiles" type="Spatial" parent="."]

44
utils/LevelGenerator.gd Normal file
View File

@ -0,0 +1,44 @@
tool
extends Node
export var generate = false setget do_generate
export var level_size = 10
func generate_level(level_node):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var num_tiles_x = level_size
var num_tiles_z = level_size
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.4 + rand_range(-0.3, 0.3)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
level_node.add_child(tile)
var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0))
print (pos)
var hex = hexgrid.get_hex_at(pos)
print (hex)
print ("Generated level with " + str(level_node.get_child_count()) + " nodes")
func do_generate(flag: bool):
print ("flag: " + str(flag))
var tiles = get_node("../Tiles")
if tiles == null:
print ("Cannot generate: node '../Tiles' not found!")
return
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
generate_level(tiles)