material_editor_plugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/viewport_container.h"
  33. #include "scene/resources/particles_material.h"
  34. void MaterialEditor::_notification(int p_what) {
  35. if (p_what == NOTIFICATION_READY) {
  36. //get_scene()->connect("node_removed",this,"_node_removed");
  37. if (first_enter) {
  38. //it's in propertyeditor so.. could be moved around
  39. light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
  40. light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
  41. light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
  42. light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
  43. sphere_switch->set_normal_texture(get_icon("MaterialPreviewSphereOff", "EditorIcons"));
  44. sphere_switch->set_pressed_texture(get_icon("MaterialPreviewSphere", "EditorIcons"));
  45. box_switch->set_normal_texture(get_icon("MaterialPreviewCubeOff", "EditorIcons"));
  46. box_switch->set_pressed_texture(get_icon("MaterialPreviewCube", "EditorIcons"));
  47. first_enter = false;
  48. }
  49. }
  50. if (p_what == NOTIFICATION_DRAW) {
  51. Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
  52. Size2 size = get_size();
  53. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  54. }
  55. }
  56. void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
  57. material = p_material;
  58. camera->set_environment(p_env);
  59. if (!material.is_null()) {
  60. sphere_instance->set_material_override(material);
  61. box_instance->set_material_override(material);
  62. } else {
  63. hide();
  64. }
  65. }
  66. void MaterialEditor::_button_pressed(Node *p_button) {
  67. if (p_button == light_1_switch) {
  68. light1->set_visible(!light_1_switch->is_pressed());
  69. }
  70. if (p_button == light_2_switch) {
  71. light2->set_visible(!light_2_switch->is_pressed());
  72. }
  73. if (p_button == box_switch) {
  74. box_instance->show();
  75. sphere_instance->hide();
  76. box_switch->set_pressed(true);
  77. sphere_switch->set_pressed(false);
  78. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false);
  79. }
  80. if (p_button == sphere_switch) {
  81. box_instance->hide();
  82. sphere_instance->show();
  83. box_switch->set_pressed(false);
  84. sphere_switch->set_pressed(true);
  85. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true);
  86. }
  87. }
  88. void MaterialEditor::_bind_methods() {
  89. ClassDB::bind_method(D_METHOD("_button_pressed"), &MaterialEditor::_button_pressed);
  90. }
  91. MaterialEditor::MaterialEditor() {
  92. vc = memnew(ViewportContainer);
  93. vc->set_stretch(true);
  94. add_child(vc);
  95. vc->set_anchors_and_margins_preset(PRESET_WIDE);
  96. viewport = memnew(Viewport);
  97. Ref<World> world;
  98. world.instance();
  99. viewport->set_world(world); //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(Camera);
  105. camera->set_transform(Transform(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(DirectionalLight);
  110. light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  111. viewport->add_child(light1);
  112. light2 = memnew(DirectionalLight);
  113. light2->set_transform(Transform().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(MeshInstance);
  117. viewport->add_child(sphere_instance);
  118. box_instance = memnew(MeshInstance);
  119. viewport->add_child(box_instance);
  120. Transform 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_margins_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", this, "_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", this, "_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", this, "_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", this, "_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. return material->get_shader_mode() == Shader::MODE_SPATIAL;
  173. }
  174. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  175. Material *material = Object::cast_to<Material>(p_object);
  176. if (!material) {
  177. return;
  178. }
  179. Ref<Material> m(material);
  180. MaterialEditor *editor = memnew(MaterialEditor);
  181. editor->edit(m, env);
  182. add_custom_control(editor);
  183. }
  184. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  185. env.instance();
  186. Ref<ProceduralSky> proc_sky = memnew(ProceduralSky(true));
  187. env->set_sky(proc_sky);
  188. env->set_background(Environment::BG_COLOR_SKY);
  189. }
  190. MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
  191. Ref<EditorInspectorPluginMaterial> plugin;
  192. plugin.instance();
  193. add_inspector_plugin(plugin);
  194. }
  195. String SpatialMaterialConversionPlugin::converts_to() const {
  196. return "ShaderMaterial";
  197. }
  198. bool SpatialMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  199. Ref<SpatialMaterial> mat = p_resource;
  200. return mat.is_valid();
  201. }
  202. Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  203. Ref<SpatialMaterial> mat = p_resource;
  204. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  205. Ref<ShaderMaterial> smat;
  206. smat.instance();
  207. Ref<Shader> shader;
  208. shader.instance();
  209. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  210. shader->set_code(code);
  211. smat->set_shader(shader);
  212. List<PropertyInfo> params;
  213. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  214. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  215. // Texture parameter has to be treated specially since SpatialMaterial saved it
  216. // as RID but ShaderMaterial needs Texture itself
  217. Ref<Texture> texture = mat->get_texture_by_name(E->get().name);
  218. if (texture.is_valid()) {
  219. smat->set_shader_param(E->get().name, texture);
  220. } else {
  221. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  222. smat->set_shader_param(E->get().name, value);
  223. }
  224. }
  225. smat->set_render_priority(mat->get_render_priority());
  226. return smat;
  227. }
  228. String ParticlesMaterialConversionPlugin::converts_to() const {
  229. return "ShaderMaterial";
  230. }
  231. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  232. Ref<ParticlesMaterial> mat = p_resource;
  233. return mat.is_valid();
  234. }
  235. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  236. Ref<ParticlesMaterial> mat = p_resource;
  237. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  238. Ref<ShaderMaterial> smat;
  239. smat.instance();
  240. Ref<Shader> shader;
  241. shader.instance();
  242. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  243. shader->set_code(code);
  244. smat->set_shader(shader);
  245. List<PropertyInfo> params;
  246. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  247. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  248. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  249. smat->set_shader_param(E->get().name, value);
  250. }
  251. smat->set_render_priority(mat->get_render_priority());
  252. return smat;
  253. }
  254. String CanvasItemMaterialConversionPlugin::converts_to() const {
  255. return "ShaderMaterial";
  256. }
  257. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  258. Ref<CanvasItemMaterial> mat = p_resource;
  259. return mat.is_valid();
  260. }
  261. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  262. Ref<CanvasItemMaterial> mat = p_resource;
  263. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  264. Ref<ShaderMaterial> smat;
  265. smat.instance();
  266. Ref<Shader> shader;
  267. shader.instance();
  268. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  269. shader->set_code(code);
  270. smat->set_shader(shader);
  271. List<PropertyInfo> params;
  272. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  273. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  274. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  275. smat->set_shader_param(E->get().name, value);
  276. }
  277. smat->set_render_priority(mat->get_render_priority());
  278. return smat;
  279. }