Skip to content

Commit

Permalink
Added support for rendering in multiple World3D's.
Browse files Browse the repository at this point in the history
Specifying the Viewport for the World3D lookup has been replaced by specifying it in the scoped config.
  • Loading branch information
DmitriySalnikov committed Mar 11, 2024
1 parent 49df6b8 commit ca6f0f2
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 108 deletions.
10 changes: 3 additions & 7 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ func main_update(delta: float) -> void:
$Panel.visible = Input.is_key_pressed(KEY_ALT)
DebugDraw2D.custom_canvas = %CustomCanvas if Input.is_key_pressed(KEY_ALT) else null


if Input.is_key_pressed(KEY_ALT):
DebugDraw3D.set_world_3d_from_viewport($OtherWorld/SubViewport)
else:
DebugDraw3D.set_world_3d_from_viewport(get_viewport())

# More property toggles
DebugDraw3D.config.freeze_3d_render = Input.is_key_pressed(KEY_DOWN)
DebugDraw3D.config.visible_instance_bounds = Input.is_key_pressed(KEY_RIGHT)
Expand Down Expand Up @@ -237,7 +231,9 @@ func main_update(delta: float) -> void:

# Other world

DebugDraw3D.draw_box_xf(%OtherWorldBox.global_transform, Color.SANDY_BROWN)
if true:
var _w1 = DebugDraw3D.new_scoped_config().set_viewport(%OtherWorldBox.get_viewport())
DebugDraw3D.draw_box_xf(%OtherWorldBox.global_transform.rotated_local(Vector3.UP, wrapf(Time.get_ticks_msec() / 1000.0, 0, TAU)), Color.SANDY_BROWN)

# Misc
if true:
Expand Down
7 changes: 4 additions & 3 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ metadata/_edit_group_ = true
transform = Transform3D(1, -7.45058e-09, 0, -7.45058e-09, 1, 0, 2.98023e-08, -1.49012e-08, 1, -9.53674e-07, 0.6, 0)

[node name="OtherWorld" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6.53219, 0, 5.30229)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6.53219, -2.5, 5.30229)
mesh = SubResource("PlaneMesh_c6mie")
skeleton = NodePath("")

Expand All @@ -501,15 +501,16 @@ own_world_3d = true
render_target_update_mode = 4

[node name="Camera3D" type="Camera3D" parent="OtherWorld/SubViewport"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6.57063, -2.5, 7.25557)
current = true

[node name="MeshInstance3D" type="MeshInstance3D" parent="OtherWorld/SubViewport"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -3.88588)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6.57063, -2.5, 4.85623)
mesh = SubResource("CapsuleMesh_tigpa")

[node name="OtherWorldBox" type="Node3D" parent="OtherWorld/SubViewport"]
unique_name_in_owner = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -3)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 6.57063, -2.5, 5.72253)

[node name="Misc" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.68259, 0, 4.46741)
Expand Down
14 changes: 14 additions & 0 deletions examples_dd3d/DebugDrawDemoSceneCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public partial class DebugDrawDemoSceneCS : Node3D
Node3D dCylinder3a;
Node3D dCylinder3b;

MeshInstance3D dOtherWorld;
SubViewport dOtherWorldViewport;
Node3D dOtherWorldBox;

Control dCustomCanvas;
Node3D dMisc_Arrow;
Camera3D dCamera;
Expand Down Expand Up @@ -148,6 +152,10 @@ public override async void _Ready()
dCylinder3a = GetNode<Node3D>("Cylinders/Cylinder3/1");
dCylinder3b = GetNode<Node3D>("Cylinders/Cylinder3/2");

dOtherWorld = GetNode<MeshInstance3D>("OtherWorld");
dOtherWorldViewport = GetNode<SubViewport>("OtherWorld/SubViewport");
dOtherWorldBox = GetNode<Node3D>("OtherWorld/SubViewport/OtherWorldBox");

dCustomCanvas = GetNode<Control>("CustomCanvas");
dMisc_Arrow = GetNode<Node3D>("Misc/Arrow");
dCamera = GetNode<Camera3D>("Camera");
Expand All @@ -167,6 +175,8 @@ public override async void _Ready()

_update_keys_just_press();

((StandardMaterial3D)((PrimitiveMesh)dOtherWorld.Mesh).Material).AlbedoTexture = dOtherWorldViewport.GetTexture();

await new SignalAwaiter(GetTree(), "process_frame", this);

// this check is required for inherited scenes, because an instance of this
Expand Down Expand Up @@ -425,6 +435,10 @@ void MainUpdate(double delta)
DebugDraw3D.DrawArrowPath(points_below3.ToArray(), Colors.Gold, 0.5f);
using (var _sl = DebugDraw3D.NewScopedConfig().SetThickness(0.05f))
DebugDraw3D.DrawPointPath(points_below4.ToArray(), DebugDraw3D.PointType.TypeSphere, 0.25f, Colors.MediumSeaGreen, Colors.MediumVioletRed);
// Other world

using (var s = DebugDraw3D.NewScopedConfig().SetViewport(dOtherWorldBox.GetViewport()))
DebugDraw3D.DrawBoxXf(dOtherWorldBox.GlobalTransform.RotatedLocal(Vector3.Up, Mathf.Wrap(Time.GetTicksMsec() / 1000.0f, 0f, Mathf.Tau)), Colors.SandyBrown);

// Misc
using (var s = DebugDraw3D.NewScopedConfig().SetThickness(0))
Expand Down
44 changes: 29 additions & 15 deletions src/3d/config_scope_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ void DebugDraw3DScopeConfig::_bind_methods() {

REG_METHOD(set_plane_size, "value");
REG_METHOD(get_plane_size);

REG_METHOD(set_viewport, "value");
REG_METHOD(get_viewport);
#undef REG_CLASS_NAME
}

