Skip to content

Commit

Permalink
add clear_color to Viewport.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhalesState committed Dec 18, 2024
1 parent cb9e702 commit 982cae7
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 2 deletions.
12 changes: 12 additions & 0 deletions drivers/gles3/storage/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,18 @@ RID TextureStorage::render_target_get_texture(RID p_render_target) {
return rt->texture;
}

#ifdef PIXEL_ENGINE
void TextureStorage::render_target_set_clear_color(RID p_render_target, const Color &p_color) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
rt->clear_color = p_color;
if (rt->overridden.color.is_null() && !rt->is_transparent) {
_clear_render_target(rt);
_update_render_target(rt);
}
}
#endif // PIXEL_ENGINE

void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_transparent) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
Expand Down
3 changes: 3 additions & 0 deletions drivers/gles3/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ class TextureStorage : public RendererTextureStorage {
virtual Point2i render_target_get_position(RID p_render_target) const override;
virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
virtual Size2i render_target_get_size(RID p_render_target) const override;
#ifdef PIXEL_ENGINE
virtual void render_target_set_clear_color(RID p_render_target, const Color &p_color) override;
#endif // PIXEL_ENGINE
virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
virtual bool render_target_get_transparent(RID p_render_target) const override;
virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
Expand Down
9 changes: 9 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3204,7 +3204,11 @@ Error Main::setup2(bool p_show_boot_logo) {
DisplayServer::get_singleton()->window_set_flag(DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP, true);
}

#ifndef PIXEL_ENGINE
Color clear = GLOBAL_DEF_BASIC("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3));
#else
Color clear = GLOBAL_DEF_BASIC("rendering/viewport/default_clear_color", Color(0.3, 0.3, 0.3));
#endif // !PIXEL_ENGINE
RenderingServer::get_singleton()->set_default_clear_color(clear);

if (p_show_boot_logo) {
Expand Down Expand Up @@ -3523,8 +3527,13 @@ void Main::setup_boot_logo() {
}
#endif
}
#ifndef PIXEL_ENGINE
RenderingServer::get_singleton()->set_default_clear_color(
GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
#else
RenderingServer::get_singleton()->set_default_clear_color(
GLOBAL_GET("rendering/viewport/default_clear_color"));
#endif // !PIXEL_ENGINE
}

String Main::get_rendering_driver_name() {
Expand Down
4 changes: 4 additions & 0 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,10 @@ SceneTree::SceneTree() {

const bool transparent_background = GLOBAL_DEF("rendering/viewport/transparent_background", false);
root->set_transparent_background(transparent_background);
#ifdef PIXEL_ENGINE
const Color clear_color = GLOBAL_GET("rendering/viewport/default_clear_color");
root->set_clear_color(clear_color);
#endif // PIXEL_ENGINE

const bool use_hdr_2d = GLOBAL_DEF_RST_BASIC("rendering/viewport/hdr_2d", false);
root->set_use_hdr_2d(use_hdr_2d);
Expand Down
25 changes: 25 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,19 @@ void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) {
canvas_layers.erase(p_canvas_layer);
}

#ifdef PIXEL_ENGINE
void Viewport::set_clear_color(const Color &p_color) {
ERR_MAIN_THREAD_GUARD;
clear_color = p_color;
RS::get_singleton()->viewport_set_clear_color(viewport, p_color);
}

Color Viewport::get_clear_color() const {
ERR_READ_THREAD_GUARD_V(Color());
return clear_color;
}
#endif // PIXEL_ENGINE

void Viewport::set_transparent_background(bool p_enable) {
ERR_MAIN_THREAD_GUARD;
transparent_bg = p_enable;
Expand Down Expand Up @@ -4659,6 +4672,10 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_screen_transform"), &Viewport::get_screen_transform);

ClassDB::bind_method(D_METHOD("get_visible_rect"), &Viewport::get_visible_rect);
#ifdef PIXEL_ENGINE
ClassDB::bind_method(D_METHOD("set_clear_color", "color"), &Viewport::set_clear_color);
ClassDB::bind_method(D_METHOD("get_clear_color"), &Viewport::get_clear_color);
#endif // PIXEL_ENGINE
ClassDB::bind_method(D_METHOD("set_transparent_background", "enable"), &Viewport::set_transparent_background);
ClassDB::bind_method(D_METHOD("has_transparent_background"), &Viewport::has_transparent_background);
ClassDB::bind_method(D_METHOD("set_use_hdr_2d", "enable"), &Viewport::set_use_hdr_2d);
Expand Down Expand Up @@ -4823,6 +4840,9 @@ void Viewport::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_3d", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d");
#endif // _3D_DISABLED
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", PROPERTY_USAGE_NONE), "set_world_2d", "get_world_2d");
#ifdef PIXEL_ENGINE
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "clear_color"), "set_clear_color", "get_clear_color");
#endif // PIXEL_ENGINE
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_transforms_to_pixel"), "set_snap_2d_transforms_to_pixel", "is_snap_2d_transforms_to_pixel_enabled");
Expand Down Expand Up @@ -4993,6 +5013,11 @@ Viewport::Viewport() {

viewport = RenderingServer::get_singleton()->viewport_create();
texture_rid = RenderingServer::get_singleton()->viewport_get_texture(viewport);
#ifdef PIXEL_ENGINE
clear_color = GLOBAL_GET("rendering/viewport/default_clear_color");
ADD_PROPERTY_DEFAULT("clear_color", clear_color);
RenderingServer::get_singleton()->viewport_set_clear_color(viewport, clear_color);
#endif // PIXEL_ENGINE

default_texture.instantiate();
default_texture->vp = this;
Expand Down
8 changes: 8 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class Viewport : public Node {

Rect2 last_vp_rect;

#ifdef PIXEL_ENGINE
Color clear_color = Color(0.0, 0.0, 0.0);
#endif // PIXEL_ENGINE
bool transparent_bg = false;
bool use_hdr_2d = false;
bool gen_mipmaps = false;
Expand Down Expand Up @@ -520,6 +523,11 @@ class Viewport : public Node {

void gui_set_root_order_dirty();

#ifdef PIXEL_ENGINE
void set_clear_color(const Color &p_color);
Color get_clear_color() const;
#endif // PIXEL_ENGINE

void set_transparent_background(bool p_enable);
bool has_transparent_background() const;

Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/dummy/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class TextureStorage : public RendererTextureStorage {
virtual Point2i render_target_get_position(RID p_render_target) const override { return Point2i(); }
virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override {}
virtual Size2i render_target_get_size(RID p_render_target) const override { return Size2i(); }
#ifdef PIXEL_ENGINE
virtual void render_target_set_clear_color(RID p_render_target, const Color &p_color) override {}
#endif // PIXEL_ENGINE
virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override {}
virtual bool render_target_get_transparent(RID p_render_target) const override { return false; }
virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override {}
Expand Down
12 changes: 12 additions & 0 deletions servers/rendering/renderer_rd/storage_rd/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3515,6 +3515,18 @@ RID TextureStorage::render_target_get_override_velocity_slice(RID p_render_targe
}
}

#ifdef PIXEL_ENGINE
void TextureStorage::render_target_set_clear_color(RID p_render_target, const Color &p_color) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
rt->clear_color = p_color;
if (rt->overridden.color.is_null() && !rt->is_transparent) {
_clear_render_target(rt);
_update_render_target(rt);
}
}
#endif // PIXEL_ENGINE

void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_is_transparent) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/renderer_rd/storage_rd/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,9 @@ class TextureStorage : public RendererTextureStorage {
virtual Point2i render_target_get_position(RID p_render_target) const override;
virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
virtual Size2i render_target_get_size(RID p_render_target) const override;
#ifdef PIXEL_ENGINE
virtual void render_target_set_clear_color(RID p_render_target, const Color &p_color) override;
#endif // PIXEL_ENGINE
virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
virtual bool render_target_get_transparent(RID p_render_target) const override;
virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
Expand Down
14 changes: 13 additions & 1 deletion servers/rendering/renderer_viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ void RendererViewport::_draw_viewport(Viewport *p_viewport) {
_configure_3d_render_buffers(p_viewport);
}

Color bgcolor = p_viewport->transparent_bg ? Color(0, 0, 0, 0) : RSG::texture_storage->get_default_clear_color();
#ifdef PIXEL_ENGINE
Color bgcolor = p_viewport->transparent_bg ? Color(0, 0, 0, 0) : p_viewport->clear_color;
#endif // PIXEL_ENGINE

if (p_viewport->clear_mode != RS::VIEWPORT_CLEAR_NEVER) {
RSG::texture_storage->render_target_request_clear(p_viewport->render_target, bgcolor);
Expand Down Expand Up @@ -1221,6 +1223,16 @@ void RendererViewport::viewport_set_canvas_transform(RID p_viewport, RID p_canva
viewport->canvas_map[p_canvas].transform = p_offset;
}

#ifdef PIXEL_ENGINE
void RendererViewport::viewport_set_clear_color(RID p_viewport, const Color &p_color) {
Viewport *viewport = viewport_owner.get_or_null(p_viewport);
ERR_FAIL_NULL(viewport);

RSG::texture_storage->render_target_set_clear_color(viewport->render_target, p_color);
viewport->clear_color = p_color;
}
#endif // PIXEL_ENGINE

void RendererViewport::viewport_set_transparent_background(RID p_viewport, bool p_enabled) {
Viewport *viewport = viewport_owner.get_or_null(p_viewport);
ERR_FAIL_NULL(viewport);
Expand Down
9 changes: 9 additions & 0 deletions servers/rendering/renderer_viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class RendererViewport {
RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;

#ifdef PIXEL_ENGINE
Color clear_color = Color(0.0, 0.0, 0.0, 1.0);
#endif // PIXEL_ENGINE
bool transparent_bg = false;
bool use_hdr_2d = false;

Expand Down Expand Up @@ -162,6 +165,9 @@ class RendererViewport {
view_count = 1;
update_mode = RS::VIEWPORT_UPDATE_WHEN_VISIBLE;
clear_mode = RS::VIEWPORT_CLEAR_ALWAYS;
#ifdef PIXEL_ENGINE
clear_color = Color(0.0, 0.0, 0.0, 1.0);
#endif // PIXEL_ENGINE
transparent_bg = false;
use_hdr_2d = false;

Expand Down Expand Up @@ -260,6 +266,9 @@ class RendererViewport {
void viewport_attach_canvas(RID p_viewport, RID p_canvas);
void viewport_remove_canvas(RID p_viewport, RID p_canvas);
void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset);
#ifdef PIXEL_ENGINE
void viewport_set_clear_color(RID p_viewport, const Color &p_color);
#endif // PIXEL_ENGINE
void viewport_set_transparent_background(RID p_viewport, bool p_enabled);
void viewport_set_use_hdr_2d(RID p_viewport, bool p_use_hdr_2d);

Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/rendering_server_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ class RenderingServerDefault : public RenderingServer {

FUNC2(viewport_remove_canvas, RID, RID)
FUNC3(viewport_set_canvas_transform, RID, RID, const Transform2D &)
#ifdef PIXEL_ENGINE
FUNC2(viewport_set_clear_color, RID, const Color &)
#endif // PIXEL_ENGINE
FUNC2(viewport_set_transparent_background, RID, bool)
FUNC2(viewport_set_use_hdr_2d, RID, bool)
FUNC1RC(bool, viewport_is_using_hdr_2d, RID)
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class RendererTextureStorage {
virtual Point2i render_target_get_position(RID p_render_target) const = 0;
virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) = 0; // Q change input to const Size2i &p_size ?
virtual Size2i render_target_get_size(RID p_render_target) const = 0;
#ifdef PIXEL_ENGINE
virtual void render_target_set_clear_color(RID p_render_target, const Color &p_clear_color) = 0;
#endif // PIXEL_ENGINE
virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) = 0;
virtual bool render_target_get_transparent(RID p_render_target) const = 0;
virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) = 0;
Expand Down
4 changes: 3 additions & 1 deletion servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2828,7 +2828,9 @@ void RenderingServer::_bind_methods() {

ClassDB::bind_method(D_METHOD("viewport_set_canvas_transform", "viewport", "canvas", "offset"), &RenderingServer::viewport_set_canvas_transform);
ClassDB::bind_method(D_METHOD("viewport_set_canvas_stacking", "viewport", "canvas", "layer", "sublayer"), &RenderingServer::viewport_set_canvas_stacking);

#ifdef PIXEL_ENGINE
ClassDB::bind_method(D_METHOD("viewport_set_clear_color", "viewport", "color"), &RenderingServer::viewport_set_clear_color);
#endif // PIXEL_ENGINE
ClassDB::bind_method(D_METHOD("viewport_set_transparent_background", "viewport", "enabled"), &RenderingServer::viewport_set_transparent_background);
ClassDB::bind_method(D_METHOD("viewport_set_global_canvas_transform", "viewport", "transform"), &RenderingServer::viewport_set_global_canvas_transform);

Expand Down
3 changes: 3 additions & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ class RenderingServer : public Object {
virtual void viewport_attach_canvas(RID p_viewport, RID p_canvas) = 0;
virtual void viewport_remove_canvas(RID p_viewport, RID p_canvas) = 0;
virtual void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) = 0;
#ifdef PIXEL_ENGINE
virtual void viewport_set_clear_color(RID p_viewport, const Color &p_color) = 0;
#endif // PIXEL_ENGINE
virtual void viewport_set_transparent_background(RID p_viewport, bool p_enabled) = 0;
virtual void viewport_set_use_hdr_2d(RID p_viewport, bool p_use_hdr) = 0;
virtual bool viewport_is_using_hdr_2d(RID p_viewport) const = 0;
Expand Down

0 comments on commit 982cae7

Please sign in to comment.