From bc18ed9e49972897e247a2913f314e0cf922c2a6 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 15 Feb 2024 00:26:37 +0100 Subject: [PATCH] Only allow valid types in Decal, Light3D projector and PointLight2D texture If an invalid type is supplied (which can still be done from a script), a warning is printed (along with a workaround for ViewportTexture). This also adds support for "negative" resource hints such as "Texture2D,-ViewportTexture" to exclude one or more subclasses from a class hint. Co-authored-by: Tomasz Chabora --- core/object/object.h | 2 +- doc/classes/Texture2D.xml | 2 +- doc/classes/ViewportTexture.xml | 3 ++- editor/editor_resource_picker.cpp | 26 +++++++++++++------ .../4.3-stable.expected | 13 ++++++++++ modules/csg/csg_shape.cpp | 3 ++- modules/csg/doc_classes/CSGMesh3D.xml | 2 +- scene/2d/light_2d.cpp | 16 +++++++++++- scene/3d/decal.cpp | 24 ++++++++++++++--- scene/3d/light_3d.cpp | 21 ++++++++++++--- 10 files changed, 91 insertions(+), 21 deletions(-) diff --git a/core/object/object.h b/core/object/object.h index da8e8d1b86ee..70785f222822 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -65,7 +65,7 @@ enum PropertyHint { PROPERTY_HINT_DIR, ///< a directory path must be passed PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc," PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed - PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type + PROPERTY_HINT_RESOURCE_TYPE, ///< a comma-separated resource object type, e.g. "NoiseTexture,GradientTexture2D". Subclasses can be excluded with a "-" prefix if placed *after* the base class, e.g. "Texture2D,-MeshTexture". PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines PROPERTY_HINT_EXPRESSION, ///< used for string properties that can contain multiple lines PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties diff --git a/doc/classes/Texture2D.xml b/doc/classes/Texture2D.xml index 7254a92e36d2..c153a796578d 100644 --- a/doc/classes/Texture2D.xml +++ b/doc/classes/Texture2D.xml @@ -124,7 +124,7 @@ Returns an [Image] that is a copy of data from this [Texture2D] (a new [Image] is created each time). [Image]s can be accessed and manipulated directly. [b]Note:[/b] This will return [code]null[/code] if this [Texture2D] is invalid. - [b]Note:[/b] This will fetch the texture data from the GPU, which might cause performance problems when overused. + [b]Note:[/b] This will fetch the texture data from the GPU, which might cause performance problems when overused. Avoid calling [method get_image] every frame, especially on large textures. diff --git a/doc/classes/ViewportTexture.xml b/doc/classes/ViewportTexture.xml index f6840d6b0952..89169c53ea10 100644 --- a/doc/classes/ViewportTexture.xml +++ b/doc/classes/ViewportTexture.xml @@ -8,11 +8,12 @@ To get a [ViewportTexture] in code, use the [method Viewport.get_texture] method on the target viewport. [b]Note:[/b] A [ViewportTexture] is always local to its scene (see [member Resource.resource_local_to_scene]). If the scene root is not ready, it may return incorrect data (see [signal Node.ready]). [b]Note:[/b] Instantiating scenes containing a high-resolution [ViewportTexture] may cause noticeable stutter. - [b]Note:[/b] When using a [Viewport] with [member Viewport.use_hdr_2d] set to [code]true[/code] the returned texture will be an HDR image encoded in linear space. This may look darker than normal when displayed directly on screen. To convert to gamma space, you can do the following: + [b]Note:[/b] When using a [Viewport] with [member Viewport.use_hdr_2d] set to [code]true[/code], the returned texture will be an HDR image encoded in linear space. This may look darker than normal when displayed directly on screen. To convert to gamma space, you can do the following: [codeblock] img.convert(Image.FORMAT_RGBA8) imb.linear_to_srgb() [/codeblock] + [b]Note:[/b] Some nodes such as [Decal], [Light3D], and [PointLight2D] do not support using [ViewportTexture] directly. To use texture data from a [ViewportTexture] in these nodes, you need to create an [ImageTexture] by calling [method Texture2D.get_image] on the [ViewportTexture] and passing the result to [method ImageTexture.create_from_image]. This conversion is a slow operation, so it should not be performed every frame. https://godotengine.org/asset-library/asset/2807 diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 897e897f2d4f..464b9d7373f0 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -578,9 +578,9 @@ String EditorResourcePicker::_get_resource_type(const Ref &p_resource) return res_type; } -static void _add_allowed_type(const StringName &p_type, HashSet *p_vector) { - if (p_vector->has(p_type)) { - // Already added +static void _add_allowed_type(const StringName &p_type, List *p_vector) { + if (p_vector->find(p_type)) { + // Already added. return; } @@ -588,7 +588,7 @@ static void _add_allowed_type(const StringName &p_type, HashSet *p_v // Engine class, if (!ClassDB::is_virtual(p_type)) { - p_vector->insert(p_type); + p_vector->push_back(p_type); } List inheriters; @@ -598,7 +598,7 @@ static void _add_allowed_type(const StringName &p_type, HashSet *p_v } } else { // Script class. - p_vector->insert(p_type); + p_vector->push_back(p_type); } List inheriters; @@ -613,12 +613,22 @@ void EditorResourcePicker::_ensure_allowed_types() const { return; } + List final_allowed; + Vector allowed_types = base_type.split(","); int size = allowed_types.size(); - for (int i = 0; i < size; i++) { - const String base = allowed_types[i].strip_edges(); - _add_allowed_type(base, &allowed_types_without_convert); + for (const String &S : allowed_types) { + const String base = S.strip_edges(); + if (base.begins_with("-")) { + final_allowed.erase(base.right(-1)); + continue; + } + _add_allowed_type(base, &final_allowed); + } + + for (const StringName &SN : final_allowed) { + allowed_types_without_convert.insert(SN); } allowed_types_with_convert = HashSet(allowed_types_without_convert); diff --git a/misc/extension_api_validation/4.3-stable.expected b/misc/extension_api_validation/4.3-stable.expected index c2b54b1e6897..5fd9456894ca 100644 --- a/misc/extension_api_validation/4.3-stable.expected +++ b/misc/extension_api_validation/4.3-stable.expected @@ -205,3 +205,16 @@ GH-99481 Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/push_meta/arguments': size changed value in new API, from 2 to 3. Optional argument added to set tooltip. Compatibility method registered. + + +GH-88349 +-------- +Validate extension JSON: Error: Field 'classes/CSGMesh3D/properties/mesh': type changed value in new API, from "Mesh" to "Mesh,-PlaneMesh,-QuadMesh,-RibbonTrailMesh". +Validate extension JSON: Error: Field 'classes/Decal/properties/texture_albedo': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". +Validate extension JSON: Error: Field 'classes/Decal/properties/texture_emission': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". +Validate extension JSON: Error: Field 'classes/Decal/properties/texture_normal': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". +Validate extension JSON: Error: Field 'classes/Decal/properties/texture_orm': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". +Validate extension JSON: Error: Field 'classes/Light3D/properties/light_projector': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". +Validate extension JSON: Error: Field 'classes/PointLight2D/properties/texture': type changed value in new API, from "Texture2D" to "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture". + +Property hints modified to disallow resource types that don't work. The types allowed are now more restricted, but this change only impacts the editor and not the actual exposed API. No adjustments should be necessary. diff --git a/modules/csg/csg_shape.cpp b/modules/csg/csg_shape.cpp index cc4595929f31..523bdf5c03ec 100644 --- a/modules/csg/csg_shape.cpp +++ b/modules/csg/csg_shape.cpp @@ -1116,7 +1116,8 @@ void CSGMesh3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material); ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); + // Hide PrimitiveMeshes that are always non-manifold and therefore can't be used as CSG meshes. + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh,-PlaneMesh,-QuadMesh,-RibbonTrailMesh"), "set_mesh", "get_mesh"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material"); } diff --git a/modules/csg/doc_classes/CSGMesh3D.xml b/modules/csg/doc_classes/CSGMesh3D.xml index 96d89ff48604..e48806e144f8 100644 --- a/modules/csg/doc_classes/CSGMesh3D.xml +++ b/modules/csg/doc_classes/CSGMesh3D.xml @@ -4,7 +4,7 @@ A CSG Mesh shape that uses a mesh resource. - This CSG node allows you to use any mesh resource as a CSG shape, provided it is closed, does not self-intersect, does not contain internal faces and has no edges that connect to more than two faces. See also [CSGPolygon3D] for drawing 2D extruded polygons to be used as CSG nodes. + This CSG node allows you to use any mesh resource as a CSG shape, provided it is [i]manifold[/i]. A manifold shape is closed, does not self-intersect, does not contain internal faces and has no edges that connect to more than two faces. See also [CSGPolygon3D] for drawing 2D extruded polygons to be used as CSG nodes. [b]Note:[/b] CSG nodes are intended to be used for level prototyping. Creating CSG nodes has a significant CPU cost compared to creating a [MeshInstance3D] with a [PrimitiveMesh]. Moving a CSG node within another CSG node also has a significant CPU cost, so it should be avoided during gameplay. diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp index 0b54d1e60462..4438e2fbe5a0 100644 --- a/scene/2d/light_2d.cpp +++ b/scene/2d/light_2d.cpp @@ -395,6 +395,19 @@ Rect2 PointLight2D::get_anchorable_rect() const { void PointLight2D::set_texture(const Ref &p_texture) { texture = p_texture; if (texture.is_valid()) { +#ifdef DEBUG_ENABLED + if ( + p_texture->is_class("AnimatedTexture") || + p_texture->is_class("AtlasTexture") || + p_texture->is_class("CameraTexture") || + p_texture->is_class("CanvasTexture") || + p_texture->is_class("MeshTexture") || + p_texture->is_class("Texture2DRD") || + p_texture->is_class("ViewportTexture")) { + WARN_PRINT(vformat("%s cannot be used as a PointLight2D texture (%s). As a workaround, assign the value returned by %s's `get_image()` instead.", p_texture->get_class(), get_path(), p_texture->get_class())); + } +#endif + RS::get_singleton()->canvas_light_set_texture(_get_light(), texture->get_rid()); } else { RS::get_singleton()->canvas_light_set_texture(_get_light(), RID()); @@ -462,7 +475,8 @@ void PointLight2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &PointLight2D::set_texture_scale); ClassDB::bind_method(D_METHOD("get_texture_scale"), &PointLight2D::get_texture_scale); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); + // Only allow texture types that display correctly. + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture"), "set_texture", "get_texture"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), "set_texture_scale", "get_texture_scale"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1024,1,or_greater,suffix:px"), "set_height", "get_height"); diff --git a/scene/3d/decal.cpp b/scene/3d/decal.cpp index 8702b1d3da0d..9e3fcbc38242 100644 --- a/scene/3d/decal.cpp +++ b/scene/3d/decal.cpp @@ -44,6 +44,20 @@ void Decal::set_texture(DecalTexture p_type, const Ref &p_texture) { ERR_FAIL_INDEX(p_type, TEXTURE_MAX); textures[p_type] = p_texture; RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); + +#ifdef DEBUG_ENABLED + if ( + p_texture->is_class("AnimatedTexture") || + p_texture->is_class("AtlasTexture") || + p_texture->is_class("CameraTexture") || + p_texture->is_class("CanvasTexture") || + p_texture->is_class("MeshTexture") || + p_texture->is_class("Texture2DRD") || + p_texture->is_class("ViewportTexture")) { + WARN_PRINT(vformat("%s cannot be used as a Decal texture (%s). As a workaround, assign the value returned by %s's `get_image()` instead.", p_texture->get_class(), get_path(), p_texture->get_class())); + } +#endif + RS::get_singleton()->decal_set_texture(decal, RS::DecalTexture(p_type), texture_rid); update_configuration_warnings(); } @@ -225,10 +239,12 @@ void Decal::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_RANGE, "0,1024,0.001,or_greater,suffix:m"), "set_size", "get_size"); ADD_GROUP("Textures", "texture_"); - ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_albedo", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_ALBEDO); - ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_NORMAL); - ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_orm", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_ORM); - ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_emission", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_EMISSION); + // Only allow texture types that display correctly. + const String texture_hint = "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture"; + ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_albedo", PROPERTY_HINT_RESOURCE_TYPE, texture_hint), "set_texture", "get_texture", TEXTURE_ALBEDO); + ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, texture_hint), "set_texture", "get_texture", TEXTURE_NORMAL); + ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_orm", PROPERTY_HINT_RESOURCE_TYPE, texture_hint), "set_texture", "get_texture", TEXTURE_ORM); + ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "texture_emission", PROPERTY_HINT_RESOURCE_TYPE, texture_hint), "set_texture", "get_texture", TEXTURE_EMISSION); ADD_GROUP("Parameters", ""); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_emission_energy", "get_emission_energy"); diff --git a/scene/3d/light_3d.cpp b/scene/3d/light_3d.cpp index 2d18e62b1029..9b40c254725b 100644 --- a/scene/3d/light_3d.cpp +++ b/scene/3d/light_3d.cpp @@ -28,10 +28,10 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ -#include "core/config/project_settings.h" - #include "light_3d.h" +#include "core/config/project_settings.h" + void Light3D::set_param(Param p_param, real_t p_value) { ERR_FAIL_INDEX(p_param, PARAM_MAX); param[p_param] = p_value; @@ -200,6 +200,20 @@ Light3D::BakeMode Light3D::get_bake_mode() const { void Light3D::set_projector(const Ref &p_texture) { projector = p_texture; RID tex_id = projector.is_valid() ? projector->get_rid() : RID(); + +#ifdef DEBUG_ENABLED + if ( + p_texture->is_class("AnimatedTexture") || + p_texture->is_class("AtlasTexture") || + p_texture->is_class("CameraTexture") || + p_texture->is_class("CanvasTexture") || + p_texture->is_class("MeshTexture") || + p_texture->is_class("Texture2DRD") || + p_texture->is_class("ViewportTexture")) { + WARN_PRINT(vformat("%s cannot be used as a Light3D projector texture (%s). As a workaround, assign the value returned by %s's `get_image()` instead.", p_texture->get_class(), get_path(), p_texture->get_class())); + } +#endif + RS::get_singleton()->light_set_projector(light, tex_id); update_configuration_warnings(); } @@ -384,7 +398,8 @@ void Light3D::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_energy", PROPERTY_HINT_RANGE, "0,16,0.001,or_greater"), "set_param", "get_param", PARAM_ENERGY); ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_indirect_energy", PROPERTY_HINT_RANGE, "0,16,0.001,or_greater"), "set_param", "get_param", PARAM_INDIRECT_ENERGY); ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_volumetric_fog_energy", PROPERTY_HINT_RANGE, "0,16,0.001,or_greater"), "set_param", "get_param", PARAM_VOLUMETRIC_FOG_ENERGY); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_projector", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_projector", "get_projector"); + // Only allow texture types that display correctly. + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_projector", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D,-AnimatedTexture,-AtlasTexture,-CameraTexture,-CanvasTexture,-MeshTexture,-Texture2DRD,-ViewportTexture"), "set_projector", "get_projector"); ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_size", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_param", "get_param", PARAM_SIZE); ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_angular_distance", PROPERTY_HINT_RANGE, "0,90,0.01,degrees"), "set_param", "get_param", PARAM_SIZE); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "light_negative"), "set_negative", "is_negative");