Skip to content

Commit 2753d33

Browse files
committed
Merge pull request #109375 from ColinSORourke/VisualShaderEmbedHandling
Fix VisualShader Conversion failing with subresources
2 parents 3632e6f + c4559c0 commit 2753d33

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

editor/docks/filesystem_dock.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,8 @@ void FileSystemDock::_convert_dialog_action() {
19251925
Ref<Resource> original_resource = selected_resources.get(i);
19261926
Ref<Resource> new_resource = converted_resources.get(i);
19271927

1928+
// Notify plugins that the original resource is removed.
1929+
emit_signal(SNAME("file_removed"), original_resource->get_path());
19281930
// Overwrite the path.
19291931
new_resource->set_path(original_resource->get_path(), true);
19301932

editor/inspector/editor_resource_picker.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,11 @@ void EditorResourcePicker::_edit_menu_cbk(int p_which) {
484484
Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(edited_resource);
485485
ERR_FAIL_INDEX(to_type, conversions.size());
486486

487-
edited_resource = conversions[to_type]->convert(edited_resource);
488-
_resource_changed();
487+
Ref<Resource> converted_resource = conversions[to_type]->convert(edited_resource);
488+
if (converted_resource.is_valid()) {
489+
edited_resource = converted_resource;
490+
_resource_changed();
491+
}
489492
break;
490493
}
491494

editor/shader/visual_shader_editor_plugin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "editor/editor_string_names.h"
4242
#include "editor/editor_undo_redo_manager.h"
4343
#include "editor/file_system/editor_paths.h"
44+
#include "editor/gui/editor_toaster.h"
4445
#include "editor/inspector/editor_properties.h"
4546
#include "editor/inspector/editor_properties_vector.h"
4647
#include "editor/scene/curve_editor_plugin.h"
@@ -8446,6 +8447,20 @@ bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) cons
84468447
Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
84478448
Ref<VisualShader> vshader = p_resource;
84488449
ERR_FAIL_COND_V(vshader.is_null(), Ref<Resource>());
8450+
int embed = vshader->has_node_embeds();
8451+
8452+
EditorToaster *toast = EditorToaster::get_singleton();
8453+
if (toast == nullptr) {
8454+
ERR_FAIL_COND_V_MSG(embed == 2, Ref<Resource>(), "Cannot convert VisualShader to GDShader because VisualShader has embedded subresources.");
8455+
if (embed == 1) {
8456+
WARN_PRINT("Visual Shader conversion cannot convert external dependencies. Resource references from Nodes will have to be rebound as ShaderParameters on a Material.");
8457+
}
8458+
} else if (embed == 2) {
8459+
toast->popup_str(TTR("Cannot convert VisualShader to GDShader because VisualShader has embedded subresources."), EditorToaster::SEVERITY_ERROR);
8460+
return Ref<Resource>();
8461+
} else if (embed == 1) {
8462+
toast->popup_str(TTR("Visual Shader conversion cannot convert external dependencies. Resource references from Nodes will have to be rebound as ShaderParameters on a Material."), EditorToaster::SEVERITY_WARNING);
8463+
}
84498464

84508465
Ref<Shader> shader;
84518466
shader.instantiate();

scene/resources/visual_shader.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,37 @@ void VisualShader::set_node_position(Type p_type, int p_id, const Vector2 &p_pos
967967
g->nodes[p_id].position = p_position;
968968
}
969969

970+
// Returns 0 if no embeds, 1 if external embeds, 2 if builtin embeds
971+
int VisualShader::has_node_embeds() const {
972+
bool external_embeds = false;
973+
for (int i = 0; i < TYPE_MAX; i++) {
974+
for (const KeyValue<int, Node> &E : graph[i].nodes) {
975+
List<PropertyInfo> props;
976+
E.value.node->get_property_list(&props);
977+
// For classes that inherit from VisualShaderNode, the class properties start at the 12th, and the last value is always 'script'
978+
for (int j = 12; j < props.size() - 1; j++) {
979+
// VisualShaderNodeCustom cannot have embeds
980+
if (props.get(j).name == "VisualShaderNodeCustom") {
981+
break;
982+
}
983+
// Ref<Resource> properties get classed as type Variant::Object
984+
if (props.get(j).type == Variant::OBJECT) {
985+
Ref<Resource> res = E.value.node->get(props.get(j).name);
986+
if (res.is_valid()) {
987+
if (res->is_built_in()) {
988+
return 2;
989+
} else {
990+
external_embeds = true;
991+
}
992+
}
993+
}
994+
}
995+
}
996+
}
997+
998+
return external_embeds;
999+
}
1000+
9701001
Vector2 VisualShader::get_node_position(Type p_type, int p_id) const {
9711002
ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector2());
9721003
const Graph *g = &graph[p_type];

scene/resources/visual_shader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class VisualShader : public Shader {
183183

184184
void add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id);
185185
void set_node_position(Type p_type, int p_id, const Vector2 &p_position);
186+
int has_node_embeds() const;
186187

187188
void add_varying(const String &p_name, VaryingMode p_mode, VaryingType p_type);
188189
void remove_varying(const String &p_name);

0 commit comments

Comments
 (0)