Skip to content

Commit

Permalink
fixed shading of beveled lines
Browse files Browse the repository at this point in the history
added brightness adjustment for each instance
  • Loading branch information
DmitriySalnikov committed Dec 8, 2023
1 parent 5556d6c commit 054f2dc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/3d/config_scoped_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ void DDScopedConfig3D::_bind_methods() {

REG_METHOD(set_thickness, "value");
REG_METHOD(get_thickness);

REG_METHOD(set_center_brightness, "value");
REG_METHOD(get_center_brightness);
#undef REG_CLASS_NAME
}

Expand All @@ -27,12 +30,22 @@ real_t DDScopedConfig3D::get_thickness() {
return thickness;
}

Ref<DDScopedConfig3D> DDScopedConfig3D::set_center_brightness(real_t value) {
center_brightness = Math::clamp(value, (real_t)0, (real_t)1);
return Ref<DDScopedConfig3D>(this);
}

real_t DDScopedConfig3D::get_center_brightness() {
return center_brightness;
}

DDScopedConfig3D::DDScopedConfig3D() {
unregister_action = nullptr;
thread_id = 0;
guard_id = 0;

thickness = 0;
center_brightness = 0;
}

DDScopedConfig3D::DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const Ref<DDScopedConfig3D> parent, const unregister_func p_unreg) {
Expand All @@ -42,6 +55,7 @@ DDScopedConfig3D::DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &
guard_id = p_guard_id;

thickness = parent->thickness;
center_brightness = parent->center_brightness;
}

