| 1 | /**************************************************************************/ |
| 2 | /* material_3d_conversion_plugins.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 | |
| 31 | #include "material_3d_conversion_plugins.h" |
| 32 | |
| 33 | #include "editor/scene/material_editor_plugin.h" |
| 34 | #include "scene/resources/3d/fog_material.h" |
| 35 | #include "scene/resources/3d/sky_material.h" |
| 36 | |
| 37 | String StandardMaterial3DConversionPlugin::converts_to() const { |
| 38 | return "ShaderMaterial"; |
| 39 | } |
| 40 | |
| 41 | bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 42 | Ref<StandardMaterial3D> mat = p_resource; |
| 43 | return mat.is_valid(); |
| 44 | } |
| 45 | |
| 46 | Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 47 | Ref<StandardMaterial3D> mat = p_resource; |
| 48 | Ref<ShaderMaterial> smat = MaterialEditor::make_shader_material(mat, false); |
| 49 | if (smat.is_null()) { |
| 50 | return smat; |
| 51 | } |
| 52 | |
| 53 | List<PropertyInfo> params; |
| 54 | RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms); |
| 55 | |
| 56 | for (const PropertyInfo &E : params) { |
| 57 | // Texture parameter has to be treated specially since StandardMaterial3D saved it |
| 58 | // as RID but ShaderMaterial needs Texture itself |
| 59 | Ref<Texture2D> texture = mat->get_texture_by_name(E.name); |
| 60 | if (texture.is_valid()) { |
| 61 | smat->set_shader_parameter(E.name, texture); |
| 62 | } else { |
| 63 | Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name); |
| 64 | smat->set_shader_parameter(E.name, value); |
| 65 | } |
| 66 | } |
| 67 | return smat; |
| 68 | } |
| 69 | |
| 70 | String ORMMaterial3DConversionPlugin::converts_to() const { |
| 71 | return "ShaderMaterial"; |
| 72 | } |
| 73 | |
| 74 | bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 75 | Ref<ORMMaterial3D> mat = p_resource; |
| 76 | return mat.is_valid(); |
| 77 | } |
| 78 | |
| 79 | Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 80 | Ref<ORMMaterial3D> mat = p_resource; |
| 81 | Ref<ShaderMaterial> smat = MaterialEditor::make_shader_material(mat, false); |
| 82 | if (smat.is_null()) { |
| 83 | return smat; |
| 84 | } |
| 85 | |
| 86 | List<PropertyInfo> params; |
| 87 | RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), ¶ms); |
| 88 | |
| 89 | for (const PropertyInfo &E : params) { |
| 90 | // Texture parameter has to be treated specially since ORMMaterial3D saved it |
| 91 | // as RID but ShaderMaterial needs Texture itself |
| 92 | Ref<Texture2D> texture = mat->get_texture_by_name(E.name); |
| 93 | if (texture.is_valid()) { |
| 94 | smat->set_shader_parameter(E.name, texture); |
| 95 | } else { |
| 96 | Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name); |
| 97 | smat->set_shader_parameter(E.name, value); |
| 98 | } |
| 99 | } |
| 100 | return smat; |
| 101 | } |
| 102 | |
| 103 | String ProceduralSkyMaterialConversionPlugin::converts_to() const { |
| 104 | return "ShaderMaterial"; |
| 105 | } |
| 106 | |
| 107 | bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 108 | Ref<ProceduralSkyMaterial> mat = p_resource; |
| 109 | return mat.is_valid(); |
| 110 | } |
| 111 | |
| 112 | Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 113 | return MaterialEditor::make_shader_material(p_resource); |
| 114 | } |
| 115 | |
| 116 | String PanoramaSkyMaterialConversionPlugin::converts_to() const { |
| 117 | return "ShaderMaterial"; |
| 118 | } |
| 119 | |
| 120 | bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 121 | Ref<PanoramaSkyMaterial> mat = p_resource; |
| 122 | return mat.is_valid(); |
| 123 | } |
| 124 | |
| 125 | Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 126 | return MaterialEditor::make_shader_material(p_resource); |
| 127 | } |
| 128 | |
| 129 | String PhysicalSkyMaterialConversionPlugin::converts_to() const { |
| 130 | return "ShaderMaterial"; |
| 131 | } |
| 132 | |
| 133 | bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 134 | Ref<PhysicalSkyMaterial> mat = p_resource; |
| 135 | return mat.is_valid(); |
| 136 | } |
| 137 | |
| 138 | Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 139 | return MaterialEditor::make_shader_material(p_resource); |
| 140 | } |
| 141 | |
| 142 | String FogMaterialConversionPlugin::converts_to() const { |
| 143 | return "ShaderMaterial"; |
| 144 | } |
| 145 | |
| 146 | bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const { |
| 147 | Ref<FogMaterial> mat = p_resource; |
| 148 | return mat.is_valid(); |
| 149 | } |
| 150 | |
| 151 | Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const { |
| 152 | return MaterialEditor::make_shader_material(p_resource); |
| 153 | } |