material_editor_plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*************************************************************************/
  2. /* material_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "scene/gui/subviewport_container.h"
  37. #include "scene/resources/fog_material.h"
  38. #include "scene/resources/particle_process_material.h"
  39. #include "scene/resources/sky_material.h"
  40. void MaterialEditor::_update_theme_item_cache() {
  41. Control::_update_theme_item_cache();
  42. theme_cache.light_1_on = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons"));
  43. theme_cache.light_1_off = get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons"));
  44. theme_cache.light_2_on = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons"));
  45. theme_cache.light_2_off = get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons"));
  46. theme_cache.sphere_on = get_theme_icon(SNAME("MaterialPreviewSphere"), SNAME("EditorIcons"));
  47. theme_cache.sphere_off = get_theme_icon(SNAME("MaterialPreviewSphereOff"), SNAME("EditorIcons"));
  48. theme_cache.box_on = get_theme_icon(SNAME("MaterialPreviewCube"), SNAME("EditorIcons"));
  49. theme_cache.box_off = get_theme_icon(SNAME("MaterialPreviewCubeOff"), SNAME("EditorIcons"));
  50. theme_cache.checkerboard = get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons"));
  51. }
  52. void MaterialEditor::_notification(int p_what) {
  53. switch (p_what) {
  54. case NOTIFICATION_THEME_CHANGED: {
  55. light_1_switch->set_normal_texture(theme_cache.light_1_on);
  56. light_1_switch->set_pressed_texture(theme_cache.light_1_off);
  57. light_2_switch->set_normal_texture(theme_cache.light_2_on);
  58. light_2_switch->set_pressed_texture(theme_cache.light_2_off);
  59. sphere_switch->set_normal_texture(theme_cache.sphere_off);
  60. sphere_switch->set_pressed_texture(theme_cache.sphere_on);
  61. box_switch->set_normal_texture(theme_cache.box_off);
  62. box_switch->set_pressed_texture(theme_cache.box_on);
  63. } break;
  64. case NOTIFICATION_DRAW: {
  65. Size2 size = get_size();
  66. draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
  67. } break;
  68. }
  69. }
  70. void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
  71. material = p_material;
  72. camera->set_environment(p_env);
  73. if (!material.is_null()) {
  74. Shader::Mode mode = p_material->get_shader_mode();
  75. switch (mode) {
  76. case Shader::MODE_CANVAS_ITEM:
  77. layout_3d->hide();
  78. layout_2d->show();
  79. vc->hide();
  80. rect_instance->set_material(material);
  81. break;
  82. case Shader::MODE_SPATIAL:
  83. layout_2d->hide();
  84. layout_3d->show();
  85. vc->show();
  86. sphere_instance->set_material_override(material);
  87. box_instance->set_material_override(material);
  88. break;
  89. default:
  90. break;
  91. }
  92. } else {
  93. hide();
  94. }
  95. }
  96. void MaterialEditor::_button_pressed(Node *p_button) {
  97. if (p_button == light_1_switch) {
  98. light1->set_visible(!light_1_switch->is_pressed());
  99. }
  100. if (p_button == light_2_switch) {
  101. light2->set_visible(!light_2_switch->is_pressed());
  102. }
  103. if (p_button == box_switch) {
  104. box_instance->show();
  105. sphere_instance->hide();
  106. box_switch->set_pressed(true);
  107. sphere_switch->set_pressed(false);
  108. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false);
  109. }
  110. if (p_button == sphere_switch) {
  111. box_instance->hide();
  112. sphere_instance->show();
  113. box_switch->set_pressed(false);
  114. sphere_switch->set_pressed(true);
  115. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true);
  116. }
  117. }
  118. void MaterialEditor::_bind_methods() {
  119. }
  120. MaterialEditor::MaterialEditor() {
  121. // canvas item
  122. layout_2d = memnew(HBoxContainer);
  123. layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  124. add_child(layout_2d);
  125. layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  126. rect_instance = memnew(ColorRect);
  127. layout_2d->add_child(rect_instance);
  128. rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);
  129. layout_2d->set_visible(false);
  130. // spatial
  131. vc = memnew(SubViewportContainer);
  132. vc->set_stretch(true);
  133. add_child(vc);
  134. vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  135. viewport = memnew(SubViewport);
  136. Ref<World3D> world_3d;
  137. world_3d.instantiate();
  138. viewport->set_world_3d(world_3d); //use own world
  139. vc->add_child(viewport);
  140. viewport->set_disable_input(true);
  141. viewport->set_transparent_background(true);
  142. viewport->set_msaa_3d(Viewport::MSAA_4X);
  143. camera = memnew(Camera3D);
  144. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 3)));
  145. // Use low field of view so the sphere/box is fully encompassed within the preview,
  146. // without much distortion.
  147. camera->set_perspective(20, 0.1, 10);
  148. camera->make_current();
  149. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  150. camera_attributes.instantiate();
  151. camera->set_attributes(camera_attributes);
  152. }
  153. viewport->add_child(camera);
  154. light1 = memnew(DirectionalLight3D);
  155. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  156. viewport->add_child(light1);
  157. light2 = memnew(DirectionalLight3D);
  158. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  159. light2->set_color(Color(0.7, 0.7, 0.7));
  160. viewport->add_child(light2);
  161. sphere_instance = memnew(MeshInstance3D);
  162. viewport->add_child(sphere_instance);
  163. box_instance = memnew(MeshInstance3D);
  164. viewport->add_child(box_instance);
  165. Transform3D box_xform;
  166. box_xform.basis.rotate(Vector3(1, 0, 0), Math::deg_to_rad(25.0));
  167. box_xform.basis = box_xform.basis * Basis().rotated(Vector3(0, 1, 0), Math::deg_to_rad(-25.0));
  168. box_xform.basis.scale(Vector3(0.7, 0.7, 0.7));
  169. box_xform.origin.y = 0.05;
  170. box_instance->set_transform(box_xform);
  171. sphere_mesh.instantiate();
  172. sphere_instance->set_mesh(sphere_mesh);
  173. box_mesh.instantiate();
  174. box_instance->set_mesh(box_mesh);
  175. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  176. layout_3d = memnew(HBoxContainer);
  177. add_child(layout_3d);
  178. layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  179. VBoxContainer *vb_shape = memnew(VBoxContainer);
  180. layout_3d->add_child(vb_shape);
  181. sphere_switch = memnew(TextureButton);
  182. sphere_switch->set_toggle_mode(true);
  183. sphere_switch->set_pressed(true);
  184. vb_shape->add_child(sphere_switch);
  185. sphere_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(sphere_switch));
  186. box_switch = memnew(TextureButton);
  187. box_switch->set_toggle_mode(true);
  188. box_switch->set_pressed(false);
  189. vb_shape->add_child(box_switch);
  190. box_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(box_switch));
  191. layout_3d->add_spacer();
  192. VBoxContainer *vb_light = memnew(VBoxContainer);
  193. layout_3d->add_child(vb_light);
  194. light_1_switch = memnew(TextureButton);
  195. light_1_switch->set_toggle_mode(true);
  196. vb_light->add_child(light_1_switch);
  197. light_1_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(light_1_switch));
  198. light_2_switch = memnew(TextureButton);
  199. light_2_switch->set_toggle_mode(true);
  200. vb_light->add_child(light_2_switch);
  201. light_2_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(light_2_switch));
  202. if (EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_on_sphere", true)) {
  203. box_instance->hide();
  204. } else {
  205. box_instance->show();
  206. sphere_instance->hide();
  207. box_switch->set_pressed(true);
  208. sphere_switch->set_pressed(false);
  209. }
  210. }
  211. ///////////////////////
  212. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  213. Material *material = Object::cast_to<Material>(p_object);
  214. if (!material) {
  215. return false;
  216. }
  217. Shader::Mode mode = material->get_shader_mode();
  218. return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
  219. }
  220. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  221. Material *material = Object::cast_to<Material>(p_object);
  222. if (!material) {
  223. return;
  224. }
  225. Ref<Material> m(material);
  226. MaterialEditor *editor = memnew(MaterialEditor);
  227. editor->edit(m, env);
  228. add_custom_control(editor);
  229. }
  230. void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, String p_property, Variant p_new_value) {
  231. Ref<EditorUndoRedoManager> undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  232. ERR_FAIL_COND(!undo_redo.is_valid());
  233. // For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
  234. // set the respective metallic or roughness factor to 1.0 as a convenience feature
  235. BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
  236. if (base_material) {
  237. Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
  238. if (texture) {
  239. if (p_property == "roughness_texture") {
  240. if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
  241. undo_redo->add_do_property(p_edited, "roughness", 1.0);
  242. bool valid = false;
  243. Variant value = p_edited->get("roughness", &valid);
  244. if (valid) {
  245. undo_redo->add_undo_property(p_edited, "roughness", value);
  246. }
  247. }
  248. } else if (p_property == "metallic_texture") {
  249. if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
  250. undo_redo->add_do_property(p_edited, "metallic", 1.0);
  251. bool valid = false;
  252. Variant value = p_edited->get("metallic", &valid);
  253. if (valid) {
  254. undo_redo->add_undo_property(p_edited, "metallic", value);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  262. env.instantiate();
  263. Ref<Sky> sky = memnew(Sky());
  264. env->set_sky(sky);
  265. env->set_background(Environment::BG_COLOR);
  266. env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
  267. env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
  268. EditorNode::get_singleton()->get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
  269. }
  270. MaterialEditorPlugin::MaterialEditorPlugin() {
  271. Ref<EditorInspectorPluginMaterial> plugin;
  272. plugin.instantiate();
  273. add_inspector_plugin(plugin);
  274. }
  275. String StandardMaterial3DConversionPlugin::converts_to() const {
  276. return "ShaderMaterial";
  277. }
  278. bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  279. Ref<StandardMaterial3D> mat = p_resource;
  280. return mat.is_valid();
  281. }
  282. Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  283. Ref<StandardMaterial3D> mat = p_resource;
  284. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  285. Ref<ShaderMaterial> smat;
  286. smat.instantiate();
  287. Ref<Shader> shader;
  288. shader.instantiate();
  289. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  290. shader->set_code(code);
  291. smat->set_shader(shader);
  292. List<PropertyInfo> params;
  293. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  294. for (const PropertyInfo &E : params) {
  295. // Texture parameter has to be treated specially since StandardMaterial3D saved it
  296. // as RID but ShaderMaterial needs Texture itself
  297. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  298. if (texture.is_valid()) {
  299. smat->set_shader_uniform(E.name, texture);
  300. } else {
  301. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  302. smat->set_shader_uniform(E.name, value);
  303. }
  304. }
  305. smat->set_render_priority(mat->get_render_priority());
  306. smat->set_local_to_scene(mat->is_local_to_scene());
  307. smat->set_name(mat->get_name());
  308. return smat;
  309. }
  310. String ORMMaterial3DConversionPlugin::converts_to() const {
  311. return "ShaderMaterial";
  312. }
  313. bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  314. Ref<ORMMaterial3D> mat = p_resource;
  315. return mat.is_valid();
  316. }
  317. Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  318. Ref<ORMMaterial3D> mat = p_resource;
  319. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  320. Ref<ShaderMaterial> smat;
  321. smat.instantiate();
  322. Ref<Shader> shader;
  323. shader.instantiate();
  324. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  325. shader->set_code(code);
  326. smat->set_shader(shader);
  327. List<PropertyInfo> params;
  328. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  329. for (const PropertyInfo &E : params) {
  330. // Texture parameter has to be treated specially since ORMMaterial3D saved it
  331. // as RID but ShaderMaterial needs Texture itself
  332. Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
  333. if (texture.is_valid()) {
  334. smat->set_shader_uniform(E.name, texture);
  335. } else {
  336. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  337. smat->set_shader_uniform(E.name, value);
  338. }
  339. }
  340. smat->set_render_priority(mat->get_render_priority());
  341. smat->set_local_to_scene(mat->is_local_to_scene());
  342. smat->set_name(mat->get_name());
  343. return smat;
  344. }
  345. String ParticleProcessMaterialConversionPlugin::converts_to() const {
  346. return "ShaderMaterial";
  347. }
  348. bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  349. Ref<ParticleProcessMaterial> mat = p_resource;
  350. return mat.is_valid();
  351. }
  352. Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  353. Ref<ParticleProcessMaterial> mat = p_resource;
  354. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  355. Ref<ShaderMaterial> smat;
  356. smat.instantiate();
  357. Ref<Shader> shader;
  358. shader.instantiate();
  359. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  360. shader->set_code(code);
  361. smat->set_shader(shader);
  362. List<PropertyInfo> params;
  363. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  364. for (const PropertyInfo &E : params) {
  365. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  366. smat->set_shader_uniform(E.name, value);
  367. }
  368. smat->set_render_priority(mat->get_render_priority());
  369. smat->set_local_to_scene(mat->is_local_to_scene());
  370. smat->set_name(mat->get_name());
  371. return smat;
  372. }
  373. String CanvasItemMaterialConversionPlugin::converts_to() const {
  374. return "ShaderMaterial";
  375. }
  376. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  377. Ref<CanvasItemMaterial> mat = p_resource;
  378. return mat.is_valid();
  379. }
  380. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  381. Ref<CanvasItemMaterial> mat = p_resource;
  382. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  383. Ref<ShaderMaterial> smat;
  384. smat.instantiate();
  385. Ref<Shader> shader;
  386. shader.instantiate();
  387. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  388. shader->set_code(code);
  389. smat->set_shader(shader);
  390. List<PropertyInfo> params;
  391. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  392. for (const PropertyInfo &E : params) {
  393. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  394. smat->set_shader_uniform(E.name, value);
  395. }
  396. smat->set_render_priority(mat->get_render_priority());
  397. smat->set_local_to_scene(mat->is_local_to_scene());
  398. smat->set_name(mat->get_name());
  399. return smat;
  400. }
  401. String ProceduralSkyMaterialConversionPlugin::converts_to() const {
  402. return "ShaderMaterial";
  403. }
  404. bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  405. Ref<ProceduralSkyMaterial> mat = p_resource;
  406. return mat.is_valid();
  407. }
  408. Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  409. Ref<ProceduralSkyMaterial> mat = p_resource;
  410. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  411. Ref<ShaderMaterial> smat;
  412. smat.instantiate();
  413. Ref<Shader> shader;
  414. shader.instantiate();
  415. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  416. shader->set_code(code);
  417. smat->set_shader(shader);
  418. List<PropertyInfo> params;
  419. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  420. for (const PropertyInfo &E : params) {
  421. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  422. smat->set_shader_uniform(E.name, value);
  423. }
  424. smat->set_render_priority(mat->get_render_priority());
  425. smat->set_local_to_scene(mat->is_local_to_scene());
  426. smat->set_name(mat->get_name());
  427. return smat;
  428. }
  429. String PanoramaSkyMaterialConversionPlugin::converts_to() const {
  430. return "ShaderMaterial";
  431. }
  432. bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  433. Ref<PanoramaSkyMaterial> mat = p_resource;
  434. return mat.is_valid();
  435. }
  436. Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  437. Ref<PanoramaSkyMaterial> mat = p_resource;
  438. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  439. Ref<ShaderMaterial> smat;
  440. smat.instantiate();
  441. Ref<Shader> shader;
  442. shader.instantiate();
  443. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  444. shader->set_code(code);
  445. smat->set_shader(shader);
  446. List<PropertyInfo> params;
  447. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  448. for (const PropertyInfo &E : params) {
  449. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  450. smat->set_shader_uniform(E.name, value);
  451. }
  452. smat->set_render_priority(mat->get_render_priority());
  453. smat->set_local_to_scene(mat->is_local_to_scene());
  454. smat->set_name(mat->get_name());
  455. return smat;
  456. }
  457. String PhysicalSkyMaterialConversionPlugin::converts_to() const {
  458. return "ShaderMaterial";
  459. }
  460. bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  461. Ref<PhysicalSkyMaterial> mat = p_resource;
  462. return mat.is_valid();
  463. }
  464. Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  465. Ref<PhysicalSkyMaterial> mat = p_resource;
  466. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  467. Ref<ShaderMaterial> smat;
  468. smat.instantiate();
  469. Ref<Shader> shader;
  470. shader.instantiate();
  471. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  472. shader->set_code(code);
  473. smat->set_shader(shader);
  474. List<PropertyInfo> params;
  475. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  476. for (const PropertyInfo &E : params) {
  477. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  478. smat->set_shader_uniform(E.name, value);
  479. }
  480. smat->set_render_priority(mat->get_render_priority());
  481. smat->set_local_to_scene(mat->is_local_to_scene());
  482. smat->set_name(mat->get_name());
  483. return smat;
  484. }
  485. String FogMaterialConversionPlugin::converts_to() const {
  486. return "ShaderMaterial";
  487. }
  488. bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  489. Ref<FogMaterial> mat = p_resource;
  490. return mat.is_valid();
  491. }
  492. Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  493. Ref<FogMaterial> mat = p_resource;
  494. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  495. Ref<ShaderMaterial> smat;
  496. smat.instantiate();
  497. Ref<Shader> shader;
  498. shader.instantiate();
  499. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  500. shader->set_code(code);
  501. smat->set_shader(shader);
  502. List<PropertyInfo> params;
  503. RS::get_singleton()->shader_get_shader_uniform_list(mat->get_shader_rid(), &params);
  504. for (const PropertyInfo &E : params) {
  505. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
  506. smat->set_shader_uniform(E.name, value);
  507. }
  508. smat->set_render_priority(mat->get_render_priority());
  509. return smat;
  510. }