diff options
author | Juan Linietsky <reduzio@gmail.com> | 2017-08-08 17:23:44 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2017-08-08 17:23:44 -0300 |
commit | df573f5c3a76155842e43ea0dbab594e3944b114 (patch) | |
tree | 1124403a4f002fd6331f7381b8a4d40999391af6 /scene | |
parent | 8a4bce6ebd843f9a8f482f74601f4933aae737a3 (diff) |
-Restored Sprite3D to working function, fixes #2061, fixes #9738
-Restored an alpha scissor property in Material
Diffstat (limited to 'scene')
-rw-r--r-- | scene/3d/sprite_3d.cpp | 5 | ||||
-rw-r--r-- | scene/resources/material.cpp | 90 | ||||
-rw-r--r-- | scene/resources/material.h | 16 |
3 files changed, 101 insertions, 10 deletions
diff --git a/scene/3d/sprite_3d.cpp b/scene/3d/sprite_3d.cpp index 1b9b58ceb1..b4e045e43f 100644 --- a/scene/3d/sprite_3d.cpp +++ b/scene/3d/sprite_3d.cpp @@ -389,7 +389,7 @@ void Sprite3D::_draw() { int axis = get_axis(); normal[axis] = 1.0; - RID mat = VS::get_singleton()->material_2d_get(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS); + RID mat = SpatialMaterial::get_material_rid_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS); VS::get_singleton()->immediate_set_material(immediate, mat); VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid()); @@ -892,7 +892,8 @@ void AnimatedSprite3D::_draw() { int axis = get_axis(); normal[axis] = 1.0; - RID mat = VS::get_singleton()->material_2d_get(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS); + RID mat = SpatialMaterial::get_material_rid_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS); + VS::get_singleton()->immediate_set_material(immediate, mat); VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid()); diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 5a79e49240..d869e72f53 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -241,6 +241,7 @@ void SpatialMaterial::init_shaders() { shader_names->rim_texture_channel = "rim_texture_channel"; shader_names->depth_texture_channel = "depth_texture_channel"; shader_names->refraction_texture_channel = "refraction_texture_channel"; + shader_names->alpha_scissor_threshold = "alpha_scissor_threshold"; shader_names->texture_names[TEXTURE_ALBEDO] = "texture_albedo"; shader_names->texture_names[TEXTURE_METALLIC] = "texture_metallic"; @@ -259,8 +260,14 @@ void SpatialMaterial::init_shaders() { shader_names->texture_names[TEXTURE_DETAIL_NORMAL] = "texture_detail_normal"; } +Ref<SpatialMaterial> SpatialMaterial::materials_for_2d[SpatialMaterial::MAX_MATERIALS_FOR_2D]; + void SpatialMaterial::finish_shaders() { + for (int i = 0; i < MAX_MATERIALS_FOR_2D; i++) { + materials_for_2d[i].unref(); + } + #ifndef NO_THREADS memdelete(material_mutex); #endif @@ -359,6 +366,9 @@ void SpatialMaterial::_update_shader() { code += "uniform float grow;\n"; } + if (flags[FLAG_USE_ALPHA_SCISSOR]) { + code += "uniform float alpha_scissor_threshold;\n"; + } code += "uniform float roughness : hint_range(0,1);\n"; code += "uniform float point_size : hint_range(0,128);\n"; code += "uniform sampler2D texture_metallic : hint_white;\n"; @@ -674,7 +684,7 @@ void SpatialMaterial::_update_shader() { code += "\tALBEDO *= 1.0 - ref_amount;\n"; code += "\tALPHA = 1.0;\n"; - } else if (features[FEATURE_TRANSPARENT]) { + } else if (features[FEATURE_TRANSPARENT] || features[FLAG_USE_ALPHA_SCISSOR]) { code += "\tALPHA = albedo.a * albedo_tex.a;\n"; } @@ -774,6 +784,10 @@ void SpatialMaterial::_update_shader() { code += "\tvec3 detail_norm = mix(NORMALMAP,detail_norm_tex.rgb,detail_tex.a);\n"; code += "\tNORMALMAP = mix(NORMALMAP,detail_norm,detail_mask_tex.r);\n"; code += "\tALBEDO.rgb = mix(ALBEDO.rgb,detail,detail_mask_tex.r);\n"; + + if (flags[FLAG_USE_ALPHA_SCISSOR]) { + code += "\tALPHA_SCISSOR=alpha_scissor_threshold;\n"; + } } code += "}\n"; @@ -1086,6 +1100,9 @@ void SpatialMaterial::set_flag(Flags p_flag, bool p_enabled) { return; flags[p_flag] = p_enabled; + if (p_flag == FLAG_USE_ALPHA_SCISSOR) { + _change_notify(); + } _queue_shader_change(); } @@ -1130,9 +1147,6 @@ void SpatialMaterial::_validate_feature(const String &text, Feature feature, Pro if (property.name.begins_with(text) && property.name != text + "_enabled" && !features[feature]) { property.usage = 0; } - if ((property.name == "depth_min_layers" || property.name == "depth_max_layers") && !deep_parallax) { - property.usage = 0; - } } void SpatialMaterial::_validate_property(PropertyInfo &property) const { @@ -1154,6 +1168,14 @@ void SpatialMaterial::_validate_property(PropertyInfo &property) const { if (property.name == "params_grow_amount" && !grow_enabled) { property.usage = 0; } + + if (property.name == "params_alpha_scissor_threshold" && !flags[FLAG_USE_ALPHA_SCISSOR]) { + property.usage = 0; + } + + if ((property.name == "depth_min_layers" || property.name == "depth_max_layers") && !deep_parallax) { + property.usage = 0; + } } void SpatialMaterial::set_line_width(float p_line_width) { @@ -1329,6 +1351,16 @@ bool SpatialMaterial::is_grow_enabled() const { return grow_enabled; } +void SpatialMaterial::set_alpha_scissor_threshold(float p_treshold) { + alpha_scissor_threshold = p_treshold; + VS::get_singleton()->material_set_param(_get_material(), shader_names->alpha_scissor_threshold, p_treshold); +} + +float SpatialMaterial::get_alpha_scissor_threshold() const { + + return alpha_scissor_threshold; +} + void SpatialMaterial::set_grow(float p_grow) { grow = p_grow; VS::get_singleton()->material_set_param(_get_material(), shader_names->grow, p_grow); @@ -1391,6 +1423,40 @@ SpatialMaterial::TextureChannel SpatialMaterial::get_refraction_texture_channel( return refraction_texture_channel; } +RID SpatialMaterial::get_material_rid_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass) { + + int version = 0; + if (p_shaded) + version = 1; + if (p_transparent) + version |= 2; + if (p_cut_alpha) + version |= 4; + if (p_opaque_prepass) + version |= 8; + if (p_double_sided) + version |= 16; + + if (materials_for_2d[version].is_valid()) { + return materials_for_2d[version]->get_rid(); + } + + Ref<SpatialMaterial> material; + material.instance(); + + material->set_flag(FLAG_UNSHADED, !p_shaded); + material->set_feature(FEATURE_TRANSPARENT, p_transparent); + material->set_cull_mode(p_double_sided ? CULL_DISABLED : CULL_BACK); + material->set_depth_draw_mode(p_opaque_prepass ? DEPTH_DRAW_ALPHA_OPAQUE_PREPASS : DEPTH_DRAW_OPAQUE_ONLY); + material->set_flag(FLAG_SRGB_VERTEX_COLOR, true); + material->set_flag(FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + material->set_flag(FLAG_USE_ALPHA_SCISSOR, p_cut_alpha); + + materials_for_2d[version] = material; + + return materials_for_2d[version]->get_rid(); +} + void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo); @@ -1516,6 +1582,9 @@ void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_grow", "amount"), &SpatialMaterial::set_grow); ClassDB::bind_method(D_METHOD("get_grow"), &SpatialMaterial::get_grow); + ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &SpatialMaterial::set_alpha_scissor_threshold); + ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &SpatialMaterial::get_alpha_scissor_threshold); + ClassDB::bind_method(D_METHOD("set_grow_enabled", "enable"), &SpatialMaterial::set_grow_enabled); ClassDB::bind_method(D_METHOD("is_grow_enabled"), &SpatialMaterial::is_grow_enabled); @@ -1553,6 +1622,8 @@ void SpatialMaterial::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "params_billboard_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard,Particle Billboard"), "set_billboard_mode", "get_billboard_mode"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "params_grow"), "set_grow_enabled", "is_grow_enabled"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_grow_amount", PROPERTY_HINT_RANGE, "-16,10,0.01"), "set_grow", "get_grow"); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "params_use_alpha_scissor"), "set_flag", "get_flag", FLAG_USE_ALPHA_SCISSOR); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold"); ADD_GROUP("Particles Anim", "particles_anim_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames"); ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames"); @@ -1697,9 +1768,13 @@ void SpatialMaterial::_bind_methods() { BIND_CONSTANT(FLAG_USE_VERTEX_LIGHTING); BIND_CONSTANT(FLAG_ONTOP); BIND_CONSTANT(FLAG_ALBEDO_FROM_VERTEX_COLOR); - BIND_CONSTANT(FLAG_SRGB_VERTEX_COLOR) - BIND_CONSTANT(FLAG_USE_POINT_SIZE) - BIND_CONSTANT(FLAG_FIXED_SIZE) + BIND_CONSTANT(FLAG_SRGB_VERTEX_COLOR); + BIND_CONSTANT(FLAG_USE_POINT_SIZE); + BIND_CONSTANT(FLAG_FIXED_SIZE); + BIND_CONSTANT(FLAG_UV1_USE_TRIPLANAR); + BIND_CONSTANT(FLAG_UV2_USE_TRIPLANAR); + BIND_CONSTANT(FLAG_AO_ON_UV2); + BIND_CONSTANT(FLAG_USE_ALPHA_SCISSOR); BIND_CONSTANT(FLAG_MAX); BIND_CONSTANT(DIFFUSE_LAMBERT); @@ -1757,6 +1832,7 @@ SpatialMaterial::SpatialMaterial() set_particles_anim_h_frames(1); set_particles_anim_v_frames(1); set_particles_anim_loop(false); + set_alpha_scissor_threshold(0.98); set_metallic_texture_channel(TEXTURE_CHANNEL_RED); set_roughness_texture_channel(TEXTURE_CHANNEL_RED); diff --git a/scene/resources/material.h b/scene/resources/material.h index 1484b79fc6..25628e272a 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -164,6 +164,7 @@ public: FLAG_UV1_USE_TRIPLANAR, FLAG_UV2_USE_TRIPLANAR, FLAG_AO_ON_UV2, + FLAG_USE_ALPHA_SCISSOR, FLAG_MAX }; @@ -207,7 +208,7 @@ private: uint64_t blend_mode : 2; uint64_t depth_draw_mode : 2; uint64_t cull_mode : 2; - uint64_t flags : 9; + uint64_t flags : 11; uint64_t detail_blend_mode : 2; uint64_t diffuse_mode : 3; uint64_t specular_mode : 2; @@ -298,6 +299,7 @@ private: StringName rim_texture_channel; StringName depth_texture_channel; StringName refraction_texture_channel; + StringName alpha_scissor_threshold; StringName texture_names[TEXTURE_MAX]; }; @@ -329,6 +331,7 @@ private: float refraction; float line_width; float point_size; + float alpha_scissor_threshold; bool grow_enabled; float grow; int particles_anim_h_frames; @@ -369,6 +372,12 @@ private: _FORCE_INLINE_ void _validate_feature(const String &text, Feature feature, PropertyInfo &property) const; + enum { + MAX_MATERIALS_FOR_2D = 32 + }; + + static Ref<SpatialMaterial> materials_for_2d[MAX_MATERIALS_FOR_2D]; //used by Sprite3D and other stuff + protected: static void _bind_methods(); void _validate_property(PropertyInfo &property) const; @@ -499,6 +508,9 @@ public: void set_grow(float p_grow); float get_grow() const; + void set_alpha_scissor_threshold(float p_treshold); + float get_alpha_scissor_threshold() const; + void set_metallic_texture_channel(TextureChannel p_channel); TextureChannel get_metallic_texture_channel() const; void set_roughness_texture_channel(TextureChannel p_channel); @@ -512,6 +524,8 @@ public: static void finish_shaders(); static void flush_changes(); + static RID get_material_rid_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass); + SpatialMaterial(); virtual ~SpatialMaterial(); }; |