From cf5778e51a883936ffc896231da8259e5ebabc0a Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 5 Oct 2016 01:26:35 -0300 Subject: [PATCH] -Added ViewportContainer, this is the only way to make viewports show up in GUI now -2D editing now seems to work -Added some functions and refactoring to Viewport --- drivers/gles2/shaders/canvas.glsl | 1 + drivers/gles3/rasterizer_canvas_gles3.cpp | 18 +-- drivers/gles3/rasterizer_gles3.cpp | 4 +- drivers/gles3/rasterizer_storage_gles3.cpp | 64 ++++++++- drivers/gles3/rasterizer_storage_gles3.h | 5 +- scene/gui/viewport_container.cpp | 103 +++++++++++++++ scene/gui/viewport_container.h | 25 ++++ scene/main/viewport.cpp | 125 +++++++----------- scene/main/viewport.h | 24 ++-- scene/register_scene_types.cpp | 4 +- servers/visual/rasterizer.h | 1 - servers/visual/visual_server_raster.h | 3 +- servers/visual/visual_server_viewport.cpp | 41 ++++-- servers/visual/visual_server_viewport.h | 27 +++- servers/visual_server.h | 5 +- tools/editor/editor_node.cpp | 13 +- tools/editor/editor_node.h | 4 +- .../plugins/canvas_item_editor_plugin.cpp | 3 +- .../editor/plugins/spatial_editor_plugin.cpp | 4 +- tools/editor/plugins/spatial_editor_plugin.h | 2 +- 20 files changed, 340 insertions(+), 136 deletions(-) create mode 100644 scene/gui/viewport_container.cpp create mode 100644 scene/gui/viewport_container.h diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl index 38c579c15c2..eeab42ee648 100644 --- a/drivers/gles2/shaders/canvas.glsl +++ b/drivers/gles2/shaders/canvas.glsl @@ -395,5 +395,6 @@ LIGHT_SHADER_CODE // color.rgb*=color.a; gl_FragColor = color; + } diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index f24560763f3..a1e755cb4ae 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -111,6 +111,15 @@ void RasterizerCanvasGLES3::light_internal_free(RID p_rid) { void RasterizerCanvasGLES3::canvas_begin(){ + if (storage->frame.current_rt && storage->frame.clear_request) { + // a clear request may be pending, so do it + + glClearColor( storage->frame.clear_request_color.r, storage->frame.clear_request_color.g, storage->frame.clear_request_color.b, storage->frame.clear_request_color.a ); + glClear(GL_COLOR_BUFFER_BIT); + storage->frame.clear_request=false; + + } + /*canvas_shader.unbind(); canvas_shader.set_custom_shader(0); canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,false); @@ -504,7 +513,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item,Item *curr } if (rect->flags&CANVAS_RECT_FLIP_V) { - src_rect.size.x*=-1; + src_rect.size.y*=-1; } if (rect->flags&CANVAS_RECT_TRANSPOSE) { @@ -769,15 +778,8 @@ void RasterizerGLES2::_canvas_item_setup_shader_params(CanvasItemMaterial *mater void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list,int p_z,const Color& p_modulate,Light *p_light) { - if (storage->frame.clear_request) { - // a clear request may be pending, so do it - glClearColor( storage->frame.clear_request_color.r, storage->frame.clear_request_color.g, storage->frame.clear_request_color.b, storage->frame.clear_request_color.a ); - glClear(GL_COLOR_BUFFER_BIT); - storage->frame.clear_request=false; - } - Item *current_clip=NULL; RasterizerStorageGLES3::Shader *shader_cache=NULL; diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp index ba83a572e52..27b06906457 100644 --- a/drivers/gles3/rasterizer_gles3.cpp +++ b/drivers/gles3/rasterizer_gles3.cpp @@ -141,7 +141,7 @@ void RasterizerGLES3::set_current_render_target(RID p_render_target){ storage->frame.current_rt=rt; storage->frame.clear_request=false; - glViewport(0,0,rt->width,rt->height); + glViewport(0,0,rt->width,rt->height); } else { storage->frame.current_rt=NULL; @@ -155,6 +155,7 @@ void RasterizerGLES3::restore_render_target() { ERR_FAIL_COND(storage->frame.current_rt==NULL); RasterizerStorageGLES3::RenderTarget * rt = storage->frame.current_rt; + glBindFramebuffer(GL_FRAMEBUFFER,rt->front.fbo); glViewport(0,0,rt->width,rt->height); } @@ -176,6 +177,7 @@ void RasterizerGLES3::blit_render_target_to_screen(RID p_render_target,const Rec ERR_FAIL_COND(!rt); canvas->canvas_begin(); + glDisable(GL_BLEND); glBindFramebuffer(GL_FRAMEBUFFER,storage->config.system_fbo); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,rt->front.color); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index 515db390983..b4d65080a47 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -1483,6 +1483,12 @@ void RasterizerStorageGLES3::_render_target_clear(RenderTarget *rt) { rt->depth=0; } + Texture *tex = texture_owner.get(rt->texture); + tex->alloc_height=0; + tex->alloc_width=0; + tex->width=0; + tex->height=0; + } void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ @@ -1490,6 +1496,8 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ if (rt->width<=0 || rt->height<=0) return; + glActiveTexture(GL_TEXTURE0); + glGenFramebuffers(1, &rt->front.fbo); glBindFramebuffer(GL_FRAMEBUFFER, rt->front.fbo); @@ -1512,6 +1520,7 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ GLuint color_internal_format; GLuint color_format; GLuint color_type; + Image::Format image_format; if (config.fbo_format==FBO_FORMAT_16_BITS) { @@ -1520,28 +1529,33 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ color_internal_format=GL_RGB5_A1; color_format=GL_RGBA; color_type=GL_UNSIGNED_SHORT_5_5_5_1; + image_format=Image::FORMAT_RGBA5551; } else { color_internal_format=GL_RGB565; color_format=GL_RGB; color_type=GL_UNSIGNED_SHORT_5_6_5; + image_format=Image::FORMAT_RGB565; } - } else if (config.fbo_format==FBO_FORMAT_32_BITS) { + } else if (config.fbo_format==FBO_FORMAT_32_BITS || (config.fbo_format==FBO_FORMAT_FLOAT && rt->flags[RENDER_TARGET_NO_3D])) { if (rt->flags[RENDER_TARGET_TRANSPARENT]) { color_internal_format=GL_RGBA8; color_format=GL_RGBA; color_type=GL_UNSIGNED_BYTE; + image_format=Image::FORMAT_RGBA8; } else { color_internal_format=GL_RGB10_A2; color_format=GL_RGBA; color_type=GL_UNSIGNED_INT_2_10_10_10_REV; + image_format=Image::FORMAT_RGBA8;//todo } } else if (config.fbo_format==FBO_FORMAT_FLOAT) { color_internal_format=GL_RGBA16F; color_format=GL_RGBA; color_type=GL_HALF_FLOAT; + image_format=Image::FORMAT_RGBAH; } glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, NULL); @@ -1559,6 +1573,19 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ ERR_FAIL_COND( status != GL_FRAMEBUFFER_COMPLETE ); } + Texture *tex = texture_owner.get(rt->texture); + tex->format=image_format; + tex->gl_format_cache=color_format; + tex->gl_type_cache=color_type; + tex->gl_internal_format_cache=color_internal_format; + tex->tex_id=rt->front.color; + tex->width=rt->width; + tex->alloc_width=rt->width; + tex->height=rt->height; + tex->alloc_height=rt->height; + + + texture_set_flags(rt->texture,tex->flags); if (!rt->flags[RENDER_TARGET_NO_SAMPLING]) { @@ -1662,6 +1689,31 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt){ RID RasterizerStorageGLES3::render_target_create(){ RenderTarget *rt = memnew( RenderTarget ); + + Texture * t = memnew( Texture ); + + t->flags=0; + t->width=0; + t->height=0; + t->alloc_height=0; + t->alloc_width=0; + t->format=Image::FORMAT_R8; + t->target=GL_TEXTURE_2D; + t->gl_format_cache=0; + t->gl_internal_format_cache=0; + t->gl_type_cache=0; + t->data_size=0; + t->compressed=false; + t->srgb=false; + t->total_data_size=0; + t->ignore_mipmaps=false; + t->mipmaps=0; + t->active=true; + t->tex_id=0; + + + rt->texture=texture_owner.make_rid(t); + return render_target_owner.make_rid(rt); } @@ -1686,13 +1738,9 @@ RID RasterizerStorageGLES3::render_target_get_texture(RID p_render_target) const RenderTarget *rt = render_target_owner.getornull(p_render_target); ERR_FAIL_COND_V(!rt,RID()); - - return RID(); + return rt->texture; } -Image RasterizerStorageGLES3::render_target_get_image(RID p_render_target) const{ - return Image(); -} void RasterizerStorageGLES3::render_target_set_flag(RID p_render_target,RenderTargetFlags p_flag,bool p_value) { RenderTarget *rt = render_target_owner.getornull(p_render_target); @@ -1889,12 +1937,16 @@ bool RasterizerStorageGLES3::free(RID p_rid){ RenderTarget *rt = render_target_owner.getornull(p_rid); _render_target_clear(rt); + Texture *t=texture_owner.get(rt->texture); + texture_owner.free(rt->texture); + memdelete(t); render_target_owner.free(p_rid); memdelete(rt); } else if (texture_owner.owns(p_rid)) { // delete the texture Texture *texture = texture_owner.get(p_rid); + ERR_FAIL_COND_V(texture->render_target,true); //cant free the render target texture, dude info.texture_mem-=texture->total_data_size; texture_owner.free(p_rid); memdelete(texture); diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index eb6cd0bd925..9820fbc941a 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -117,7 +117,6 @@ public: bool active; GLuint tex_id; - RenderTarget *render_target; Texture() { @@ -361,6 +360,8 @@ public: bool used_in_frame; + RID texture; + RenderTarget() { width=0; @@ -387,7 +388,7 @@ public: virtual RID render_target_create(); virtual void render_target_set_size(RID p_render_target,int p_width, int p_height); virtual RID render_target_get_texture(RID p_render_target) const; - virtual Image render_target_get_image(RID p_render_target) const; + virtual void render_target_set_flag(RID p_render_target,RenderTargetFlags p_flag,bool p_value); virtual bool render_target_renedered_in_frame(RID p_render_target); diff --git a/scene/gui/viewport_container.cpp b/scene/gui/viewport_container.cpp new file mode 100644 index 00000000000..3418a495e9f --- /dev/null +++ b/scene/gui/viewport_container.cpp @@ -0,0 +1,103 @@ +#include "viewport_container.h" +#include "scene/main/viewport.h" +Size2 ViewportContainer::get_minimum_size() const { + + + if (stretch) + return Size2(); + Size2 ms; + for(int i=0;icast_to(); + if (!c) + continue; + + Size2 minsize = c->get_size(); + ms.width = MAX(ms.width , minsize.width); + ms.height = MAX(ms.height , minsize.height); + } + + return ms; + +} + + +void ViewportContainer::set_stretch(bool p_enable) { + + stretch=p_enable; + queue_sort(); + update(); + +} + +bool ViewportContainer::is_stretch_enabled() const { + + return stretch; +} + + +void ViewportContainer::_notification(int p_what) { + + + if (p_what==NOTIFICATION_RESIZED) { + + if (!stretch) + return; + + for(int i=0;icast_to(); + if (!c) + continue; + + c->set_size(get_size()); + } + } + + if (p_what==NOTIFICATION_ENTER_TREE || p_what==NOTIFICATION_VISIBILITY_CHANGED) { + + for(int i=0;icast_to(); + if (!c) + continue; + + + if (is_visible()) + c->set_update_mode(Viewport::UPDATE_ALWAYS); + else + c->set_update_mode(Viewport::UPDATE_DISABLED); + } + + } + + if (p_what==NOTIFICATION_DRAW) { + + for(int i=0;icast_to(); + if (!c) + continue; + + if (stretch) + draw_texture_rect(c->get_texture(),Rect2(Vector2(),get_size()*Size2(1,-1))); + else + draw_texture_rect(c->get_texture(),Rect2(Vector2(),c->get_size()*Size2(1,-1))); + } + } + +} + +void ViewportContainer::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_stretch","enable"),&ViewportContainer::set_stretch); + ObjectTypeDB::bind_method(_MD("is_stretch_enabled"),&ViewportContainer::is_stretch_enabled); + + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"stretch"),_SCS("set_stretch"),_SCS("is_stretch_enabled")); +} + +ViewportContainer::ViewportContainer() { + + stretch=false; +} diff --git a/scene/gui/viewport_container.h b/scene/gui/viewport_container.h new file mode 100644 index 00000000000..cfc58f7d6e3 --- /dev/null +++ b/scene/gui/viewport_container.h @@ -0,0 +1,25 @@ +#ifndef VIEWPORTCONTAINER_H +#define VIEWPORTCONTAINER_H + +#include "scene/gui/container.h" + +class ViewportContainer : public Container { + + OBJ_TYPE( ViewportContainer, Container ); + + bool stretch; +protected: + + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_stretch(bool p_enable); + bool is_stretch_enabled() const; + + virtual Size2 get_minimum_size() const; + + ViewportContainer(); +}; + +#endif // VIEWPORTCONTAINER_H diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 44a3f41a4ae..e93a16371d3 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -51,35 +51,37 @@ #include "globals.h" -int RenderTargetTexture::get_width() const { +int ViewportTexture::get_width() const { ERR_FAIL_COND_V(!vp,0); return vp->size.width; } -int RenderTargetTexture::get_height() const{ +int ViewportTexture::get_height() const{ ERR_FAIL_COND_V(!vp,0); return vp->size.height; } -Size2 RenderTargetTexture::get_size() const{ +Size2 ViewportTexture::get_size() const{ ERR_FAIL_COND_V(!vp,Size2()); return vp->size; } -RID RenderTargetTexture::get_rid() const{ +RID ViewportTexture::get_rid() const{ ERR_FAIL_COND_V(!vp,RID()); return vp->texture_rid; } -bool RenderTargetTexture::has_alpha() const{ +bool ViewportTexture::has_alpha() const{ return false; } -void RenderTargetTexture::set_flags(uint32_t p_flags){ +void ViewportTexture::set_flags(uint32_t p_flags){ + + if (!vp) + return; - ERR_FAIL_COND(!vp); if (p_flags&FLAG_FILTER) flags=FLAG_FILTER; else @@ -89,12 +91,12 @@ void RenderTargetTexture::set_flags(uint32_t p_flags){ } -uint32_t RenderTargetTexture::get_flags() const{ +uint32_t ViewportTexture::get_flags() const{ return flags; } -RenderTargetTexture::RenderTargetTexture(Viewport *p_vp){ +ViewportTexture::ViewportTexture(Viewport *p_vp){ vp=p_vp; flags=0; @@ -207,7 +209,7 @@ void Viewport::_parent_draw() { void Viewport::_parent_visibility_changed() { - +/* if (parent_control) { Control *c = parent_control; @@ -216,14 +218,14 @@ void Viewport::_parent_visibility_changed() { _update_listener(); _update_listener_2d(); } - +*/ } void Viewport::_vp_enter_tree() { - if (parent_control) { +/* if (parent_control) { Control *cparent=parent_control; RID parent_ci = cparent->get_canvas_item(); @@ -240,7 +242,7 @@ void Viewport::_vp_enter_tree() { // VisualServer::get_singleton()->viewport_attach_to_screen(viewport,0); } - +*/ } @@ -327,24 +329,10 @@ void Viewport::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { if (get_parent()) { - Node *parent=get_parent(); - if (parent) { - parent_control=parent->cast_to(); - } - } - - - parent=NULL; - Node *parent_node=get_parent(); - - - while(parent_node) { - - parent = parent_node->cast_to(); - if (parent) - break; - - parent_node=parent_node->get_parent(); + parent = get_parent()->get_viewport(); + VisualServer::get_singleton()->viewport_set_parent_viewport(viewport,parent->get_viewport()); + } else { + parent=NULL; } current_canvas=find_world_2d()->get_canvas(); @@ -434,7 +422,7 @@ void Viewport::_notification(int p_what) { } remove_from_group("_viewports"); - parent_control=NULL; + VS::get_singleton()->viewport_set_active(viewport,false); @@ -1263,7 +1251,7 @@ bool Viewport::is_set_as_render_target() const{ void Viewport::set_update_mode(UpdateMode p_mode){ update_mode=p_mode; -// VS::get_singleton()->viewport_set_update_mode(viewport,VS::RenderTargetUpdateMode(p_mode)); + VS::get_singleton()->viewport_set_update_mode(viewport,VS::ViewportUpdateMode(p_mode)); } Viewport::UpdateMode Viewport::get_update_mode() const{ @@ -1282,7 +1270,7 @@ Image Viewport::get_screen_capture() const { return Image(); } -Ref Viewport::get_texture() const { +Ref Viewport::get_texture() const { return texture; } @@ -1290,7 +1278,7 @@ Ref Viewport::get_texture() const { void Viewport::set_vflip(bool p_enable) { vflip=p_enable; -// VisualServer::get_singleton()->viewport_set_vflip(viewport,p_enable); + VisualServer::get_singleton()->viewport_set_vflip(viewport,p_enable); } bool Viewport::get_vflip() const{ @@ -1315,30 +1303,6 @@ void Viewport::clear() { // VisualServer::get_singleton()->viewport_clear(viewport); } -void Viewport::set_filter(bool p_enable) { - - texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0)); - -} - -bool Viewport::get_filter() const{ - - return (texture->get_flags()&Texture::FLAG_FILTER)!=0; -} - -void Viewport::set_gen_mipmaps(bool p_enable) { - - //texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0)); - gen_mipmaps=p_enable; - -} - -bool Viewport::get_gen_mipmaps() const{ - - //return (texture->get_flags()&Texture::FLAG_FILTER)!=0; - return gen_mipmaps; -} - Matrix32 Viewport::_get_input_pre_xform() const { @@ -1356,9 +1320,9 @@ Matrix32 Viewport::_get_input_pre_xform() const { Vector2 Viewport::_get_window_offset() const { - if (parent_control) { - return (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin(); - } +// if (parent_control) { +// return (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin(); +// } return Vector2(); } @@ -1454,8 +1418,6 @@ void Viewport::_vp_input(const InputEvent& p_ev) { } #endif - if (parent_control && !parent_control->is_visible()) - return; if (to_screen_rect==Rect2()) return; //if render target, can't get input events @@ -1480,8 +1442,8 @@ void Viewport::_vp_unhandled_input(const InputEvent& p_ev) { } #endif - if (parent_control && !parent_control->is_visible()) - return; +// if (parent_control && !parent_control->is_visible()) +// return; if (to_screen_rect==Rect2()) return; //if render target, can't get input events @@ -2575,6 +2537,16 @@ bool Viewport::is_input_disabled() const { return disable_input; } +void Viewport::set_disable_3d(bool p_disable) { + disable_3d=p_disable; + VS::get_singleton()->viewport_set_disable_3d(viewport,p_disable); +} + +bool Viewport::is_3d_disabled() const { + + return disable_3d; +} + Variant Viewport::gui_get_drag_data() const { return gui.drag_data; } @@ -2648,17 +2620,10 @@ void Viewport::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_clear_on_new_frame"), &Viewport::get_clear_on_new_frame); ObjectTypeDB::bind_method(_MD("clear"), &Viewport::clear); - - ObjectTypeDB::bind_method(_MD("set_filter","enable"), &Viewport::set_filter); - ObjectTypeDB::bind_method(_MD("get_filter"), &Viewport::get_filter); - - ObjectTypeDB::bind_method(_MD("set_gen_mipmaps","enable"), &Viewport::set_gen_mipmaps); - ObjectTypeDB::bind_method(_MD("get_gen_mipmaps"), &Viewport::get_gen_mipmaps); - ObjectTypeDB::bind_method(_MD("set_update_mode","mode"), &Viewport::set_update_mode); ObjectTypeDB::bind_method(_MD("get_update_mode"), &Viewport::get_update_mode); - ObjectTypeDB::bind_method(_MD("get_texture:RenderTargetTexture"), &Viewport::get_texture); + ObjectTypeDB::bind_method(_MD("get_texture:ViewportTexture"), &Viewport::get_texture); ObjectTypeDB::bind_method(_MD("set_physics_object_picking","enable"), &Viewport::set_physics_object_picking); ObjectTypeDB::bind_method(_MD("get_physics_object_picking"), &Viewport::get_physics_object_picking); @@ -2690,6 +2655,9 @@ void Viewport::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_disable_input","disable"), &Viewport::set_disable_input); ObjectTypeDB::bind_method(_MD("is_input_disabled"), &Viewport::is_input_disabled); + ObjectTypeDB::bind_method(_MD("set_disable_3d","disable"), &Viewport::set_disable_3d); + ObjectTypeDB::bind_method(_MD("is_3d_disabled"), &Viewport::is_3d_disabled); + ObjectTypeDB::bind_method(_MD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip); ObjectTypeDB::bind_method(_MD("_gui_remove_focus"), &Viewport::_gui_remove_focus); @@ -2700,13 +2668,12 @@ void Viewport::_bind_methods() { ADD_PROPERTY( PropertyInfo(Variant::BOOL,"transparent_bg"), _SCS("set_transparent_background"), _SCS("has_transparent_background") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/v_flip"), _SCS("set_vflip"), _SCS("get_vflip") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/clear_on_new_frame"), _SCS("set_clear_on_new_frame"), _SCS("get_clear_on_new_frame") ); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/filter"), _SCS("set_filter"), _SCS("get_filter") ); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/gen_mipmaps"), _SCS("set_gen_mipmaps"), _SCS("get_gen_mipmaps") ); ADD_PROPERTY( PropertyInfo(Variant::INT,"render_target/update_mode",PROPERTY_HINT_ENUM,"Disabled,Once,When Visible,Always"), _SCS("set_update_mode"), _SCS("get_update_mode") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"audio_listener/enable_2d"), _SCS("set_as_audio_listener_2d"), _SCS("is_audio_listener_2d") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"audio_listener/enable_3d"), _SCS("set_as_audio_listener"), _SCS("is_audio_listener") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"physics/object_picking"), _SCS("set_physics_object_picking"), _SCS("get_physics_object_picking") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"gui/disable_input"), _SCS("set_disable_input"), _SCS("is_input_disabled") ); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"3d/disable_3d"), _SCS("set_disable_3d"), _SCS("is_3d_disabled") ); ADD_SIGNAL(MethodInfo("size_changed")); @@ -2727,6 +2694,7 @@ Viewport::Viewport() { world_2d = Ref( memnew( World2D )); viewport = VisualServer::get_singleton()->viewport_create(); + texture_rid=VisualServer::get_singleton()->viewport_get_texture(viewport); internal_listener = SpatialSoundServer::get_singleton()->listener_create(); audio_listener=false; internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create(); @@ -2744,7 +2712,7 @@ Viewport::Viewport() { clear_on_new_frame=true; //clear=true; update_mode=UPDATE_WHEN_VISIBLE; - texture = Ref( memnew( RenderTargetTexture(this) ) ); + texture = Ref( memnew( ViewportTexture(this) ) ); physics_object_picking=false; physics_object_capture=0; @@ -2759,6 +2727,7 @@ Viewport::Viewport() { unhandled_key_input_group = "_vp_unhandled_key_input"+id; disable_input=false; + disable_3d=false; //window tooltip gui.tooltip_timer = -1; @@ -2773,7 +2742,7 @@ Viewport::Viewport() { gui.canvas_sort_index=0; - parent_control=NULL; + } diff --git a/scene/main/viewport.h b/scene/main/viewport.h index d610237bec8..c5d61469dd4 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -48,9 +48,9 @@ class Label; class Timer; class Viewport; -class RenderTargetTexture : public Texture { +class ViewportTexture : public Texture { - OBJ_TYPE( RenderTargetTexture, Texture ); + OBJ_TYPE( ViewportTexture, Texture ); int flags; friend class Viewport; @@ -70,7 +70,7 @@ public: virtual void set_flags(uint32_t p_flags); virtual uint32_t get_flags() const; - RenderTargetTexture(Viewport *p_vp=NULL); + ViewportTexture(Viewport *p_vp=NULL); }; @@ -88,10 +88,10 @@ public: private: -friend class RenderTargetTexture; +friend class ViewportTexture; + - Control *parent_control; Viewport *parent; Listener *listener; @@ -101,7 +101,6 @@ friend class RenderTargetTexture; Set cameras; RID viewport; - RID canvas_item; RID current_canvas; bool audio_listener; @@ -171,9 +170,10 @@ friend class RenderTargetTexture; void _update_global_transform(); + bool disable_3d; UpdateMode update_mode; RID texture_rid; - Ref texture; + Ref texture; struct GUI { @@ -342,15 +342,10 @@ public: bool get_clear_on_new_frame() const; void clear(); - void set_filter(bool p_enable); - bool get_filter() const; - - void set_gen_mipmaps(bool p_enable); - bool get_gen_mipmaps() const; void set_update_mode(UpdateMode p_mode); UpdateMode get_update_mode() const; - Ref get_texture() const; + Ref get_texture() const; Vector2 get_camera_coords(const Vector2& p_viewport_coords) const; @@ -368,6 +363,9 @@ public: void set_disable_input(bool p_disable); bool is_input_disabled() const; + void set_disable_3d(bool p_disable); + bool is_3d_disabled() const; + void set_attach_to_screen_rect(const Rect2& p_rect); Rect2 get_attach_to_screen_rect() const; diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index dcb2edf8724..1bff6801fe5 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -66,6 +66,7 @@ #include "scene/gui/center_container.h" #include "scene/gui/scroll_container.h" #include "scene/gui/margin_container.h" +#include "scene/gui/viewport_container.h" #include "scene/gui/panel.h" #include "scene/gui/spin_box.h" #include "scene/gui/file_dialog.h" @@ -300,7 +301,7 @@ void register_scene_types() { ObjectTypeDB::register_virtual_type(); ObjectTypeDB::register_type(); - ObjectTypeDB::register_virtual_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); @@ -390,6 +391,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); OS::get_singleton()->yield(); //may take time to init diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index 6a6eb7ad20f..865298f64ec 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -233,7 +233,6 @@ public: virtual RID render_target_create()=0; virtual void render_target_set_size(RID p_render_target,int p_width, int p_height)=0; virtual RID render_target_get_texture(RID p_render_target) const=0; - virtual Image render_target_get_image(RID p_render_target) const=0; virtual void render_target_set_flag(RID p_render_target,RenderTargetFlags p_flag,bool p_value)=0; virtual bool render_target_renedered_in_frame(RID p_render_target)=0; diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index cd6d7b38d36..2171998f3fe 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -807,6 +807,7 @@ public: BIND3(viewport_set_size,RID,int ,int ) BIND2(viewport_set_active,RID ,bool ) + BIND2(viewport_set_parent_viewport,RID,RID) BIND2(viewport_set_clear_mode,RID,ViewportClearMode ) @@ -818,11 +819,11 @@ public: BIND1RC(RID,viewport_get_texture,RID ) - BIND1RC(Image,viewport_capture,RID ) BIND2(viewport_set_hide_scenario,RID,bool ) BIND2(viewport_set_hide_canvas,RID,bool ) BIND2(viewport_set_disable_environment,RID,bool ) + BIND2(viewport_set_disable_3d,RID,bool ) BIND2(viewport_attach_camera,RID,RID ) BIND2(viewport_set_scenario,RID,RID ) diff --git a/servers/visual/visual_server_viewport.cpp b/servers/visual/visual_server_viewport.cpp index b30a57a5b84..baf18b7a562 100644 --- a/servers/visual/visual_server_viewport.cpp +++ b/servers/visual/visual_server_viewport.cpp @@ -1,6 +1,7 @@ #include "visual_server_viewport.h" #include "visual_server_global.h" #include "visual_server_canvas.h" +#include "globals.h" void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { @@ -50,7 +51,12 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { } #endif - VSG::rasterizer->clear_render_target(Color(0.5,0.5,0.5,1.0)); + if (p_viewport->clear_mode!=VS::VIEWPORT_CLEAR_NEVER) { + VSG::rasterizer->clear_render_target(clear_color); + if (p_viewport->clear_mode==VS::VIEWPORT_CLEAR_ONLY_NEXT_FRAME) { + p_viewport->clear_mode=VS::VIEWPORT_CLEAR_NEVER; + } + } if (!p_viewport->hide_canvas) { int i=0; @@ -161,10 +167,10 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { light=light->shadows_next_ptr; } - VSG::rasterizer->restore_render_target(); // VSG::canvas_render->reset_canvas(); } + VSG::rasterizer->restore_render_target(); #if 0 @@ -175,6 +181,8 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport) { } #endif + + for (Map::Element *E=canvas_map.front();E;E=E->next()) { VisualServerCanvas::Canvas *canvas = static_cast(E->get()->canvas); @@ -226,6 +234,10 @@ void VisualServerViewport::draw_viewports() { //draw viewports + clear_color=GLOBAL_DEF("rendering/viewport/default_clear_color",Color(0.5,0.5,0.5)); + + + active_viewports.sort_custom(); for(int i=0;iparent=p_parent_viewport; +} + void VisualServerViewport::viewport_set_clear_mode(RID p_viewport,VS::ViewportClearMode p_clear_mode) { Viewport * viewport = viewport_owner.getornull(p_viewport); @@ -349,13 +369,6 @@ RID VisualServerViewport::viewport_get_texture(RID p_viewport) const{ return VSG::storage->render_target_get_texture(viewport->render_target); -} -Image VisualServerViewport::viewport_capture(RID p_viewport) const{ - - const Viewport * viewport = viewport_owner.getornull(p_viewport); - ERR_FAIL_COND_V(!viewport,Image()); - return VSG::storage->render_target_get_image(viewport->render_target); - } void VisualServerViewport::viewport_set_hide_scenario(RID p_viewport,bool p_hide){ @@ -381,6 +394,16 @@ void VisualServerViewport::viewport_set_disable_environment(RID p_viewport,bool viewport->disable_environment=p_disable; } +void VisualServerViewport::viewport_set_disable_3d(RID p_viewport,bool p_disable){ + + Viewport * viewport = viewport_owner.getornull(p_viewport); + ERR_FAIL_COND(!viewport); + + + viewport->disable_3d=p_disable; + VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_NO_3D,p_disable); +} + void VisualServerViewport::viewport_attach_camera(RID p_viewport,RID p_camera){ Viewport * viewport = viewport_owner.getornull(p_viewport); diff --git a/servers/visual/visual_server_viewport.h b/servers/visual/visual_server_viewport.h index e5c888fbcb9..cba33a3b2d1 100644 --- a/servers/visual/visual_server_viewport.h +++ b/servers/visual/visual_server_viewport.h @@ -13,6 +13,8 @@ public: }; + + struct Viewport : public RID_Data { RID self; @@ -32,8 +34,8 @@ public: bool hide_scenario; bool hide_canvas; bool disable_environment; + bool disable_3d; - Image capture; VS::ViewportClearMode clear_mode; @@ -70,8 +72,27 @@ public: }; mutable RID_Owner viewport_owner; + + + struct ViewportSort { + _FORCE_INLINE_ bool operator()(const Viewport*p_left,const Viewport* p_right) const { + + bool left_to_screen = p_left->viewport_to_screen_rect.size!=Size2(); + bool right_to_screen = p_right->viewport_to_screen_rect.size!=Size2(); + + if (left_to_screen==right_to_screen) { + + return p_left->parent==p_right->self; + } else { + return right_to_screen; + } + } + }; + + Vector active_viewports; private: + Color clear_color; void _draw_viewport(Viewport *p_viewport); public: @@ -84,7 +105,7 @@ public: void viewport_detach(RID p_viewport); void viewport_set_active(RID p_viewport,bool p_active); - + void viewport_set_parent_viewport(RID p_viewport,RID p_parent_viewport); void viewport_set_update_mode(RID p_viewport,VS::ViewportUpdateMode p_mode); void viewport_set_vflip(RID p_viewport,bool p_enable); @@ -92,11 +113,11 @@ public: void viewport_set_clear_mode(RID p_viewport,VS::ViewportClearMode p_clear_mode); RID viewport_get_texture(RID p_viewport) const; - Image viewport_capture(RID p_viewport) const; void viewport_set_hide_scenario(RID p_viewport,bool p_hide); void viewport_set_hide_canvas(RID p_viewport,bool p_hide); void viewport_set_disable_environment(RID p_viewport,bool p_disable); + void viewport_set_disable_3d(RID p_viewport,bool p_disable); void viewport_attach_camera(RID p_viewport,RID p_camera); void viewport_set_scenario(RID p_viewport,RID p_scenario); diff --git a/servers/visual_server.h b/servers/visual_server.h index 4eb2b2a5fa5..238bcc216da 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -424,6 +424,7 @@ public: virtual void viewport_set_size(RID p_viewport,int p_width,int p_height)=0; virtual void viewport_set_active(RID p_viewport,bool p_active)=0; + virtual void viewport_set_parent_viewport(RID p_viewport,RID p_parent_viewport)=0; virtual void viewport_attach_to_screen(RID p_viewport,const Rect2& p_rect=Rect2(),int p_screen=0)=0; virtual void viewport_detach(RID p_viewport)=0; @@ -442,18 +443,18 @@ public: enum ViewportClearMode { VIEWPORT_CLEAR_ALWAYS, - VIWEPORT_CLEAR_NEVER, + VIEWPORT_CLEAR_NEVER, VIEWPORT_CLEAR_ONLY_NEXT_FRAME }; virtual void viewport_set_clear_mode(RID p_viewport,ViewportClearMode p_clear_mode)=0; virtual RID viewport_get_texture(RID p_viewport) const=0; - virtual Image viewport_capture(RID p_viewport) const=0; virtual void viewport_set_hide_scenario(RID p_viewport,bool p_hide)=0; virtual void viewport_set_hide_canvas(RID p_viewport,bool p_hide)=0; virtual void viewport_set_disable_environment(RID p_viewport,bool p_disable)=0; + virtual void viewport_set_disable_3d(RID p_viewport,bool p_disable)=0; virtual void viewport_attach_camera(RID p_viewport,RID p_camera)=0; virtual void viewport_set_scenario(RID p_viewport,RID p_scenario)=0; diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index 7f026241074..9b5ed422c5a 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -266,14 +266,12 @@ void EditorNode::_notification(int p_what) { circle_step=0; circle_step_msec=tick; - circle_step_frame=frame+1; + circle_step_frame=frame+1; update_menu->set_icon(gui_base->get_icon("Progress"+itos(circle_step+1),"EditorIcons")); } - scene_root->set_size_override(true,Size2(Globals::get_singleton()->get("display/width"),Globals::get_singleton()->get("display/height"))); - editor_selection->update(); { @@ -300,7 +298,7 @@ void EditorNode::_notification(int p_what) { } if (p_what==NOTIFICATION_ENTER_TREE) { - + get_tree()->get_root()->set_disable_3d(true); //MessageQueue::get_singleton()->push_call(this,"_get_scene_metadata"); get_tree()->set_editor_hint(true); get_tree()->get_root()->set_as_audio_listener(false); @@ -5663,6 +5661,7 @@ EditorNode::EditorNode() { scene_root_parent->set_custom_minimum_size(Size2(0,80)*EDSCALE); + //Ref sp = scene_root_parent->get_stylebox("panel","TabContainer"); //scene_root_parent->add_style_override("panel",sp); @@ -5676,6 +5675,8 @@ EditorNode::EditorNode() { scene_root = memnew( Viewport ); + scene_root->set_disable_3d(true); + //scene_root_base->add_child(scene_root); @@ -5683,7 +5684,7 @@ EditorNode::EditorNode() { VisualServer::get_singleton()->viewport_set_hide_scenario(scene_root->get_viewport(),true); scene_root->set_disable_input(true); scene_root->set_as_audio_listener_2d(true); - scene_root->set_size_override(true,Size2(Globals::get_singleton()->get("display/width"),Globals::get_singleton()->get("display/height"))); + //scene_root->set_size_override(true,Size2(Globals::get_singleton()->get("display/width"),Globals::get_singleton()->get("display/height"))); // scene_root->set_world_2d( Ref( memnew( World2D )) ); @@ -5696,6 +5697,7 @@ EditorNode::EditorNode() { scene_root_parent->add_child(viewport); + PanelContainer *top_region = memnew( PanelContainer ); top_region->add_style_override("panel",gui_base->get_stylebox("hover","Button")); HBoxContainer *left_menu_hb = memnew( HBoxContainer ); @@ -6673,6 +6675,7 @@ EditorNode::EditorNode() { load_error_dialog->set_child_rect(load_errors); gui_base->add_child(load_error_dialog); + //EditorImport::add_importer( Ref( memnew(EditorImporterCollada ))); EditorFileSystem::get_singleton()->connect("sources_changed",this,"_sources_changed"); diff --git a/tools/editor/editor_node.h b/tools/editor/editor_node.h index 0393cd19a94..c5271860eb5 100644 --- a/tools/editor/editor_node.h +++ b/tools/editor/editor_node.h @@ -79,7 +79,7 @@ #include "fileserver/editor_file_server.h" #include "editor_resource_preview.h" - +#include "scene/gui/viewport_container.h" #include "progress_dialog.h" @@ -207,7 +207,7 @@ private: //Ref scene_import_metadata; - Control* scene_root_parent; + PanelContainer* scene_root_parent; Control *gui_base; VBoxContainer *main_vbox; diff --git a/tools/editor/plugins/canvas_item_editor_plugin.cpp b/tools/editor/plugins/canvas_item_editor_plugin.cpp index b0e002ba448..2025fe825e5 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.cpp +++ b/tools/editor/plugins/canvas_item_editor_plugin.cpp @@ -3320,7 +3320,8 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { vp_base->set_v_size_flags(SIZE_EXPAND_FILL); palette_split->add_child(vp_base); - Control *vp = memnew (Control); + ViewportContainer *vp = memnew (ViewportContainer); + vp->set_stretch(true); vp_base->add_child(vp); vp->set_area_as_parent_rect(); vp->add_child(p_editor->get_scene_root()); diff --git a/tools/editor/plugins/spatial_editor_plugin.cpp b/tools/editor/plugins/spatial_editor_plugin.cpp index afea6f93dd7..55a4c6c9347 100644 --- a/tools/editor/plugins/spatial_editor_plugin.cpp +++ b/tools/editor/plugins/spatial_editor_plugin.cpp @@ -2348,7 +2348,7 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed message_time=0; spatial_editor=p_spatial_editor; - Control *c=memnew(Control); + ViewportContainer *c=memnew(ViewportContainer); add_child(c); c->set_area_as_parent_rect(); viewport = memnew( Viewport ); @@ -3973,7 +3973,7 @@ SpatialEditor::SpatialEditor(EditorNode *p_editor) { - settings_light_base = memnew( Control ); + settings_light_base = memnew( ViewportContainer ); settings_light_base->set_custom_minimum_size(Size2(128,128)); settings_light_base->connect("input_event",this,"_default_light_angle_input"); settings_vbc->add_margin_child(TTR("Default Light Normal:"),settings_light_base); diff --git a/tools/editor/plugins/spatial_editor_plugin.h b/tools/editor/plugins/spatial_editor_plugin.h index 2a52eab777f..4751cf70717 100644 --- a/tools/editor/plugins/spatial_editor_plugin.h +++ b/tools/editor/plugins/spatial_editor_plugin.h @@ -430,7 +430,7 @@ private: float settings_default_light_rot_x; float settings_default_light_rot_y; - Control *settings_light_base; + ViewportContainer *settings_light_base; Viewport *settings_light_vp; ColorPickerButton *settings_ambient_color; Image settings_light_dir_image;