DDScopedConfig3D::~DDScopedConfig3D() {
Expand Down
6 changes: 6 additions & 0 deletions src/3d/config_scoped_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ class DDScopedConfig3D : public RefCounted {

typedef std::function<void(uint64_t, uint64_t)> unregister_func;
unregister_func unregister_action;

// Update constructor!
real_t thickness;
real_t center_brightness;

public:
void _manual_unregister();

Ref<DDScopedConfig3D> set_thickness(real_t value);
real_t get_thickness();

Ref<DDScopedConfig3D> set_center_brightness(real_t value);
real_t get_center_brightness();

DDScopedConfig3D();

// `DDScopedConfig3D` is passed as Ref to avoid a random unreference
Expand Down
9 changes: 6 additions & 3 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ GODOT_WARNING_RESTORE()

DebugDraw3D *DebugDraw3D::singleton = nullptr;
const char *DebugDraw3D::s_add_bevel_to_volumetric = "add_bevel_to_volumetric_geometry";
const char *DebugDraw3D::s_default_thickness = "default_volumetric_thickness";
const char *DebugDraw3D::s_default_thickness = "volumetric_defaults/thickness";
const char *DebugDraw3D::s_default_center_brightness = "volumetric_defaults/center_brightness";

void DebugDraw3D::_bind_methods() {
#define REG_CLASS_NAME DebugDraw3D
Expand Down Expand Up @@ -102,12 +103,14 @@ void DebugDraw3D::init(DebugDrawManager *root) {

root_settings_section = String(Utils::root_settings_section) + "3d/";
DEFINE_SETTING_AND_GET(bool add_bevel, root_settings_section + s_add_bevel_to_volumetric, true, Variant::BOOL);
DEFINE_SETTING_AND_GET(real_t def_thickness, root_settings_section + s_default_thickness, 0.05f, Variant::FLOAT);
DEFINE_SETTING_AND_GET_HINT(real_t def_thickness, root_settings_section + s_default_thickness, 0.05f, Variant::FLOAT, PROPERTY_HINT_RANGE, "0,100,0.0001");
DEFINE_SETTING_AND_GET_HINT(real_t def_brightness, root_settings_section + s_default_center_brightness, 0.8f, Variant::FLOAT, PROPERTY_HINT_RANGE, "0,1,0.0001");

stats_3d.instantiate();
default_scoped_config.instantiate();

default_scoped_config->set_thickness(def_thickness);
default_scoped_config->set_center_brightness(def_brightness);

_load_materials();

Expand Down Expand Up @@ -173,7 +176,7 @@ DebugDraw3D::GeometryType DebugDraw3D::_scoped_config_get_geometry_type(DDScoped
Color DebugDraw3D::_scoped_config_to_custom(DDScopedConfig3D *cfg) {
ZoneScoped;
if (_scoped_config_get_geometry_type(cfg) == GeometryType::Volumetric)
return Color(cfg->get_thickness(), 0, 0, 0);
return Color(cfg->get_thickness(), cfg->get_center_brightness(), 0, 0);

return Color();
}
Expand Down
1 change: 1 addition & 0 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DebugDraw3D : public Object, public IScopedStorage<DDScopedConfig3D> {
String root_settings_section;
const static char *s_add_bevel_to_volumetric;
const static char *s_default_thickness;
const static char *s_default_center_brightness;

std::vector<SubViewport *> custom_editor_viewports;
DebugDrawManager *root_node = nullptr;
Expand Down
16 changes: 8 additions & 8 deletions src/3d/geometry_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,25 +377,25 @@ void GeometryGenerator::GenerateVolumetricSegmentBevel(const Vector3 &a, const V
vertexes.push_back(pos_b + left_b * debug_mult); // global 5, local 3

indexes.append(base_idx + 0);
indexes.append(start_idx + 2);
indexes.append(start_idx + 0);
indexes.append(start_idx + 1);

indexes.append(base_idx + 0);
indexes.append(base_idx + 1);
indexes.append(start_idx + 0);
indexes.append(start_idx + 2);

indexes.append(start_idx + 1);
indexes.append(base_idx + 1);
indexes.append(base_idx + 0);

indexes.append(start_idx + 1);
indexes.append(start_idx + 3);
indexes.append(start_idx + 2);

indexes.append(start_idx + 2);
indexes.append(base_idx + 1);
indexes.append(start_idx + 3);

uv.push_back(Vector2(1, 0));
uv.push_back(Vector2(1, 1));
uv.push_back(Vector2(0, 0));
uv.push_back(Vector2(1, 1));
uv.push_back(Vector2(0, 1));
uv.push_back(Vector2(0, 0));

custom0.push_back(right_a);
custom0.push_back(left_a);
Expand Down
5 changes: 3 additions & 2 deletions src/resources/extendable_meshes.gdshader
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
shader_type spatial;
render_mode cull_disabled, shadows_disabled, unshaded, world_vertex_coords, depth_prepass_alpha;

uniform float shade_power : hint_range(0.0, 1.0, 0.01) = 0.8f;
varying float brightness_of_center;

mat3 orthonormalize(mat3 m) {
vec3 x = normalize(m[0]);
Expand All @@ -11,6 +11,7 @@ mat3 orthonormalize(mat3 m) {
}

void vertex() {
brightness_of_center = INSTANCE_CUSTOM.y;
VERTEX = VERTEX + (CUSTOM0.xyz * INSTANCE_CUSTOM.x) * orthonormalize(inverse(mat3(normalize(MODEL_MATRIX[0].xyz), normalize(MODEL_MATRIX[1].xyz), normalize(MODEL_MATRIX[2].xyz))));
}

Expand All @@ -19,7 +20,7 @@ vec3 toLinearFast(vec3 col) {
}

void fragment() {
ALBEDO = mix(COLOR.xyz * shade_power, COLOR.xyz, pow(length(UV * 2.0 - 1.0), .7));
ALBEDO = mix(COLOR.xyz * brightness_of_center, COLOR.xyz, clamp(length(UV * 1.8 - 0.9), 0, 1));
if (!OUTPUT_IS_SRGB)
ALBEDO = toLinearFast(ALBEDO);
NORMAL = ALBEDO;
Expand Down

0 comments on commit 054f2dc

Please sign in to comment.