Skip to content

Commit

Permalink
Merge pull request #37861 from reduz/implement-decals
Browse files Browse the repository at this point in the history
Implement decals
  • Loading branch information
akien-mga committed Apr 14, 2020
2 parents c364c96 + 5944eb6 commit 5e5103f
Show file tree
Hide file tree
Showing 35 changed files with 1,654 additions and 67 deletions.
1 change: 1 addition & 0 deletions core/callable_method_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/callable.h"
#include "core/hashfuncs.h"
#include "core/object.h"
#include "core/os/copymem.h"
#include "core/simple_type.h"

class CallableCustomMethodPointerBase : public CallableCustom {
Expand Down
3 changes: 2 additions & 1 deletion core/hashfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#include "core/math/math_funcs.h"
#include "core/node_path.h"
#include "core/object_id.h"
#include "core/rid.h"
#include "core/string_name.h"
#include "core/typedefs.h"
#include "core/ustring.h"

/**
* Hashing functions
*/
Expand Down Expand Up @@ -150,6 +150,7 @@ struct HashMapHasherDefault {
static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
static _FORCE_INLINE_ uint32_t hash(const RID &p_rid) { return hash_one_uint64(p_rid.get_id()); }

static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
Expand Down
5 changes: 0 additions & 5 deletions core/rid.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
#ifndef RID_H
#define RID_H

#include "core/list.h"
#include "core/oa_hash_map.h"
#include "core/os/memory.h"
#include "core/safe_refcount.h"
#include "core/set.h"
#include "core/typedefs.h"

class RID_AllocBase;
Expand Down
5 changes: 5 additions & 0 deletions core/rid_owner.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
#ifndef RID_OWNER_H
#define RID_OWNER_H

#include "core/list.h"
#include "core/oa_hash_map.h"
#include "core/os/memory.h"
#include "core/print_string.h"
#include "core/rid.h"
#include "core/safe_refcount.h"
#include "core/set.h"
#include "core/spin_lock.h"
#include <stdio.h>
#include <typeinfo>
Expand Down
8 changes: 4 additions & 4 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2851,8 +2851,8 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color,
src_layer_count *= 6;
}

ERR_FAIL_COND_V(src_tex->base_mipmap + p_base_mipmap + p_mipmaps > src_tex->mipmaps, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(src_tex->base_layer + p_base_layer + p_layers > src_layer_count, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_base_mipmap + p_mipmaps > src_tex->mipmaps, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_base_layer + p_layers > src_layer_count, ERR_INVALID_PARAMETER);

VkCommandBuffer command_buffer = p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer;

Expand Down Expand Up @@ -2888,9 +2888,9 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color,

VkImageSubresourceRange range;
range.aspectMask = src_tex->read_aspect_mask;
range.baseArrayLayer = p_base_layer;
range.baseArrayLayer = src_tex->base_layer + p_base_layer;
range.layerCount = p_layers;
range.baseMipLevel = p_base_mipmap;
range.baseMipLevel = src_tex->base_mipmap + p_base_mipmap;
range.levelCount = p_mipmaps;

vkCmdClearColorImage(command_buffer, src_tex->image, layout, &clear_color, 1, &range);
Expand Down
137 changes: 137 additions & 0 deletions editor/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "scene/3d/collision_polygon_3d.h"
#include "scene/3d/collision_shape_3d.h"
#include "scene/3d/cpu_particles_3d.h"
#include "scene/3d/decal.h"
#include "scene/3d/gi_probe.h"
#include "scene/3d/gpu_particles_3d.h"
#include "scene/3d/light_3d.h"
Expand Down Expand Up @@ -2718,7 +2719,143 @@ void ReflectionProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
p_gizmo->add_unscaled_billboard(icon, 0.05);
p_gizmo->add_handles(handles, get_material("handles"));
}
///////////////////////////////

////

DecalGizmoPlugin::DecalGizmoPlugin() {
Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/decal", Color(0.6, 0.5, 1.0));

create_material("decal_material", gizmo_color);

create_handle_material("handles");
}

bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) {
return Object::cast_to<Decal>(p_spatial) != nullptr;
}

