From 5ef3f7392faf0d2d9c136fc176f7a08cb774fe40 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 18 Feb 2015 19:39:44 -0300 Subject: [PATCH] support for light and normal mapping in 2D --- core/math/math_2d.h | 162 ++++++- demos/2d/navpoly/navigation2.scn | Bin 0 -> 3564 bytes drivers/gles2/rasterizer_gles2.cpp | 555 +++++++++++++++--------- drivers/gles2/rasterizer_gles2.h | 12 +- drivers/gles2/shader_compiler_gles2.cpp | 7 +- drivers/gles2/shaders/canvas.glsl | 62 ++- platform/winrt/os_winrt.cpp | 5 +- scene/2d/canvas_item.cpp | 15 + scene/2d/canvas_item.h | 6 +- scene/2d/canvas_modulate.cpp | 46 ++ scene/2d/canvas_modulate.h | 23 + scene/2d/light_2d.cpp | 103 ++++- scene/2d/light_2d.h | 36 +- scene/gui/popup_menu.h | 2 +- scene/register_scene_types.cpp | 2 + servers/visual/rasterizer.h | 29 +- servers/visual/visual_server_raster.cpp | 144 +++++- servers/visual/visual_server_raster.h | 28 +- servers/visual/visual_server_wrap_mt.h | 7 +- servers/visual_server.h | 18 +- 20 files changed, 964 insertions(+), 298 deletions(-) create mode 100644 demos/2d/navpoly/navigation2.scn create mode 100644 scene/2d/canvas_modulate.cpp create mode 100644 scene/2d/canvas_modulate.h diff --git a/core/math/math_2d.h b/core/math/math_2d.h index fa40d305f5a..5bc1b5f0be1 100644 --- a/core/math/math_2d.h +++ b/core/math/math_2d.h @@ -159,8 +159,8 @@ struct Vector2 { operator String() const { return String::num(x)+","+String::num(y); } - inline Vector2(float p_x,float p_y) { x=p_x; y=p_y; } - inline Vector2() { x=0; y=0; } + _FORCE_INLINE_ Vector2(float p_x,float p_y) { x=p_x; y=p_y; } + _FORCE_INLINE_ Vector2() { x=0; y=0; } }; _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2& p_vec) const { @@ -198,6 +198,8 @@ Vector2 Vector2::linear_interpolate(const Vector2& p_a, const Vector2& p_b,float typedef Vector2 Size2; typedef Vector2 Point2; +struct Matrix32; + struct Rect2 { @@ -224,6 +226,8 @@ struct Rect2 { return true; } + _FORCE_INLINE_ bool intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const; + bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const; inline bool encloses(const Rect2& p_rect) const { @@ -597,6 +601,160 @@ struct Matrix32 { }; +bool Rect2::intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const { + + //SAT intersection between local and transformed rect2 + + Vector2 xf_points[4]={ + p_xform.xform(p_rect.pos), + p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y)), + p_xform.xform(Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y)), + p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y)), + }; + + real_t low_limit; + + //base rect2 first (faster) + + if (xf_points[0].y>pos.y) + goto next1; + if (xf_points[1].y>pos.y) + goto next1; + if (xf_points[2].y>pos.y) + goto next1; + if (xf_points[3].y>pos.y) + goto next1; + + return false; + + next1: + + low_limit=pos.y+size.y; + + if (xf_points[0].ypos.x) + goto next3; + if (xf_points[1].x>pos.x) + goto next3; + if (xf_points[2].x>pos.x) + goto next3; + if (xf_points[3].x>pos.x) + goto next3; + + return false; + + next3: + + low_limit=pos.x+size.x; + + if (xf_points[0].x maxb ) + return false; + if ( minb > maxa ) + return false; + + maxa=p_xform.elements[1].dot(xf_points2[0]); + mina=maxa; + + dp = p_xform.elements[1].dot(xf_points2[1]); + maxa=MAX(dp,maxa); + mina=MIN(dp,mina); + + dp = p_xform.elements[1].dot(xf_points2[2]); + maxa=MAX(dp,maxa); + mina=MIN(dp,mina); + + dp = p_xform.elements[1].dot(xf_points2[3]); + maxa=MAX(dp,maxa); + mina=MIN(dp,mina); + + maxb=p_xform.elements[1].dot(xf_points[0]); + minb=maxb; + + dp = p_xform.elements[1].dot(xf_points[1]); + maxb=MAX(dp,maxb); + minb=MIN(dp,minb); + + dp = p_xform.elements[1].dot(xf_points[2]); + maxb=MAX(dp,maxb); + minb=MIN(dp,minb); + + dp = p_xform.elements[1].dot(xf_points[3]); + maxb=MAX(dp,maxb); + minb=MIN(dp,minb); + + + if ( mina > maxb ) + return false; + if ( minb > maxa ) + return false; + + + return true; + +} Vector2 Matrix32::basis_xform(const Vector2& v) const { diff --git a/demos/2d/navpoly/navigation2.scn b/demos/2d/navpoly/navigation2.scn new file mode 100644 index 0000000000000000000000000000000000000000..224aed73f505d13cdd3ff9bff441f64bad0c0808 GIT binary patch literal 3564 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 L*nkc6Kkxqlpyh3~ literal 0 HcmV?d00001 diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp index d1e55f2488c..382f4aa2e3f 100644 --- a/drivers/gles2/rasterizer_gles2.cpp +++ b/drivers/gles2/rasterizer_gles2.cpp @@ -6409,7 +6409,7 @@ void RasterizerGLES2::_render_list_forward(RenderList *p_render_list,const Trans } break; case VS::MATERIAL_BLEND_MODE_SUB: { - glBlendEquation(GL_FUNC_SUBTRACT); + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); glBlendFunc(GL_SRC_ALPHA,GL_ONE); } break; case VS::MATERIAL_BLEND_MODE_MUL: { @@ -7824,8 +7824,10 @@ void RasterizerGLES2::canvas_begin() { //material_shader.unbind(); canvas_shader.unbind(); canvas_shader.set_custom_shader(0); + canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,false); canvas_shader.bind(); canvas_shader.set_uniform(CanvasShaderGLES2::TEXTURE, 0); + canvas_use_modulate=false; _set_color_attrib(Color(1,1,1)); canvas_transform=Transform(); canvas_transform.translate(-(viewport.width / 2.0f), -(viewport.height / 2.0f), 0.0f); @@ -7840,7 +7842,6 @@ void RasterizerGLES2::canvas_begin() { canvas_opacity=1.0; canvas_blend_mode=VS::MATERIAL_BLEND_MODE_MIX; - canvas_texscreen_used=false; uses_texpixel_size=false; canvas_last_shader=RID(); @@ -7876,7 +7877,7 @@ void RasterizerGLES2::canvas_set_blend_mode(VS::MaterialBlendMode p_mode) { } break; case VS::MATERIAL_BLEND_MODE_SUB: { - glBlendEquation(GL_FUNC_SUBTRACT); + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); glBlendFunc(GL_SRC_ALPHA,GL_ONE); } break; case VS::MATERIAL_BLEND_MODE_MUL: { @@ -8325,13 +8326,265 @@ void RasterizerGLES2::canvas_set_transform(const Matrix32& p_transform) { //canvas_transform = Variant(p_transform); } +void RasterizerGLES2::_canvas_normal_set_flip(const Vector2& p_flip) { -void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { + if (p_flip==normal_flip) + return; + normal_flip=p_flip; + canvas_shader.set_uniform(CanvasShaderGLES2::NORMAL_FLIP,normal_flip); +} + + +template +void RasterizerGLES2::_canvas_item_render_commands(CanvasItem *p_item,CanvasItem *current_clip,bool &reclip) { + + int cc=p_item->commands.size(); + CanvasItem::Command **commands = p_item->commands.ptr(); + + + for(int i=0;itype) { + case CanvasItem::Command::TYPE_LINE: { + + CanvasItem::CommandLine* line = static_cast(c); + canvas_draw_line(line->from,line->to,line->color,line->width); + } break; + case CanvasItem::Command::TYPE_RECT: { + + CanvasItem::CommandRect* rect = static_cast(c); +// canvas_draw_rect(rect->rect,rect->region,rect->source,rect->flags&CanvasItem::CommandRect::FLAG_TILE,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V,rect->texture,rect->modulate); +#if 0 + int flags=0; + + if (rect->flags&CanvasItem::CommandRect::FLAG_REGION) { + flags|=Rasterizer::CANVAS_RECT_REGION; + } + if (rect->flags&CanvasItem::CommandRect::FLAG_TILE) { + flags|=Rasterizer::CANVAS_RECT_TILE; + } + if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H) { + + flags|=Rasterizer::CANVAS_RECT_FLIP_H; + } + if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V) { + + flags|=Rasterizer::CANVAS_RECT_FLIP_V; + } +#else + + int flags=rect->flags; +#endif + if (use_normalmap) + _canvas_normal_set_flip(Vector2((flags&CANVAS_RECT_FLIP_H)?-1:1,(flags&CANVAS_RECT_FLIP_V)?-1:1)); + canvas_draw_rect(rect->rect,flags,rect->source,rect->texture,rect->modulate); + + } break; + case CanvasItem::Command::TYPE_STYLE: { + + CanvasItem::CommandStyle* style = static_cast(c); + if (use_normalmap) + _canvas_normal_set_flip(Vector2(1,1)); + canvas_draw_style_box(style->rect,style->texture,style->margin,style->draw_center,style->color); + + } break; + case CanvasItem::Command::TYPE_PRIMITIVE: { + + if (use_normalmap) + _canvas_normal_set_flip(Vector2(1,1)); + CanvasItem::CommandPrimitive* primitive = static_cast(c); + canvas_draw_primitive(primitive->points,primitive->colors,primitive->uvs,primitive->texture,primitive->width); + } break; + case CanvasItem::Command::TYPE_POLYGON: { + + if (use_normalmap) + _canvas_normal_set_flip(Vector2(1,1)); + CanvasItem::CommandPolygon* polygon = static_cast(c); + canvas_draw_polygon(polygon->count,polygon->indices.ptr(),polygon->points.ptr(),polygon->uvs.ptr(),polygon->colors.ptr(),polygon->texture,polygon->colors.size()==1); + + } break; + + case CanvasItem::Command::TYPE_POLYGON_PTR: { + + if (use_normalmap) + _canvas_normal_set_flip(Vector2(1,1)); + CanvasItem::CommandPolygonPtr* polygon = static_cast(c); + canvas_draw_polygon(polygon->count,polygon->indices,polygon->points,polygon->uvs,polygon->colors,polygon->texture,false); + } break; + case CanvasItem::Command::TYPE_CIRCLE: { + + CanvasItem::CommandCircle* circle = static_cast(c); + static const int numpoints=32; + Vector2 points[numpoints+1]; + points[numpoints]=circle->pos; + int indices[numpoints*3]; + + for(int i=0;ipos+Vector2( Math::sin(i*Math_PI*2.0/numpoints),Math::cos(i*Math_PI*2.0/numpoints) )*circle->radius; + indices[i*3+0]=i; + indices[i*3+1]=(i+1)%numpoints; + indices[i*3+2]=numpoints; + } + canvas_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true); + //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1); + } break; + case CanvasItem::Command::TYPE_TRANSFORM: { + + CanvasItem::CommandTransform* transform = static_cast(c); + canvas_set_transform(transform->xform); + } break; + case CanvasItem::Command::TYPE_BLEND_MODE: { + + CanvasItem::CommandBlendMode* bm = static_cast(c); + canvas_set_blend_mode(bm->blend_mode); + + } break; + case CanvasItem::Command::TYPE_CLIP_IGNORE: { + + CanvasItem::CommandClipIgnore* ci = static_cast(c); + if (current_clip) { + + if (ci->ignore!=reclip) { + if (ci->ignore) { + + glDisable(GL_SCISSOR_TEST); + reclip=true; + } else { + + glEnable(GL_SCISSOR_TEST); + glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)), + current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height); + reclip=false; + } + } + } + + + + } break; + } + } + +} + +void RasterizerGLES2::_canvas_item_setup_shader_params(CanvasItem *shader_owner,Shader* shader) { + + if (canvas_shader.bind()) + rebind_texpixel_size=true; + + if (shader_owner->shader_version!=shader->version) { + //todo optimize uniforms + shader_owner->shader_version=shader->version; + } + + if (shader->has_texscreen && framebuffer.active) { + + int x = viewport.x; + int y = window_size.height-(viewport.height+viewport.y); + + canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_SCREEN_MULT,Vector2(float(viewport.width)/framebuffer.width,float(viewport.height)/framebuffer.height)); + canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_SCREEN_CLAMP,Color(float(x)/framebuffer.width,float(y)/framebuffer.height,float(x+viewport.width)/framebuffer.width,float(y+viewport.height)/framebuffer.height)); + canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_TEX,max_texture_units-1); + glActiveTexture(GL_TEXTURE0+max_texture_units-1); + glBindTexture(GL_TEXTURE_2D,framebuffer.sample_color); + if (framebuffer.scale==1 && !canvas_texscreen_used) { +#ifdef GLEW_ENABLED + glReadBuffer(GL_COLOR_ATTACHMENT0); +#endif + glCopyTexSubImage2D(GL_TEXTURE_2D,0,x,y,x,y,viewport.width,viewport.height); +// if (current_clip) { +// // print_line(" a clip "); +// } + + canvas_texscreen_used=true; + } + + glActiveTexture(GL_TEXTURE0); + + } + + if (shader->has_screen_uv) { + canvas_shader.set_uniform(CanvasShaderGLES2::SCREEN_UV_MULT,Vector2(1.0/viewport.width,1.0/viewport.height)); + } + + if (shader->uses_time) { + canvas_shader.set_uniform(CanvasShaderGLES2::TIME,Math::fmod(last_time,300.0)); + draw_next_frame=true; + } + //if uses TIME - draw_next_frame=true + + uses_texpixel_size=shader->uses_texpixel_size; + +} + +void RasterizerGLES2::_canvas_item_setup_shader_uniforms(CanvasItem *shader_owner,Shader* shader) { + + //this can be optimized.. + int tex_id=1; + int idx=0; + for(Map::Element *E=shader->uniforms.front();E;E=E->next()) { + + Map::Element *F=shader_owner->shader_param.find(E->key()); + + if ((E->get().type==ShaderLanguage::TYPE_TEXTURE || E->get().type==ShaderLanguage::TYPE_CUBEMAP)) { + + RID rid; + if (F) { + rid=F->get(); + } + + if (!rid.is_valid()) { + + Map::Element *DT=shader->default_textures.find(E->key()); + if (DT) { + rid=DT->get(); + } + } + + if (rid.is_valid()) { + + int loc = canvas_shader.get_custom_uniform_location(idx); //should be automatic.. + + glActiveTexture(GL_TEXTURE0+tex_id); + Texture *t=texture_owner.get(rid); + if (!t) + glBindTexture(GL_TEXTURE_2D,white_tex); + else + glBindTexture(t->target,t->tex_id); + + glUniform1i(loc,tex_id); + tex_id++; + } + } else { + Variant &v=F?F->get():E->get().default_value; + canvas_shader.set_custom_uniform(idx,v); + } + + idx++; + } + + if (tex_id>1) { + glActiveTexture(GL_TEXTURE0); + } + +} + +void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list,int p_z,const Color& p_modulate,CanvasLight *p_light) { CanvasItem *current_clip=NULL; Shader *shader_cache=NULL; + + bool rebind_shader=true; + canvas_opacity=1.0; + canvas_use_modulate=p_modulate!=Color(1,1,1,1); + canvas_modulate=p_modulate; + canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,canvas_use_modulate); + while(p_item_list) { CanvasItem *ci=p_item_list; @@ -8343,6 +8596,12 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { memdelete(ci->vp_render); ci->vp_render=NULL; canvas_last_shader=RID(); + canvas_use_modulate=p_modulate!=Color(1,1,1,1); + canvas_modulate=p_modulate; + canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,canvas_use_modulate); + rebind_shader=true; + + } if (current_clip!=ci->final_clip_owner) { @@ -8365,7 +8624,7 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { //begin rect CanvasItem *shader_owner = ci->shader_owner?ci->shader_owner:ci; - if (shader_owner->shader!=canvas_last_shader) { + if (shader_owner->shader!=canvas_last_shader || rebind_shader) { Shader *shader = NULL; if (shader_owner->shader.is_valid()) { @@ -8379,52 +8638,7 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { if (shader) { canvas_shader.set_custom_shader(shader->custom_code_id); - if (canvas_shader.bind()) - rebind_texpixel_size=true; - - if (shader_owner->shader_version!=shader->version) { - //todo optimize uniforms - shader_owner->shader_version=shader->version; - } - - if (shader->has_texscreen && framebuffer.active) { - - int x = viewport.x; - int y = window_size.height-(viewport.height+viewport.y); - - canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_SCREEN_MULT,Vector2(float(viewport.width)/framebuffer.width,float(viewport.height)/framebuffer.height)); - canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_SCREEN_CLAMP,Color(float(x)/framebuffer.width,float(y)/framebuffer.height,float(x+viewport.width)/framebuffer.width,float(y+viewport.height)/framebuffer.height)); - canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_TEX,max_texture_units-1); - glActiveTexture(GL_TEXTURE0+max_texture_units-1); - glBindTexture(GL_TEXTURE_2D,framebuffer.sample_color); - if (framebuffer.scale==1 && !canvas_texscreen_used) { -#ifdef GLEW_ENABLED - glReadBuffer(GL_COLOR_ATTACHMENT0); -#endif - glCopyTexSubImage2D(GL_TEXTURE_2D,0,x,y,x,y,viewport.width,viewport.height); - if (current_clip) { - // print_line(" a clip "); - } - - canvas_texscreen_used=true; - } - - glActiveTexture(GL_TEXTURE0); - - } - - if (shader->has_screen_uv) { - canvas_shader.set_uniform(CanvasShaderGLES2::SCREEN_UV_MULT,Vector2(1.0/viewport.width,1.0/viewport.height)); - } - - if (shader->uses_time) { - canvas_shader.set_uniform(CanvasShaderGLES2::TIME,Math::fmod(last_time,300.0)); - draw_next_frame=true; - } - //if uses TIME - draw_next_frame=true - - uses_texpixel_size=shader->uses_texpixel_size; - + _canvas_item_setup_shader_params(shader_owner,shader); } else { shader_cache=NULL; canvas_shader.set_custom_shader(0); @@ -8435,60 +8649,15 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX,canvas_transform); + if (canvas_use_modulate) + canvas_shader.set_uniform(CanvasShaderGLES2::MODULATE,canvas_modulate); canvas_last_shader=shader_owner->shader; + rebind_shader=false; } if (shader_cache) { - Shader *shader = shader_cache; - //this can be optimized.. - int tex_id=1; - int idx=0; - for(Map::Element *E=shader->uniforms.front();E;E=E->next()) { - - Map::Element *F=shader_owner->shader_param.find(E->key()); - - if ((E->get().type==ShaderLanguage::TYPE_TEXTURE || E->get().type==ShaderLanguage::TYPE_CUBEMAP)) { - - RID rid; - if (F) { - rid=F->get(); - } - - if (!rid.is_valid()) { - - Map::Element *DT=shader->default_textures.find(E->key()); - if (DT) { - rid=DT->get(); - } - } - - if (rid.is_valid()) { - - int loc = canvas_shader.get_custom_uniform_location(idx); //should be automatic.. - - glActiveTexture(GL_TEXTURE0+tex_id); - Texture *t=texture_owner.get(rid); - if (!t) - glBindTexture(GL_TEXTURE_2D,white_tex); - else - glBindTexture(t->target,t->tex_id); - - glUniform1i(loc,tex_id); - tex_id++; - } - } else { - Variant &v=F?F->get():E->get().default_value; - canvas_shader.set_custom_uniform(idx,v); - } - - idx++; - } - - if (tex_id>1) { - glActiveTexture(GL_TEXTURE0); - } - + _canvas_item_setup_shader_uniforms(shader_owner,shader_cache); } canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,ci->final_transform); @@ -8514,7 +8683,7 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { } break; case VS::MATERIAL_BLEND_MODE_SUB: { - glBlendEquation(GL_FUNC_SUBTRACT); + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); glBlendFunc(GL_SRC_ALPHA,GL_ONE); } break; case VS::MATERIAL_BLEND_MODE_MUL: { @@ -8531,127 +8700,113 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) { canvas_blend_mode=ci->blend_mode; } - int cc=ci->commands.size(); - CanvasItem::Command **commands = ci->commands.ptr(); - canvas_opacity = ci->final_opacity; - for(int i=0;i(ci,current_clip,reclip); - CanvasItem::Command *c=commands[i]; + if (canvas_blend_mode==VS::MATERIAL_BLEND_MODE_MIX && p_light) { - switch(c->type) { - case CanvasItem::Command::TYPE_LINE: { + CanvasLight *light = p_light; + bool light_used=false; + bool subtract=false; - CanvasItem::CommandLine* line = static_cast(c); - canvas_draw_line(line->from,line->to,line->color,line->width); - } break; - case CanvasItem::Command::TYPE_RECT: { - CanvasItem::CommandRect* rect = static_cast(c); -// canvas_draw_rect(rect->rect,rect->region,rect->source,rect->flags&CanvasItem::CommandRect::FLAG_TILE,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V,rect->texture,rect->modulate); -#if 0 - int flags=0; + while(light) { - if (rect->flags&CanvasItem::CommandRect::FLAG_REGION) { - flags|=Rasterizer::CANVAS_RECT_REGION; - } - if (rect->flags&CanvasItem::CommandRect::FLAG_TILE) { - flags|=Rasterizer::CANVAS_RECT_TILE; - } - if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H) { + if (ci->light_mask&light->item_mask && p_z>=light->z_min && p_z<=light->z_max && ci->global_rect_cache.intersects_transformed(light->xform_cache,light->rect_cache)) { - flags|=Rasterizer::CANVAS_RECT_FLIP_H; - } - if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V) { + //intersects this light - flags|=Rasterizer::CANVAS_RECT_FLIP_V; - } -#else + if (!light_used || subtract!=light->subtract) { - int flags=rect->flags; -#endif - canvas_draw_rect(rect->rect,flags,rect->source,rect->texture,rect->modulate); + subtract=light->subtract; - } break; - case CanvasItem::Command::TYPE_STYLE: { + if (subtract) { - CanvasItem::CommandStyle* style = static_cast(c); - canvas_draw_style_box(style->rect,style->texture,style->margin,style->draw_center,style->color); + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); + glBlendFunc(GL_SRC_ALPHA,GL_ONE); - } break; - case CanvasItem::Command::TYPE_PRIMITIVE: { + } else { - CanvasItem::CommandPrimitive* primitive = static_cast(c); - canvas_draw_primitive(primitive->points,primitive->colors,primitive->uvs,primitive->texture,primitive->width); - } break; - case CanvasItem::Command::TYPE_POLYGON: { + glBlendEquation(GL_FUNC_ADD); + glBlendFunc(GL_SRC_ALPHA,GL_ONE); - CanvasItem::CommandPolygon* polygon = static_cast(c); - canvas_draw_polygon(polygon->count,polygon->indices.ptr(),polygon->points.ptr(),polygon->uvs.ptr(),polygon->colors.ptr(),polygon->texture,polygon->colors.size()==1); - - } break; - - case CanvasItem::Command::TYPE_POLYGON_PTR: { - - CanvasItem::CommandPolygonPtr* polygon = static_cast(c); - canvas_draw_polygon(polygon->count,polygon->indices,polygon->points,polygon->uvs,polygon->colors,polygon->texture,false); - } break; - case CanvasItem::Command::TYPE_CIRCLE: { - - CanvasItem::CommandCircle* circle = static_cast(c); - static const int numpoints=32; - Vector2 points[numpoints+1]; - points[numpoints]=circle->pos; - int indices[numpoints*3]; - - for(int i=0;ipos+Vector2( Math::sin(i*Math_PI*2.0/numpoints),Math::cos(i*Math_PI*2.0/numpoints) )*circle->radius; - indices[i*3+0]=i; - indices[i*3+1]=(i+1)%numpoints; - indices[i*3+2]=numpoints; - } - canvas_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true); - //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1); - } break; - case CanvasItem::Command::TYPE_TRANSFORM: { - - CanvasItem::CommandTransform* transform = static_cast(c); - canvas_set_transform(transform->xform); - } break; - case CanvasItem::Command::TYPE_BLEND_MODE: { - - CanvasItem::CommandBlendMode* bm = static_cast(c); - canvas_set_blend_mode(bm->blend_mode); - - } break; - case CanvasItem::Command::TYPE_CLIP_IGNORE: { - - CanvasItem::CommandClipIgnore* ci = static_cast(c); - if (current_clip) { - - if (ci->ignore!=reclip) { - if (ci->ignore) { - - glDisable(GL_SCISSOR_TEST); - reclip=true; - } else { - - glEnable(GL_SCISSOR_TEST); - glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)), - current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height); - reclip=false; - } } } + if (!light_used) { + + canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING,true); + canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,false); + light_used=true; + normal_flip=Vector2(1,1); + + } + bool light_rebind = canvas_shader.bind(); + + if (light_rebind) { + + if (shader_owner && shader_cache) { + _canvas_item_setup_shader_params(shader_owner,shader_cache); + _canvas_item_setup_shader_uniforms(shader_owner,shader_cache); + } + + canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,ci->final_transform); + canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Matrix32()); + canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX,canvas_transform); + if (canvas_use_modulate) + canvas_shader.set_uniform(CanvasShaderGLES2::MODULATE,canvas_modulate); + canvas_shader.set_uniform(CanvasShaderGLES2::NORMAL_FLIP,Vector2(1,1)); - } break; + } + + canvas_shader.set_uniform(CanvasShaderGLES2::LIGHT_MATRIX,light->light_shader_xform); + canvas_shader.set_uniform(CanvasShaderGLES2::LIGHT_POS,light->light_shader_pos); + canvas_shader.set_uniform(CanvasShaderGLES2::LIGHT_COLOR,light->color); + canvas_shader.set_uniform(CanvasShaderGLES2::LIGHT_HEIGHT,light->height); + glActiveTexture(GL_TEXTURE0+max_texture_units-2); + canvas_shader.set_uniform(CanvasShaderGLES2::LIGHT_TEXTURE,max_texture_units-2); + Texture *t = texture_owner.get(light->texture); + if (!t) { + glBindTexture(GL_TEXTURE_2D,white_tex); + } else { + + glBindTexture(t->target,t->tex_id); + } + + glActiveTexture(GL_TEXTURE0); + _canvas_item_render_commands(ci,current_clip,reclip); //redraw using light + + } + + light=light->next_ptr; } - } + if (light_used) { + + + canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING,false); + canvas_shader.set_conditional(CanvasShaderGLES2::USE_MODULATE,canvas_use_modulate); + + canvas_shader.bind(); + + if (shader_owner && shader_cache) { + _canvas_item_setup_shader_params(shader_owner,shader_cache); + _canvas_item_setup_shader_uniforms(shader_owner,shader_cache); + } + + canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,ci->final_transform); + canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Matrix32()); + if (canvas_use_modulate) + canvas_shader.set_uniform(CanvasShaderGLES2::MODULATE,canvas_modulate); + + glBlendEquation(GL_FUNC_ADD); + glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); + } + + + } if (reclip) { diff --git a/drivers/gles2/rasterizer_gles2.h b/drivers/gles2/rasterizer_gles2.h index 508adf2859b..f1ba04adc54 100644 --- a/drivers/gles2/rasterizer_gles2.h +++ b/drivers/gles2/rasterizer_gles2.h @@ -1127,6 +1127,7 @@ class RasterizerGLES2 : public Rasterizer { bool active; int blur_size; + struct Blur { GLuint fbo; @@ -1186,11 +1187,15 @@ class RasterizerGLES2 : public Rasterizer { GLuint white_tex; RID canvas_tex; float canvas_opacity; + Color canvas_modulate; + bool canvas_use_modulate; bool uses_texpixel_size; bool rebind_texpixel_size; Transform canvas_transform; RID canvas_last_shader; bool canvas_texscreen_used; + Vector2 normal_flip; + _FORCE_INLINE_ void _canvas_normal_set_flip(const Vector2& p_flip); _FORCE_INLINE_ Texture* _bind_canvas_texture(const RID& p_texture); @@ -1247,6 +1252,10 @@ class RasterizerGLES2 : public Rasterizer { GLuint tc0_id_cache; GLuint tc0_idx; + template + _FORCE_INLINE_ void _canvas_item_render_commands(CanvasItem *p_item,CanvasItem *current_clip,bool &reclip); + _FORCE_INLINE_ void _canvas_item_setup_shader_params(CanvasItem *shader_owner,Shader* p_shader); + _FORCE_INLINE_ void _canvas_item_setup_shader_uniforms(CanvasItem *shader_owner,Shader* p_shader); public: /* TEXTURE API */ @@ -1562,7 +1571,8 @@ public: virtual void canvas_draw_polygon(int p_vertex_count, const int* p_indices, const Vector2* p_vertices, const Vector2* p_uvs, const Color* p_colors,const RID& p_texture,bool p_singlecolor); virtual void canvas_set_transform(const Matrix32& p_transform); - virtual void canvas_render_items(CanvasItem *p_item_list); + virtual void canvas_render_items(CanvasItem *p_item_list,int p_z,const Color& p_modulate,CanvasLight *p_light); + /* ENVIRONMENT */ diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp index d8841d407e8..b2052c7cbb5 100644 --- a/drivers/gles2/shader_compiler_gles2.cpp +++ b/drivers/gles2/shader_compiler_gles2.cpp @@ -261,6 +261,11 @@ String ShaderCompilerGLES2::dump_node_code(SL::Node *p_node,int p_level,bool p_a uses_light=true; } + if (vnode->name==vname_normal) { + uses_normal=true; + } + + } if (vnode->name==vname_time) { @@ -636,7 +641,7 @@ Error ShaderCompilerGLES2::compile(const String& p_code, ShaderLanguage::ShaderT r_flags.uses_light=uses_light; r_flags.uses_time=uses_time; r_flags.uses_normalmap=uses_normalmap; - r_flags.uses_normal=uses_normalmap; + r_flags.uses_normal=uses_normal; r_flags.uses_texpixel_size=uses_texpixel_size; r_flags.uses_worldvec=uses_worldvec; r_code_line=code; diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl index dc0af017d0b..227a8b7bdd4 100644 --- a/drivers/gles2/shaders/canvas.glsl +++ b/drivers/gles2/shaders/canvas.glsl @@ -26,7 +26,13 @@ uniform float time; #ifdef USE_LIGHTING uniform highp mat4 light_matrix; -varying vec4 light_tex_pos; +uniform vec2 light_pos; +varying vec4 light_uv_interp; + +#if defined(NORMAL_USED) +varying vec4 local_rot; +uniform vec2 normal_flip; +#endif #endif @@ -67,8 +73,13 @@ VERTEX_SHADER_CODE #ifdef USE_LIGHTING - light_tex_pos.xy = light_matrix * gl_Position; - light_tex_pos.zw=outvec.xy - light_matrix[4].xy; //likely wrong + light_uv_interp.xy = (light_matrix * outvec).xy; + light_uv_interp.zw = outvec.xy-light_pos; + +#if defined(NORMAL_USED) + local_rot.xy=normalize( (modelview_matrix * ( extra_matrix * vec4(1.0,0.0,0.0,0.0) )).xy )*normal_flip.x; + local_rot.zw=normalize( (modelview_matrix * ( extra_matrix * vec4(0.0,1.0,0.0,0.0) )).xy )*normal_flip.y; +#endif #endif @@ -121,11 +132,22 @@ varying vec4 var2_interp; uniform float time; #endif +#ifdef USE_MODULATE + +uniform vec4 modulate; + +#endif #ifdef USE_LIGHTING uniform sampler2D light_texture; -varying vec4 light_tex_pos; +uniform vec4 light_color; +uniform float light_height; +varying vec4 light_uv_interp; + +#if defined(NORMAL_USED) +varying vec4 local_rot; +#endif #ifdef USE_SHADOWS @@ -151,6 +173,11 @@ void main() { vec3 normal = vec3(0,0,1); #endif +#ifdef USE_MODULATE + + color*=modulate; +#endif + color *= texture2D( texture, uv_interp ); #if defined(ENABLE_SCREEN_UV) vec2 screen_uv = gl_FragCoord.xy*screen_uv_mult; @@ -166,9 +193,13 @@ FRAGMENT_SHADER_CODE #ifdef USE_LIGHTING +#if defined(NORMAL_USED) + normal.xy = mat2(local_rot.xy,local_rot.zw) * normal.xy; +#endif + float att=1.0; - vec3 light = texture2D(light_texture,light_tex_pos).rgb; + vec4 light = texture2D(light_texture,light_uv_interp.xy) * light_color; #ifdef USE_SHADOWS //this might not be that great on mobile? float light_dist = length(light_texture.zw); @@ -183,18 +214,29 @@ FRAGMENT_SHADER_CODE #if defined(USE_LIGHT_SHADER_CODE) //light is written by the light shader { - vec2 light_dir = normalize(light_tex_pos.zw); - float light_distance = length(light_tex_pos.zw); + vec2 light_dir = normalize(light_uv_interp.zw); + float light_distance = length(light_uv_interp.zw); LIGHT_SHADER_CODE } + #else #if defined(NORMAL_USED) - vec2 light_normal = normalize(light_tex_pos.zw); - light = color.rgb * light * max(dot(light_normal,normal),0); + vec3 light_normal = normalize(vec3(light_uv_interp.zw,-light_height)); + light*=max(dot(-light_normal,normal),0); #endif - color.rgb=light; + color*=light; +/* +#ifdef USE_NORMAL + color.xy=local_rot.xy;//normal.xy; + color.zw=vec2(0.0,1.0); +#endif +*/ + if (any(lessThan(light_uv_interp.xy,vec2(0.0,0.0))) || any(greaterThanEqual(light_uv_interp.xy,vec2(1.0,1.0)))) { + color.a=0.0; //invisible + } + //light shader code #endif diff --git a/platform/winrt/os_winrt.cpp b/platform/winrt/os_winrt.cpp index 89fa93c5c4f..21a77b89cb7 100644 --- a/platform/winrt/os_winrt.cpp +++ b/platform/winrt/os_winrt.cpp @@ -27,7 +27,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "drivers/gles2/rasterizer_gles2.h" -#include "drivers/gles1/rasterizer_gles1.h" #include "os_winrt.h" #include "drivers/nedmalloc/memory_pool_static_nedmalloc.h" #include "drivers/unix/memory_pool_static_malloc.h" @@ -62,11 +61,11 @@ using namespace Microsoft::WRL; int OSWinrt::get_video_driver_count() const { - return 2; + return 1; } const char * OSWinrt::get_video_driver_name(int p_driver) const { - return p_driver==0?"GLES2":"GLES1"; + return "GLES2"; } OS::VideoMode OSWinrt::get_default_video_mode() const { diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp index 44a7e257259..4021ba59108 100644 --- a/scene/2d/canvas_item.cpp +++ b/scene/2d/canvas_item.cpp @@ -458,6 +458,16 @@ CanvasItem::BlendMode CanvasItem::get_blend_mode() const { return blend_mode; } +void CanvasItem::set_light_mask(int p_light_mask) { + + light_mask=p_light_mask; + VS::get_singleton()->canvas_item_set_light_mask(canvas_item,p_light_mask); +} + +int CanvasItem::get_light_mask() const{ + + return light_mask; +} void CanvasItem::item_rect_changed() { @@ -857,6 +867,9 @@ void CanvasItem::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_blend_mode","blend_mode"),&CanvasItem::set_blend_mode); ObjectTypeDB::bind_method(_MD("get_blend_mode"),&CanvasItem::get_blend_mode); + ObjectTypeDB::bind_method(_MD("set_light_mask","light_mask"),&CanvasItem::set_light_mask); + ObjectTypeDB::bind_method(_MD("get_light_mask"),&CanvasItem::get_light_mask); + ObjectTypeDB::bind_method(_MD("set_opacity","opacity"),&CanvasItem::set_opacity); ObjectTypeDB::bind_method(_MD("get_opacity"),&CanvasItem::get_opacity); ObjectTypeDB::bind_method(_MD("set_self_opacity","self_opacity"),&CanvasItem::set_self_opacity); @@ -912,6 +925,7 @@ void CanvasItem::_bind_methods() { ADD_PROPERTY( PropertyInfo(Variant::BOOL,"visibility/on_top",PROPERTY_HINT_NONE,"",0), _SCS("_set_on_top"),_SCS("_is_on_top") ); //compatibility ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"visibility/blend_mode",PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul,PMAlpha"), _SCS("set_blend_mode"),_SCS("get_blend_mode") ); + ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"visibility/light_mask",PROPERTY_HINT_ALL_FLAGS), _SCS("set_light_mask"),_SCS("get_light_mask") ); ADD_PROPERTYNZ( PropertyInfo(Variant::OBJECT,"shader/shader",PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemShader,CanvasItemShaderGraph"), _SCS("set_shader"),_SCS("get_shader") ); ADD_PROPERTYNZ( PropertyInfo(Variant::BOOL,"shader/use_parent"), _SCS("set_use_parent_shader"),_SCS("get_use_parent_shader") ); //exporting these two things doesn't really make much sense i think @@ -992,6 +1006,7 @@ CanvasItem::CanvasItem() : xform_change(this) { canvas_layer=NULL; use_parent_shader=false; global_invalid=true; + light_mask=1; C=NULL; diff --git a/scene/2d/canvas_item.h b/scene/2d/canvas_item.h index e7260a65307..cccb63fe4c3 100644 --- a/scene/2d/canvas_item.h +++ b/scene/2d/canvas_item.h @@ -71,6 +71,7 @@ private: List::Element *C; BlendMode blend_mode; + int light_mask; bool first_draw; bool hidden; @@ -80,8 +81,8 @@ private: bool drawing; bool block_transform_notify; bool behind; - bool use_parent_shader; + Ref shader; mutable Matrix32 global_transform; @@ -158,6 +159,9 @@ public: void set_blend_mode(BlendMode p_blend_mode); BlendMode get_blend_mode() const; + void set_light_mask(int p_light_mask); + int get_light_mask() const; + void set_opacity(float p_opacity); float get_opacity() const; diff --git a/scene/2d/canvas_modulate.cpp b/scene/2d/canvas_modulate.cpp new file mode 100644 index 00000000000..82dd8012a5d --- /dev/null +++ b/scene/2d/canvas_modulate.cpp @@ -0,0 +1,46 @@ +#include "canvas_modulate.h" + + +void CanvasModulate::_notification(int p_what) { + + if (p_what==NOTIFICATION_ENTER_CANVAS) { + + VS::get_singleton()->canvas_set_modulate(get_canvas(),color); + } else if (p_what==NOTIFICATION_EXIT_CANVAS) { + + VS::get_singleton()->canvas_set_modulate(get_canvas(),Color(1,1,1,1)); + } +} + +void CanvasModulate::_bind_methods(){ + + ObjectTypeDB::bind_method(_MD("set_color","color"),&CanvasModulate::set_color); + ObjectTypeDB::bind_method(_MD("get_color"),&CanvasModulate::get_color); + + ADD_PROPERTY(PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color")); +} + + +void CanvasModulate::set_color(const Color& p_color){ + + color=p_color; + if (is_inside_tree()) { + VS::get_singleton()->canvas_set_modulate(get_canvas(),color); + } +} +Color CanvasModulate::get_color() const { + + return color; +} + + +CanvasModulate::CanvasModulate() +{ + color=Color(1,1,1,1); +} + +CanvasModulate::~CanvasModulate() +{ + +} + diff --git a/scene/2d/canvas_modulate.h b/scene/2d/canvas_modulate.h new file mode 100644 index 00000000000..a6894f29c2d --- /dev/null +++ b/scene/2d/canvas_modulate.h @@ -0,0 +1,23 @@ +#ifndef CANVASMODULATE_H +#define CANVASMODULATE_H + +#include "scene/2d/node_2d.h" + +class CanvasModulate : public Node2D { + + OBJ_TYPE(CanvasModulate,Node2D); + + Color color; +protected: + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_color(const Color& p_color); + Color get_color() const; + + CanvasModulate(); + ~CanvasModulate(); +}; + +#endif // CANVASMODULATE_H diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp index 073e3a16457..cea8c06d3fc 100644 --- a/scene/2d/light_2d.cpp +++ b/scene/2d/light_2d.cpp @@ -1,6 +1,39 @@ #include "light_2d.h" #include "servers/visual_server.h" +void Light2D::edit_set_pivot(const Point2& p_pivot) { + + set_texture_offset(p_pivot); + +} + +Point2 Light2D::edit_get_pivot() const { + + return get_texture_offset(); +} +bool Light2D::edit_has_pivot() const { + + return true; +} + +Rect2 Light2D::get_item_rect() const { + + if (texture.is_null()) + return Rect2(0,0,1,1); + + Size2i s; + + s = texture->get_size(); + Point2i ofs=texture_offset; + ofs-=s/2; + + if (s==Size2(0,0)) + s=Size2(1,1); + + return Rect2(ofs,s); +} + + void Light2D::set_enabled( bool p_enabled) { VS::get_singleton()->canvas_light_set_enabled(canvas_light,p_enabled); @@ -81,6 +114,28 @@ int Light2D::get_z_range_max() const { return z_max; } +void Light2D::set_layer_range_min( int p_min_layer) { + + layer_min=p_min_layer; + VS::get_singleton()->canvas_light_set_layer_range(canvas_light,layer_min,layer_max); + +} +int Light2D::get_layer_range_min() const { + + return layer_min; +} + +void Light2D::set_layer_range_max( int p_max_layer) { + + layer_max=p_max_layer; + VS::get_singleton()->canvas_light_set_layer_range(canvas_light,layer_min,layer_max); + +} +int Light2D::get_layer_range_max() const { + + return layer_max; +} + void Light2D::set_item_mask( int p_mask) { item_mask=p_mask; @@ -93,15 +148,15 @@ int Light2D::get_item_mask() const { return item_mask; } -void Light2D::set_blend_mode( LightBlendMode p_blend_mode ) { +void Light2D::set_subtract_mode( bool p_enable ) { - blend_mode=p_blend_mode; - VS::get_singleton()->canvas_light_set_blend_mode(canvas_light,VS::CanvasLightBlendMode(blend_mode)); + subtract_mode=p_enable; + VS::get_singleton()->canvas_light_set_subtract_mode(canvas_light,p_enable); } -Light2D::LightBlendMode Light2D::get_blend_mode() const { +bool Light2D::get_subtract_mode() const { - return blend_mode; + return subtract_mode; } void Light2D::set_shadow_enabled( bool p_enabled) { @@ -115,6 +170,25 @@ bool Light2D::is_shadow_enabled() const { return shadow; } +void Light2D::_notification(int p_what) { + + if (p_what==NOTIFICATION_ENTER_TREE) { + + VS::get_singleton()->canvas_light_attach_to_canvas( canvas_light, get_canvas() ); + } + + if (p_what==NOTIFICATION_TRANSFORM_CHANGED) { + + VS::get_singleton()->canvas_light_set_transform( canvas_light, get_global_transform()); + } + + if (p_what==NOTIFICATION_EXIT_TREE) { + + VS::get_singleton()->canvas_light_attach_to_canvas( canvas_light, RID() ); + } + +} + void Light2D::_bind_methods() { @@ -139,11 +213,18 @@ void Light2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_z_range_max","z"),&Light2D::set_z_range_max); ObjectTypeDB::bind_method(_MD("get_z_range_max"),&Light2D::get_z_range_max); + ObjectTypeDB::bind_method(_MD("set_layer_range_min","layer"),&Light2D::set_layer_range_min); + ObjectTypeDB::bind_method(_MD("get_layer_range_min"),&Light2D::get_layer_range_min); + + ObjectTypeDB::bind_method(_MD("set_layer_range_max","layer"),&Light2D::set_layer_range_max); + ObjectTypeDB::bind_method(_MD("get_layer_range_max"),&Light2D::get_layer_range_max); + + ObjectTypeDB::bind_method(_MD("set_item_mask","item_mask"),&Light2D::set_item_mask); ObjectTypeDB::bind_method(_MD("get_item_mask"),&Light2D::get_item_mask); - ObjectTypeDB::bind_method(_MD("set_blend_mode","blend_mode"),&Light2D::set_blend_mode); - ObjectTypeDB::bind_method(_MD("get_blend_mode"),&Light2D::get_blend_mode); + ObjectTypeDB::bind_method(_MD("set_subtract_mode","enable"),&Light2D::set_subtract_mode); + ObjectTypeDB::bind_method(_MD("get_subtract_mode"),&Light2D::get_subtract_mode); ObjectTypeDB::bind_method(_MD("set_shadow_enabled","enabled"),&Light2D::set_shadow_enabled); ObjectTypeDB::bind_method(_MD("is_shadow_enabled"),&Light2D::is_shadow_enabled); @@ -155,8 +236,10 @@ void Light2D::_bind_methods() { ADD_PROPERTY( PropertyInfo(Variant::REAL,"height"),_SCS("set_height"),_SCS("get_height")); ADD_PROPERTY( PropertyInfo(Variant::INT,"z_range_min",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_min"),_SCS("get_z_range_min")); ADD_PROPERTY( PropertyInfo(Variant::INT,"z_range_max",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_max"),_SCS("get_z_range_max")); + ADD_PROPERTY( PropertyInfo(Variant::INT,"layer_range_min",PROPERTY_HINT_RANGE,"-512,512,1"),_SCS("set_layer_range_min"),_SCS("get_layer_range_min")); + ADD_PROPERTY( PropertyInfo(Variant::INT,"layer_range_max",PROPERTY_HINT_RANGE,"-512,512,1"),_SCS("set_layer_range_max"),_SCS("get_layer_range_max")); ADD_PROPERTY( PropertyInfo(Variant::INT,"item_mask",PROPERTY_HINT_ALL_FLAGS),_SCS("set_item_mask"),_SCS("get_item_mask")); - ADD_PROPERTY( PropertyInfo(Variant::INT,"blend_mode",PROPERTY_HINT_ENUM,"Add,Sub,Mul,Dodge,Burn,Lighten,Darken,Overlay,Screen"),_SCS("set_blend_mode"),_SCS("get_blend_mode")); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"subtract"),_SCS("set_subtract_mode"),_SCS("get_subtract_mode")); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"shadow_enabled"),_SCS("set_shadow_enabled"),_SCS("is_shadow_enabled")); @@ -171,8 +254,10 @@ Light2D::Light2D() { height=0; z_min=-1024; z_max=1024; + layer_min=0; + layer_max=0; item_mask=1; - blend_mode=LIGHT_BLEND_ADD; + subtract_mode=false; } diff --git a/scene/2d/light_2d.h b/scene/2d/light_2d.h index ac8d587ea7c..dbfd233d391 100644 --- a/scene/2d/light_2d.h +++ b/scene/2d/light_2d.h @@ -6,20 +6,6 @@ class Light2D : public Node2D { OBJ_TYPE(Light2D,Node2D); -public: - - enum LightBlendMode { - LIGHT_BLEND_ADD, - LIGHT_BLEND_SUB, - LIGHT_BLEND_MULTIPLY, - LIGHT_BLEND_DODGE, - LIGHT_BLEND_BURN, - LIGHT_BLEND_LIGHTEN, - LIGHT_BLEND_DARKEN, - LIGHT_BLEND_OVERLAY, - LIGHT_BLEND_SCREEN, - }; - private: RID canvas_light; bool enabled; @@ -28,17 +14,24 @@ private: float height; int z_min; int z_max; + int layer_min; + int layer_max; int item_mask; - LightBlendMode blend_mode; + bool subtract_mode; Ref texture; Vector2 texture_offset; protected: + void _notification(int p_what); static void _bind_methods(); public: + virtual void edit_set_pivot(const Point2& p_pivot); + virtual Point2 edit_get_pivot() const; + virtual bool edit_has_pivot() const; + void set_enabled( bool p_enabled); bool is_enabled() const; @@ -60,21 +53,26 @@ public: void set_z_range_max( int p_max_z); int get_z_range_max() const; + void set_layer_range_min( int p_min_layer); + int get_layer_range_min() const; + + void set_layer_range_max( int p_max_layer); + int get_layer_range_max() const; + void set_item_mask( int p_mask); int get_item_mask() const; - void set_blend_mode( LightBlendMode p_blend_mode ); - LightBlendMode get_blend_mode() const; + void set_subtract_mode( bool p_enable ); + bool get_subtract_mode() const; void set_shadow_enabled( bool p_enabled); bool is_shadow_enabled() const; + virtual Rect2 get_item_rect() const; Light2D(); ~Light2D(); }; -VARIANT_ENUM_CAST(Light2D::LightBlendMode); - #endif // LIGHT_2D_H diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h index c2e988de954..d825ea2b683 100644 --- a/scene/gui/popup_menu.h +++ b/scene/gui/popup_menu.h @@ -90,7 +90,7 @@ public: void add_icon_check_item(const Ref& p_icon,const String& p_label,int p_ID=-1,uint32_t p_accel=0); void add_check_item(const String& p_label,int p_ID=-1,uint32_t p_accel=0); void add_submenu_item(const String& p_label,const String& p_submenu, int p_ID=-1); - + void set_item_text(int p_idx,const String& p_text); void set_item_icon(int p_idx,const Ref& p_icon); void set_item_checked(int p_idx,bool p_checked); diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 89ce164ce9e..ff525203bfa 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -104,6 +104,7 @@ #include "scene/2d/remote_transform_2d.h" #include "scene/2d/y_sort.h" #include "scene/2d/navigation2d.h" +#include "scene/2d/canvas_modulate.h" #include "scene/2d/position_2d.h" #include "scene/2d/tile_map.h" @@ -264,6 +265,7 @@ void register_scene_types() { ObjectTypeDB::register_virtual_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); /* REGISTER GUI */ diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index 63ebdbc34a8..2ae283f3b78 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -570,6 +570,7 @@ public: struct CanvasLight { + bool enabled; bool shadow; Color color; @@ -577,12 +578,23 @@ public: float height; int z_min; int z_max; + int layer_min; + int layer_max; int item_mask; - VS::CanvasLightBlendMode blend_mode; + bool subtract; RID texture; - void *texture_cache; // implementation dependent Vector2 texture_offset; + RID canvas; + + void *texture_cache; // implementation dependent + Rect2 rect_cache; + Matrix32 xform_cache; + + Matrix32 light_shader_xform; + Vector2 light_shader_pos; + + CanvasLight *filter_next_ptr; CanvasLight *next_ptr; CanvasLight() { @@ -592,10 +604,13 @@ public: height=0; z_min=-1024; z_max=1024; + layer_min=0; + layer_max=0; item_mask=1; - blend_mode=VS::CANVAS_LIGHT_BLEND_ADD; + subtract=false; texture_cache=NULL; next_ptr=NULL; + filter_next_ptr=NULL; } }; @@ -722,6 +737,7 @@ public: bool visible; bool ontop; VS::MaterialBlendMode blend_mode; + int light_mask; Vector commands; mutable bool custom_rect; mutable bool rect_dirty; @@ -739,8 +755,9 @@ public: CanvasItem* shader_owner; ViewportRender *vp_render; - const Rect2& get_rect() const { + Rect2 global_rect_cache; + const Rect2& get_rect() const { if (custom_rect || !rect_dirty) return rect; @@ -864,7 +881,7 @@ public: } void clear() { for (int i=0;ichild_items[idx].mirror; } +void VisualServerRaster::canvas_set_modulate(RID p_canvas,const Color& p_color) { + + Canvas * canvas = canvas_owner.get(p_canvas); + ERR_FAIL_COND(!canvas); + canvas->modulate=p_color; +} + + RID VisualServerRaster::canvas_item_create() { @@ -3305,14 +3314,27 @@ bool VisualServerRaster::canvas_item_is_visible(RID p_item) const { } +void VisualServerRaster::canvas_item_set_light_mask(RID p_canvas_item,int p_mask) { + + VS_CHANGED; + + CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); + ERR_FAIL_COND(!canvas_item); + + if (canvas_item->light_mask==p_mask) + return; + VS_CHANGED; + + canvas_item->light_mask=p_mask; + +} + + void VisualServerRaster::canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend) { VS_CHANGED; CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); - if (!canvas_item) { - printf("!canvas_item\n"); - }; ERR_FAIL_COND(!canvas_item); if (canvas_item->blend_mode==p_blend) @@ -3832,6 +3854,23 @@ void VisualServerRaster::canvas_light_attach_to_canvas(RID p_light,RID p_canvas) Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); ERR_FAIL_COND(!clight); + if (clight->canvas.is_valid()) { + + Canvas *canvas = canvas_owner.get(clight->canvas); + canvas->lights.erase(clight); + } + + if (!canvas_owner.owns(p_canvas)) + p_canvas=RID(); + clight->canvas=p_canvas; + + if (clight->canvas.is_valid()) { + + Canvas *canvas = canvas_owner.get(clight->canvas); + canvas->lights.insert(clight); + } + + } void VisualServerRaster::canvas_light_set_enabled(RID p_light, bool p_enabled){ @@ -3885,6 +3924,16 @@ void VisualServerRaster::canvas_light_set_z_range(RID p_light, int p_min_z,int p clight->z_max=p_max_z; } + +void VisualServerRaster::canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer) { + + Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); + ERR_FAIL_COND(!clight); + clight->layer_min=p_min_layer; + clight->layer_max=p_max_layer; + +} + void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){ Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); @@ -3893,11 +3942,12 @@ void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){ } -void VisualServerRaster::canvas_light_set_blend_mode(RID p_light, CanvasLightBlendMode p_blend_mode){ +void VisualServerRaster::canvas_light_set_subtract_mode(RID p_light, bool p_enable) { + Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); ERR_FAIL_COND(!clight); - clight->blend_mode=p_blend_mode; + clight->subtract=p_enable; } void VisualServerRaster::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled){ @@ -4194,7 +4244,13 @@ void VisualServerRaster::free( RID p_rid ) { canvas->child_items[i].item->parent=RID(); } - + + for (Set::Element *E=canvas->lights.front();E;E=E->next()) { + + E->get()->canvas=RID(); + } + + canvas_owner.free( p_rid ); memdelete( canvas ); @@ -4232,6 +4288,12 @@ void VisualServerRaster::free( RID p_rid ) { Rasterizer::CanvasLight *canvas_light = canvas_light_owner.get(p_rid); ERR_FAIL_COND(!canvas_light); + if (canvas_light->canvas.is_valid()) { + Canvas* canvas = canvas_owner.get(canvas_light->canvas); + if (canvas) + canvas->lights.erase(canvas_light); + } + canvas_light_owner.free( p_rid ); memdelete( canvas_light ); @@ -6280,7 +6342,7 @@ void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, S } -void VisualServerRaster::_render_canvas_item_tree(CanvasItem *p_canvas_item,const Matrix32& p_transform,const Rect2& p_clip_rect) { +void VisualServerRaster::_render_canvas_item_tree(CanvasItem *p_canvas_item, const Matrix32& p_transform, const Rect2& p_clip_rect, const Color& p_modulate, Rasterizer::CanvasLight *p_lights) { static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1; @@ -6298,7 +6360,7 @@ void VisualServerRaster::_render_canvas_item_tree(CanvasItem *p_canvas_item,cons for(int i=0;icanvas_render_items(z_list[i]); + rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_modulate,p_lights); } } @@ -6404,7 +6466,7 @@ void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Mat //something to draw? ci->final_transform=xform; ci->final_opacity=opacity * ci->self_opacity; - + ci->global_rect_cache=global_rect; int zidx = p_z-CANVAS_ITEM_Z_MIN; @@ -6417,6 +6479,8 @@ void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Mat z_last_list[zidx]=ci; } + + ci->next=NULL; } @@ -6430,7 +6494,7 @@ void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Mat } -void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform) { +void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform,Rasterizer::CanvasLight *p_lights) { rasterizer->canvas_begin(); @@ -6463,30 +6527,30 @@ void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_trans for(int i=0;icanvas_render_items(z_list[i]); + rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_canvas->modulate,p_lights); } } else { for(int i=0;ichild_items[i]; - _render_canvas_item_tree(ci.item,p_transform,clip_rect); + _render_canvas_item_tree(ci.item,p_transform,clip_rect,p_canvas->modulate,p_lights); //mirroring (useful for scrolling backgrounds) if (ci.mirror.x!=0) { Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0)); - _render_canvas_item_tree(ci.item,xform2,clip_rect); + _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); } if (ci.mirror.y!=0) { Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y)); - _render_canvas_item_tree(ci.item,xform2,clip_rect); + _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); } if (ci.mirror.y!=0 && ci.mirror.x!=0) { Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror); - _render_canvas_item_tree(ci.item,xform2,clip_rect); + _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); } } @@ -6549,7 +6613,43 @@ void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ Map canvas_map; + Rect2 clip_rect(0,0,viewport_rect.width,viewport_rect.height); + Rasterizer::CanvasLight *lights=NULL; + for (Map::Element *E=p_viewport->canvas_map.front();E;E=E->next()) { + + Matrix32 xf = p_viewport->global_transform * E->get().transform; + + //find lights in canvas + + + for(Set::Element *F=E->get().canvas->lights.front();F;F=F->next()) { + + Rasterizer::CanvasLight* cl=F->get(); + if (cl->enabled && cl->texture.is_valid()) { + //not super efficient.. + Size2 tsize(rasterizer->texture_get_width(cl->texture),rasterizer->texture_get_height(cl->texture)); + Vector2 offset=tsize/2.0; + cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize); + cl->xform_cache=xf * cl->xform; + + if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) { + cl->filter_next_ptr=lights; + lights=cl; + cl->texture_cache=NULL; + Matrix32 scale; + scale.scale(cl->rect_cache.size); + scale.elements[2]=cl->rect_cache.pos; + cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse(); + cl->light_shader_pos=cl->xform_cache[2]; + } + + + + + } + } + canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get(); } @@ -6560,7 +6660,19 @@ void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size())); //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform); Matrix32 xform = p_viewport->global_transform * E->get()->transform; - _render_canvas( E->get()->canvas,xform ); + + Rasterizer::CanvasLight *canvas_lights=NULL; + + Rasterizer::CanvasLight *ptr=lights; + while(ptr) { + if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) { + ptr->next_ptr=canvas_lights; + canvas_lights=ptr; + } + ptr=ptr->filter_next_ptr; + } + + _render_canvas( E->get()->canvas,xform,canvas_lights ); i++; } diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index c15b6ebb262..eec677068ed 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -410,6 +410,8 @@ class VisualServerRaster : public VisualServer { } }; + struct CanvasLight; + struct Canvas { Set viewports; @@ -419,8 +421,10 @@ class VisualServerRaster : public VisualServer { CanvasItem *item; }; + Set lights; Vector child_items; + Color modulate; int find_item(CanvasItem *p_item) { for(int i=0;i _camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max); Vector _camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max); @@ -1075,6 +1079,8 @@ public: virtual RID canvas_create(); virtual void canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring); virtual Point2 canvas_get_item_mirroring(RID p_canvas,RID p_item) const; + virtual void canvas_set_modulate(RID p_canvas,const Color& p_color); + virtual RID canvas_item_create(); @@ -1085,6 +1091,8 @@ public: virtual bool canvas_item_is_visible(RID p_item) const; virtual void canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend); + virtual void canvas_item_set_light_mask(RID p_canvas_item,int p_mask); + //virtual void canvas_item_set_rect(RID p_item, const Rect2& p_rect); @@ -1137,20 +1145,10 @@ public: virtual void canvas_light_set_color(RID p_light, const Color& p_color); virtual void canvas_light_set_height(RID p_light, float p_height); virtual void canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z); + virtual void canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer); virtual void canvas_light_set_item_mask(RID p_light, int p_mask); - enum CanvasightBlendMode { - CANVAS_LIGHT_BLEND_ADD, - CANVAS_LIGHT_BLEND_SUB, - CANVAS_LIGHT_BLEND_MULTIPLY, - CANVAS_LIGHT_BLEND_DODGE, - CANVAS_LIGHT_BLEND_BURN, - CANVAS_LIGHT_BLEND_LIGHTEN, - CANVAS_LIGHT_BLEND_DARKEN, - CANVAS_LIGHT_BLEND_OVERLAY, - CANVAS_LIGHT_BLEND_SCREEN, - }; - virtual void canvas_light_set_blend_mode(RID p_light, CanvasLightBlendMode p_blend_mode); + virtual void canvas_light_set_subtract_mode(RID p_light, bool p_enable); virtual void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled); virtual void canvas_light_set_shadow_buffer_size(RID p_light, int p_size); virtual void canvas_light_set_shadow_filter(RID p_light, int p_size); diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h index 9574dff018c..c94425c5868 100644 --- a/servers/visual/visual_server_wrap_mt.h +++ b/servers/visual/visual_server_wrap_mt.h @@ -1087,6 +1087,8 @@ public: FUNC0R(RID,canvas_create); FUNC3(canvas_set_item_mirroring,RID,RID,const Point2&); FUNC2RC(Point2,canvas_get_item_mirroring,RID,RID); + FUNC2(canvas_set_modulate,RID,const Color&); + FUNC0R(RID,canvas_item_create); @@ -1097,7 +1099,7 @@ public: FUNC1RC(bool,canvas_item_is_visible,RID); FUNC2(canvas_item_set_blend_mode,RID,MaterialBlendMode ); - + FUNC2(canvas_item_set_light_mask,RID,int ); //FUNC(canvas_item_set_rect,RID, const Rect2& p_rect); FUNC2(canvas_item_set_transform,RID, const Matrix32& ); @@ -1155,10 +1157,11 @@ public: FUNC2(canvas_light_set_texture_offset,RID,const Vector2&); FUNC2(canvas_light_set_color,RID,const Color&); FUNC2(canvas_light_set_height,RID,float); + FUNC3(canvas_light_set_layer_range,RID,int,int); FUNC3(canvas_light_set_z_range,RID,int,int); FUNC2(canvas_light_set_item_mask,RID,int); - FUNC2(canvas_light_set_blend_mode,RID,CanvasLightBlendMode); + FUNC2(canvas_light_set_subtract_mode,RID,bool); FUNC2(canvas_light_set_shadow_enabled,RID,bool); FUNC2(canvas_light_set_shadow_buffer_size,RID,int); FUNC2(canvas_light_set_shadow_filter,RID,int); diff --git a/servers/visual_server.h b/servers/visual_server.h index 49ae8ce4e69..e9bc97628f3 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -944,6 +944,8 @@ public: virtual RID canvas_create()=0; virtual void canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring)=0; virtual Point2 canvas_get_item_mirroring(RID p_canvas,RID p_item) const=0; + virtual void canvas_set_modulate(RID p_canvas,const Color& p_color)=0; + virtual RID canvas_item_create()=0; @@ -953,6 +955,8 @@ public: virtual void canvas_item_set_visible(RID p_item,bool p_visible)=0; virtual bool canvas_item_is_visible(RID p_item) const=0; + virtual void canvas_item_set_light_mask(RID p_item,int p_mask)=0; + virtual void canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend)=0; virtual void canvas_item_attach_viewport(RID p_item, RID p_viewport)=0; @@ -1008,20 +1012,10 @@ public: virtual void canvas_light_set_color(RID p_light, const Color& p_color)=0; virtual void canvas_light_set_height(RID p_light, float p_height)=0; virtual void canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z)=0; + virtual void canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer)=0; virtual void canvas_light_set_item_mask(RID p_light, int p_mask)=0; - enum CanvasLightBlendMode { - CANVAS_LIGHT_BLEND_ADD, - CANVAS_LIGHT_BLEND_SUB, - CANVAS_LIGHT_BLEND_MULTIPLY, - CANVAS_LIGHT_BLEND_DODGE, - CANVAS_LIGHT_BLEND_BURN, - CANVAS_LIGHT_BLEND_LIGHTEN, - CANVAS_LIGHT_BLEND_DARKEN, - CANVAS_LIGHT_BLEND_OVERLAY, - CANVAS_LIGHT_BLEND_SCREEN, - }; - virtual void canvas_light_set_blend_mode(RID p_light, CanvasLightBlendMode p_blend_mode)=0; + virtual void canvas_light_set_subtract_mode(RID p_light, bool p_enable)=0; virtual void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled)=0; virtual void canvas_light_set_shadow_buffer_size(RID p_light, int p_size)=0; virtual void canvas_light_set_shadow_filter(RID p_light, int p_size)=0;