Path: blob/master/editor/scene/material_editor_plugin.cpp
9896 views
/**************************************************************************/1/* material_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "material_editor_plugin.h"3132#include "core/config/project_settings.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/settings/editor_settings.h"37#include "editor/themes/editor_scale.h"38#include "scene/3d/camera_3d.h"39#include "scene/3d/light_3d.h"40#include "scene/3d/mesh_instance_3d.h"41#include "scene/gui/box_container.h"42#include "scene/gui/button.h"43#include "scene/gui/color_rect.h"44#include "scene/gui/label.h"45#include "scene/gui/subviewport_container.h"46#include "scene/main/viewport.h"47#include "scene/resources/3d/fog_material.h"48#include "scene/resources/3d/sky_material.h"49#include "scene/resources/canvas_item_material.h"50#include "scene/resources/particle_process_material.h"5152void MaterialEditor::gui_input(const Ref<InputEvent> &p_event) {53ERR_FAIL_COND(p_event.is_null());5455Ref<InputEventMouseMotion> mm = p_event;56if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {57rot.x -= mm->get_relative().y * 0.01;58rot.y -= mm->get_relative().x * 0.01;59if (quad_instance->is_visible()) {60// Clamp rotation so the quad is always visible.61const real_t limit = Math::deg_to_rad(80.0);62rot = rot.clampf(-limit, limit);63} else {64rot.x = CLAMP(rot.x, -Math::PI / 2, Math::PI / 2);65}66_update_rotation();67_store_rotation_metadata();68}69}7071void MaterialEditor::_update_theme_item_cache() {72Control::_update_theme_item_cache();7374theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));75theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));7677theme_cache.sphere_icon = get_editor_theme_icon(SNAME("MaterialPreviewSphere"));78theme_cache.box_icon = get_editor_theme_icon(SNAME("MaterialPreviewCube"));79theme_cache.quad_icon = get_editor_theme_icon(SNAME("MaterialPreviewQuad"));8081theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));82}8384void MaterialEditor::_notification(int p_what) {85switch (p_what) {86case NOTIFICATION_THEME_CHANGED: {87light_1_switch->set_button_icon(theme_cache.light_1_icon);88light_2_switch->set_button_icon(theme_cache.light_2_icon);8990sphere_switch->set_button_icon(theme_cache.sphere_icon);91box_switch->set_button_icon(theme_cache.box_icon);92quad_switch->set_button_icon(theme_cache.quad_icon);9394error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));95} break;9697case NOTIFICATION_DRAW: {98if (!is_unsupported_shader_mode) {99Size2 size = get_size();100draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);101}102} break;103}104}105106void MaterialEditor::_set_rotation(real_t p_x_degrees, real_t p_y_degrees) {107rot.x = Math::deg_to_rad(p_x_degrees);108rot.y = Math::deg_to_rad(p_y_degrees);109_update_rotation();110}111112// Store the rotation so it can persist when switching between materials.113void MaterialEditor::_store_rotation_metadata() {114Vector2 rotation_degrees = Vector2(Math::rad_to_deg(rot.x), Math::rad_to_deg(rot.y));115EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_rotation", rotation_degrees);116}117118void MaterialEditor::_update_rotation() {119Transform3D t;120t.basis.rotate(Vector3(0, 1, 0), -rot.y);121t.basis.rotate(Vector3(1, 0, 0), -rot.x);122rotation->set_transform(t);123}124125void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {126material = p_material;127camera->set_environment(p_env);128129is_unsupported_shader_mode = false;130if (material.is_valid()) {131Shader::Mode mode = p_material->get_shader_mode();132switch (mode) {133case Shader::MODE_CANVAS_ITEM:134layout_error->hide();135layout_3d->hide();136layout_2d->show();137vc->hide();138rect_instance->set_material(material);139break;140case Shader::MODE_SPATIAL:141layout_error->hide();142layout_2d->hide();143layout_3d->show();144vc->show();145sphere_instance->set_material_override(material);146box_instance->set_material_override(material);147quad_instance->set_material_override(material);148break;149default:150layout_error->show();151layout_2d->hide();152layout_3d->hide();153vc->hide();154is_unsupported_shader_mode = true;155break;156}157} else {158hide();159}160}161162void MaterialEditor::_on_light_1_switch_pressed() {163light1->set_visible(light_1_switch->is_pressed());164}165166void MaterialEditor::_on_light_2_switch_pressed() {167light2->set_visible(light_2_switch->is_pressed());168}169170void MaterialEditor::_on_sphere_switch_pressed() {171sphere_instance->show();172box_instance->hide();173quad_instance->hide();174box_switch->set_pressed(false);175quad_switch->set_pressed(false);176_set_rotation(-15.0, 30.0);177_store_rotation_metadata();178EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "sphere");179}180181void MaterialEditor::_on_box_switch_pressed() {182sphere_instance->hide();183box_instance->show();184quad_instance->hide();185sphere_switch->set_pressed(false);186quad_switch->set_pressed(false);187_set_rotation(-15.0, 30.0);188_store_rotation_metadata();189EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "box");190}191192void MaterialEditor::_on_quad_switch_pressed() {193sphere_instance->hide();194box_instance->hide();195quad_instance->show();196sphere_switch->set_pressed(false);197box_switch->set_pressed(false);198_set_rotation(0.0, 0.0);199_store_rotation_metadata();200EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "quad");201}202203MaterialEditor::MaterialEditor() {204// Canvas item205206vc_2d = memnew(SubViewportContainer);207vc_2d->set_stretch(true);208add_child(vc_2d);209vc_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);210211viewport_2d = memnew(SubViewport);212vc_2d->add_child(viewport_2d);213viewport_2d->set_disable_input(true);214viewport_2d->set_transparent_background(true);215216layout_2d = memnew(HBoxContainer);217layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);218viewport_2d->add_child(layout_2d);219layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);220221rect_instance = memnew(ColorRect);222layout_2d->add_child(rect_instance);223rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);224225layout_2d->set_visible(false);226227layout_error = memnew(VBoxContainer);228layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);229layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);230231error_label = memnew(Label);232error_label->set_focus_mode(FOCUS_ACCESSIBILITY);233error_label->set_text(TTR("Preview is not available for this shader mode."));234error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);235error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);236error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);237238layout_error->add_child(error_label);239layout_error->hide();240add_child(layout_error);241242// Spatial243244vc = memnew(SubViewportContainer);245vc->set_stretch(true);246add_child(vc);247vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);248viewport = memnew(SubViewport);249Ref<World3D> world_3d;250world_3d.instantiate();251viewport->set_world_3d(world_3d); // Use own world.252vc->add_child(viewport);253viewport->set_disable_input(true);254viewport->set_transparent_background(true);255viewport->set_msaa_3d(Viewport::MSAA_4X);256257camera = memnew(Camera3D);258camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));259// Use low field of view so the sphere/box/quad is fully encompassed within the preview,260// without much distortion.261camera->set_perspective(20, 0.1, 10);262camera->make_current();263if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {264camera_attributes.instantiate();265camera->set_attributes(camera_attributes);266}267viewport->add_child(camera);268269light1 = memnew(DirectionalLight3D);270light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));271viewport->add_child(light1);272273light2 = memnew(DirectionalLight3D);274light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));275light2->set_color(Color(0.7, 0.7, 0.7));276viewport->add_child(light2);277278rotation = memnew(Node3D);279viewport->add_child(rotation);280281sphere_instance = memnew(MeshInstance3D);282rotation->add_child(sphere_instance);283284box_instance = memnew(MeshInstance3D);285rotation->add_child(box_instance);286287quad_instance = memnew(MeshInstance3D);288rotation->add_child(quad_instance);289290sphere_instance->set_transform(Transform3D() * 0.375);291box_instance->set_transform(Transform3D() * 0.25);292quad_instance->set_transform(Transform3D() * 0.375);293294sphere_mesh.instantiate();295sphere_instance->set_mesh(sphere_mesh);296box_mesh.instantiate();297box_instance->set_mesh(box_mesh);298quad_mesh.instantiate();299quad_instance->set_mesh(quad_mesh);300301set_custom_minimum_size(Size2(1, 150) * EDSCALE);302303layout_3d = memnew(HBoxContainer);304add_child(layout_3d);305layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);306307VBoxContainer *vb_shape = memnew(VBoxContainer);308layout_3d->add_child(vb_shape);309310sphere_switch = memnew(Button);311sphere_switch->set_theme_type_variation("PreviewLightButton");312sphere_switch->set_toggle_mode(true);313sphere_switch->set_accessibility_name(TTRC("Sphere"));314vb_shape->add_child(sphere_switch);315sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));316317box_switch = memnew(Button);318box_switch->set_theme_type_variation("PreviewLightButton");319box_switch->set_toggle_mode(true);320box_switch->set_accessibility_name(TTRC("Box"));321vb_shape->add_child(box_switch);322box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));323324quad_switch = memnew(Button);325quad_switch->set_theme_type_variation("PreviewLightButton");326quad_switch->set_toggle_mode(true);327quad_switch->set_accessibility_name(TTRC("Quad"));328vb_shape->add_child(quad_switch);329quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));330331layout_3d->add_spacer();332333VBoxContainer *vb_light = memnew(VBoxContainer);334layout_3d->add_child(vb_light);335336light_1_switch = memnew(Button);337light_1_switch->set_theme_type_variation("PreviewLightButton");338light_1_switch->set_toggle_mode(true);339light_1_switch->set_pressed(true);340light_1_switch->set_accessibility_name(TTRC("First Light"));341vb_light->add_child(light_1_switch);342light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));343344light_2_switch = memnew(Button);345light_2_switch->set_theme_type_variation("PreviewLightButton");346light_2_switch->set_toggle_mode(true);347light_2_switch->set_pressed(true);348light_2_switch->set_accessibility_name(TTRC("Second Light"));349vb_light->add_child(light_2_switch);350light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));351352String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");353if (shape == "sphere") {354box_instance->hide();355quad_instance->hide();356sphere_switch->set_pressed_no_signal(true);357} else if (shape == "box") {358sphere_instance->hide();359quad_instance->hide();360box_switch->set_pressed_no_signal(true);361} else {362sphere_instance->hide();363box_instance->hide();364quad_switch->set_pressed_no_signal(true);365}366367Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());368_set_rotation(stored_rot.x, stored_rot.y);369}370371///////////////////////372373bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {374Material *material = Object::cast_to<Material>(p_object);375if (!material) {376return false;377}378Shader::Mode mode = material->get_shader_mode();379return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;380}381382void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {383Material *material = Object::cast_to<Material>(p_object);384if (!material) {385return;386}387Ref<Material> m(material);388389MaterialEditor *editor = memnew(MaterialEditor);390editor->edit(m, env);391add_custom_control(editor);392}393394void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {395EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);396ERR_FAIL_NULL(undo_redo);397398// For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,399// set the respective metallic or roughness factor to 1.0 as a convenience feature400BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);401if (base_material) {402Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);403if (texture) {404if (p_property == "roughness_texture") {405if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {406undo_redo->add_do_property(p_edited, "roughness", 1.0);407408bool valid = false;409Variant value = p_edited->get("roughness", &valid);410if (valid) {411undo_redo->add_undo_property(p_edited, "roughness", value);412}413}414} else if (p_property == "metallic_texture") {415if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {416undo_redo->add_do_property(p_edited, "metallic", 1.0);417418bool valid = false;419Variant value = p_edited->get("metallic", &valid);420if (valid) {421undo_redo->add_undo_property(p_edited, "metallic", value);422}423}424}425}426}427}428429EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {430env.instantiate();431Ref<Sky> sky = memnew(Sky());432env->set_sky(sky);433env->set_background(Environment::BG_COLOR);434env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);435env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);436437EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));438}439440MaterialEditorPlugin::MaterialEditorPlugin() {441Ref<EditorInspectorPluginMaterial> plugin;442plugin.instantiate();443add_inspector_plugin(plugin);444}445446String StandardMaterial3DConversionPlugin::converts_to() const {447return "ShaderMaterial";448}449450bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {451Ref<StandardMaterial3D> mat = p_resource;452return mat.is_valid();453}454455Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {456Ref<StandardMaterial3D> mat = p_resource;457ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());458459Ref<ShaderMaterial> smat;460smat.instantiate();461462Ref<Shader> shader;463shader.instantiate();464465String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());466467shader->set_code(code);468469smat->set_shader(shader);470471List<PropertyInfo> params;472RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);473474for (const PropertyInfo &E : params) {475// Texture parameter has to be treated specially since StandardMaterial3D saved it476// as RID but ShaderMaterial needs Texture itself477Ref<Texture2D> texture = mat->get_texture_by_name(E.name);478if (texture.is_valid()) {479smat->set_shader_parameter(E.name, texture);480} else {481Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);482smat->set_shader_parameter(E.name, value);483}484}485486smat->set_render_priority(mat->get_render_priority());487smat->set_local_to_scene(mat->is_local_to_scene());488smat->set_name(mat->get_name());489return smat;490}491492String ORMMaterial3DConversionPlugin::converts_to() const {493return "ShaderMaterial";494}495496bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {497Ref<ORMMaterial3D> mat = p_resource;498return mat.is_valid();499}500501Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {502Ref<ORMMaterial3D> mat = p_resource;503ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());504505Ref<ShaderMaterial> smat;506smat.instantiate();507508Ref<Shader> shader;509shader.instantiate();510511String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());512513shader->set_code(code);514515smat->set_shader(shader);516517List<PropertyInfo> params;518RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);519520for (const PropertyInfo &E : params) {521// Texture parameter has to be treated specially since ORMMaterial3D saved it522// as RID but ShaderMaterial needs Texture itself523Ref<Texture2D> texture = mat->get_texture_by_name(E.name);524if (texture.is_valid()) {525smat->set_shader_parameter(E.name, texture);526} else {527Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);528smat->set_shader_parameter(E.name, value);529}530}531532smat->set_render_priority(mat->get_render_priority());533smat->set_local_to_scene(mat->is_local_to_scene());534smat->set_name(mat->get_name());535return smat;536}537538String ParticleProcessMaterialConversionPlugin::converts_to() const {539return "ShaderMaterial";540}541542bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {543Ref<ParticleProcessMaterial> mat = p_resource;544return mat.is_valid();545}546547Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {548Ref<ParticleProcessMaterial> mat = p_resource;549ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());550551Ref<ShaderMaterial> smat;552smat.instantiate();553554Ref<Shader> shader;555shader.instantiate();556557String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());558559shader->set_code(code);560561smat->set_shader(shader);562563List<PropertyInfo> params;564RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);565566for (const PropertyInfo &E : params) {567Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);568smat->set_shader_parameter(E.name, value);569}570571smat->set_render_priority(mat->get_render_priority());572smat->set_local_to_scene(mat->is_local_to_scene());573smat->set_name(mat->get_name());574return smat;575}576577String CanvasItemMaterialConversionPlugin::converts_to() const {578return "ShaderMaterial";579}580581bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {582Ref<CanvasItemMaterial> mat = p_resource;583return mat.is_valid();584}585586Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {587Ref<CanvasItemMaterial> mat = p_resource;588ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());589590Ref<ShaderMaterial> smat;591smat.instantiate();592593Ref<Shader> shader;594shader.instantiate();595596String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());597598shader->set_code(code);599600smat->set_shader(shader);601602List<PropertyInfo> params;603RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);604605for (const PropertyInfo &E : params) {606Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);607smat->set_shader_parameter(E.name, value);608}609610smat->set_render_priority(mat->get_render_priority());611smat->set_local_to_scene(mat->is_local_to_scene());612smat->set_name(mat->get_name());613return smat;614}615616String ProceduralSkyMaterialConversionPlugin::converts_to() const {617return "ShaderMaterial";618}619620bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {621Ref<ProceduralSkyMaterial> mat = p_resource;622return mat.is_valid();623}624625Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {626Ref<ProceduralSkyMaterial> mat = p_resource;627ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());628629Ref<ShaderMaterial> smat;630smat.instantiate();631632Ref<Shader> shader;633shader.instantiate();634635String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());636637shader->set_code(code);638639smat->set_shader(shader);640641List<PropertyInfo> params;642RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);643644for (const PropertyInfo &E : params) {645Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);646smat->set_shader_parameter(E.name, value);647}648649smat->set_render_priority(mat->get_render_priority());650smat->set_local_to_scene(mat->is_local_to_scene());651smat->set_name(mat->get_name());652return smat;653}654655String PanoramaSkyMaterialConversionPlugin::converts_to() const {656return "ShaderMaterial";657}658659bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {660Ref<PanoramaSkyMaterial> mat = p_resource;661return mat.is_valid();662}663664Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {665Ref<PanoramaSkyMaterial> mat = p_resource;666ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());667668Ref<ShaderMaterial> smat;669smat.instantiate();670671Ref<Shader> shader;672shader.instantiate();673674String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());675676shader->set_code(code);677678smat->set_shader(shader);679680List<PropertyInfo> params;681RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);682683for (const PropertyInfo &E : params) {684Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);685smat->set_shader_parameter(E.name, value);686}687688smat->set_render_priority(mat->get_render_priority());689smat->set_local_to_scene(mat->is_local_to_scene());690smat->set_name(mat->get_name());691return smat;692}693694String PhysicalSkyMaterialConversionPlugin::converts_to() const {695return "ShaderMaterial";696}697698bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {699Ref<PhysicalSkyMaterial> mat = p_resource;700return mat.is_valid();701}702703Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {704Ref<PhysicalSkyMaterial> mat = p_resource;705ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());706707Ref<ShaderMaterial> smat;708smat.instantiate();709710Ref<Shader> shader;711shader.instantiate();712713String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());714715shader->set_code(code);716717smat->set_shader(shader);718719List<PropertyInfo> params;720RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);721722for (const PropertyInfo &E : params) {723Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);724smat->set_shader_parameter(E.name, value);725}726727smat->set_render_priority(mat->get_render_priority());728smat->set_local_to_scene(mat->is_local_to_scene());729smat->set_name(mat->get_name());730return smat;731}732733String FogMaterialConversionPlugin::converts_to() const {734return "ShaderMaterial";735}736737bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {738Ref<FogMaterial> mat = p_resource;739return mat.is_valid();740}741742Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {743Ref<FogMaterial> mat = p_resource;744ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());745746Ref<ShaderMaterial> smat;747smat.instantiate();748749Ref<Shader> shader;750shader.instantiate();751752String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());753754shader->set_code(code);755756smat->set_shader(shader);757758List<PropertyInfo> params;759RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms);760761for (const PropertyInfo &E : params) {762Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);763smat->set_shader_parameter(E.name, value);764}765766smat->set_render_priority(mat->get_render_priority());767return smat;768}769770771