Skip to content

Commit 2ddf03a

Browse files
committed
Adjust groups for parameters and uniforms
1 parent f1b19d5 commit 2ddf03a

File tree

14 files changed

+78
-74
lines changed

14 files changed

+78
-74
lines changed

Terrain3D.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@
283283
<None Include="doc\docs\displacement.md">
284284
<Filter>2. Docs</Filter>
285285
</None>
286+
<None Include="src\shaders\macro_variation.glsl">
287+
<Filter>4. Shaders</Filter>
288+
</None>
289+
<None Include="src\shaders\projection.glsl">
290+
<Filter>4. Shaders</Filter>
291+
</None>
286292
</ItemGroup>
287293
<ItemGroup>
288294
<Text Include=".readthedocs.yaml">

project/addons/terrain_3d/extras/shaders/lightweight.gdshader

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright © 2025 Cory Petkovsek, Roope Palmroos, and Contributors.
2+
// This shader is the minimum needed to allow the terrain to function, without any texturing.
3+
14
shader_type spatial;
25
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx,skip_vertex_transform;
36

@@ -13,9 +16,6 @@ render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlic
1316
*/
1417

1518
// Defined Constants
16-
#define SKIP_PASS 0
17-
#define VERTEX_PASS 1
18-
#define FRAGMENT_PASS 2
1919
#define COLOR_MAP vec4(1.0, 1.0, 1.0, 0.5)
2020
#define DIV_255 0.003921568627450 // 1. / 255.
2121

@@ -33,6 +33,7 @@ render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlic
3333
#endif
3434

3535
// Private uniforms
36+
group_uniforms shader_uniforms;
3637
uniform vec3 _target_pos = vec3(0.f);
3738
uniform float _mesh_size = 48.f;
3839
uniform uint _background_mode = 1u; // NONE = 0, FLAT = 1, NOISE = 2
@@ -56,24 +57,25 @@ uniform highp sampler2DArray _control_maps : repeat_disable;
5657
uniform highp sampler2DArray _color_maps : source_color, filter_linear_mipmap_anisotropic, repeat_disable;
5758
uniform highp sampler2DArray _texture_array_albedo : source_color, filter_linear_mipmap_anisotropic, repeat_enable;
5859
uniform highp sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
60+
group_uniforms;
5961

6062
// Public uniforms
61-
group_uniforms general;
63+
group_uniforms shader_uniforms.general;
6264
uniform float ground_level : hint_range(-1000., 1000.) = 0.0;
6365
uniform float region_blend : hint_range(.001, 1., 0.001) = 0.25;
6466
uniform bool flat_terrain_normals = false;
65-
uniform bool enable_textures = true;
67+
uniform bool textures_enabled = true;
6668
uniform float blend_sharpness : hint_range(0, 1) = 0.5;
6769
group_uniforms;
6870

69-
group_uniforms auto_shader;
71+
group_uniforms shader_uniforms.auto_shader;
7072
uniform float auto_slope : hint_range(0, 10) = 1.0;
7173
uniform float auto_height_reduction : hint_range(0, 1) = 0.1;
7274
uniform int auto_base_texture : hint_range(0, 31) = 0;
7375
uniform int auto_overlay_texture : hint_range(0, 31) = 1;
7476
group_uniforms;
7577

