diff options
Diffstat (limited to 'scene')
-rw-r--r-- | scene/2d/canvas_item.cpp | 86 | ||||
-rw-r--r-- | scene/2d/canvas_item.h | 15 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 3 | ||||
-rw-r--r-- | scene/resources/material.cpp | 141 | ||||
-rw-r--r-- | scene/resources/material.h | 62 | ||||
-rw-r--r-- | scene/resources/shader.cpp | 2 | ||||
-rw-r--r-- | scene/resources/shader.h | 19 |
7 files changed, 140 insertions, 188 deletions
diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp index f90da51eea..c28bf71e04 100644 --- a/scene/2d/canvas_item.cpp +++ b/scene/2d/canvas_item.cpp @@ -720,6 +720,84 @@ bool CanvasItem::is_draw_behind_parent_enabled() const{ return behind; } +void CanvasItem::set_shader(const Ref<Shader>& p_shader) { + + ERR_FAIL_COND(p_shader.is_valid() && p_shader->get_mode()!=Shader::MODE_CANVAS_ITEM); + +#ifdef TOOLS_ENABLED + + if (shader.is_valid()) { + shader->disconnect("changed",this,"_shader_changed"); + } +#endif + shader=p_shader; + +#ifdef TOOLS_ENABLED + + if (shader.is_valid()) { + shader->connect("changed",this,"_shader_changed"); + } +#endif + + RID rid; + if (shader.is_valid()) + rid=shader->get_rid(); + VS::get_singleton()->canvas_item_set_shader(canvas_item,rid); + _change_notify(); //properties for shader exposed +} + +Ref<Shader> CanvasItem::get_shader() const{ + + return shader; +} + +void CanvasItem::set_shader_param(const StringName& p_param,const Variant& p_value) { + + VS::get_singleton()->canvas_item_set_shader_param(canvas_item,p_param,p_value); +} + +Variant CanvasItem::get_shader_param(const StringName& p_param) const { + + return VS::get_singleton()->canvas_item_get_shader_param(canvas_item,p_param); +} + +bool CanvasItem::_set(const StringName& p_name, const Variant& p_value) { + + if (shader.is_valid()) { + StringName pr = shader->remap_param(p_name); + if (pr) { + set_shader_param(pr,p_value); + return true; + } + } + return false; +} + +bool CanvasItem::_get(const StringName& p_name,Variant &r_ret) const{ + + if (shader.is_valid()) { + StringName pr = shader->remap_param(p_name); + if (pr) { + r_ret=get_shader_param(pr); + return true; + } + } + return false; + +} +void CanvasItem::_get_property_list( List<PropertyInfo> *p_list) const{ + + if (shader.is_valid()) { + shader->get_param_list(p_list); + } +} + +#ifdef TOOLS_ENABLED +void CanvasItem::_shader_changed() { + + _change_notify(); +} +#endif void CanvasItem::_bind_methods() { @@ -761,7 +839,9 @@ void CanvasItem::_bind_methods() { ObjectTypeDB::bind_method(_MD("_set_on_top","on_top"),&CanvasItem::_set_on_top); ObjectTypeDB::bind_method(_MD("_is_on_top"),&CanvasItem::_is_on_top); - +#ifdef TOOLS_ENABLED + ObjectTypeDB::bind_method(_MD("_shader_changed"),&CanvasItem::_shader_changed); +#endif //ObjectTypeDB::bind_method(_MD("get_transform"),&CanvasItem::get_transform); ObjectTypeDB::bind_method(_MD("draw_line","from","to","color","width"),&CanvasItem::draw_line,DEFVAL(1.0)); @@ -786,6 +866,9 @@ void CanvasItem::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_world_2d"),&CanvasItem::get_world_2d); //ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasItem::get_viewport); + ObjectTypeDB::bind_method(_MD("set_shader","shader"),&CanvasItem::set_shader); + ObjectTypeDB::bind_method(_MD("get_shader"),&CanvasItem::get_shader); + BIND_VMETHOD(MethodInfo("_draw")); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"visibility/visible"), _SCS("_set_visible_"),_SCS("_is_visible_") ); @@ -795,6 +878,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::OBJECT,"shader/shader",PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemShader,CanvasItemShaderGraph"), _SCS("set_shader"),_SCS("get_shader") ); //exporting these two things doesn't really make much sense i think //ADD_PROPERTY( PropertyInfo(Variant::BOOL,"transform/toplevel"), _SCS("set_as_toplevel"),_SCS("is_set_as_toplevel") ); //ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),_SCS("set_transform_notify"),_SCS("is_transform_notify_enabled")); diff --git a/scene/2d/canvas_item.h b/scene/2d/canvas_item.h index dbf8fd79e9..7c162d9c81 100644 --- a/scene/2d/canvas_item.h +++ b/scene/2d/canvas_item.h @@ -32,6 +32,7 @@ #include "scene/main/node.h" #include "scene/resources/texture.h" #include "scene/main/scene_main_loop.h" +#include "scene/resources/shader.h" class CanvasLayer; class Viewport; @@ -80,6 +81,8 @@ private: bool block_transform_notify; bool behind; + Ref<Shader> shader; + mutable Matrix32 global_transform; mutable bool global_invalid; @@ -99,8 +102,9 @@ private: void _queue_sort_children(); void _sort_children(); - - +#ifdef TOOLS_ENABLED + void _shader_changed(); +#endif void _notify_transform(CanvasItem *p_node); void _set_on_top(bool p_on_top) { set_draw_behind_parent(!p_on_top); } @@ -108,6 +112,9 @@ private: protected: + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List<PropertyInfo> *p_list) const; _FORCE_INLINE_ void _notify_transform() { if (!is_inside_tree()) return; _notify_transform(this); if (!block_transform_notify) notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); } @@ -204,7 +211,11 @@ public: RID get_canvas() const; Ref<World2D> get_world_2d() const; + void set_shader(const Ref<Shader>& p_shader); + Ref<Shader> get_shader() const; + void set_shader_param(const StringName& p_param,const Variant& p_value); + Variant get_shader_param(const StringName& p_param) const; CanvasItem(); ~CanvasItem(); diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index cf49979118..2592751274 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -500,14 +500,13 @@ void register_scene_types() { ObjectTypeDB::register_type<Mesh>(); ObjectTypeDB::register_virtual_type<Material>(); ObjectTypeDB::register_type<FixedMaterial>(); - //ObjectTypeDB::register_type<ParticleSystemMaterial>(); - //ObjectTypeDB::register_type<UnshadedMaterial>(); ObjectTypeDB::register_type<ShaderMaterial>(); ObjectTypeDB::register_type<RoomBounds>(); ObjectTypeDB::register_virtual_type<Shader>(); ObjectTypeDB::register_virtual_type<ShaderGraph>(); ObjectTypeDB::register_type<MaterialShaderGraph>(); ObjectTypeDB::register_type<MaterialShader>(); + ObjectTypeDB::register_type<CanvasItemShader>(); ObjectTypeDB::add_compatibility_type("Shader","MaterialShader"); ObjectTypeDB::add_compatibility_type("ParticleSystemMaterial","FixedMaterial"); ObjectTypeDB::add_compatibility_type("UnshadedMaterial","FixedMaterial"); diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index faba339fe1..355cc8884c 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -467,10 +467,20 @@ bool ShaderMaterial::_set(const StringName& p_name, const Variant& p_value) { return true; } else { - String n = p_name; - if (n.begins_with("param/")) { - VisualServer::get_singleton()->material_set_param(material,String(n.ptr()+6),p_value); - return true; + if (shader.is_valid()) { + + + StringName pr = shader->remap_param(p_name); + if (!pr) { + String n = p_name; + if (n.find("param/")==0) { //backwards compatibility + pr = n.substr(6,n.length()); + } + } + if (pr) { + VisualServer::get_singleton()->material_set_param(material,pr,p_value); + return true; + } } } @@ -486,10 +496,13 @@ bool ShaderMaterial::_get(const StringName& p_name,Variant &r_ret) const { return true; } else { - String n = p_name; - if (n.begins_with("param/")) { - r_ret=VisualServer::get_singleton()->material_get_param(material,String(n.ptr()+6)); - return true; + if (shader.is_valid()) { + + StringName pr = shader->remap_param(p_name); + if (pr) { + r_ret=VisualServer::get_singleton()->material_get_param(material,pr); + return true; + } } } @@ -583,115 +596,3 @@ ShaderMaterial::ShaderMaterial() :Material(VisualServer::get_singleton()->materi ///////////////////////////////// - -void ParticleSystemMaterial::_bind_methods() { - - ObjectTypeDB::bind_method(_MD("set_texture","texture"),&ParticleSystemMaterial::set_texture); - ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&ParticleSystemMaterial::get_texture); - - ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture" ), _SCS("set_texture"), _SCS("get_texture")); - -} - -void ParticleSystemMaterial::set_texture(const Ref<Texture>& p_texture) { - texture=p_texture; - RID rid; - if (texture.is_valid()) - rid=texture->get_rid(); - - VS::get_singleton()->fixed_material_set_texture(material,VS::FIXED_MATERIAL_PARAM_DIFFUSE,rid); -} - -Ref<Texture> ParticleSystemMaterial::get_texture() const { - - return texture; -} - - -ParticleSystemMaterial::ParticleSystemMaterial() :Material(VisualServer::get_singleton()->fixed_material_create()){ - - set_flag(FLAG_DOUBLE_SIDED,true); - set_flag(FLAG_UNSHADED,true); - set_depth_draw_mode(DEPTH_DRAW_NEVER); - VisualServer::get_singleton()->fixed_material_set_flag(material,VS::FIXED_MATERIAL_FLAG_USE_ALPHA,true); - VisualServer::get_singleton()->fixed_material_set_flag(material,VS::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true); - set_flag(FLAG_COLOR_ARRAY_SRGB,true); - -} - -ParticleSystemMaterial::~ParticleSystemMaterial() { - - -} - -////////////////////////////// - - - -void UnshadedMaterial::_bind_methods() { - - ObjectTypeDB::bind_method(_MD("set_texture","texture"),&UnshadedMaterial::set_texture); - ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&UnshadedMaterial::get_texture); - - ObjectTypeDB::bind_method(_MD("set_use_alpha","enable"),&UnshadedMaterial::set_use_alpha); - ObjectTypeDB::bind_method(_MD("is_using_alpha"),&UnshadedMaterial::is_using_alpha); - - ObjectTypeDB::bind_method(_MD("set_use_color_array","enable"),&UnshadedMaterial::set_use_color_array); - ObjectTypeDB::bind_method(_MD("is_using_color_array"),&UnshadedMaterial::is_using_color_array); - - ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture" ), _SCS("set_texture"), _SCS("get_texture")); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "alpha" ), _SCS("set_use_alpha"), _SCS("is_using_alpha")); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "color_array" ), _SCS("set_use_color_array"), _SCS("is_using_color_array")); - -} - -void UnshadedMaterial::set_texture(const Ref<Texture>& p_texture) { - RID rid; - if (texture.is_valid()) - rid=texture->get_rid(); - - VS::get_singleton()->fixed_material_set_texture(material,VS::FIXED_MATERIAL_PARAM_DIFFUSE,rid); -} -Ref<Texture> UnshadedMaterial::get_texture() const { - - return texture; -} - -void UnshadedMaterial::set_use_alpha(bool p_use_alpha) { - - alpha=p_use_alpha; - VS::get_singleton()->fixed_material_set_flag(material,VS::FIXED_MATERIAL_FLAG_USE_ALPHA,p_use_alpha); - //set_depth_draw_mode(); - //set_hint(HINT,p_use_alpha); - -} - -bool UnshadedMaterial::is_using_alpha() const{ - - return alpha; -} - -void UnshadedMaterial::set_use_color_array(bool p_use_color_array){ - - color_array=p_use_color_array; - VS::get_singleton()->fixed_material_set_flag(material,VS::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,p_use_color_array); - -} - -bool UnshadedMaterial::is_using_color_array() const{ - - return color_array; -} - -UnshadedMaterial::UnshadedMaterial() :Material(VisualServer::get_singleton()->fixed_material_create()){ - - set_flag(FLAG_UNSHADED,true); - set_use_alpha(true); - set_flag(FLAG_COLOR_ARRAY_SRGB,true); - -} - -UnshadedMaterial::~UnshadedMaterial() { - - -} diff --git a/scene/resources/material.h b/scene/resources/material.h index 2b10078e16..73d1a4e188 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -253,68 +253,6 @@ public: -class ParticleSystemMaterial : public Material { - - OBJ_TYPE( ParticleSystemMaterial, Material ); - REVERSE_GET_PROPERTY_LIST - -private: - - - - Ref<Texture> texture; - -protected: - - - static void _bind_methods(); - -public: - - void set_texture(const Ref<Texture>& p_texture); - Ref<Texture> get_texture() const; - - - ParticleSystemMaterial(); - ~ParticleSystemMaterial(); - -}; - -/////////////////////////////////////////// - - -class UnshadedMaterial : public Material { - - OBJ_TYPE( UnshadedMaterial, Material ); - REVERSE_GET_PROPERTY_LIST - -private: - - - bool alpha; - bool color_array; - Ref<Texture> texture; - -protected: - - - static void _bind_methods(); - -public: - - void set_texture(const Ref<Texture>& p_texture); - Ref<Texture> get_texture() const; - - void set_use_alpha(bool p_use_alpha); - bool is_using_alpha() const; - - void set_use_color_array(bool p_use_color_array); - bool is_using_color_array() const; - - UnshadedMaterial(); - ~UnshadedMaterial(); - -}; #endif diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index f3e625917a..42251124bd 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -84,7 +84,7 @@ void Shader::get_param_list(List<PropertyInfo> *p_params) const { for(List<PropertyInfo>::Element *E=local.front();E;E=E->next()) { PropertyInfo pi=E->get(); - pi.name="param/"+pi.name; + pi.name="shader_param/"+pi.name; params_cache[pi.name]=E->get().name; if (p_params) { diff --git a/scene/resources/shader.h b/scene/resources/shader.h index 8c15ca43d4..4a380d455b 100644 --- a/scene/resources/shader.h +++ b/scene/resources/shader.h @@ -80,6 +80,16 @@ public: Ref<Texture> get_default_texture_param(const StringName& p_param) const; void get_default_texture_param_list(List<StringName>* r_textures) const; + _FORCE_INLINE_ StringName remap_param(const StringName& p_param) const { + if (params_cache_dirty) + get_param_list(NULL); + + const Map<StringName,StringName>::Element *E=params_cache.find(p_param); + if (E) + return E->get(); + return StringName(); + } + virtual RID get_rid() const; Shader(Mode p_mode); @@ -98,6 +108,15 @@ public: MaterialShader() : Shader(MODE_MATERIAL) {}; }; +class CanvasItemShader : public Shader { + + OBJ_TYPE(CanvasItemShader,Shader); + +public: + + CanvasItemShader() : Shader(MODE_CANVAS_ITEM) {}; +}; + class ResourceFormatLoaderShader : public ResourceFormatLoader { |