material_editor_plugin.cpp 22 KB

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