76-
group_uniforms macro_variation;
78+
group_uniforms shader_uniforms.macro_variation;
7779
uniform bool macro_variation_enabled = true;
7880
uniform vec3 macro_variation1 : source_color = vec3(1.);
7981
uniform vec3 macro_variation2 : source_color = vec3(1.);
@@ -232,13 +234,13 @@ void vertex() {
232234
u = mix(texelFetch(_height_maps, coord_ua, 0).r, texelFetch(_height_maps, coord_ub, 0).r, vertex_lerp);
233235
v = mix(texelFetch(_height_maps, coord_va, 0).r, texelFetch(_height_maps, coord_vb, 0).r, vertex_lerp);
234236
}
235-
237+
236238
// Apply background ground level and region blend
237239
vec2 ruv = UV * _region_texel_size;
238240
h += ground_level * smoothstep(1.0 - region_blend, 1.0, get_region_blend(ruv));
239241
u += ground_level * smoothstep(1.0 - region_blend, 1.0, get_region_blend(ruv + vec2(_region_texel_size, 0.)));
240242
v += ground_level * smoothstep(1.0 - region_blend, 1.0, get_region_blend(ruv + vec2(0., _region_texel_size)));
241-
243+
242244
v_vertex.y = h;
243245
v_normal = normalize(vec3(h - u, _vertex_spacing, h - v));
244246
}
@@ -312,7 +314,7 @@ void fragment() {
312314
float ao = 1.0;
313315
float ao_affect = 0.;
314316

315-
if (enable_textures) {
317+
if (textures_enabled) {
316318
// set to zero before accumulation
317319
albedo_height = vec4(0.);
318320
normal_rough = vec4(0.);
@@ -334,9 +336,9 @@ void fragment() {
334336
float auto_blend = clamp(fma(auto_slope * 2.0, (v_normal.y - 1.0), 1.0)
335337
- auto_height_reduction * 0.01 * v_vertex.y, 0.0, 1.0);
336338
// Enable Autoshader if outside regions or painted in regions, otherwise manual painted
337-
uvec4 is_auto = (control & uvec4(0x1u)) |
339+
uvec4 is_auto = (control & uvec4(0x1u)) |
338340
uvec4(lessThan(ivec4(index[0].z, index[1].z, index[2].z, index[3].z), ivec4(0)));
339-
uint u_auto =
341+
uint u_auto =
340342
((uint(auto_base_texture) & 0x1Fu) << 27u) |
341343
((uint(auto_overlay_texture) & 0x1Fu) << 22u) |
342344
((uint(fma(auto_blend, 255.0 , 0.5)) & 0xFFu) << 14u);

project/addons/terrain_3d/extras/shaders/minimum.gdshader

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
shader_type spatial;
55
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx,skip_vertex_transform;
66

7-
// Defined Constants
8-
#define SKIP_PASS 0
9-
#define VERTEX_PASS 1
10-
#define FRAGMENT_PASS 2
11-
127
#if CURRENT_RENDERER == RENDERER_COMPATIBILITY
138
#define fma(a, b, c) ((a) * (b) + (c))
149
#define dFdxCoarse(a) dFdx(a)
@@ -17,15 +12,16 @@ render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlic
1712

1813
// Private uniforms
1914
// Commented uniforms aren't needed for this shader, but are available for your own needs.
15+
group_uniforms shader_uniforms;
2016
uniform vec3 _target_pos = vec3(0.f);
2117
uniform float _mesh_size = 48.f;
2218
uniform float _subdiv = 1.f;
2319
uniform uint _background_mode = 1u; // NONE = 0, FLAT = 1, NOISE = 2
2420
uniform uint _mouse_layer = 0x80000000u; // Layer 32
2521
uniform float _vertex_spacing = 1.0;
26-
uniform float _vertex_density = 1.0; // = 1/_vertex_spacing
22+
uniform float _vertex_density = 1.0; // = 1./_vertex_spacing
2723
uniform float _region_size = 1024.0;
28-
uniform float _region_texel_size = 0.0009765625; // = 1/1024
24+
uniform float _region_texel_size = 0.0009765625; // = 1./region_size
2925
uniform int _region_map_size = 32;
3026
uniform int _region_map[1024];
3127
//uniform vec2 _region_locations[1024];
@@ -41,9 +37,12 @@ uniform highp sampler2DArray _control_maps : repeat_disable;
4137
//uniform highp sampler2DArray _color_maps : source_color, filter_linear_mipmap_anisotropic, repeat_disable;
4238
//uniform highp sampler2DArray _texture_array_albedo : source_color, filter_linear_mipmap_anisotropic, repeat_enable;
4339
//uniform highp sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
40+
group_uniforms;
4441

45-
// Public Uniforms
42+
// Public uniforms
43+
group_uniforms shader_uniforms.general;
4644
uniform bool flat_terrain_normals = false;
45+
group_uniforms;
4746

4847
// Varyings & Types
4948
// Some are required for editor functions
@@ -55,24 +54,16 @@ varying vec3 v_camera_pos;
5554
// Vertex
5655
////////////////////////
5756

58-
// Takes in world space XZ (UV) coordinates & search depth (only applicable for background mode none)
57+
// Takes in world space XZ (UV) coordinates
5958
// Returns ivec3 with:
6059
// XY: (0 to _region_size - 1) coordinates within a region
6160
// Z: layer index used for texturearrays, -1 if not in a region
62-
ivec3 get_index_coord(const vec2 uv, const int search) {
61+
ivec3 get_index_coord(const vec2 uv) {
6362
vec2 r_uv = round(uv);
64-
vec2 o_uv = mod(r_uv,_region_size);
65-
ivec2 pos;
66-
int bounds, layer_index = -1;
67-
for (int i = -1; i < clamp(search, SKIP_PASS, FRAGMENT_PASS); i++) {
68-
if ((layer_index == -1 && _background_mode == 0u ) || i < 0) {
69-
r_uv -= i == -1 ? vec2(0.0) : vec2(float(o_uv.x <= o_uv.y), float(o_uv.y <= o_uv.x));
70-
pos = ivec2(floor((r_uv) * _region_texel_size)) + (_region_map_size / 2);
71-
bounds = int(uint(pos.x | pos.y) < uint(_region_map_size));
72-
layer_index = (_region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1);
73-
}
74-
}
75-
return ivec3(ivec2(mod(r_uv,_region_size)), layer_index);
63+
ivec2 pos = ivec2(floor(r_uv * _region_texel_size)) + (_region_map_size / 2);
64+
int bounds = int(uint(pos.x | pos.y) < uint(_region_map_size));
65+
int layer_index = _region_map[pos.y * _region_map_size + pos.x] * bounds - 1;
66+
return ivec3(ivec2(mod(r_uv, _region_size)), layer_index);
7667
}
7768

7869
// Takes in world space XZ (UV) and returns if the coordinate should be part of the NONE background.
@@ -89,10 +80,10 @@ float interpolated_height(vec2 pos) {
8980
const vec2 offsets = vec2(0, 1);
9081
vec2 index_id = floor(pos);
9182
ivec3 index[4];
92-
index[0] = get_index_coord(index_id + offsets.xy, VERTEX_PASS);
93-
index[1] = get_index_coord(index_id + offsets.yy, VERTEX_PASS);
94-
index[2] = get_index_coord(index_id + offsets.yx, VERTEX_PASS);
95-
index[3] = get_index_coord(index_id + offsets.xx, VERTEX_PASS);
83+
index[0] = get_index_coord(index_id + offsets.xy);
84+
index[1] = get_index_coord(index_id + offsets.yy);
85+
index[2] = get_index_coord(index_id + offsets.yx);
86+
index[3] = get_index_coord(index_id + offsets.xx);
9687
float h0 = texelFetch(_height_maps, index[0], 0).r;
9788
float h1 = texelFetch(_height_maps, index[1], 0).r;
9889
float h2 = texelFetch(_height_maps, index[2], 0).r;
@@ -139,7 +130,7 @@ void vertex() {
139130
UV2 = fma(UV, vec2(_region_texel_size), vec2(0.5 * _region_texel_size));
140131

141132
// Discard vertices for Holes. 1 lookup
142-
ivec3 v_region = get_index_coord(start_pos, VERTEX_PASS);
133+
ivec3 v_region = get_index_coord(start_pos);
143134
uint control = floatBitsToUint(texelFetch(_control_maps, v_region, 0)).r;
144135
bool hole = bool(control >>2u & 0x1u);
145136

@@ -155,8 +146,8 @@ void vertex() {
155146
if (scale < _vertex_spacing) {
156147
h = interpolated_height(UV);
157148
} else {
158-
ivec3 coord_a = get_index_coord(start_pos, VERTEX_PASS);
159-
ivec3 coord_b = get_index_coord(end_pos, VERTEX_PASS);
149+
ivec3 coord_a = get_index_coord(start_pos);
150+
ivec3 coord_b = get_index_coord(end_pos);
160151
h = mix(texelFetch(_height_maps, coord_a, 0).r,texelFetch(_height_maps, coord_b, 0).r,vertex_lerp);
161152
}
162153
v_vertex.y = h;
@@ -200,10 +191,10 @@ void fragment() {
200191

201192
ivec3 index[4];
202193
// control map lookups, used for some normal lookups as well
203-
index[0] = get_index_coord(index_id + offsets.xy, FRAGMENT_PASS);
204-
index[1] = get_index_coord(index_id + offsets.yy, FRAGMENT_PASS);
205-
index[2] = get_index_coord(index_id + offsets.yx, FRAGMENT_PASS);
206-
index[3] = get_index_coord(index_id + offsets.xx, FRAGMENT_PASS);
194+
index[0] = get_index_coord(index_id + offsets.xy);
195+
index[1] = get_index_coord(index_id + offsets.yy);
196+
index[2] = get_index_coord(index_id + offsets.yx);
197+
index[3] = get_index_coord(index_id + offsets.xx);
207198

208199
// Terrain normals
209200
vec3 index_normal[4];
@@ -226,10 +217,10 @@ void fragment() {
226217
// 5 lookups
227218
// Fetch the additional required height values for smooth normals
228219
h[1] = texelFetch(_height_maps, index[1], 0).r; // 3 (1,1)
229-
float h_4 = texelFetch(_height_maps, get_index_coord(index_id + offsets.yz, FRAGMENT_PASS), 0).r; // 4 (1,2)
230-
float h_5 = texelFetch(_height_maps, get_index_coord(index_id + offsets.zy, FRAGMENT_PASS), 0).r; // 5 (2,1)
231-
float h_6 = texelFetch(_height_maps, get_index_coord(index_id + offsets.zx, FRAGMENT_PASS), 0).r; // 6 (2,0)
232-
float h_7 = texelFetch(_height_maps, get_index_coord(index_id + offsets.xz, FRAGMENT_PASS), 0).r; // 7 (0,2)
220+
float h_4 = texelFetch(_height_maps, get_index_coord(index_id + offsets.yz), 0).r; // 4 (1,2)
221+
float h_5 = texelFetch(_height_maps, get_index_coord(index_id + offsets.zy), 0).r; // 5 (2,1)
222+
float h_6 = texelFetch(_height_maps, get_index_coord(index_id + offsets.zx), 0).r; // 6 (2,0)
223+
float h_7 = texelFetch(_height_maps, get_index_coord(index_id + offsets.xz), 0).r; // 7 (0,2)
233224

234225
// Calculate the normal for the remaining index ids.
235226
index_normal[0] = normalize(vec3(h[0] - h[1] + u, _vertex_spacing, h[0] - h_7 + v));

project/demo/Demo.tscn

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,35 @@ _shader_parameters = {
3636
"auto_base_texture": 0,
3737
"auto_height_reduction": 0.05,
3838
"auto_overlay_texture": 1,
39-
&"auto_shader": null,
4039
"auto_slope": 0.45,
4140
"bias_distance": 512.0,
4241
"blend_sharpness": 0.5,
4342
"depth_blur": 0.0,
44-
&"displacement": null,
4543
"dual_scale_far": 170.0,
4644
"dual_scale_near": 100.0,
4745
"dual_scale_reduction": 0.3,
4846
"dual_scale_texture": 0,
49-
&"dual_scaling": null,
5047
&"flat_terrain_normals": false,
51-
&"general": null,
5248
&"ground_level": 0.0,
53-
&"macro_variation": true,
5449
"macro_variation1": Color(0.855, 0.8625, 0.9, 1),
5550
"macro_variation2": Color(0.9, 0.885, 0.81, 1),
5651
"macro_variation_slope": 0.333,
5752
"mipmap_bias": 1.0,
58-
&"mipmaps": null,
5953
"noise1_angle": 0.1,
6054
"noise1_offset": Vector2(0.5, 0.5),
6155
"noise1_scale": 0.04,
6256
"noise2_scale": 0.076,
6357
"noise_texture": SubResource("NoiseTexture2D_bov7h"),
6458
&"region_blend": 0.25,
59+
&"shader_uniforms": null,
60+
&"shader_uniforms::auto_shader": null,
61+
&"shader_uniforms::displacement": null,
62+
&"shader_uniforms::dual_scaling": null,
63+
&"shader_uniforms::general": null,
64+
&"shader_uniforms::macro_variation": null,
65+
&"shader_uniforms::mipmaps": null,
66+
&"shader_uniforms::world_background_noise": null,
6567
"tri_scale_reduction": 0.3,
66-
&"world_background_noise": null,
6768
"world_noise_fragment_normals": false,
6869
"world_noise_height": 34.0,
6970
"world_noise_lod_distance": 7500.0,

src/shaders/auto_shader.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ R"(
44

55
//INSERT: AUTO_SHADER_UNIFORMS
66
#define AUTO_SHADER
7-
group_uniforms auto_shader;
7+
group_uniforms shader_uniforms.auto_shader;
88
uniform float auto_slope : hint_range(0, 10) = 1.0;
99
uniform float auto_height_reduction : hint_range(0, 1) = 0.1;
1010
uniform int auto_base_texture : hint_range(0, 31) = 0;

src/shaders/backgrounds.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ float get_region_blend(vec2 uv2) {
5555
}
5656

5757
//INSERT: WORLD_NOISE_UNIFORMS
58-
group_uniforms world_background_noise;
58+
group_uniforms shader_uniforms.world_background_noise;
5959
uniform bool world_noise_fragment_normals = false;
6060
uniform int world_noise_max_octaves : hint_range(0, 15) = 4;
6161
uniform int world_noise_min_octaves : hint_range(0, 15) = 2;

src/shaders/debug_views.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ R"(
3030
}
3131

3232
//INSERT: DEBUG_HEIGHTMAP_SETUP
33-
group_uniforms debug_heightmap;
33+
group_uniforms shader_uniforms.debug_heightmap;
3434
uniform float heightmap_black_height: hint_range(-2048.,2048.,.5) = -100.0;
3535
uniform float heightmap_white_height: hint_range(-2048.,2048.,.5) = 300.0;
3636
group_uniforms;

src/shaders/displacement_buffer.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ uniform highp sampler2DArray _texture_array_albedo : source_color, FILTER_METHOD
5959
uniform highp sampler2DArray _texture_array_normal : hint_normal, FILTER_METHOD, repeat_enable;
6060

6161
// Public uniforms
62-
group_uniforms general;
62+
group_uniforms shader_uniforms.general;
6363
uniform float blend_sharpness : hint_range(0, 1) = 0.5;
6464
group_uniforms;
6565
//INSERT: AUTO_SHADER_UNIFORMS
@@ -70,7 +70,7 @@ group_uniforms;
7070
// Uniquely named displacement uniforms should be in this group.
7171
// Uniforms that are shared with the main shader are automatically synchronised.
7272
// Subgroups should work as expected.
73-
group_uniforms displacement;
73+
group_uniforms shader_uniforms.displacement;
7474
uniform float _displacement_sharpness : hint_range(0.0, 1.0, 0.01) = 0.25;
7575
group_uniforms;
7676

src/shaders/dual_scaling.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
R"(
44

55
//INSERT: DUAL_SCALING_UNIFORMS
6-
group_uniforms dual_scaling;
6+
group_uniforms shader_uniforms.dual_scaling;
77
uniform int dual_scale_texture : hint_range(0,31) = 0;
88
uniform float dual_scale_reduction : hint_range(0.001,1) = 0.3;
99
uniform float tri_scale_reduction : hint_range(0.001,1) = 0.3;

src/shaders/macro_variation.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
R"(
44

55
//INSERT: MACRO_VARIATION_UNIFORMS
6-
group_uniforms macro_variation;
6+
group_uniforms shader_uniforms.macro_variation;
77
uniform vec3 macro_variation1 : source_color = vec3(1.);
88
uniform vec3 macro_variation2 : source_color = vec3(1.);
99
uniform float macro_variation_slope : hint_range(0., 1.) = 0.333;

0 commit comments

Comments
 (0)