material_editor_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.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_text(TTR("Preview is not available for this shader mode."));
  204. error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  205. error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  206. error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  207. layout_error->add_child(error_label);
  208. layout_error->hide();
  209. add_child(layout_error);
  210. // Spatial
  211. vc = memnew(SubViewportContainer);
  212. vc->set_stretch(true);
  213. add_child(vc);
  214. vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  215. viewport = memnew(SubViewport);
  216. Ref<World3D> world_3d;
  217. world_3d.instantiate();
  218. viewport->set_world_3d(world_3d); // Use own world.
  219. vc->add_child(viewport);
  220. viewport->set_disable_input(true);
  221. viewport->set_transparent_background(true);
  222. viewport->set_msaa_3d(Viewport::MSAA_4X);
  223. camera = memnew(Camera3D);
  224. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
  225. // Use low field of view so the sphere/box/quad is fully encompassed within the preview,
  226. // without much distortion.
  227. camera->set_perspective(20, 0.1, 10);
  228. camera->make_current();
  229. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  230. camera_attributes.instantiate();
  231. camera->set_attributes(camera_attributes);
  232. }
  233. viewport->add_child(camera);
  234. light1 = memnew(DirectionalLight3D);
  235. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  236. viewport->add_child(light1);
  237. light2 = memnew(DirectionalLight3D);
  238. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  239. light2->set_color(Color(0.7, 0.7, 0.7));
  240. viewport->add_child(light2);
  241. rotation = memnew(Node3D);
  242. viewport->add_child(rotation);
  243. sphere_instance = memnew(MeshInstance3D);
  244. rotation->add_child(sphere_instance);
  245. box_instance = memnew(MeshInstance3D);
  246. rotation->add_child(box_instance);
  247. quad_instance = memnew(MeshInstance3D);
  248. rotation->add_child(quad_instance);
  249. sphere_instance->set_transform(Transform3D() * 0.375);
  250. box_instance->set_transform(Transform3D() * 0.25);
  251. quad_instance->set_transform(Transform3D() * 0.375);
  252. sphere_mesh.instantiate();
  253. sphere_instance->set_mesh(sphere_mesh);
  254. box_mesh.instantiate();
  255. box_instance->set_mesh(box_mesh);
  256. quad_mesh.instantiate();
  257. quad_instance->set_mesh(quad_mesh);
  258. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  259. layout_3d = memnew(HBoxContainer);
  260. add_child(layout_3d);
  261. layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  262. VBoxContainer *vb_shape = memnew(VBoxContainer);
  263. layout_3d->add_child(vb_shape);
  264. sphere_switch = memnew(Button);
  265. sphere_switch->set_theme_type_variation("PreviewLightButton");
  266. sphere_switch->set_toggle_mode(true);
  267. vb_shape->add_child(sphere_switch);
  268. sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));
  269. box_switch = memnew(Button);
  270. box_switch->set_theme_type_variation("PreviewLightButton");
  271. box_switch->set_toggle_mode(true);
  272. vb_shape->add_child(box_switch);
  273. box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));
  274. quad_switch = memnew(Button);
  275. quad_switch->set_theme_type_variation("PreviewLightButton");
  276. quad_switch->set_toggle_mode(true);
  277. vb_shape->add_child(quad_switch);
  278. quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));
  279. layout_3d->add_spacer();
  280. VBoxContainer *vb_light = memnew(VBoxContainer);
  281. layout_3d->add_child(vb_light);
  282. light_1_switch = memnew(Button);
  283. light_1_switch->set_theme_type_variation("PreviewLightButton");
  284. light_1_switch->set_toggle_mode(true);
  285. light_1_switch->set_pressed(true);
  286. vb_light->add_child(light_1_switch);
  287. light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));
  288. light_2_switch = memnew(Button);
  289. light_2_switch->set_theme_type_variation("PreviewLightButton");
  290. light_2_switch->set_toggle_mode(true);
  291. light_2_switch->set_pressed(true);
  292. vb_light->add_child(light_2_switch);
  293. light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));
  294. String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");
  295. if (shape == "sphere") {
  296. box_instance->hide();
  297. quad_instance->hide();
  298. sphere_switch->set_pressed_no_signal(true);
  299. } else if (shape == "box") {
  300. sphere_instance->hide();
  301. quad_instance->hide();
  302. box_switch->set_pressed_no_signal(true);
  303. } else {
  304. sphere_instance->hide();
  305. box_instance->hide();
  306. quad_switch->set_pressed_no_signal(true);
  307. }
  308. Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());
  309. _set_rotation(stored_rot.x, stored_rot.y);
  310. }
  311. ///////////////////////
  312. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  313. Material *material = Object::cast_to<Material>(p_object);
  314. if (!material) {
  315. return false;
  316. }
  317. Shader::Mode mode = material->get_shader_mode();
  318. return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
  319. }
  320. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  321. Material *material = Object::cast_to<Material>(p_object);
  322. if (!material) {
  323. return;
  324. }
  325. Ref<Material> m(material);
  326. MaterialEditor *editor = memnew(MaterialEditor);
  327. editor->edit(m, env);
  328. add_custom_control(editor);
  329. }
  330. void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
  331. EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  332. ERR_FAIL_NULL(undo_redo);
  333. // For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
  334. // set the respective metallic or roughness factor to 1.0 as a convenience feature
  335. BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
  336. if (base_material) {
  337. Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
  338. if (texture) {
  339. if (p_property == "roughness_texture") {
  340. if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
  341. undo_redo->add_do_property(p_edited, "roughness", 1.0);
  342. bool valid = false;
  343. Variant value = p_edited->get("roughness", &valid);
  344. if (valid) {
  345. undo_redo->add_undo_property(p_edited, "roughness", value);
  346. }
  347. }
  348. } else if (p_property == "metallic_texture") {
  349. if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
  350. undo_redo->add_do_property(p_edited, "metallic", 1.0);
  351. bool valid = false;
  352. Variant value = p_edited->get("metallic", &valid);
  353. if (valid) {
  354. undo_redo->add_undo_property(p_edited, "metallic", value);
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  362. env.instantiate();
  363. Ref<Sky> sky = memnew(Sky());
  364. env->set_sky(sky);
  365. env->set_background(Environment::BG_COLOR);
  366. env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
  367. env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
  368. EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
  369. }
  370. MaterialEditorPlugin::MaterialEditorPlugin() {
  371. Ref<EditorInspectorPluginMaterial> plugin;
  372. plugin.instantiate();
  373. add_inspector_plugin(plugin);
  374. }
  375. String StandardMaterial3DConversionPlugin::converts_to() const {
  376. return "ShaderMaterial";
  377. }
  378. bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  379. Ref<StandardMaterial3D> mat = p_resource;
  380. return mat.is_valid();
  381. }
  382. Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  383. Ref<StandardMaterial3D> mat = p_resource;
  384. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  385. Ref<ShaderMaterial> smat;
  386. smat.instantiate();
  387. Ref<Shader> shader;
  388. shader.instantiate();
  389. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  390. shader->set_code(code);
  391. smat->set_shader(shader);
  392. List<PropertyInfo> params;
  393. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  394. for (const PropertyInfo &E : params) {
  395. // Texture parameter has to be treated specially since StandardMaterial3D saved it
  396. // as RID but ShaderMaterial needs Texture itself
  397. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  398. if (texture.is_valid()) {
  399. smat->set_shader_parameter(E.name, texture);
  400. } else {
  401. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  402. smat->set_shader_parameter(E.name, value);
  403. }
  404. }
  405. smat->set_render_priority(mat->get_render_priority());
  406. smat->set_local_to_scene(mat->is_local_to_scene());
  407. smat->set_name(mat->get_name());
  408. return smat;
  409. }
  410. String ORMMaterial3DConversionPlugin::converts_to() const {
  411. return "ShaderMaterial";
  412. }
  413. bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  414. Ref<ORMMaterial3D> mat = p_resource;
  415. return mat.is_valid();
  416. }
  417. Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  418. Ref<ORMMaterial3D> mat = p_resource;
  419. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  420. Ref<ShaderMaterial> smat;
  421. smat.instantiate();
  422. Ref<Shader> shader;
  423. shader.instantiate();
  424. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  425. shader->set_code(code);
  426. smat->set_shader(shader);
  427. List<PropertyInfo> params;
  428. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  429. for (const PropertyInfo &E : params) {
  430. // Texture parameter has to be treated specially since ORMMaterial3D saved it
  431. // as RID but ShaderMaterial needs Texture itself
  432. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  433. if (texture.is_valid()) {
  434. smat->set_shader_parameter(E.name, texture);
  435. } else {
  436. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  437. smat->set_shader_parameter(E.name, value);
  438. }
  439. }
  440. smat->set_render_priority(mat->get_render_priority());
  441. smat->set_local_to_scene(mat->is_local_to_scene());
  442. smat->set_name(mat->get_name());
  443. return smat;
  444. }
  445. String ParticleProcessMaterialConversionPlugin::converts_to() const {
  446. return "ShaderMaterial";
  447. }
  448. bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  449. Ref<ParticleProcessMaterial> mat = p_resource;
  450. return mat.is_valid();
  451. }
  452. Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  453. Ref<ParticleProcessMaterial> mat = p_resource;
  454. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  455. Ref<ShaderMaterial> smat;
  456. smat.instantiate();
  457. Ref<Shader> shader;
  458. shader.instantiate();
  459. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  460. shader->set_code(code);
  461. smat->set_shader(shader);
  462. List<PropertyInfo> params;
  463. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  464. for (const PropertyInfo &E : params) {
  465. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  466. smat->set_shader_parameter(E.name, value);
  467. }
  468. smat->set_render_priority(mat->get_render_priority());
  469. smat->set_local_to_scene(mat->is_local_to_scene());
  470. smat->set_name(mat->get_name());
  471. return smat;
  472. }
  473. String CanvasItemMaterialConversionPlugin::converts_to() const {
  474. return "ShaderMaterial";
  475. }
  476. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  477. Ref<CanvasItemMaterial> mat = p_resource;
  478. return mat.is_valid();
  479. }
  480. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  481. Ref<CanvasItemMaterial> mat = p_resource;
  482. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  483. Ref<ShaderMaterial> smat;
  484. smat.instantiate();
  485. Ref<Shader> shader;
  486. shader.instantiate();
  487. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  488. shader->set_code(code);
  489. smat->set_shader(shader);
  490. List<PropertyInfo> params;
  491. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  492. for (const PropertyInfo &E : params) {
  493. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  494. smat->set_shader_parameter(E.name, value);
  495. }
  496. smat->set_render_priority(mat->get_render_priority());
  497. smat->set_local_to_scene(mat->is_local_to_scene());
  498. smat->set_name(mat->get_name());
  499. return smat;
  500. }
  501. String ProceduralSkyMaterialConversionPlugin::converts_to() const {
  502. return "ShaderMaterial";
  503. }
  504. bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  505. Ref<ProceduralSkyMaterial> mat = p_resource;
  506. return mat.is_valid();
  507. }
  508. Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  509. Ref<ProceduralSkyMaterial> mat = p_resource;
  510. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  511. Ref<ShaderMaterial> smat;
  512. smat.instantiate();
  513. Ref<Shader> shader;
  514. shader.instantiate();
  515. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  516. shader->set_code(code);
  517. smat->set_shader(shader);
  518. List<PropertyInfo> params;
  519. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  520. for (const PropertyInfo &E : params) {
  521. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  522. smat->set_shader_parameter(E.name, value);
  523. }
  524. smat->set_render_priority(mat->get_render_priority());
  525. smat->set_local_to_scene(mat->is_local_to_scene());
  526. smat->set_name(mat->get_name());
  527. return smat;
  528. }
  529. String PanoramaSkyMaterialConversionPlugin::converts_to() const {
  530. return "ShaderMaterial";
  531. }
  532. bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  533. Ref<PanoramaSkyMaterial> mat = p_resource;
  534. return mat.is_valid();
  535. }
  536. Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  537. Ref<PanoramaSkyMaterial> mat = p_resource;
  538. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  539. Ref<ShaderMaterial> smat;
  540. smat.instantiate();
  541. Ref<Shader> shader;
  542. shader.instantiate();
  543. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  544. shader->set_code(code);
  545. smat->set_shader(shader);
  546. List<PropertyInfo> params;
  547. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  548. for (const PropertyInfo &E : params) {
  549. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  550. smat->set_shader_parameter(E.name, value);
  551. }
  552. smat->set_render_priority(mat->get_render_priority());
  553. smat->set_local_to_scene(mat->is_local_to_scene());
  554. smat->set_name(mat->get_name());
  555. return smat;
  556. }
  557. String PhysicalSkyMaterialConversionPlugin::converts_to() const {
  558. return "ShaderMaterial";
  559. }
  560. bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  561. Ref<PhysicalSkyMaterial> mat = p_resource;
  562. return mat.is_valid();
  563. }
  564. Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  565. Ref<PhysicalSkyMaterial> mat = p_resource;
  566. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  567. Ref<ShaderMaterial> smat;
  568. smat.instantiate();
  569. Ref<Shader> shader;
  570. shader.instantiate();
  571. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  572. shader->set_code(code);
  573. smat->set_shader(shader);
  574. List<PropertyInfo> params;
  575. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  576. for (const PropertyInfo &E : params) {
  577. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  578. smat->set_shader_parameter(E.name, value);
  579. }
  580. smat->set_render_priority(mat->get_render_priority());
  581. smat->set_local_to_scene(mat->is_local_to_scene());
  582. smat->set_name(mat->get_name());
  583. return smat;
  584. }
  585. String FogMaterialConversionPlugin::converts_to() const {
  586. return "ShaderMaterial";
  587. }
  588. bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  589. Ref<FogMaterial> mat = p_resource;
  590. return mat.is_valid();
  591. }
  592. Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  593. Ref<FogMaterial> mat = p_resource;
  594. ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
  595. Ref<ShaderMaterial> smat;
  596. smat.instantiate();
  597. Ref<Shader> shader;
  598. shader.instantiate();
  599. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  600. shader->set_code(code);
  601. smat->set_shader(shader);
  602. List<PropertyInfo> params;
  603. RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
  604. for (const PropertyInfo &E : params) {
  605. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  606. smat->set_shader_parameter(E.name, value);
  607. }
  608. smat->set_render_priority(mat->get_render_priority());
  609. return smat;
  610. }