material_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "editor/editor_scale.h"
  32. #include "scene/gui/subviewport_container.h"
  33. #include "scene/resources/particles_material.h"
  34. #include "scene/resources/sky_material.h"
  35. void MaterialEditor::_notification(int p_what) {
  36. if (p_what == NOTIFICATION_READY) {
  37. //get_scene()->connect("node_removed",this,"_node_removed");
  38. if (first_enter) {
  39. //it's in propertyeditor so.. could be moved around
  40. light_1_switch->set_normal_texture(get_theme_icon("MaterialPreviewLight1", "EditorIcons"));
  41. light_1_switch->set_pressed_texture(get_theme_icon("MaterialPreviewLight1Off", "EditorIcons"));
  42. light_2_switch->set_normal_texture(get_theme_icon("MaterialPreviewLight2", "EditorIcons"));
  43. light_2_switch->set_pressed_texture(get_theme_icon("MaterialPreviewLight2Off", "EditorIcons"));
  44. sphere_switch->set_normal_texture(get_theme_icon("MaterialPreviewSphereOff", "EditorIcons"));
  45. sphere_switch->set_pressed_texture(get_theme_icon("MaterialPreviewSphere", "EditorIcons"));
  46. box_switch->set_normal_texture(get_theme_icon("MaterialPreviewCubeOff", "EditorIcons"));
  47. box_switch->set_pressed_texture(get_theme_icon("MaterialPreviewCube", "EditorIcons"));
  48. first_enter = false;
  49. }
  50. }
  51. if (p_what == NOTIFICATION_DRAW) {
  52. Ref<Texture2D> checkerboard = get_theme_icon("Checkerboard", "EditorIcons");
  53. Size2 size = get_size();
  54. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  55. }
  56. }
  57. void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
  58. material = p_material;
  59. camera->set_environment(p_env);
  60. if (!material.is_null()) {
  61. sphere_instance->set_material_override(material);
  62. box_instance->set_material_override(material);
  63. } else {
  64. hide();
  65. }
  66. }
  67. void MaterialEditor::_button_pressed(Node *p_button) {
  68. if (p_button == light_1_switch) {
  69. light1->set_visible(!light_1_switch->is_pressed());
  70. }
  71. if (p_button == light_2_switch) {
  72. light2->set_visible(!light_2_switch->is_pressed());
  73. }
  74. if (p_button == box_switch) {
  75. box_instance->show();
  76. sphere_instance->hide();
  77. box_switch->set_pressed(true);
  78. sphere_switch->set_pressed(false);
  79. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false);
  80. }
  81. if (p_button == sphere_switch) {
  82. box_instance->hide();
  83. sphere_instance->show();
  84. box_switch->set_pressed(false);
  85. sphere_switch->set_pressed(true);
  86. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true);
  87. }
  88. }
  89. void MaterialEditor::_bind_methods() {
  90. }
  91. MaterialEditor::MaterialEditor() {
  92. vc = memnew(SubViewportContainer);
  93. vc->set_stretch(true);
  94. add_child(vc);
  95. vc->set_anchors_and_offsets_preset(PRESET_WIDE);
  96. viewport = memnew(SubViewport);
  97. Ref<World3D> world_3d;
  98. world_3d.instance();
  99. viewport->set_world_3d(world_3d); //use own world
  100. vc->add_child(viewport);
  101. viewport->set_disable_input(true);
  102. viewport->set_transparent_background(true);
  103. viewport->set_msaa(Viewport::MSAA_4X);
  104. camera = memnew(Camera3D);
  105. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 3)));
  106. camera->set_perspective(45, 0.1, 10);
  107. camera->make_current();
  108. viewport->add_child(camera);
  109. light1 = memnew(DirectionalLight3D);
  110. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  111. viewport->add_child(light1);
  112. light2 = memnew(DirectionalLight3D);
  113. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  114. light2->set_color(Color(0.7, 0.7, 0.7));
  115. viewport->add_child(light2);
  116. sphere_instance = memnew(MeshInstance3D);
  117. viewport->add_child(sphere_instance);
  118. box_instance = memnew(MeshInstance3D);
  119. viewport->add_child(box_instance);
  120. Transform3D box_xform;
  121. box_xform.basis.rotate(Vector3(1, 0, 0), Math::deg2rad(25.0));
  122. box_xform.basis = box_xform.basis * Basis().rotated(Vector3(0, 1, 0), Math::deg2rad(-25.0));
  123. box_xform.basis.scale(Vector3(0.8, 0.8, 0.8));
  124. box_xform.origin.y = 0.2;
  125. box_instance->set_transform(box_xform);
  126. sphere_mesh.instance();
  127. sphere_instance->set_mesh(sphere_mesh);
  128. box_mesh.instance();
  129. box_instance->set_mesh(box_mesh);
  130. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  131. HBoxContainer *hb = memnew(HBoxContainer);
  132. add_child(hb);
  133. hb->set_anchors_and_offsets_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 2);
  134. VBoxContainer *vb_shape = memnew(VBoxContainer);
  135. hb->add_child(vb_shape);
  136. sphere_switch = memnew(TextureButton);
  137. sphere_switch->set_toggle_mode(true);
  138. sphere_switch->set_pressed(true);
  139. vb_shape->add_child(sphere_switch);
  140. sphere_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed), varray(sphere_switch));
  141. box_switch = memnew(TextureButton);
  142. box_switch->set_toggle_mode(true);
  143. box_switch->set_pressed(false);
  144. vb_shape->add_child(box_switch);
  145. box_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed), varray(box_switch));
  146. hb->add_spacer();
  147. VBoxContainer *vb_light = memnew(VBoxContainer);
  148. hb->add_child(vb_light);
  149. light_1_switch = memnew(TextureButton);
  150. light_1_switch->set_toggle_mode(true);
  151. vb_light->add_child(light_1_switch);
  152. light_1_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed), varray(light_1_switch));
  153. light_2_switch = memnew(TextureButton);
  154. light_2_switch->set_toggle_mode(true);
  155. vb_light->add_child(light_2_switch);
  156. light_2_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed), varray(light_2_switch));
  157. first_enter = true;
  158. if (EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_on_sphere", true)) {
  159. box_instance->hide();
  160. } else {
  161. box_instance->show();
  162. sphere_instance->hide();
  163. box_switch->set_pressed(true);
  164. sphere_switch->set_pressed(false);
  165. }
  166. }
  167. ///////////////////////
  168. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  169. Material *material = Object::cast_to<Material>(p_object);
  170. if (!material) {
  171. return false;
  172. }
  173. return material->get_shader_mode() == Shader::MODE_SPATIAL;
  174. }
  175. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  176. Material *material = Object::cast_to<Material>(p_object);
  177. if (!material) {
  178. return;
  179. }
  180. Ref<Material> m(material);
  181. MaterialEditor *editor = memnew(MaterialEditor);
  182. editor->edit(m, env);
  183. add_custom_control(editor);
  184. }
  185. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  186. env.instance();
  187. Ref<Sky> sky = memnew(Sky());
  188. env->set_sky(sky);
  189. env->set_background(Environment::BG_COLOR);
  190. env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
  191. env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
  192. }
  193. MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
  194. Ref<EditorInspectorPluginMaterial> plugin;
  195. plugin.instance();
  196. add_inspector_plugin(plugin);
  197. }
  198. String StandardMaterial3DConversionPlugin::converts_to() const {
  199. return "ShaderMaterial";
  200. }
  201. bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  202. Ref<StandardMaterial3D> mat = p_resource;
  203. return mat.is_valid();
  204. }
  205. Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  206. Ref<StandardMaterial3D> mat = p_resource;
  207. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  208. Ref<ShaderMaterial> smat;
  209. smat.instance();
  210. Ref<Shader> shader;
  211. shader.instance();
  212. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  213. shader->set_code(code);
  214. smat->set_shader(shader);
  215. List<PropertyInfo> params;
  216. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  217. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  218. // Texture parameter has to be treated specially since StandardMaterial3D saved it
  219. // as RID but ShaderMaterial needs Texture itself
  220. Ref<Texture2D> texture = mat->get_texture_by_name(E->get().name);
  221. if (texture.is_valid()) {
  222. smat->set_shader_param(E->get().name, texture);
  223. } else {
  224. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  225. smat->set_shader_param(E->get().name, value);
  226. }
  227. }
  228. smat->set_render_priority(mat->get_render_priority());
  229. return smat;
  230. }
  231. String ParticlesMaterialConversionPlugin::converts_to() const {
  232. return "ShaderMaterial";
  233. }
  234. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  235. Ref<ParticlesMaterial> mat = p_resource;
  236. return mat.is_valid();
  237. }
  238. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  239. Ref<ParticlesMaterial> mat = p_resource;
  240. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  241. Ref<ShaderMaterial> smat;
  242. smat.instance();
  243. Ref<Shader> shader;
  244. shader.instance();
  245. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  246. shader->set_code(code);
  247. smat->set_shader(shader);
  248. List<PropertyInfo> params;
  249. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  250. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  251. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  252. smat->set_shader_param(E->get().name, value);
  253. }
  254. smat->set_render_priority(mat->get_render_priority());
  255. return smat;
  256. }
  257. String CanvasItemMaterialConversionPlugin::converts_to() const {
  258. return "ShaderMaterial";
  259. }
  260. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  261. Ref<CanvasItemMaterial> mat = p_resource;
  262. return mat.is_valid();
  263. }
  264. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  265. Ref<CanvasItemMaterial> mat = p_resource;
  266. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  267. Ref<ShaderMaterial> smat;
  268. smat.instance();
  269. Ref<Shader> shader;
  270. shader.instance();
  271. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  272. shader->set_code(code);
  273. smat->set_shader(shader);
  274. List<PropertyInfo> params;
  275. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  276. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  277. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  278. smat->set_shader_param(E->get().name, value);
  279. }
  280. smat->set_render_priority(mat->get_render_priority());
  281. return smat;
  282. }
  283. String ProceduralSkyMaterialConversionPlugin::converts_to() const {
  284. return "ShaderMaterial";
  285. }
  286. bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  287. Ref<ProceduralSkyMaterial> mat = p_resource;
  288. return mat.is_valid();
  289. }
  290. Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  291. Ref<ProceduralSkyMaterial> mat = p_resource;
  292. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  293. Ref<ShaderMaterial> smat;
  294. smat.instance();
  295. Ref<Shader> shader;
  296. shader.instance();
  297. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  298. shader->set_code(code);
  299. smat->set_shader(shader);
  300. List<PropertyInfo> params;
  301. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  302. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  303. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  304. smat->set_shader_param(E->get().name, value);
  305. }
  306. smat->set_render_priority(mat->get_render_priority());
  307. return smat;
  308. }
  309. String PanoramaSkyMaterialConversionPlugin::converts_to() const {
  310. return "ShaderMaterial";
  311. }
  312. bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  313. Ref<PanoramaSkyMaterial> mat = p_resource;
  314. return mat.is_valid();
  315. }
  316. Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  317. Ref<PanoramaSkyMaterial> mat = p_resource;
  318. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  319. Ref<ShaderMaterial> smat;
  320. smat.instance();
  321. Ref<Shader> shader;
  322. shader.instance();
  323. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  324. shader->set_code(code);
  325. smat->set_shader(shader);
  326. List<PropertyInfo> params;
  327. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  328. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  329. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  330. smat->set_shader_param(E->get().name, value);
  331. }
  332. smat->set_render_priority(mat->get_render_priority());
  333. return smat;
  334. }
  335. String PhysicalSkyMaterialConversionPlugin::converts_to() const {
  336. return "ShaderMaterial";
  337. }
  338. bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  339. Ref<PhysicalSkyMaterial> mat = p_resource;
  340. return mat.is_valid();
  341. }
  342. Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  343. Ref<PhysicalSkyMaterial> mat = p_resource;
  344. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  345. Ref<ShaderMaterial> smat;
  346. smat.instance();
  347. Ref<Shader> shader;
  348. shader.instance();
  349. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  350. shader->set_code(code);
  351. smat->set_shader(shader);
  352. List<PropertyInfo> params;
  353. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  354. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  355. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  356. smat->set_shader_param(E->get().name, value);
  357. }
  358. smat->set_render_priority(mat->get_render_priority());
  359. return smat;
  360. }