Skip to content

Commit

Permalink
Make terrain edges soft, closes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jan 4, 2025
1 parent cbdb94f commit f6573cd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
6 changes: 5 additions & 1 deletion source/match/Map.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@tool
extends Node3D

# TODO: add editor-only 2nd pass shader to 'Terrain' mesh highlighting map boundries

const EXTRA_MARGIN = 2

@export var size = Vector2(50, 50):
set(a_size):
size = a_size
find_child("Terrain").mesh.size = size
find_child("Terrain").mesh.size = size + Vector2(EXTRA_MARGIN, EXTRA_MARGIN) * 2
find_child("Terrain").mesh.center_offset = Vector3(size.x, 0.0, size.y) / 2.0


Expand Down
4 changes: 3 additions & 1 deletion source/match/Map.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ size = Vector2(1000, 1000)
[sub_resource type="PlaneMesh" id="PlaneMesh_ypksr"]
resource_local_to_scene = true
material = ExtResource("1_81yiw")
size = Vector2(50, 50)
size = Vector2(54, 54)
center_offset = Vector3(25, 0, 25)

[node name="Map" type="Node3D"]
Expand All @@ -24,9 +24,11 @@ script = ExtResource("1_1svvv")
[node name="BlackBackgroundFixingAntiAliasingBug" type="MeshInstance3D" parent="Geometry"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.1, 0)
material_override = SubResource("StandardMaterial3D_ve5fg")
cast_shadow = 0
mesh = SubResource("PlaneMesh_0fj4w")

[node name="Terrain" type="MeshInstance3D" parent="Geometry" groups=["terrain_navigation_input"]]
cast_shadow = 0
mesh = SubResource("PlaneMesh_ypksr")

[node name="SpawnPoints" type="Node3D" parent="."]
Expand Down
1 change: 1 addition & 0 deletions source/match/Match.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ render_priority = 2
shader = ExtResource("13_jo8b7")
shader_parameter/color = Color(0, 0, 0, 1)
shader_parameter/texture_units_per_world_unit = 2
shader_parameter/outer_margin_for_fade_out = 2.0
shader_parameter/debug_texture_view = false
shader_parameter/world_visibility_texture = SubResource("ViewportTexture_7yqgo")

Expand Down
2 changes: 1 addition & 1 deletion source/match/maps/BigArena.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[sub_resource type="PlaneMesh" id="PlaneMesh_43n1t"]
resource_local_to_scene = true
material = ExtResource("2_hmoqb")
size = Vector2(100, 100)
size = Vector2(104, 104)
center_offset = Vector3(50, 0, 50)

[node name="Map" instance=ExtResource("1_c0hq7")]
Expand Down
2 changes: 1 addition & 1 deletion source/match/maps/PlainAndSimple.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[sub_resource type="PlaneMesh" id="PlaneMesh_frqq2"]
resource_local_to_scene = true
material = ExtResource("2_sguro")
size = Vector2(50, 50)
size = Vector2(54, 54)
center_offset = Vector3(25, 0, 25)

[node name="Map" instance=ExtResource("1_sqrvi")]
Expand Down
36 changes: 32 additions & 4 deletions source/shaders/3d/simple_fog_of_war.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ render_mode unshaded;
uniform vec4 color : source_color = vec4(vec3(0.0), 1.0);
uniform sampler2D world_visibility_texture : hint_default_white, filter_linear, repeat_disable;
uniform int texture_units_per_world_unit = 2;
uniform float outer_margin_for_fade_out = 2.0;
uniform bool debug_texture_view = false;
uniform sampler2D depth_texture : hint_depth_texture;

Expand Down Expand Up @@ -37,14 +38,15 @@ void fragment()
INV_VIEW_MATRIX * inverse(PROJECTION_MATRIX) * vec4(normalized_device_coordinates, 1.0);
world_position.xyz /= world_position.w;

if (depth == 1.0)
if (depth == 1.0) // infinite depth (OpenGL only)
{
ALPHA = 1.0;
}
else
{
vec2 world_visibility_texture_uv = world_position.xz /
vec2(textureSize(world_visibility_texture, 0)) * float(texture_units_per_world_unit);
vec2 world_visibility_texture_size_scaled =
vec2(textureSize(world_visibility_texture, 0)) / float(texture_units_per_world_unit);
vec2 world_visibility_texture_uv = world_position.xz / world_visibility_texture_size_scaled;
if (world_visibility_texture_uv.x >= 0.0 && world_visibility_texture_uv.x <= 1.0 &&
world_visibility_texture_uv.y >= 0.0 && world_visibility_texture_uv.y <= 1.0)
{
Expand All @@ -53,9 +55,35 @@ void fragment()
textureLod(world_visibility_texture, world_visibility_texture_uv, 0.0).r;
ALPHA = max(ALPHA, 1.0 - transparency_from_world_visibility_texture);
}
else if (
outer_margin_for_fade_out > 0.0 &&
((world_position.x < 0.0 && world_position.x >= -outer_margin_for_fade_out) ||
(world_position.x > world_visibility_texture_size_scaled.x &&
world_position.x <= world_visibility_texture_size_scaled.x + outer_margin_for_fade_out) ||
(world_position.z < 0.0 && world_position.z >= -outer_margin_for_fade_out) ||
(world_position.z > world_visibility_texture_size_scaled.y &&
world_position.z <= world_visibility_texture_size_scaled.y + outer_margin_for_fade_out)))
{
float max_distance_from_boundary = 0.0 - world_position.x;
max_distance_from_boundary = max(
max_distance_from_boundary, world_position.x - world_visibility_texture_size_scaled.x);
max_distance_from_boundary = max(max_distance_from_boundary, 0.0 - world_position.z);
max_distance_from_boundary = max(
max_distance_from_boundary, world_position.z - world_visibility_texture_size_scaled.y);
float alpha_from_margin =
smoothstep(0.0, outer_margin_for_fade_out, max_distance_from_boundary) * 0.2 + 0.8;
ALPHA = max(ALPHA, alpha_from_margin);

float alpha_from_world_visibility_texture = 0.0;
alpha_from_world_visibility_texture =
1.0 - textureLod(world_visibility_texture, world_visibility_texture_uv, 0.0).r;
ALPHA = max(ALPHA, alpha_from_world_visibility_texture);

ALPHA = min(ALPHA, 1.0);
}
else
{
ALPHA = 0.0;
ALPHA = 1.0;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/manual/TestPlayerVsAI.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ render_priority = 1
shader = ExtResource("12_cnst8")
shader_parameter/color = Color(0, 0, 0, 1)
shader_parameter/texture_units_per_world_unit = 2
shader_parameter/outer_margin_for_fade_out = 2.0
shader_parameter/debug_texture_view = false
shader_parameter/world_visibility_texture = SubResource("ViewportTexture_v46ss")

Expand Down
2 changes: 1 addition & 1 deletion tests/manual/maps/NonQuadratic.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[sub_resource type="PlaneMesh" id="PlaneMesh_0gpme"]
resource_local_to_scene = true
material = ExtResource("2_cl6u2")
size = Vector2(100, 50)
size = Vector2(104, 54)
center_offset = Vector3(50, 0, 25)

[node name="Map" instance=ExtResource("1_ame05")]
Expand Down

0 comments on commit f6573cd

Please sign in to comment.