String DecalGizmoPlugin::get_name() const {
return "Decal";
}

int DecalGizmoPlugin::get_priority() const {
return -1;
}

String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_idx) const {

switch (p_idx) {
case 0: return "Extents X";
case 1: return "Extents Y";
case 2: return "Extents Z";
}

return "";
}
Variant DecalGizmoPlugin::get_handle_value(EditorNode3DGizmo *p_gizmo, int p_idx) const {

Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
return decal->get_extents();
}
void DecalGizmoPlugin::set_handle(EditorNode3DGizmo *p_gizmo, int p_idx, Camera3D *p_camera, const Point2 &p_point) {

Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
Transform gt = decal->get_global_transform();

Transform gi = gt.affine_inverse();

Vector3 extents = decal->get_extents();

Vector3 ray_from = p_camera->project_ray_origin(p_point);
Vector3 ray_dir = p_camera->project_ray_normal(p_point);

Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };

Vector3 axis;
axis[p_idx] = 1.0;

Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, Node3DEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

extents[p_idx] = d;
decal->set_extents(extents);
}

void DecalGizmoPlugin::commit_handle(EditorNode3DGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {

Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());

Vector3 restore = p_restore;

if (p_cancel) {
decal->set_extents(restore);
return;
}

UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
ur->create_action(TTR("Change Decal Extents"));
ur->add_do_method(decal, "set_extents", decal->get_extents());
ur->add_undo_method(decal, "set_extents", restore);
ur->commit_action();
}

void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {

Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());

p_gizmo->clear();

Vector<Vector3> lines;
Vector3 extents = decal->get_extents();

AABB aabb;
aabb.position = -extents;
aabb.size = extents * 2;

for (int i = 0; i < 12; i++) {
Vector3 a, b;
aabb.get_edge(i, a, b);
if (a.y == b.y) {
lines.push_back(a);
lines.push_back(b);
} else {
Vector3 ah = a.linear_interpolate(b, 0.2);
lines.push_back(a);
lines.push_back(ah);
Vector3 bh = b.linear_interpolate(a, 0.2);
lines.push_back(b);
lines.push_back(bh);
}
}

lines.push_back(Vector3(0, extents.y, 0));
lines.push_back(Vector3(0, extents.y * 1.2, 0));

Vector<Vector3> handles;

for (int i = 0; i < 3; i++) {

Vector3 ax;
ax[i] = aabb.position[i] + aabb.size[i];
handles.push_back(ax);
}

Ref<Material> material = get_material("decal_material", p_gizmo);

p_gizmo->add_lines(lines, material);

p_gizmo->add_handles(handles, get_material("handles"));
}

///////////////////////////////
GIProbeGizmoPlugin::GIProbeGizmoPlugin() {
Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/gi_probe", Color(0.5, 1, 0.6));

Expand Down
18 changes: 18 additions & 0 deletions editor/node_3d_editor_gizmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ class ReflectionProbeGizmoPlugin : public EditorNode3DGizmoPlugin {
ReflectionProbeGizmoPlugin();
};

class DecalGizmoPlugin : public EditorNode3DGizmoPlugin {

GDCLASS(DecalGizmoPlugin, EditorNode3DGizmoPlugin);

public:
bool has_gizmo(Node3D *p_spatial);
String get_name() const;
int get_priority() const;
void redraw(EditorNode3DGizmo *p_gizmo);

String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_idx) const;
Variant get_handle_value(EditorNode3DGizmo *p_gizmo, int p_idx) const;
void set_handle(EditorNode3DGizmo *p_gizmo, int p_idx, Camera3D *p_camera, const Point2 &p_point);
void commit_handle(EditorNode3DGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel = false);

DecalGizmoPlugin();
};

