From 81f0604ae00151f6c81e24d2ff28fde0dda0b912 Mon Sep 17 00:00:00 2001 From: DmitriySalnikov Date: Tue, 21 May 2024 17:42:56 +0300 Subject: [PATCH] Added the ability to change render_priority and transparency mode --- examples_dd3d/DebugDrawDemoScene.gd | 6 +++- examples_dd3d/DebugDrawDemoScene.tscn | 27 ++++++++++++++++- examples_dd3d/DebugDrawDemoSceneCS.cs | 10 +++++- src/3d/debug_draw_3d.cpp | 37 ++++++++++++++++++----- src/3d/debug_draw_3d.h | 3 ++ src/resources/billboard_unshaded.gdshader | 5 +++ src/resources/extendable_meshes.gdshader | 5 +++ src/resources/plane_unshaded.gdshader | 5 +++ src/resources/wireframe_unshaded.gdshader | 5 +++ src/utils/utils.h | 13 ++++++++ src/version.h | 2 +- 11 files changed, 107 insertions(+), 11 deletions(-) diff --git a/examples_dd3d/DebugDrawDemoScene.gd b/examples_dd3d/DebugDrawDemoScene.gd index 5aaa5812..64ac3974 100644 --- a/examples_dd3d/DebugDrawDemoScene.gd +++ b/examples_dd3d/DebugDrawDemoScene.gd @@ -275,6 +275,10 @@ func main_update(delta: float) -> void: var tn1 = $Grids/GridCentered/Subdivision.transform.origin DebugDraw3D.draw_grid_xf($Grids/GridCentered.global_transform, Vector2i(tn1.x*10, tn1.z*10)) + if true: + var _s32 = DebugDraw3D.new_scoped_config().set_thickness(0.05) + DebugDraw3D.draw_box_xf($PostProcess.global_transform, Color.SEA_GREEN) + # 2D DebugDraw2D.config.text_default_size = text_groups_default_font_size DebugDraw2D.config.text_block_offset = text_groups_offset @@ -302,7 +306,7 @@ func main_update(delta: float) -> void: # Lag Test $LagTest.position = $LagTest/RESET.get_animation("RESET").track_get_key_value(0,0) + Vector3(sin(Time.get_ticks_msec() / 100.0) * 2.5, 0, 0) - DebugDraw3D.draw_box($LagTest.global_position, Quaternion.IDENTITY, Vector3.ONE * 2.01, DebugDraw3D.empty_color, true) + DebugDraw3D.draw_box($LagTest.global_position, Quaternion.IDENTITY, Vector3.ONE * 2.01, Color.CHOCOLATE, true) if more_test_cases: for ray in $HitTest/RayEmitter.get_children(): diff --git a/examples_dd3d/DebugDrawDemoScene.tscn b/examples_dd3d/DebugDrawDemoScene.tscn index 0ddef81f..5233dc65 100644 --- a/examples_dd3d/DebugDrawDemoScene.tscn +++ b/examples_dd3d/DebugDrawDemoScene.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=36 format=3 uid="uid://c3sccy6x0ht5j"] +[gd_scene load_steps=39 format=3 uid="uid://c3sccy6x0ht5j"] [ext_resource type="Script" path="res://examples_dd3d/DebugDrawDemoScene.gd" id="1"] [ext_resource type="FontFile" uid="uid://erdgllynwqkw" path="res://examples_dd3d/Roboto-Bold.ttf" id="2_aedbq"] @@ -260,6 +260,26 @@ _data = { "RESET": SubResource("8") } +[sub_resource type="Shader" id="Shader_3cmiq"] +code = "shader_type spatial; +render_mode unshaded; + +uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; + +void fragment() { + vec4 col = texture(screen_texture, SCREEN_UV); + ALBEDO = col.brg; + ALPHA = col.a; +} +" + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_t3isk"] +render_priority = 0 +shader = SubResource("Shader_3cmiq") + +[sub_resource type="BoxMesh" id="BoxMesh_0xv07"] +material = SubResource("ShaderMaterial_t3isk") + [sub_resource type="Gradient" id="Gradient_tup4c"] offsets = PackedFloat32Array(0.00471698, 0.316038, 0.646226, 1) colors = PackedColorArray(0, 0.0156863, 1, 1, 0.0988327, 1, 0.122977, 1, 1, 0.111986, 0.118936, 1, 0, 0.0156863, 1, 1) @@ -664,6 +684,11 @@ libraries = { "": SubResource("AnimationLibrary_a7f1a") } +[node name="PostProcess" type="MeshInstance3D" parent="."] +transform = Transform3D(-2.18557e-07, 0, 1.5, 0, 5, 0, -5, 0, -6.55671e-08, 16, 0, 0) +mesh = SubResource("BoxMesh_0xv07") +skeleton = NodePath("../Lines") + [node name="MusicVisualizer" type="VBoxContainer" parent="."] offset_left = 10.0 offset_top = 10.0 diff --git a/examples_dd3d/DebugDrawDemoSceneCS.cs b/examples_dd3d/DebugDrawDemoSceneCS.cs index 41b65a56..20e7b02f 100644 --- a/examples_dd3d/DebugDrawDemoSceneCS.cs +++ b/examples_dd3d/DebugDrawDemoSceneCS.cs @@ -112,6 +112,7 @@ public partial class DebugDrawDemoSceneCS : Node3D Node3D dMisc_Grids_GridCentered_Subdivision; Node3D dMisc_Grids_GridCentered; + MeshInstance3D dPostProcess; AnimationPlayer dLagTest_RESET; Node3D dHitTest_RayEmitter; @@ -171,6 +172,8 @@ public override async void _Ready() dMisc_Grids_GridCentered_Subdivision = GetNode("Grids/GridCentered/Subdivision"); dMisc_Grids_GridCentered = GetNode("Grids/GridCentered"); + dPostProcess = GetNode("PostProcess"); + dLagTest_RESET = GetNode("LagTest/RESET"); dHitTest_RayEmitter = GetNode("HitTest/RayEmitter"); @@ -482,6 +485,11 @@ void MainUpdate(double delta) var tn1 = dMisc_Grids_GridCentered_Subdivision.Transform.Origin; DebugDraw3D.DrawGridXf(dMisc_Grids_GridCentered.GlobalTransform, new Vector2I((int)(tn1.X * 10), (int)(tn1.Z * 10))); + using (var s = DebugDraw3D.NewScopedConfig().SetThickness(0.05f)) + { + DebugDraw3D.DrawBoxXf(dPostProcess.GlobalTransform, Colors.SeaGreen); + } + // 2D DebugDraw2D.Config.TextDefaultSize = text_groups_default_font_size; DebugDraw2D.Config.TextBlockOffset = text_groups_offset; @@ -521,7 +529,7 @@ void MainUpdate(double delta) // Lag Test dLagTest.Position = ((Vector3)dLagTest_RESET.GetAnimation("RESET").TrackGetKeyValue(0, 0)) + new Vector3(Mathf.Sin(Time.GetTicksMsec() / 100.0f) * 2.5f, 0, 0); - DebugDraw3D.DrawBox(dLagTest.GlobalPosition, Quaternion.Identity, Vector3.One * 2.01f, null, true); + DebugDraw3D.DrawBox(dLagTest.GlobalPosition, Quaternion.Identity, Vector3.One * 2.01f, Colors.Chocolate, true); if (more_test_cases) { diff --git a/src/3d/debug_draw_3d.cpp b/src/3d/debug_draw_3d.cpp index ee06eac4..a8a88a66 100644 --- a/src/3d/debug_draw_3d.cpp +++ b/src/3d/debug_draw_3d.cpp @@ -55,6 +55,9 @@ const char *DebugDraw3D::s_default_center_brightness = "volumetric_defaults/cent const char *DebugDraw3D::s_default_hd_spheres = "volumetric_defaults/hd_spheres"; const char *DebugDraw3D::s_default_plane_size = "volumetric_defaults/plane_size"; +const char *DebugDraw3D::s_render_priority = "rendering/render_priority"; +const char *DebugDraw3D::s_render_mode = "rendering/render_mode"; + void DebugDraw3D::_bind_methods() { #define REG_CLASS_NAME DebugDraw3D @@ -152,6 +155,9 @@ void DebugDraw3D::init(DebugDrawManager *p_root) { DEFINE_SETTING_AND_GET(real_t def_hd_sphere, root_settings_section + s_default_hd_spheres, false, Variant::BOOL); DEFINE_SETTING_AND_GET_HINT(real_t def_plane_size, root_settings_section + s_default_plane_size, 0, Variant::FLOAT, PROPERTY_HINT_RANGE, "0,10000,0.001"); + DEFINE_SETTING(root_settings_section + s_render_priority, 0, Variant::INT); + DEFINE_SETTING_HINT(root_settings_section + s_render_mode, 0, Variant::INT, PROPERTY_HINT_ENUM, "Default,Forced Transparent,Forced Opaque"); + default_scoped_config.instantiate(); config->set_frustum_length_scale(def_frustum_scale); @@ -489,21 +495,35 @@ Ref DebugDraw3D::scoped_config() { void DebugDraw3D::_load_materials() { ZoneScoped; #ifndef DISABLE_DEBUG_RENDERING -#define LOAD_SHADER(mat, source) \ - { \ - Ref code; \ - code.instantiate(); \ - code->set_code(source); \ - mat.instantiate(); \ - mat->set_shader(code); \ +#define LOAD_SHADER(mat, source) \ + { \ + Ref code; \ + code.instantiate(); \ + code->set_code(source); \ + mat.instantiate(); \ + mat->set_shader(code); \ + mat->set_render_priority(render_priority); \ } + int render_priority = PS()->get_setting(root_settings_section + s_render_priority); + int render_mode = PS()->get_setting(root_settings_section + s_render_mode); // default, transparent, opaque + for (int variant = 0; variant < (int)MeshMaterialVariant::MAX; variant++) { String prefix = ""; if (variant == (int)MeshMaterialVariant::NoDepth) { prefix = "#define NO_DEPTH\n"; } + switch (render_mode) { + case 0: break; + case 1: + prefix = "#define FORCED_TRANSPARENT\n"; + break; + case 2: + prefix = "#define FORCED_OPAQUE\n"; + break; + } + LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Wireframe][variant], prefix + DD3DResources::src_resources_wireframe_unshaded_gdshader); LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Billboard][variant], prefix + DD3DResources::src_resources_billboard_unshaded_gdshader); LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Plane][variant], prefix + DD3DResources::src_resources_plane_unshaded_gdshader); @@ -610,6 +630,9 @@ void DebugDraw3D::regenerate_geometry_meshes() { #ifndef DISABLE_DEBUG_RENDERING LOCK_GUARD(datalock); + // Reload materials + _load_materials(); + // Force regenerate meshes shared_generated_meshes.clear(); diff --git a/src/3d/debug_draw_3d.h b/src/3d/debug_draw_3d.h index 01e20fe7..9d190e26 100644 --- a/src/3d/debug_draw_3d.h +++ b/src/3d/debug_draw_3d.h @@ -146,6 +146,9 @@ class DebugDraw3D : public Object, public IScopeStorage custom_editor_viewports; DebugDrawManager *root_node = nullptr; diff --git a/src/resources/billboard_unshaded.gdshader b/src/resources/billboard_unshaded.gdshader index eb5677c4..301d4cfb 100644 --- a/src/resources/billboard_unshaded.gdshader +++ b/src/resources/billboard_unshaded.gdshader @@ -1,4 +1,5 @@ //#define NO_DEPTH +//#define FORCED_TRANSPARENT shader_type spatial; render_mode cull_back, shadows_disabled, unshaded @@ -26,4 +27,8 @@ void fragment() { if (!OUTPUT_IS_SRGB) ALBEDO = toLinearFast(ALBEDO); NORMAL = ALBEDO; + + #if defined(FORCED_TRANSPARENT) + ALPHA = ALPHA; + #endif } diff --git a/src/resources/extendable_meshes.gdshader b/src/resources/extendable_meshes.gdshader index 86b4e84d..350beaf9 100644 --- a/src/resources/extendable_meshes.gdshader +++ b/src/resources/extendable_meshes.gdshader @@ -1,4 +1,5 @@ //#define NO_DEPTH +//#define FORCED_TRANSPARENT shader_type spatial; render_mode cull_disabled, shadows_disabled, unshaded, world_vertex_coords @@ -31,4 +32,8 @@ void fragment() { if (!OUTPUT_IS_SRGB) ALBEDO = toLinearFast(ALBEDO); NORMAL = ALBEDO; + + #if defined(FORCED_TRANSPARENT) + ALPHA = ALPHA; + #endif } diff --git a/src/resources/plane_unshaded.gdshader b/src/resources/plane_unshaded.gdshader index 69d8e991..4ca2a539 100644 --- a/src/resources/plane_unshaded.gdshader +++ b/src/resources/plane_unshaded.gdshader @@ -1,4 +1,5 @@ //#define NO_DEPTH +//#define FORCED_OPAQUE shader_type spatial; render_mode cull_disabled, shadows_disabled, unshaded @@ -21,10 +22,14 @@ vec3 toLinearFast(vec3 col) { void fragment() { if (FRONT_FACING){ ALBEDO = COLOR.xyz; + #if !defined(FORCED_OPAQUE) ALPHA = COLOR.a; + #endif }else{ ALBEDO = custom.xyz; + #if !defined(FORCED_OPAQUE) ALPHA = custom.a; + #endif } if (!OUTPUT_IS_SRGB) diff --git a/src/resources/wireframe_unshaded.gdshader b/src/resources/wireframe_unshaded.gdshader index 9ea3008c..1b892bba 100644 --- a/src/resources/wireframe_unshaded.gdshader +++ b/src/resources/wireframe_unshaded.gdshader @@ -1,4 +1,5 @@ //#define NO_DEPTH +//#define FORCED_TRANSPARENT shader_type spatial; render_mode cull_disabled, shadows_disabled, unshaded @@ -17,4 +18,8 @@ void fragment() { if (!OUTPUT_IS_SRGB) ALBEDO = toLinearFast(ALBEDO); NORMAL = ALBEDO; + + #if defined(FORCED_TRANSPARENT) + ALPHA = ALPHA; + #endif } diff --git a/src/utils/utils.h b/src/utils/utils.h index 8518ad5c..625b580a 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -110,6 +110,19 @@ constexpr size_t INSTANCE_DATA_FLOAT_COUNT = ((sizeof(godot::Transform3D) + size PS()->add_property_info(info); \ PS()->set_initial_value(path, def); \ } +#define DEFINE_SETTING_HINT(path, def, type, hint, hint_string) \ + { \ + if (!PS()->has_setting(path)) { \ + PS()->set_setting(path, def); \ + } \ + Dictionary info; \ + info["name"] = path; \ + info["type"] = type; \ + info["hint"] = hint; \ + info["hint_string"] = hint_string; \ + PS()->add_property_info(info); \ + PS()->set_initial_value(path, def); \ + } #define DEFINE_SETTING_AND_GET(var, path, def, type) \ { \ if (!PS()->has_setting(path)) { \ diff --git a/src/version.h b/src/version.h index fd6779ab..cd197ad1 100644 --- a/src/version.h +++ b/src/version.h @@ -2,7 +2,7 @@ #define DD3D_MAJOR 1 #define DD3D_MINOR 4 -#define DD3D_PATCH 1 +#define DD3D_PATCH 2 #define DD3D_VERSION ((DD3D_MAJOR << (8 * 3)) + (DD3D_MINOR << (8 * 2)) + (DD3D_PATCH << (8 * 1))) #define _DD3D_VERSION_STR_TEXT(text) #text