material_editor_plugin.cpp 21 KB

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