Skip to content

Commit

Permalink
Implement paintable visual holes. World background now enum: none, fl…
Browse files Browse the repository at this point in the history
…at, noise
  • Loading branch information
TokisanGames committed Nov 18, 2023
1 parent 3f881a7 commit 2456e0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
4 changes: 0 additions & 4 deletions src/shaders/debug_views.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ R"(
float autoshader = float(control00 & 0x1u);
ALBEDO = vec3(autoshader);

//INSERT: DEBUG_HOLES
float holes = float(control00 >>2u & 0x1u);
ALBEDO = vec3(holes);

//INSERT: DEBUG_NAVIGATION
float navigation = float(control00 >>1u & 0x1u);
ALBEDO = vec3(navigation);
Expand Down
23 changes: 14 additions & 9 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ uniform sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap
uniform float _texture_uv_scale_array[32];
uniform float _texture_uv_rotation_array[32];
uniform vec4 _texture_color_array[32];
uniform bool _infinite_background = true;

// Public uniforms

Expand Down Expand Up @@ -96,15 +97,20 @@ void vertex() {
// UV coordinates in world space. Values are 0 to _region_size within regions
UV = v_vertex.xz;

// UV coordinates in region space + texel offset. Values are 0 to 1 within regions
UV2 = (UV + vec2(0.5)) * _region_texel_size;

// Get final vertex location and save it
VERTEX.y = get_height(UV2);
v_vertex = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
// Discard vertices if designated as a hole or background disabled. 1 lookup.
ivec3 ruv = get_region_uv(UV);
uint control = texelFetch(_control_maps, ruv, 0).r;
bool hole = bool(control >>2u & 0x1u);
if ( hole || ( !_infinite_background && ruv.z < 0 ) ) {
VERTEX.x = 0./0.;
} else {
// UV coordinates in region space + texel offset. Values are 0 to 1 within regions
UV2 = (UV + vec2(0.5)) * _region_texel_size;

// Flatten normal to be calculated in fragment()
NORMAL = vec3(0, 1, 0);
// Get final vertex location and save it
VERTEX.y = get_height(UV2);
v_vertex = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
}
}

