material_editor_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**************************************************************************/
  2. /* material_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "material_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/settings/editor_settings.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "scene/3d/camera_3d.h"
  38. #include "scene/3d/light_3d.h"
  39. #include "scene/3d/mesh_instance_3d.h"
  40. #include "scene/gui/box_container.h"
  41. #include "scene/gui/button.h"
  42. #include "scene/gui/color_rect.h"
  43. #include "scene/gui/label.h"
  44. #include "scene/gui/subviewport_container.h"
  45. #include "scene/main/viewport.h"
  46. #include "scene/resources/3d/fog_material.h"
  47. #include "scene/resources/3d/sky_material.h"
  48. #include "scene/resources/canvas_item_material.h"
  49. #include "scene/resources/particle_process_material.h"
  50. void MaterialEditor::gui_input(const Ref<InputEvent> &p_event) {
  51. ERR_FAIL_COND(p_event.is_null());
  52. Ref<InputEventMouseMotion> mm = p_event;
  53. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  54. rot.x -= mm->get_relative().y * 0.01;
  55. rot.y -= mm->get_relative().x * 0.01;
  56. if (quad_instance->is_visible()) {
  57. // Clamp rotation so the quad is always visible.
  58. const real_t limit = Math::deg_to_rad(80.0);
  59. rot = rot.clampf(-limit, limit);
  60. } else {
  61. rot.x = CLAMP(rot.x, -Math::PI / 2, Math::PI / 2);
  62. }
  63. _update_rotation();
  64. _store_rotation_metadata();
  65. }
  66. }
  67. void MaterialEditor::_update_theme_item_cache() {
  68. Control::_update_theme_item_cache();
  69. theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
  70. theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
  71. theme_cache.sphere_icon = get_editor_theme_icon(SNAME("MaterialPreviewSphere"));
  72. theme_cache.box_icon = get_editor_theme_icon(SNAME("MaterialPreviewCube"));
  73. theme_cache.quad_icon = get_editor_theme_icon(SNAME("MaterialPreviewQuad"));
  74. theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  75. }
  76. void MaterialEditor::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_THEME_CHANGED: {
  79. light_1_switch->set_button_icon(theme_cache.light_1_icon);
  80. light_2_switch->set_button_icon(theme_cache.light_2_icon);
  81. sphere_switch->set_button_icon(theme_cache.sphere_icon);
  82. box_switch->set_button_icon(theme_cache.box_icon);
  83. quad_switch->set_button_icon(theme_cache.quad_icon);
  84. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  85. } break;
  86. case NOTIFICATION_DRAW: {
  87. if (!is_unsupported_shader_mode) {
  88. Size2 size = get_size();
  89. draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
  90. }
  91. } break;
  92. }
  93. }
  94. void MaterialEditor::_set_rotation(real_t p_x_degrees, real_t p_y_degrees) {
  95. rot.x = Math::deg_to_rad(p_x_degrees);
  96. rot.y = Math::deg_to_rad(p_y_degrees);
  97. _update_rotation();
  98. }
  99. // Store the rotation so it can persist when switching between materials.
  100. void MaterialEditor::_store_rotation_metadata() {
  101. Vector2 rotation_degrees = Vector2(Math::rad_to_deg(rot.x), Math::rad_to_deg(rot.y));
  102. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_rotation", rotation_degrees);
  103. }
  104. void MaterialEditor::_update_rotation() {
  105. Transform3D t;
  106. t.basis.rotate(Vector3(0, 1, 0), -rot.y);
  107. t.basis.rotate(Vector3(1, 0, 0), -rot.x);
  108. rotation->set_transform(t);
  109. }
  110. void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
  111. material = p_material;
  112. camera->set_environment(p_env);
  113. is_unsupported_shader_mode = false;
  114. if (material.is_valid()) {
  115. Shader::Mode mode = p_material->get_shader_mode();
  116. switch (mode) {
  117. case Shader::MODE_CANVAS_ITEM:
  118. layout_error->hide();
  119. layout_3d->hide();
  120. layout_2d->show();
  121. vc->hide();
  122. rect_instance->set_material(material);
  123. break;
  124. case Shader::MODE_SPATIAL:
  125. layout_error->hide();
  126. layout_2d->hide();
  127. layout_3d->show();
  128. vc->show();
  129. sphere_instance->set_material_override(material);
  130. box_instance->set_material_override(material);
  131. quad_instance->set_material_override(material);
  132. break;
  133. default:
  134. layout_error->show();
  135. layout_2d->hide();
  136. layout_3d->hide();
  137. vc->hide();
  138. is_unsupported_shader_mode = true;
  139. break;
  140. }
  141. } else {
  142. hide();
  143. }
  144. }
  145. void MaterialEditor::_on_light_1_switch_pressed() {
  146. light1->set_visible(light_1_switch->is_pressed());
  147. }
  148. void MaterialEditor::_on_light_2_switch_pressed() {
  149. light2->set_visible(light_2_switch->is_pressed());
  150. }
  151. void MaterialEditor::_on_sphere_switch_pressed() {
  152. sphere_instance->show();
  153. box_instance->hide();
  154. quad_instance->hide();
  155. box_switch->set_pressed(false);
  156. quad_switch->set_pressed(false);
  157. _set_rotation(-15.0, 30.0);
  158. _store_rotation_metadata();
  159. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "sphere");
  160. }
  161. void MaterialEditor::_on_box_switch_pressed() {
  162. sphere_instance->hide();
  163. box_instance->show();
  164. quad_instance->hide();
  165. sphere_switch->set_pressed(false);
  166. quad_switch->set_pressed(false);
  167. _set_rotation(-15.0, 30.0);
  168. _store_rotation_metadata();
  169. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "box");
  170. }
  171. void MaterialEditor::_on_quad_switch_pressed() {
  172. sphere_instance->hide();
  173. box_instance->hide();
  174. quad_instance->show();
  175. sphere_switch->set_pressed(false);
  176. box_switch->set_pressed(false);
  177. _set_rotation(0.0, 0.0);
  178. _store_rotation_metadata();
  179. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "quad");
  180. }
  181. MaterialEditor::MaterialEditor() {
  182. // Canvas item
  183. vc_2d = memnew(SubViewportContainer);
  184. vc_2d->set_stretch(true);
  185. add_child(vc_2d);
  186. vc_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  187. viewport_2d = memnew(SubViewport);
  188. vc_2d->add_child(viewport_2d);
  189. viewport_2d->set_disable_input(true);
  190. viewport_2d->set_transparent_background(true);
  191. layout_2d = memnew(HBoxContainer);
  192. layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  193. viewport_2d->add_child(layout_2d);
  194. layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  195. rect_instance = memnew(ColorRect);
  196. layout_2d->add_child(rect_instance);
  197. rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);
  198. layout_2d->set_visible(false);
  199. layout_error = memnew(VBoxContainer);
  200. layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  201. layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  202. error_label = memnew(Label);
  203. error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  204. error_label->set_text(TTR("Preview is not available for this shader mode."));
  205. error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  206. error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  207. error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  208. layout_error->add_child(error_label);
  209. layout_error->hide();
  210. add_child(layout_error);
  211. // Spatial
  212. vc = memnew(SubViewportContainer);
  213. vc->set_stretch(true);
  214. add_child(vc);
  215. vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  216. viewport = memnew(SubViewport);
  217. Ref<World3D> world_3d;
  218. world_3d.instantiate();
  219. viewport->set_world_3d(world_3d); // Use own world.
  220. vc->add_child(viewport);
  221. viewport->set_disable_input(true);
  222. viewport->set_transparent_background(true);
  223. viewport->set_msaa_3d(Viewport::MSAA_4X);
  224. camera = memnew(Camera3D);
  225. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
  226. // Use low field of view so the sphere/box/quad is fully encompassed within the preview,
  227. // without much distortion.
  228. camera->set_perspective(20, 0.1, 10);
  229. camera->make_current();
  230. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  231. camera_attributes.instantiate();
  232. camera->set_attributes(camera_attributes);
  233. }
  234. viewport->add_child(camera);
  235. light1 = memnew(DirectionalLight3D);
  236. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  237. viewport->add_child(light1);
  238. light2 = memnew(DirectionalLight3D);
  239. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  240. light2->set_color(Color(0.7, 0.7, 0.7));
  241. viewport->add_child(light2);
  242. rotation = memnew(Node3D);
  243. viewport->add_child(rotation);
  244. sphere_instance = memnew(MeshInstance3D);
  245. rotation->add_child(sphere_instance);
  246. box_instance = memnew(MeshInstance3D);
  247. rotation->add_child(box_instance);
  248. quad_instance = memnew(MeshInstance3D);
  249. rotation->add_child(quad_instance);
  250. sphere_instance->set_transform(Transform3D() * 0.375);
  251. box_instance->set_transform(Transform3D() * 0.25);
  252. quad_instance->set_transform(Transform3D() * 0.375);
  253. sphere_mesh.instantiate();
  254. sphere_instance->set_mesh(sphere_mesh);
  255. box_mesh.instantiate();
  256. box_instance->set_mesh(box_mesh);
  257. quad_mesh.instantiate();
  258. quad_instance->set_mesh(quad_mesh);
  259. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  260. layout_3d = memnew(HBoxContainer);
  261. add_child(layout_3d);
  262. layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  263. VBoxContainer *vb_shape = memnew(VBoxContainer);
  264. layout_3d->add_child(vb_shape);
  265. sphere_switch = memnew(Button);
  266. sphere_switch->set_theme_type_variation("PreviewLightButton");
  267. sphere_switch->set_toggle_mode(true);
  268. sphere_switch->set_accessibility_name(TTRC("Sphere"));
  269. vb_shape->add_child(sphere_switch);
  270. sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));
  271. box_switch = memnew(Button);
  272. box_switch->set_theme_type_variation("PreviewLightButton");
  273. box_switch->set_toggle_mode(true);
  274. box_switch->set_accessibility_name(TTRC("Box"));
  275. vb_shape->add_child(box_switch);
  276. box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));
  277. quad_switch = memnew(Button);
  278. quad_switch->set_theme_type_variation("PreviewLightButton");
  279. quad_switch->set_toggle_mode(true);
  280. quad_switch->set_accessibility_name(TTRC("Quad"));
  281. vb_shape->add_child(quad_switch);
  282. quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));
  283. layout_3d->add_spacer();
  284. VBoxContainer *vb_light = memnew(VBoxContainer);
  285. layout_3d->add_child(vb_light);
  286. light_1_switch = memnew(Button);
  287. light_1_switch->set_theme_type_variation("PreviewLightButton");
  288. light_1_switch->set_toggle_mode(true);
  289. light_1_switch->set_pressed(true);
  290. light_1_switch->set_accessibility_name(TTRC("First Light"));
  291. vb_light->add_child(light_1_switch);
  292. light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));
  293. light_2_switch = memnew(Button);
  294. light_2_switch->set_theme_type_variation("PreviewLightButton");
  295. light_2_switch->set_toggle_mode(true);
  296. light_2_switch->set_pressed(true);
  297. light_2_switch->set_accessibility_name(TTRC("Second Light"));
  298. vb_light->add_child(light_2_switch);
  299. light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));
  300. String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");
  301. if (shape == "sphere") {
  302. box_instance->hide();
  303. quad_instance->hide();
  304. sphere_switch->set_pressed_no_signal(true);
  305. } else if (shape == "box") {
  306. sphere_instance->hide();
  307. quad_instance->hide();
  308. box_switch->set_pressed_no_signal(true);
  309. } else {
  310. sphere_instance->hide();
  311. box_instance->hide();
  312. quad_switch->set_pressed_no_signal(true);
  313. }
  314. Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());
  315. _set_rotation(stored_rot.x, stored_rot.y);
  316. }
  317. ///////////////////////
  318. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  319. Material *material = Object::cast_to<Material>(p_object);
  320. if (!material) {
  321. return false;
  322. }
  323. Shader::Mode mode = material->get_shader_mode();
  324. return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
  325. }
  326. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  327. Material *material = Object::cast_to<Material>(p_object);
  328. if (!material) {
  329. return;
  330. }
  331. Ref<Material> m(material);
  332. MaterialEditor *editor = memnew(MaterialEditor);
  333. editor->edit(m, env);
  334. add_custom_control(editor);
  335. }
  336. void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
  337. EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  338. ERR_FAIL_NULL(undo_redo);
  339. // For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
  340. // set the respective metallic or roughness factor to 1.0 as a convenience feature
  341. BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
  342. if (base_material) {
  343. Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
  344. if (texture) {
  345. if (p_property == "roughness_texture") {
  346. if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
  347. undo_redo->add_do_property(p_edited, "roughness", 1.0);
  348. bool valid = false;
  349. Variant value = p_edited->get("roughness", &valid);
  350. if (valid) {
  351. undo_redo->add_undo_property(p_edited, "roughness", value);
  352. }
  353. }
  354. } else if (p_property == "metallic_texture") {
  355. if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
  356. undo_redo->add_do_property(p_edited, "metallic", 1.0);
  357. bool valid = false;
  358. Variant value = p_edited->get("metallic", &valid);
  359. if (valid) {
  360. undo_redo->add_undo_property(p_edited, "metallic", value);
  361. }
  362. }
  363. }
  364. }
  365. }
  366. }
  367. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  368. env.instantiate();
  369. Ref<Sky> sky = memnew(Sky());
  370. env->set_sky(sky);
  371. env->set_background(Environment::BG_COLOR);
  372. env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
  373. env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
  374. EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
  375. }
  376. MaterialEditorPlugin::MaterialEditorPlugin() {
  377. Ref<EditorInspectorPluginMaterial> plugin;
  378. plugin.instantiate();
  379. add_inspector_plugin(plugin);
  380. }
  381. String StandardMaterial3DConversionPlugin::converts_to() const {
  382. return "ShaderMaterial";
  383. }
  384. bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  385. Ref<StandardMaterial3D> mat = p_resource;
  386. return mat.is_valid();
  387. }
  388. Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  389. Ref<StandardMaterial3D> mat = p_resource;
  390. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  391. Ref<ShaderMaterial> smat;
  392. smat.instantiate();
  393. Ref<Shader> shader;
  394. shader.instantiate();
  395. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  396. shader->set_code(code);
  397. smat->set_shader(shader);
  398. List<PropertyInfo> params;
  399. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  400. for (const PropertyInfo &E : params) {
  401. // Texture parameter has to be treated specially since StandardMaterial3D saved it
  402. // as RID but ShaderMaterial needs Texture itself
  403. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  404. if (texture.is_valid()) {
  405. smat->set_shader_parameter(E.name, texture);
  406. } else {
  407. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  408. smat->set_shader_parameter(E.name, value);
  409. }
  410. }
  411. smat->set_render_priority(mat->get_render_priority());
  412. smat->set_local_to_scene(mat->is_local_to_scene());
  413. smat->set_name(mat->get_name());
  414. return smat;
  415. }
  416. String ORMMaterial3DConversionPlugin::converts_to() const {
  417. return "ShaderMaterial";
  418. }
  419. bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  420. Ref<ORMMaterial3D> mat = p_resource;
  421. return mat.is_valid();
  422. }
  423. Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  424. Ref<ORMMaterial3D> mat = p_resource;
  425. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  426. Ref<ShaderMaterial> smat;
  427. smat.instantiate();
  428. Ref<Shader> shader;
  429. shader.instantiate();
  430. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  431. shader->set_code(code);
  432. smat->set_shader(shader);
  433. List<PropertyInfo> params;
  434. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  435. for (const PropertyInfo &E : params) {
  436. // Texture parameter has to be treated specially since ORMMaterial3D saved it
  437. // as RID but ShaderMaterial needs Texture itself
  438. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  439. if (texture.is_valid()) {
  440. smat->set_shader_parameter(E.name, texture);
  441. } else {
  442. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  443. smat->set_shader_parameter(E.name, value);
  444. }
  445. }
  446. smat->set_render_priority(mat->get_render_priority());
  447. smat->set_local_to_scene(mat->is_local_to_scene());
  448. smat->set_name(mat->get_name());
  449. return smat;
  450. }
  451. String ParticleProcessMaterialConversionPlugin::converts_to() const {
  452. return "ShaderMaterial";
  453. }
  454. bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  455. Ref<ParticleProcessMaterial> mat = p_resource;
  456. return mat.is_valid();
  457. }
  458. Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  459. Ref<ParticleProcessMaterial> mat = p_resource;
  460. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  461. Ref<ShaderMaterial> smat;
  462. smat.instantiate();
  463. Ref<Shader> shader;
  464. shader.instantiate();
  465. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  466. shader->set_code(code);
  467. smat->set_shader(shader);
  468. List<PropertyInfo> params;
  469. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  470. for (const PropertyInfo &E : params) {
  471. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  472. smat->set_shader_parameter(E.name, value);
  473. }
  474. smat->set_render_priority(mat->get_render_priority());
  475. smat->set_local_to_scene(mat->is_local_to_scene());
  476. smat->set_name(mat->get_name());
  477. return smat;
  478. }
  479. String CanvasItemMaterialConversionPlugin::converts_to() const {
  480. return "ShaderMaterial";
  481. }
  482. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  483. Ref<CanvasItemMaterial> mat = p_resource;
  484. return mat.is_valid();
  485. }
  486. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  487. Ref<CanvasItemMaterial> mat = p_resource;
  488. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  489. Ref<ShaderMaterial> smat;
  490. smat.instantiate();
  491. Ref<Shader> shader;
  492. shader.instantiate();
  493. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  494. shader->set_code(code);
  495. smat->set_shader(shader);
  496. List<PropertyInfo> params;
  497. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  498. for (const PropertyInfo &E : params) {
  499. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  500. smat->set_shader_parameter(E.name, value);
  501. }
  502. smat->set_render_priority(mat->get_render_priority());
  503. smat->set_local_to_scene(mat->is_local_to_scene());
  504. smat->set_name(mat->get_name());
  505. return smat;
  506. }
  507. String ProceduralSkyMaterialConversionPlugin::converts_to() const {
  508. return "ShaderMaterial";
  509. }
  510. bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  511. Ref<ProceduralSkyMaterial> mat = p_resource;
  512. return mat.is_valid();
  513. }
  514. Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  515. Ref<ProceduralSkyMaterial> mat = p_resource;
  516. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  517. Ref<ShaderMaterial> smat;
  518. smat.instantiate();
  519. Ref<Shader> shader;
  520. shader.instantiate();
  521. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  522. shader->set_code(code);
  523. smat->set_shader(shader);
  524. List<PropertyInfo> params;
  525. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  526. for (const PropertyInfo &E : params) {
  527. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  528. smat->set_shader_parameter(E.name, value);
  529. }
  530. smat->set_render_priority(mat->get_render_priority());
  531. smat->set_local_to_scene(mat->is_local_to_scene());
  532. smat->set_name(mat->get_name());
  533. return smat;
  534. }
  535. String PanoramaSkyMaterialConversionPlugin::converts_to() const {
  536. return "ShaderMaterial";
  537. }
  538. bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  539. Ref<PanoramaSkyMaterial> mat = p_resource;
  540. return mat.is_valid();
  541. }
  542. Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  543. Ref<PanoramaSkyMaterial> mat = p_resource;
  544. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  545. Ref<ShaderMaterial> smat;
  546. smat.instantiate();
  547. Ref<Shader> shader;
  548. shader.instantiate();
  549. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  550. shader->set_code(code);
  551. smat->set_shader(shader);
  552. List<PropertyInfo> params;
  553. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  554. for (const PropertyInfo &E : params) {
  555. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  556. smat->set_shader_parameter(E.name, value);
  557. }
  558. smat->set_render_priority(mat->get_render_priority());
  559. smat->set_local_to_scene(mat->is_local_to_scene());
  560. smat->set_name(mat->get_name());
  561. return smat;
  562. }
  563. String PhysicalSkyMaterialConversionPlugin::converts_to() const {
  564. return "ShaderMaterial";
  565. }
  566. bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  567. Ref<PhysicalSkyMaterial> mat = p_resource;
  568. return mat.is_valid();
  569. }
  570. Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  571. Ref<PhysicalSkyMaterial> mat = p_resource;
  572. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  573. Ref<ShaderMaterial> smat;
  574. smat.instantiate();
  575. Ref<Shader> shader;
  576. shader.instantiate();
  577. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  578. shader->set_code(code);
  579. smat->set_shader(shader);
  580. List<PropertyInfo> params;
  581. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  582. for (const PropertyInfo &E : params) {
  583. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  584. smat->set_shader_parameter(E.name, value);
  585. }
  586. smat->set_render_priority(mat->get_render_priority());
  587. smat->set_local_to_scene(mat->is_local_to_scene());
  588. smat->set_name(mat->get_name());
  589. return smat;
  590. }
  591. String FogMaterialConversionPlugin::converts_to() const {
  592. return "ShaderMaterial";
  593. }
  594. bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  595. Ref<FogMaterial> mat = p_resource;
  596. return mat.is_valid();
  597. }
  598. Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  599. Ref<FogMaterial> mat = p_resource;
  600. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  601. Ref<ShaderMaterial> smat;
  602. smat.instantiate();
  603. Ref<Shader> shader;
  604. shader.instantiate();
  605. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  606. shader->set_code(code);
  607. smat->set_shader(shader);
  608. List<PropertyInfo> params;
  609. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  610. for (const PropertyInfo &E : params) {
  611. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  612. smat->set_shader_parameter(E.name, value);
  613. }
  614. smat->set_render_priority(mat->get_render_priority());
  615. return smat;
  616. }