class GIProbeGizmoPlugin : public EditorNode3DGizmoPlugin {

GDCLASS(GIProbeGizmoPlugin, EditorNode3DGizmoPlugin);
Expand Down
6 changes: 6 additions & 0 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,7 @@ void Node3DEditorViewport::_menu_option(int p_option) {
case VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE:
case VIEW_DISPLAY_DEBUG_SSAO:
case VIEW_DISPLAY_DEBUG_PSSM_SPLITS:
case VIEW_DISPLAY_DEBUG_DECAL_ATLAS:
case VIEW_DISPLAY_DEBUG_ROUGHNESS_LIMITER: {

static const int display_options[] = {
Expand All @@ -3053,6 +3054,7 @@ void Node3DEditorViewport::_menu_option(int p_option) {
VIEW_DISPLAY_DEBUG_SSAO,
VIEW_DISPLAY_DEBUG_ROUGHNESS_LIMITER,
VIEW_DISPLAY_DEBUG_PSSM_SPLITS,
VIEW_DISPLAY_DEBUG_DECAL_ATLAS,
VIEW_MAX
};
static const Viewport::DebugDraw debug_draw_modes[] = {
Expand All @@ -3072,6 +3074,7 @@ void Node3DEditorViewport::_menu_option(int p_option) {
Viewport::DEBUG_DRAW_SSAO,
Viewport::DEBUG_DRAW_ROUGHNESS_LIMITER,
Viewport::DEBUG_DRAW_PSSM_SPLITS,
Viewport::DEBUG_DRAW_DECAL_ATLAS,
};

int idx = 0;
Expand Down Expand Up @@ -3933,6 +3936,8 @@ Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, Edito
display_submenu->add_radio_check_item(TTR("Shadow Atlas"), VIEW_DISPLAY_DEBUG_SHADOW_ATLAS);
display_submenu->add_radio_check_item(TTR("Directional Shadow"), VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS);
display_submenu->add_separator();
display_submenu->add_radio_check_item(TTR("Decal Atlas"), VIEW_DISPLAY_DEBUG_DECAL_ATLAS);
display_submenu->add_separator();
display_submenu->add_radio_check_item(TTR("GIProbe Lighting"), VIEW_DISPLAY_DEBUG_GIPROBE_LIGHTING);
display_submenu->add_radio_check_item(TTR("GIProbe Albedo"), VIEW_DISPLAY_DEBUG_GIPROBE_ALBEDO);
display_submenu->add_radio_check_item(TTR("GIProbe Emission"), VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION);
Expand Down Expand Up @@ -5963,6 +5968,7 @@ void Node3DEditor::_register_all_gizmos() {
add_gizmo_plugin(Ref<GPUParticles3DGizmoPlugin>(memnew(GPUParticles3DGizmoPlugin)));
add_gizmo_plugin(Ref<CPUParticles3DGizmoPlugin>(memnew(CPUParticles3DGizmoPlugin)));
add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
add_gizmo_plugin(Ref<DecalGizmoPlugin>(memnew(DecalGizmoPlugin)));
add_gizmo_plugin(Ref<GIProbeGizmoPlugin>(memnew(GIProbeGizmoPlugin)));
// add_gizmo_plugin(Ref<BakedIndirectLightGizmoPlugin>(memnew(BakedIndirectLightGizmoPlugin)));
add_gizmo_plugin(Ref<CollisionShape3DGizmoPlugin>(memnew(CollisionShape3DGizmoPlugin)));
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class Node3DEditorViewport : public Control {
VIEW_DISPLAY_DEBUG_SSAO,
VIEW_DISPLAY_DEBUG_ROUGHNESS_LIMITER,
VIEW_DISPLAY_DEBUG_PSSM_SPLITS,
VIEW_DISPLAY_DEBUG_DECAL_ATLAS,
VIEW_LOCK_ROTATION,
VIEW_CINEMATIC_PREVIEW,
VIEW_AUTO_ORTHOGONAL,
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define GDSCRIPT_TOKENIZER_H

#include "core/pair.h"
#include "core/set.h"
#include "core/string_name.h"
#include "core/ustring.h"
#include "core/variant.h"
Expand Down
Loading

0 comments on commit 5e5103f

Please sign in to comment.