////////////////////////
Expand Down Expand Up @@ -294,7 +300,6 @@ void fragment() {
//INSERT: DEBUG_CONTROL_TEXTURE
//INSERT: DEBUG_CONTROL_BLEND
//INSERT: DEBUG_AUTOSHADER
//INSERT: DEBUG_HOLES
//INSERT: DEBUG_NAVIGATION
//INSERT: DEBUG_TEXTURE_HEIGHT
//INSERT: DEBUG_TEXTURE_NORMAL
Expand Down
34 changes: 15 additions & 19 deletions src/terrain_3d_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ String Terrain3DMaterial::_parse_shader(String p_shader, String p_name, Array p_
String Terrain3DMaterial::_generate_shader_code() {
LOG(INFO, "Generating default shader code");
Array excludes;
if (!_world_noise_enabled) {
if (_world_background != NOISE) {
excludes.push_back("WORLD_NOISE1");
excludes.push_back("WORLD_NOISE2");
}
Expand Down Expand Up @@ -119,9 +119,6 @@ String Terrain3DMaterial::_generate_shader_code() {
if (!_debug_view_autoshader) {
excludes.push_back("DEBUG_AUTOSHADER");
}
if (!_debug_view_holes) {
excludes.push_back("DEBUG_HOLES");
}
if (!_debug_view_navigation) {
excludes.push_back("DEBUG_NAVIGATION");
}
Expand Down Expand Up @@ -287,6 +284,7 @@ void Terrain3DMaterial::_update_shader() {
Util::print_dict("_shader_params", _shader_params, DEBUG);
}

// Fetch saved shader parameters, converting textures to RIDs
for (int i = 0; i < _active_params.size(); i++) {
StringName param = _active_params[i];
Variant value = _shader_params[param];
Expand All @@ -302,6 +300,9 @@ void Terrain3DMaterial::_update_shader() {
}
}

// Set world background noise/flat/none
RS->material_set_param(_material, "_infinite_background", _world_background != NONE);

if (_active_params.has("noise_texture") && RS->material_get_param(_material, "noise_texture").get_type() == Variant::NIL) {
LOG(INFO, "Generating default noise_texture for shader");
Ref<FastNoiseLite> fnoise;
Expand Down Expand Up @@ -431,9 +432,9 @@ Variant Terrain3DMaterial::get_shader_param(const StringName &p_name) const {
return value;
}

void Terrain3DMaterial::set_world_noise_enabled(bool p_enabled) {
LOG(INFO, "Enable world noise: ", p_enabled);
_world_noise_enabled = p_enabled;
void Terrain3DMaterial::set_world_background(WorldBackground p_background) {
LOG(INFO, "Enable world background: ", p_background);
_world_background = p_background;
_update_shader();
}

Expand Down Expand Up @@ -485,12 +486,6 @@ void Terrain3DMaterial::set_show_autoshader(bool p_enabled) {
_update_shader();
}

void Terrain3DMaterial::set_show_holes(bool p_enabled) {
LOG(INFO, "Enable show_holes: ", p_enabled);
_debug_view_holes = p_enabled;
_update_shader();
}

void Terrain3DMaterial::set_show_navigation(bool p_enabled) {
LOG(INFO, "Enable show_navigation: ", p_enabled);
_debug_view_navigation = p_enabled;
Expand Down Expand Up @@ -656,6 +651,10 @@ bool Terrain3DMaterial::_get(const StringName &p_name, Variant &r_property) cons
}

void Terrain3DMaterial::_bind_methods() {
BIND_ENUM_CONSTANT(NONE);
BIND_ENUM_CONSTANT(FLAT);
BIND_ENUM_CONSTANT(NOISE);

// Private, but Public workaround until callable_mp is implemented
// https://github.com/godotengine/godot-cpp/pull/1155
ClassDB::bind_method(D_METHOD("_update_regions", "args"), &Terrain3DMaterial::_update_regions);
Expand All @@ -681,8 +680,8 @@ void Terrain3DMaterial::_bind_methods() {

ClassDB::bind_method(D_METHOD("get_region_blend_map"), &Terrain3DMaterial::get_region_blend_map);

ClassDB::bind_method(D_METHOD("set_world_noise_enabled", "enabled"), &Terrain3DMaterial::set_world_noise_enabled);
ClassDB::bind_method(D_METHOD("get_world_noise_enabled"), &Terrain3DMaterial::get_world_noise_enabled);
ClassDB::bind_method(D_METHOD("set_world_background", "background"), &Terrain3DMaterial::set_world_background);
ClassDB::bind_method(D_METHOD("get_world_background"), &Terrain3DMaterial::get_world_background);

ClassDB::bind_method(D_METHOD("set_show_checkered", "enabled"), &Terrain3DMaterial::set_show_checkered);
ClassDB::bind_method(D_METHOD("get_show_checkered"), &Terrain3DMaterial::get_show_checkered);
Expand All @@ -700,8 +699,6 @@ void Terrain3DMaterial::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_show_control_blend"), &Terrain3DMaterial::get_show_control_blend);
ClassDB::bind_method(D_METHOD("set_show_autoshader", "enabled"), &Terrain3DMaterial::set_show_autoshader);
ClassDB::bind_method(D_METHOD("get_show_autoshader"), &Terrain3DMaterial::get_show_autoshader);
ClassDB::bind_method(D_METHOD("set_show_holes", "enabled"), &Terrain3DMaterial::set_show_holes);
ClassDB::bind_method(D_METHOD("get_show_holes"), &Terrain3DMaterial::get_show_holes);
ClassDB::bind_method(D_METHOD("set_show_navigation", "enabled"), &Terrain3DMaterial::set_show_navigation);
ClassDB::bind_method(D_METHOD("get_show_navigation"), &Terrain3DMaterial::get_show_navigation);
ClassDB::bind_method(D_METHOD("set_show_texture_height", "enabled"), &Terrain3DMaterial::set_show_texture_height);
Expand All @@ -713,7 +710,7 @@ void Terrain3DMaterial::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_show_vertex_grid", "enabled"), &Terrain3DMaterial::set_show_vertex_grid);
ClassDB::bind_method(D_METHOD("get_show_vertex_grid"), &Terrain3DMaterial::get_show_vertex_grid);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "world_noise_enabled", PROPERTY_HINT_NONE), "set_world_noise_enabled", "get_world_noise_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "world_background", PROPERTY_HINT_ENUM, "None,Flat,Noise"), "set_world_background", "get_world_background");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shader_override_enabled", PROPERTY_HINT_NONE), "enable_shader_override", "is_shader_override_enabled");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shader_override", PROPERTY_HINT_RESOURCE_TYPE, "Shader"), "set_shader_override", "get_shader_override");

Expand All @@ -726,7 +723,6 @@ void Terrain3DMaterial::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_control_texture", PROPERTY_HINT_NONE), "set_show_control_texture", "get_show_control_texture");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_control_blend", PROPERTY_HINT_NONE), "set_show_control_blend", "get_show_control_blend");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_autoshader", PROPERTY_HINT_NONE), "set_show_autoshader", "get_show_autoshader");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_holes", PROPERTY_HINT_NONE), "set_show_holes", "get_show_holes");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_navigation", PROPERTY_HINT_NONE), "set_show_navigation", "get_show_navigation");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_texture_height", PROPERTY_HINT_NONE), "set_show_texture_height", "get_show_texture_height");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_texture_normal", PROPERTY_HINT_NONE), "set_show_texture_normal", "get_show_texture_normal");
Expand Down
17 changes: 12 additions & 5 deletions src/terrain_3d_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class Terrain3DMaterial : public Resource {
// Constants
static inline const char *__class__ = "Terrain3DMaterial";

enum WorldBackground {
NONE,
FLAT,
NOISE,
};

private:
bool _initialized = false;
RID _material;
Expand All @@ -26,8 +32,9 @@ class Terrain3DMaterial : public Resource {
mutable TypedArray<StringName> _active_params;
mutable Dictionary _shader_params;

WorldBackground _world_background = FLAT;

// Built in alternate shaders
bool _world_noise_enabled = false;
bool _debug_view_checkered = false;
bool _debug_view_grey = false;
bool _debug_view_heightmap = false;
Expand Down Expand Up @@ -82,8 +89,8 @@ class Terrain3DMaterial : public Resource {

RID get_region_blend_map() { return _generated_region_blend_map.get_rid(); }

void set_world_noise_enabled(bool p_enabled);
bool get_world_noise_enabled() const { return _world_noise_enabled; }
void set_world_background(WorldBackground p_background);
WorldBackground get_world_background() const { return _world_background; }

void set_show_checkered(bool p_enabled);
bool get_show_checkered() const { return _debug_view_checkered; }
Expand All @@ -101,8 +108,6 @@ class Terrain3DMaterial : public Resource {
bool get_show_control_blend() const { return _debug_view_control_blend; }
void set_show_autoshader(bool p_enabled);
bool get_show_autoshader() const { return _debug_view_autoshader; }
void set_show_holes(bool p_enabled);
bool get_show_holes() const { return _debug_view_holes; }
void set_show_navigation(bool p_enabled);
bool get_show_navigation() const { return _debug_view_navigation; }
void set_show_texture_height(bool p_enabled);
Expand All @@ -124,4 +129,6 @@ class Terrain3DMaterial : public Resource {
static void _bind_methods();
};

VARIANT_ENUM_CAST(Terrain3DMaterial::WorldBackground);

#endif // TERRAIN3D_MATERIAL_CLASS_H

0 comments on commit 2456e0d

Please sign in to comment.