Skip to content

Commit

Permalink
Added the ability to change render_priority and transparency mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed May 21, 2024
1 parent 961821f commit 81f0604
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 11 deletions.
6 changes: 5 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
27 changes: 26 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion examples_dd3d/DebugDrawDemoSceneCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -171,6 +172,8 @@ public override async void _Ready()
dMisc_Grids_GridCentered_Subdivision = GetNode<Node3D>("Grids/GridCentered/Subdivision");
dMisc_Grids_GridCentered = GetNode<Node3D>("Grids/GridCentered");

dPostProcess = GetNode<MeshInstance3D>("PostProcess");

dLagTest_RESET = GetNode<AnimationPlayer>("LagTest/RESET");
dHitTest_RayEmitter = GetNode<Node3D>("HitTest/RayEmitter");

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
37 changes: 30 additions & 7 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -489,21 +495,35 @@ Ref<DebugDraw3DScopeConfig> DebugDraw3D::scoped_config() {
void DebugDraw3D::_load_materials() {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
#define LOAD_SHADER(mat, source) \
{ \
Ref<Shader> code; \
code.instantiate(); \
code->set_code(source); \
mat.instantiate(); \
mat->set_shader(code); \
#define LOAD_SHADER(mat, source) \
{ \
Ref<Shader> 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);
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig,
const static char *s_default_hd_spheres;
const static char *s_default_plane_size;

const static char *s_render_priority;
const static char *s_render_mode;

std::vector<SubViewport *> custom_editor_viewports;
DebugDrawManager *root_node = nullptr;

Expand Down
5 changes: 5 additions & 0 deletions src/resources/billboard_unshaded.gdshader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#define NO_DEPTH
//#define FORCED_TRANSPARENT

shader_type spatial;
render_mode cull_back, shadows_disabled, unshaded
Expand Down Expand Up @@ -26,4 +27,8 @@ void fragment() {
if (!OUTPUT_IS_SRGB)
ALBEDO = toLinearFast(ALBEDO);
NORMAL = ALBEDO;

#if defined(FORCED_TRANSPARENT)
ALPHA = ALPHA;
#endif
}
5 changes: 5 additions & 0 deletions src/resources/extendable_meshes.gdshader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#define NO_DEPTH
//#define FORCED_TRANSPARENT

shader_type spatial;
render_mode cull_disabled, shadows_disabled, unshaded, world_vertex_coords
Expand Down Expand Up @@ -31,4 +32,8 @@ void fragment() {
if (!OUTPUT_IS_SRGB)
ALBEDO = toLinearFast(ALBEDO);
NORMAL = ALBEDO;

#if defined(FORCED_TRANSPARENT)
ALPHA = ALPHA;
#endif
}
5 changes: 5 additions & 0 deletions src/resources/plane_unshaded.gdshader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#define NO_DEPTH
//#define FORCED_OPAQUE

shader_type spatial;
render_mode cull_disabled, shadows_disabled, unshaded
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/resources/wireframe_unshaded.gdshader
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#define NO_DEPTH
//#define FORCED_TRANSPARENT

shader_type spatial;
render_mode cull_disabled, shadows_disabled, unshaded
Expand All @@ -17,4 +18,8 @@ void fragment() {
if (!OUTPUT_IS_SRGB)
ALBEDO = toLinearFast(ALBEDO);
NORMAL = ALBEDO;

#if defined(FORCED_TRANSPARENT)
ALPHA = ALPHA;
#endif
}
13 changes: 13 additions & 0 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)) { \
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81f0604

Please sign in to comment.