Skip to content

Commit b8d3312

Browse files
committed
insert search code only when none bg
1 parent d80c8e4 commit b8d3312

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/shaders/backgrounds.glsl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
// Copyright © 2025 Cory Petkovsek, Roope Palmroos, and Contributors.
22

33
R"(
4+
//INSERT: INDEX_COORD_STANDARD
5+
ivec3 get_index_coord(const vec2 uv, const int search) {
6+
vec2 r_uv = round(uv);
7+
ivec2 pos = ivec2(floor(r_uv * _region_texel_size)) + (_region_map_size / 2);
8+
int bounds = int(uint(pos.x | pos.y) < uint(_region_map_size));
9+
int layer_index = _region_map[pos.y * _region_map_size + pos.x] * bounds - 1;
10+
return ivec3(ivec2(mod(r_uv, _region_size)), layer_index);
11+
}
12+
13+
//INSERT: INDEX_COORD_BG_NONE
14+
ivec3 get_index_coord(const vec2 uv, const int search) {
15+
vec2 r_uv = round(uv);
16+
vec2 o_uv = mod(r_uv, _region_size);
17+
ivec2 pos;
18+
int bounds, layer_index = -1;
19+
for (int i = -1; i < clamp(search, SKIP_PASS, FRAGMENT_PASS); i++) {
20+
if ((layer_index == -1 && _background_mode == 0u ) || i < 0) {
21+
r_uv -= i == -1 ? vec2(0.0) : vec2(float(o_uv.x <= o_uv.y), float(o_uv.y <= o_uv.x));
22+
pos = ivec2(floor((r_uv) * _region_texel_size)) + (_region_map_size / 2);
23+
bounds = int(uint(pos.x | pos.y) < uint(_region_map_size));
24+
layer_index = (_region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1);
25+
}
26+
}
27+
return ivec3(ivec2(mod(r_uv, _region_size)), layer_index);
28+
}
29+
430
//INSERT: FLAT_UNIFORMS
531
uniform float ground_level : hint_range(-1000., 1000.) = 0.0;
632
uniform float region_blend : hint_range(.001, 1., 0.001) = 0.25;

src/shaders/main.glsl

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,8 @@ varying vec3 v_camera_pos;
118118
// Returns ivec3 with:
119119
// XY: (0 to _region_size - 1) coordinates within a region
120120
// Z: layer index used for texturearrays, -1 if not in a region
121-
ivec3 get_index_coord(const vec2 uv, const int search) {
122-
vec2 r_uv = round(uv);
123-
vec2 o_uv = mod(r_uv, _region_size);
124-
ivec2 pos;
125-
int bounds, layer_index = -1;
126-
for (int i = -1; i < clamp(search, SKIP_PASS, FRAGMENT_PASS); i++) {
127-
if ((layer_index == -1 && _background_mode == 0u ) || i < 0) {
128-
r_uv -= i == -1 ? vec2(0.0) : vec2(float(o_uv.x <= o_uv.y), float(o_uv.y <= o_uv.x));
129-
pos = ivec2(floor((r_uv) * _region_texel_size)) + (_region_map_size / 2);
130-
bounds = int(uint(pos.x | pos.y) < uint(_region_map_size));
131-
layer_index = (_region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1);
132-
}
133-
}
134-
return ivec3(ivec2(mod(r_uv, _region_size)), layer_index);
135-
}
121+
//INSERT: INDEX_COORD_STANDARD
122+
//INSERT: INDEX_COORD_BG_NONE
136123

137124
// Takes in descaled (world_space / region_size) world to region space XZ (UV2) coordinates, returns vec3 with:
138125
// XY: (0. to 1.) coordinates within a region

src/terrain_3d_material.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ String Terrain3DMaterial::_apply_inserts(const String &p_shader, const Array &p_
139139
String Terrain3DMaterial::_generate_shader_code() const {
140140
LOG(INFO, "Generating default shader code");
141141
Array excludes;
142+
if (_world_background != NONE) {
143+
excludes.push_back("INDEX_COORD_BG_NONE");
144+
} else {
145+
excludes.push_back("INDEX_COORD_STANDARD");
146+
}
142147
if (_world_background != FLAT) {
143148
excludes.push_back("FLAT_UNIFORMS");
144149
excludes.push_back("FLAT_FUNCTIONS");

0 commit comments

Comments
 (0)