diff --git a/assets/textures/terrain_blend/bricky_inner.png b/assets/textures/terrain_blend/bricky_inner.png new file mode 100644 index 0000000..b81107a Binary files /dev/null and b/assets/textures/terrain_blend/bricky_inner.png differ diff --git a/assets/textures/terrain_blend/bricky_inner.png.import b/assets/textures/terrain_blend/bricky_inner.png.import new file mode 100644 index 0000000..bc42c33 --- /dev/null +++ b/assets/textures/terrain_blend/bricky_inner.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://n7hbvgjiktlg" +path="res://.godot/imported/bricky_inner.png-b94122a758411344f70f7c2760d21808.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/bricky_inner.png" +dest_files=["res://.godot/imported/bricky_inner.png-b94122a758411344f70f7c2760d21808.image"] + +[params] + diff --git a/assets/textures/terrain_blend/bricky_outer.png b/assets/textures/terrain_blend/bricky_outer.png new file mode 100644 index 0000000..11e3a2d Binary files /dev/null and b/assets/textures/terrain_blend/bricky_outer.png differ diff --git a/assets/textures/terrain_blend/bricky_outer.png.import b/assets/textures/terrain_blend/bricky_outer.png.import new file mode 100644 index 0000000..c12e56b --- /dev/null +++ b/assets/textures/terrain_blend/bricky_outer.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://c8ntkfa6t0t4a" +path="res://.godot/imported/bricky_outer.png-8d14c741e6e00049a6f23c5a7f0ba31a.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/bricky_outer.png" +dest_files=["res://.godot/imported/bricky_outer.png-8d14c741e6e00049a6f23c5a7f0ba31a.image"] + +[params] + diff --git a/assets/textures/terrain_blend/bricky_straight.png b/assets/textures/terrain_blend/bricky_straight.png new file mode 100644 index 0000000..ce93c69 Binary files /dev/null and b/assets/textures/terrain_blend/bricky_straight.png differ diff --git a/assets/textures/terrain_blend/bricky_straight.png.import b/assets/textures/terrain_blend/bricky_straight.png.import new file mode 100644 index 0000000..40d9155 --- /dev/null +++ b/assets/textures/terrain_blend/bricky_straight.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://cl5gio1e8md5p" +path="res://.godot/imported/bricky_straight.png-6b7be3b817156c5655dc51c502e77349.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/bricky_straight.png" +dest_files=["res://.godot/imported/bricky_straight.png-6b7be3b817156c5655dc51c502e77349.image"] + +[params] + diff --git a/assets/textures/terrain_blend/default.png b/assets/textures/terrain_blend/default.png new file mode 100644 index 0000000..f93b7fb Binary files /dev/null and b/assets/textures/terrain_blend/default.png differ diff --git a/assets/textures/terrain_blend/default.png.import b/assets/textures/terrain_blend/default.png.import new file mode 100644 index 0000000..11d3eaf --- /dev/null +++ b/assets/textures/terrain_blend/default.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://bd65fhvitmpis" +path="res://.godot/imported/default.png-16f623a6df352f7770c4e23ca5d9106b.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/default.png" +dest_files=["res://.godot/imported/default.png-16f623a6df352f7770c4e23ca5d9106b.image"] + +[params] + diff --git a/assets/textures/terrain_blend/grassy_inner.png b/assets/textures/terrain_blend/grassy_inner.png new file mode 100644 index 0000000..deccb3b Binary files /dev/null and b/assets/textures/terrain_blend/grassy_inner.png differ diff --git a/assets/textures/terrain_blend/grassy_inner.png.import b/assets/textures/terrain_blend/grassy_inner.png.import new file mode 100644 index 0000000..3d918e9 --- /dev/null +++ b/assets/textures/terrain_blend/grassy_inner.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://cglldg71qap5f" +path="res://.godot/imported/grassy_inner.png-dbbcf6e38e5a9f6ac5aa0bd1d57058a3.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/grassy_inner.png" +dest_files=["res://.godot/imported/grassy_inner.png-dbbcf6e38e5a9f6ac5aa0bd1d57058a3.image"] + +[params] + diff --git a/assets/textures/terrain_blend/grassy_outer.png b/assets/textures/terrain_blend/grassy_outer.png new file mode 100644 index 0000000..0778204 Binary files /dev/null and b/assets/textures/terrain_blend/grassy_outer.png differ diff --git a/assets/textures/terrain_blend/grassy_outer.png.import b/assets/textures/terrain_blend/grassy_outer.png.import new file mode 100644 index 0000000..e460a2a --- /dev/null +++ b/assets/textures/terrain_blend/grassy_outer.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://cxwk1srj6obxh" +path="res://.godot/imported/grassy_outer.png-23c05e3ec7820ad524433733076341dc.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/grassy_outer.png" +dest_files=["res://.godot/imported/grassy_outer.png-23c05e3ec7820ad524433733076341dc.image"] + +[params] + diff --git a/assets/textures/terrain_blend/grassy_straight.png b/assets/textures/terrain_blend/grassy_straight.png new file mode 100644 index 0000000..b443361 Binary files /dev/null and b/assets/textures/terrain_blend/grassy_straight.png differ diff --git a/assets/textures/terrain_blend/grassy_straight.png.import b/assets/textures/terrain_blend/grassy_straight.png.import new file mode 100644 index 0000000..1a351cb --- /dev/null +++ b/assets/textures/terrain_blend/grassy_straight.png.import @@ -0,0 +1,14 @@ +[remap] + +importer="image" +type="Image" +uid="uid://cqgah04b0buil" +path="res://.godot/imported/grassy_straight.png-f11f767de0d78d943ad64f3bc3a0dc4c.image" + +[deps] + +source_file="res://assets/textures/terrain_blend/grassy_straight.png" +dest_files=["res://.godot/imported/grassy_straight.png-f11f767de0d78d943ad64f3bc3a0dc4c.image"] + +[params] + diff --git a/terrain/Terrain.cs b/terrain/Terrain.cs index 7c17ba2..8e8e4eb 100644 --- a/terrain/Terrain.cs +++ b/terrain/Terrain.cs @@ -149,7 +149,13 @@ public partial class Terrain var minDiff = Abs(sorted[0].Value.Y - sorted[2].Value.Y); // Difference between lowest and 3rd lowest point. var maxDiff = Abs(sorted[3].Value.Y - sorted[1].Value.Y); // Difference between highest and 3rd highest point. - mesh.SurfaceSetUV2(new(rnd.Next(4), 0.0f)); +// Randomly select two different textures and one blend texture. + mesh.SurfaceSetColor(new( + rnd.NextSingle(), +rnd.NextSingle(), + rnd.NextSingle() +)); + var first = sorted[(minDiff > maxDiff) ? 0 : 3].Corner; if (first is Corner.TopLeft or Corner.BottomRight) { AddTriangle(corners.TopLeft , new(0.0f, 0.0f), diff --git a/terrain/terrain_material.tres b/terrain/terrain_material.tres index 6ba8a5a..74e961d 100644 --- a/terrain/terrain_material.tres +++ b/terrain/terrain_material.tres @@ -1,15 +1,28 @@ -[gd_resource type="ShaderMaterial" load_steps=7 format=4 uid="uid://doe8owgx4jeu1"] +[gd_resource type="ShaderMaterial" load_steps=15 format=4 uid="uid://doe8owgx4jeu1"] [ext_resource type="Shader" path="res://terrain/terrain_shader.gdshader" id="1_mf55c"] + [ext_resource type="Image" uid="uid://b0jp1dyxugbr7" path="res://assets/textures/terrain/grass.png" id="5_mau31"] [ext_resource type="Image" uid="uid://bpo7mkr6sctqr" path="res://assets/textures/terrain/dirt.png" id="6_xc3ue"] [ext_resource type="Image" uid="uid://dqyqg6yt7yk3k" path="res://assets/textures/terrain/rock.png" id="7_84wdo"] [ext_resource type="Image" uid="uid://bkwjxg6g2itag" path="res://assets/textures/terrain/sand.png" id="8_3fpcn"] +[ext_resource type="Image" uid="uid://bd65fhvitmpis" path="res://assets/textures/terrain_blend/default.png" id="5_qdlds"] +[ext_resource type="Image" uid="uid://cqgah04b0buil" path="res://assets/textures/terrain_blend/grassy_straight.png" id="6_s3ccy"] +[ext_resource type="Image" uid="uid://cxwk1srj6obxh" path="res://assets/textures/terrain_blend/grassy_outer.png" id="7_wgbkx"] +[ext_resource type="Image" uid="uid://cglldg71qap5f" path="res://assets/textures/terrain_blend/grassy_inner.png" id="8_dm7dx"] +[ext_resource type="Image" uid="uid://cl5gio1e8md5p" path="res://assets/textures/terrain_blend/bricky_straight.png" id="9_fwpox"] +[ext_resource type="Image" uid="uid://c8ntkfa6t0t4a" path="res://assets/textures/terrain_blend/bricky_outer.png" id="10_kq5au"] +[ext_resource type="Image" uid="uid://n7hbvgjiktlg" path="res://assets/textures/terrain_blend/bricky_inner.png" id="11_0i2i8"] + [sub_resource type="Texture2DArray" id="Texture2DArray_3nq13"] _images = Array[Image]([ExtResource("5_mau31"), ExtResource("6_xc3ue"), ExtResource("7_84wdo"), ExtResource("8_3fpcn")]) +[sub_resource type="Texture2DArray" id="Texture2DArray_3nq14"] +_images = Array[Image]([ExtResource("5_qdlds"), ExtResource("6_s3ccy"), ExtResource("7_wgbkx"), ExtResource("8_dm7dx"), ExtResource("9_fwpox"), ExtResource("10_kq5au"), ExtResource("11_0i2i8")]) + [resource] render_priority = 0 shader = ExtResource("1_mf55c") shader_parameter/textures = SubResource("Texture2DArray_3nq13") +shader_parameter/blend_textures = SubResource("Texture2DArray_3nq14") diff --git a/terrain/terrain_shader.gdshader b/terrain/terrain_shader.gdshader index f07119c..d5939ac 100644 --- a/terrain/terrain_shader.gdshader +++ b/terrain/terrain_shader.gdshader @@ -1,10 +1,22 @@ shader_type spatial; render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx; -uniform sampler2DArray textures : source_color, filter_linear_mipmap, repeat_enable; +uniform sampler2DArray textures : source_color, filter_linear_mipmap, repeat_enable; +uniform sampler2DArray blend_textures : source_color, filter_nearest_mipmap, repeat_enable; void fragment() { - ALBEDO = texture(textures, vec3(UV, UV2.x)).rgb; + int num_textures = textureSize(textures, 0).z; + int num_blend_textures = textureSize(blend_textures, 0).z; + + float tex_primary_index = COLOR.r * float(num_textures); + float tex_secondary_index = COLOR.g * float(num_textures); + float tex_blend_index = COLOR.b * float(num_blend_textures); + + vec4 tex_primary = texture(textures, vec3(UV, tex_primary_index)); + vec4 tex_secondary = texture(textures, vec3(UV, tex_secondary_index)); + + vec4 blend = texture(blend_textures, vec3(UV, tex_blend_index)); + ALBEDO = mix(tex_secondary, tex_primary, blend.a).rgb; METALLIC = 0.0; SPECULAR = 0.5;