From b6e0e659a5795513b98ff2b9ba59d23fda3be7cb Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 14 Oct 2022 11:36:40 +0200 Subject: [PATCH] Experimenting with noise based world generation --- addons/gdhexgrid/HexShape.tscn | 2 +- addons/gdhexgrid/demo_3d.tscn | 4 +- doc/world.txt | 33 +++++ scenes/HexGrid3DTest.tscn | 100 +++++++++------ scenes/HexShape.tscn | 48 ++++++++ scenes/NoiseWorld.gd | 156 ++++++++++++++++++++++++ scenes/NoiseWorld.tscn | 144 ++++++++++++++++++++++ scenes/{World.gd => RandomWalkWorld.gd} | 8 +- scenes/RandomWalkWorld.tscn | 144 ++++++++++++++++++++++ 9 files changed, 592 insertions(+), 47 deletions(-) create mode 100644 doc/world.txt create mode 100644 scenes/HexShape.tscn create mode 100644 scenes/NoiseWorld.gd create mode 100644 scenes/NoiseWorld.tscn rename scenes/{World.gd => RandomWalkWorld.gd} (98%) create mode 100644 scenes/RandomWalkWorld.tscn diff --git a/addons/gdhexgrid/HexShape.tscn b/addons/gdhexgrid/HexShape.tscn index 9b8d474..09b9872 100644 --- a/addons/gdhexgrid/HexShape.tscn +++ b/addons/gdhexgrid/HexShape.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://HexShape.gd" type="Script" id=1] +[ext_resource path="res://addons/gdhexgrid/HexShape.gd" type="Script" id=1] [node name="Highlight" type="Polygon2D"] color = Color( 1, 1, 1, 0 ) diff --git a/addons/gdhexgrid/demo_3d.tscn b/addons/gdhexgrid/demo_3d.tscn index 328039d..453262d 100644 --- a/addons/gdhexgrid/demo_3d.tscn +++ b/addons/gdhexgrid/demo_3d.tscn @@ -21,13 +21,13 @@ roughness = 0.0 [sub_resource type="QuadMesh" id=5] size = Vector2( 2, 1 ) -[sub_resource type="ViewportTexture" id=6] +[sub_resource type="ViewportTexture" id=12] viewport_path = NodePath("HexGrid/Highlight/Viewport") [sub_resource type="SpatialMaterial" id=7] resource_local_to_scene = true flags_transparent = true -albedo_texture = SubResource( 6 ) +albedo_texture = SubResource( 12 ) [sub_resource type="BoxShape" id=8] extents = Vector3( 4, 0.1, 2 ) diff --git a/doc/world.txt b/doc/world.txt new file mode 100644 index 0000000..f961fa6 --- /dev/null +++ b/doc/world.txt @@ -0,0 +1,33 @@ +# Patch based world + +Per patch we have: + + - tiles + - water plane + - with clickable area + +A world consists of a fixed amount of tiles in x and y direction. + +Patches are used to stream the world in and out. + +# Cross-patch island generation: + + + +# Shaping under water tiles: + +for each tile of an island: + - create inside set I: + - add all island tiles to I + - for steps i = 0, ..., n: + - create empty rim sets RI and RN + - for all tiles in I: + - for all neighbours of tile: + - if neighbour not in I -> add tile to RI + - add tile to I + + - for all tiles in RI: + - get all neighbours of tile: + - if neighbour not in I -> add neighbour to RN + - for all neighbours not in I + - diff --git a/scenes/HexGrid3DTest.tscn b/scenes/HexGrid3DTest.tscn index 6a38dcc..04d144c 100644 --- a/scenes/HexGrid3DTest.tscn +++ b/scenes/HexGrid3DTest.tscn @@ -1,20 +1,11 @@ -[gd_scene load_steps=15 format=2] +[gd_scene load_steps=17 format=2] [ext_resource path="res://entities/PlayerEntity.gd" type="Script" id=1] [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/NoiseWorld.gd" type="Script" id=4] [ext_resource path="res://assets/water_diffuse.png" type="Texture" id=5] [ext_resource path="res://components/MovableComponent.tscn" type="PackedScene" id=6] -[ext_resource path="res://scenes/World.gd" type="Script" id=7] - -[sub_resource type="CapsuleMesh" id=4] -radius = 0.2 -mid_height = 0.5 -radial_segments = 16 - -[sub_resource type="CapsuleShape" id=5] -radius = 0.25 -height = 0.5 [sub_resource type="CylinderMesh" id=7] top_radius = 0.5 @@ -30,6 +21,7 @@ albedo_color = Color( 1, 1, 1, 0.407843 ) roughness = 0.0 [sub_resource type="PlaneMesh" id=1] +size = Vector2( 1, 1 ) subdivide_width = 24 subdivide_depth = 24 @@ -122,10 +114,67 @@ shader_param/texture_scale = Vector2( 8, 8 ) shader_param/texturemap = ExtResource( 5 ) [sub_resource type="HeightMapShape" id=6] +map_width = 1 +map_depth = 1 +map_data = PoolRealArray( 0 ) + +[sub_resource type="OpenSimplexNoise" id=11] +octaves = 1 +period = 101.6 + +[sub_resource type="NoiseTexture" id=10] +flags = 3 +width = 1024 +height = 1024 +seamless = true +noise = SubResource( 11 ) + +[sub_resource type="CapsuleMesh" id=4] +radius = 0.2 +mid_height = 0.5 +radial_segments = 16 + +[sub_resource type="CapsuleShape" id=5] +radius = 0.25 +height = 0.5 [node name="HexGrid3DTest" type="Spatial"] script = ExtResource( 2 ) +[node name="World" type="Spatial" parent="."] +script = ExtResource( 4 ) +world_size = 100 + +[node name="Scene" type="Spatial" parent="World"] + +[node name="Navigation" type="Spatial" parent="World"] + +[node name="Tiles" type="Spatial" parent="World"] + +[node name="Highlight" type="MeshInstance" parent="World"] +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 ) +cast_shadow = 0 +mesh = SubResource( 7 ) +material/0 = SubResource( 8 ) + +[node name="Water" type="MeshInstance" parent="World"] +transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, 0, 0, 0 ) +cast_shadow = 0 +mesh = SubResource( 1 ) +skeleton = NodePath("../..") +material/0 = SubResource( 3 ) + +[node name="ClckArea" type="Area" parent="World"] +transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, 0, 0, 0 ) + +[node name="CollisionShape" type="CollisionShape" parent="World/ClckArea"] +transform = Transform( 2.5, 0, 0, 0, 1, 0, 0, 0, 2.5, 0, 0, 0 ) +shape = SubResource( 6 ) + +[node name="Noise" type="Sprite" parent="World"] +visible = false +texture = SubResource( 10 ) + [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 @@ -192,35 +241,6 @@ shape = SubResource( 5 ) [node name="Movable" parent="Player" instance=ExtResource( 6 )] max_speed = 3 -[node name="World" type="Spatial" parent="."] -script = ExtResource( 7 ) -generate_flag = true - -[node name="Scene" type="Spatial" parent="World"] - -[node name="Navigation" type="Spatial" parent="World"] - -[node name="Tiles" type="Spatial" parent="World"] - -[node name="Highlight" type="MeshInstance" parent="World"] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 ) -cast_shadow = 0 -mesh = SubResource( 7 ) -material/0 = SubResource( 8 ) - -[node name="Water" type="MeshInstance" parent="World"] -transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 ) -cast_shadow = 0 -mesh = SubResource( 1 ) -skeleton = NodePath("../..") -material/0 = SubResource( 3 ) - -[node name="ClckArea" type="Area" parent="World"] - -[node name="CollisionShape" type="CollisionShape" parent="World/ClckArea"] -transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 ) -shape = SubResource( 6 ) - [connection signal="input_event" from="World/ClckArea" to="World" method="_on_ClckArea_input_event"] [connection signal="mouse_entered" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_entered"] [connection signal="mouse_exited" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_exited"] diff --git a/scenes/HexShape.tscn b/scenes/HexShape.tscn new file mode 100644 index 0000000..ce8d193 --- /dev/null +++ b/scenes/HexShape.tscn @@ -0,0 +1,48 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/gdhexgrid/HexShape.gd" type="Script" id=1] + +[node name="Highlight" type="MeshInstance"] +script = ExtResource( 1 ) + +[node name="Label" type="Label" parent="."] +visible = false +margin_left = 5.0 +margin_top = -39.0 +margin_right = 52.0 +margin_bottom = -25.0 +text = "SCREEN" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="AreaCoords" type="Label" parent="."] +visible = false +margin_left = 55.0 +margin_top = -39.0 +margin_right = 105.0 +margin_bottom = -25.0 +text = "SCREEN" + +[node name="Label2" type="Label" parent="."] +visible = false +margin_left = 25.0 +margin_top = -19.0 +margin_right = 56.0 +margin_bottom = -5.0 +text = "HEX" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="HexCoords" type="Label" parent="."] +margin_left = -26.0 +margin_top = -8.0 +margin_right = 24.0 +margin_bottom = 6.0 +text = "HEX" +align = 1 +valign = 1 +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/scenes/NoiseWorld.gd b/scenes/NoiseWorld.gd new file mode 100644 index 0000000..8506e35 --- /dev/null +++ b/scenes/NoiseWorld.gd @@ -0,0 +1,156 @@ +tool + +extends Spatial + +var GameTile = load("res://utils/GameTile.gd") +var HexCell = preload("res://addons/gdhexgrid/HexCell.gd") +onready var HexGrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() +var IslandGenerator = preload("res://utils/IslandGenerator.gd").new() +onready var click_area = $ClckArea/CollisionShape +onready var water = $Water +onready var highlight = $Highlight +onready var noise = $Noise +onready var tiles = $Tiles + +var has_tiles_initialized = false +var tile_map = {} +var active_tile = null +var island_count = 5 + +export var world_size = 50 setget do_resize +export var generate_flag: bool = false setget do_generate + +func _ready(): + assert(get_node("Tiles") != null) + + do_resize(world_size) + reset() + generate() + pass + + +func _process(_delta): + pass + + +func do_generate(_flag): + reset() +# generate() + generate_flag = false + + +func do_resize(new_size): + print ("resizing world to size " + str (new_size)) + + var water = $Water + if water == null: + return + + print ("water node: " + str(water)) + water.transform = Transform.IDENTITY.scaled(Vector3(new_size, new_size, new_size) * 0.5) + + var click_area = $ClckArea + click_area.transform = water.transform + + world_size = new_size + return world_size + +func reset(): + var tiles = get_node("Tiles") + if tiles == null: + return + + print ("clearing old tiles: " + str(tiles.get_child_count())) + for tile in tiles.get_children(): + tiles.remove_child(tile) + tile.queue_free() + tile_map = {} + + +func generate(): + print ("generating world with size " + str(world_size)) + reset() + generate_base_tiles() + apply_noise_as_height() + + +func apply_noise_as_height(): + var texture : Texture = noise.texture + yield(texture, "changed") + var noise = texture.get_data() + noise.lock() + + var vec1 = Vector2.ONE + vec1 = vec1 * Vector2(0.5, 0.2) + + for i in range (-world_size / 2, world_size / 2): + for j in range (-world_size / 2, world_size / 2): + var coord = Vector2(i,j) + var tile = tile_map[coord] + var tex_coord_normalized = (coord / world_size) * 0.5 + Vector2(0.5, 0.5) + var tex_coord = tex_coord_normalized * texture.get_size() +# -25 ... 25 -> 0 ... 1 +# (coord / (size * 0.5)) * 0.5 + 0.5: -1 .. 1 + var noise_value = texture.get_data().get_pixelv(tex_coord) + tile.transform.origin.y = noise_value[0] * 5.0 - 3 + + noise.unlock() + + +func generate_base_tiles(): + for i in range (-world_size / 2, world_size / 2): + for j in range (-world_size / 2, world_size / 2): + var tile = GameTile.new() + tile.type = GameTile.TileType.Water + tile.offset_coords = Vector2(i,j) + + add_tile_for_hex(tile) + + +func add_tile_for_hex(game_tile): + var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() + var HexTile3D = preload("res://scenes/HexTile3D.tscn") + + var tile = HexTile3D.instance() + var pos = hexgrid.get_hex_center_from_offset(game_tile.offset_coords) + var height = -2.0 # if game_tile.type == GameTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05) + + tiles.add_child(tile) + tile_map[game_tile.offset_coords] = tile + + tile.set_tiletype(game_tile.type) + tile.transform.origin = Vector3(pos.x, height, pos.y) + tile.game_tile = game_tile + + return tile + + +func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx): + var hex_at_position = HexGrid.get_hex_at(position) + var offset_coords = hex_at_position.offset_coords + var hex_center = HexGrid.get_hex_center_from_offset(offset_coords) + highlight.transform.origin = Vector3 (hex_center.x, 0.0, hex_center.y) + + var world_tile = null + if offset_coords in tile_map.keys(): + world_tile = tile_map[offset_coords] + + if active_tile != null and world_tile != active_tile: + pass + + if active_tile == world_tile: + return + + if world_tile == null: + return + + active_tile = world_tile + + +func _on_ClckArea_mouse_entered(): + highlight.visible = true + + +func _on_ClckArea_mouse_exited(): + highlight.visible = false + diff --git a/scenes/NoiseWorld.tscn b/scenes/NoiseWorld.tscn new file mode 100644 index 0000000..bf52f73 --- /dev/null +++ b/scenes/NoiseWorld.tscn @@ -0,0 +1,144 @@ +[gd_scene load_steps=9 format=2] + +[ext_resource path="res://scenes/NoiseWorld.gd" type="Script" id=1] +[ext_resource path="res://assets/water_diffuse.png" type="Texture" id=2] + +[sub_resource type="CylinderMesh" id=7] +top_radius = 0.5 +bottom_radius = 0.5 +height = 0.1 +radial_segments = 6 +rings = 1 + +[sub_resource type="SpatialMaterial" id=8] +render_priority = 1 +flags_transparent = true +albedo_color = Color( 1, 1, 1, 0.407843 ) +roughness = 0.0 + +[sub_resource type="PlaneMesh" id=1] +subdivide_width = 24 +subdivide_depth = 24 + +[sub_resource type="Shader" id=2] +code = "shader_type spatial; + +uniform vec2 amplitude = vec2(0.01, 0.05); +uniform vec2 frequency = vec2(3.0, 2.5); +uniform vec2 time_factor = vec2(2.0, 3.0); + +uniform float beer_factor = 100.01; +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) { + 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() { + 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)); + 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); +} + +void fragment() { + 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; + + // 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); + } + + 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; + EMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 2.0).rgb * (1.0 - ALPHA); + + ALBEDO *= ALPHA; + ALPHA = 1.0; +}" + +[sub_resource type="ShaderMaterial" id=3] +shader = SubResource( 2 ) +shader_param/amplitude = Vector2( 0.005, 0.005 ) +shader_param/frequency = Vector2( 50, 48 ) +shader_param/time_factor = Vector2( 7, 8 ) +shader_param/beer_factor = 2.0 +shader_param/refraction = 0.001 +shader_param/uv_offset_scale = Vector2( 0.2, 0.2 ) +shader_param/uv_offset_time_scale = 0.1 +shader_param/uv_offset_amplitude = 1.2 +shader_param/texture_scale = Vector2( 8, 8 ) +shader_param/texturemap = ExtResource( 2 ) + +[sub_resource type="HeightMapShape" id=6] + +[node name="World" type="Spatial"] +script = ExtResource( 1 ) +size = Vector2( 20, 3 ) + +[node name="Scene" type="Spatial" parent="."] + +[node name="Navigation" type="Spatial" parent="."] + +[node name="Tiles" type="Spatial" parent="."] + +[node name="Highlight" type="MeshInstance" parent="."] +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 ) +cast_shadow = 0 +mesh = SubResource( 7 ) +material/0 = SubResource( 8 ) + +[node name="Water" type="MeshInstance" parent="."] +transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 ) +cast_shadow = 0 +mesh = SubResource( 1 ) +skeleton = NodePath("../..") +material/0 = SubResource( 3 ) + +[node name="ClckArea" type="Area" parent="."] + +[node name="CollisionShape" type="CollisionShape" parent="ClckArea"] +transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 ) +shape = SubResource( 6 ) + +[connection signal="input_event" from="ClckArea" to="." method="_on_ClckArea_input_event"] +[connection signal="mouse_entered" from="ClckArea" to="." method="_on_ClckArea_mouse_entered"] +[connection signal="mouse_exited" from="ClckArea" to="." method="_on_ClckArea_mouse_exited"] diff --git a/scenes/World.gd b/scenes/RandomWalkWorld.gd similarity index 98% rename from scenes/World.gd rename to scenes/RandomWalkWorld.gd index c80eb57..b8473f4 100644 --- a/scenes/World.gd +++ b/scenes/RandomWalkWorld.gd @@ -63,10 +63,10 @@ func generate(): )) mark_sand_tiles() - smooth_water_tiles() - smooth_water_tiles() - smooth_water_tiles() - smooth_water_tiles() +# smooth_water_tiles() +# smooth_water_tiles() +# smooth_water_tiles() +# smooth_water_tiles() func generate_base_tiles(): diff --git a/scenes/RandomWalkWorld.tscn b/scenes/RandomWalkWorld.tscn new file mode 100644 index 0000000..303c22d --- /dev/null +++ b/scenes/RandomWalkWorld.tscn @@ -0,0 +1,144 @@ +[gd_scene load_steps=9 format=2] + +[ext_resource path="res://scenes/World.gd" type="Script" id=1] +[ext_resource path="res://assets/water_diffuse.png" type="Texture" id=2] + +[sub_resource type="CylinderMesh" id=7] +top_radius = 0.5 +bottom_radius = 0.5 +height = 0.1 +radial_segments = 6 +rings = 1 + +[sub_resource type="SpatialMaterial" id=8] +render_priority = 1 +flags_transparent = true +albedo_color = Color( 1, 1, 1, 0.407843 ) +roughness = 0.0 + +[sub_resource type="PlaneMesh" id=1] +subdivide_width = 24 +subdivide_depth = 24 + +[sub_resource type="Shader" id=2] +code = "shader_type spatial; + +uniform vec2 amplitude = vec2(0.01, 0.05); +uniform vec2 frequency = vec2(3.0, 2.5); +uniform vec2 time_factor = vec2(2.0, 3.0); + +uniform float beer_factor = 100.01; +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) { + 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() { + 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)); + 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); +} + +void fragment() { + 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; + + // 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); + } + + 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; + EMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 2.0).rgb * (1.0 - ALPHA); + + ALBEDO *= ALPHA; + ALPHA = 1.0; +}" + +[sub_resource type="ShaderMaterial" id=3] +shader = SubResource( 2 ) +shader_param/amplitude = Vector2( 0.005, 0.005 ) +shader_param/frequency = Vector2( 50, 48 ) +shader_param/time_factor = Vector2( 7, 8 ) +shader_param/beer_factor = 2.0 +shader_param/refraction = 0.001 +shader_param/uv_offset_scale = Vector2( 0.2, 0.2 ) +shader_param/uv_offset_time_scale = 0.1 +shader_param/uv_offset_amplitude = 1.2 +shader_param/texture_scale = Vector2( 8, 8 ) +shader_param/texturemap = ExtResource( 2 ) + +[sub_resource type="HeightMapShape" id=6] + +[node name="World" type="Spatial"] +script = ExtResource( 1 ) +generate_flag = true + +[node name="Scene" type="Spatial" parent="."] + +[node name="Navigation" type="Spatial" parent="."] + +[node name="Tiles" type="Spatial" parent="."] + +[node name="Highlight" type="MeshInstance" parent="."] +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 ) +cast_shadow = 0 +mesh = SubResource( 7 ) +material/0 = SubResource( 8 ) + +[node name="Water" type="MeshInstance" parent="."] +transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 ) +cast_shadow = 0 +mesh = SubResource( 1 ) +skeleton = NodePath("../..") +material/0 = SubResource( 3 ) + +[node name="ClckArea" type="Area" parent="."] + +[node name="CollisionShape" type="CollisionShape" parent="ClckArea"] +transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 ) +shape = SubResource( 6 ) + +[connection signal="input_event" from="ClckArea" to="." method="_on_ClckArea_input_event"] +[connection signal="mouse_entered" from="ClckArea" to="." method="_on_ClckArea_mouse_entered"] +[connection signal="mouse_exited" from="ClckArea" to="." method="_on_ClckArea_mouse_exited"]