Allow tiles to blend between two textures

main
copygirl 3 months ago
parent 7cab61de50
commit 464c0f3f1c
  1. BIN
      assets/textures/terrain_blend/bricky_inner.png
  2. 14
      assets/textures/terrain_blend/bricky_inner.png.import
  3. BIN
      assets/textures/terrain_blend/bricky_outer.png
  4. 14
      assets/textures/terrain_blend/bricky_outer.png.import
  5. BIN
      assets/textures/terrain_blend/bricky_straight.png
  6. 14
      assets/textures/terrain_blend/bricky_straight.png.import
  7. BIN
      assets/textures/terrain_blend/default.png
  8. 14
      assets/textures/terrain_blend/default.png.import
  9. BIN
      assets/textures/terrain_blend/grassy_inner.png
  10. 14
      assets/textures/terrain_blend/grassy_inner.png.import
  11. BIN
      assets/textures/terrain_blend/grassy_outer.png
  12. 14
      assets/textures/terrain_blend/grassy_outer.png.import
  13. BIN
      assets/textures/terrain_blend/grassy_straight.png
  14. 14
      assets/textures/terrain_blend/grassy_straight.png.import
  15. 8
      terrain/Terrain.cs
  16. 15
      terrain/terrain_material.tres
  17. 14
      terrain/terrain_shader.gdshader

Binary file not shown.

After

Width:  |  Height:  |  Size: 622 B

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 639 B

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 650 B

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 582 B

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

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

@ -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 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. 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; var first = sorted[(minDiff > maxDiff) ? 0 : 3].Corner;
if (first is Corner.TopLeft or Corner.BottomRight) { if (first is Corner.TopLeft or Corner.BottomRight) {
AddTriangle(corners.TopLeft , new(0.0f, 0.0f), AddTriangle(corners.TopLeft , new(0.0f, 0.0f),

@ -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="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://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://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://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://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"] [sub_resource type="Texture2DArray" id="Texture2DArray_3nq13"]
_images = Array[Image]([ExtResource("5_mau31"), ExtResource("6_xc3ue"), ExtResource("7_84wdo"), ExtResource("8_3fpcn")]) _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] [resource]
render_priority = 0 render_priority = 0
shader = ExtResource("1_mf55c") shader = ExtResource("1_mf55c")
shader_parameter/textures = SubResource("Texture2DArray_3nq13") shader_parameter/textures = SubResource("Texture2DArray_3nq13")
shader_parameter/blend_textures = SubResource("Texture2DArray_3nq14")

@ -2,9 +2,21 @@ shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx; 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() { 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; METALLIC = 0.0;
SPECULAR = 0.5; SPECULAR = 0.5;

Loading…
Cancel
Save