Expand All @@ -32,7 +35,7 @@ Ref<DebugDraw3DScopeConfig> DebugDraw3DScopeConfig::set_thickness(real_t value)
return Ref<DebugDraw3DScopeConfig>(this);
}

real_t DebugDraw3DScopeConfig::get_thickness() {
real_t DebugDraw3DScopeConfig::get_thickness() const {
return data->thickness;
}

Expand All @@ -41,7 +44,7 @@ Ref<DebugDraw3DScopeConfig> DebugDraw3DScopeConfig::set_center_brightness(real_t
return Ref<DebugDraw3DScopeConfig>(this);
}

real_t DebugDraw3DScopeConfig::get_center_brightness() {
real_t DebugDraw3DScopeConfig::get_center_brightness() const {
return data->center_brightness;
}

Expand All @@ -50,7 +53,7 @@ Ref<DebugDraw3DScopeConfig> DebugDraw3DScopeConfig::set_hd_sphere(bool value) {
return this;
}

bool DebugDraw3DScopeConfig::is_hd_sphere() {
bool DebugDraw3DScopeConfig::is_hd_sphere() const {
return data->hd_sphere;
}

Expand All @@ -59,10 +62,19 @@ Ref<DebugDraw3DScopeConfig> DebugDraw3DScopeConfig::set_plane_size(real_t value)
return this;
}

real_t DebugDraw3DScopeConfig::get_plane_size() {
real_t DebugDraw3DScopeConfig::get_plane_size() const {
return data->plane_size;
}

Ref<DebugDraw3DScopeConfig> DebugDraw3DScopeConfig::set_viewport(Viewport *value) {
data->viewport = value;
return this;
}

Viewport *DebugDraw3DScopeConfig::get_viewport() const {
return data->viewport;
}

DebugDraw3DScopeConfig::DebugDraw3DScopeConfig() {
unregister_action = nullptr;
thread_id = 0;
Expand All @@ -71,13 +83,6 @@ DebugDraw3DScopeConfig::DebugDraw3DScopeConfig() {
data = std::make_shared<Data>();
}

DebugDraw3DScopeConfig::Data::Data() {
thickness = 0;
center_brightness = 0;
hd_sphere = false;
plane_size = INFINITY;
}

DebugDraw3DScopeConfig::DebugDraw3DScopeConfig(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const std::shared_ptr<DebugDraw3DScopeConfig::Data> &parent, const unregister_func p_unreg) {
unregister_action = p_unreg;

Expand All @@ -87,13 +92,22 @@ DebugDraw3DScopeConfig::DebugDraw3DScopeConfig(const uint64_t &p_thread_id, cons
data = std::make_shared<Data>(parent);
}

DebugDraw3DScopeConfig::~DebugDraw3DScopeConfig() {
_manual_unregister();
}

DebugDraw3DScopeConfig::Data::Data() {
thickness = 0;
center_brightness = 0;
hd_sphere = false;
plane_size = INFINITY;
viewport = nullptr;
}

DebugDraw3DScopeConfig::Data::Data(const std::shared_ptr<Data> &parent) {
thickness = parent->thickness;
center_brightness = parent->center_brightness;
hd_sphere = parent->hd_sphere;
plane_size = parent->plane_size;
}

DebugDraw3DScopeConfig::~DebugDraw3DScopeConfig() {
_manual_unregister();
viewport = parent->viewport;
}
20 changes: 16 additions & 4 deletions src/3d/config_scope_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/classes/viewport.hpp>
GODOT_WARNING_RESTORE()
using namespace godot;

Expand Down Expand Up @@ -75,6 +76,7 @@ class DebugDraw3DScopeConfig : public RefCounted {
real_t center_brightness;
bool hd_sphere;
real_t plane_size;
Viewport *viewport;

Data();
Data(const std::shared_ptr<Data> &parent);
Expand All @@ -92,31 +94,41 @@ class DebugDraw3DScopeConfig : public RefCounted {
* ![](docs/images/classes/LineThickness.webp)
*/
Ref<DebugDraw3DScopeConfig> set_thickness(real_t value);
real_t get_thickness();
real_t get_thickness() const;

/**
* Set the brightness of the central part of the volumetric lines.
*
* ![](docs/images/classes/LineCenterBrightness.webp)
*/
Ref<DebugDraw3DScopeConfig> set_center_brightness(real_t value);
real_t get_center_brightness();
real_t get_center_brightness() const;

/**
* Set the mesh density of the sphere
*
* ![](docs/images/classes/SphereDensity.webp)
*/
Ref<DebugDraw3DScopeConfig> set_hd_sphere(bool value);
bool is_hd_sphere();
bool is_hd_sphere() const;

/**
* Set the size of the `Plane` in DebugDraw3D.draw_plane. If set to `INF`, the `Far` parameter of the current camera will be used.
*
* ![](docs/images/classes/PlaneSize.webp)
*/
Ref<DebugDraw3DScopeConfig> set_plane_size(real_t value);
real_t get_plane_size();
real_t get_plane_size() const;

/**
* Set which Viewport will be used to get World3D.
*
* If the World3D of this Viewport has not been used before,
* then the owner of this World3D will be found in the current branch of the tree,
* and special observer nodes will be added to it.
*/
Ref<DebugDraw3DScopeConfig> set_viewport(Viewport *value);
Viewport *get_viewport() const;

/// @private
DebugDraw3DScopeConfig();
Expand Down
Loading

0 comments on commit ca6f0f2

Please sign in to comment.