material_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "editor/editor_scale.h"
  32. #include "scene/gui/viewport_container.h"
  33. #include "scene/resources/particles_material.h"
  34. void MaterialEditor::_gui_input(const Ref<InputEvent> &p_event) {
  35. ERR_FAIL_COND(p_event.is_null());
  36. Ref<InputEventMouseMotion> mm = p_event;
  37. if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_LEFT)) {
  38. rot.x -= mm->get_relative().y * 0.01;
  39. rot.y -= mm->get_relative().x * 0.01;
  40. rot.x = CLAMP(rot.x, -Math_PI / 2, Math_PI / 2);
  41. _update_rotation();
  42. }
  43. }
  44. void MaterialEditor::_notification(int p_what) {
  45. if (p_what == NOTIFICATION_READY) {
  46. //get_scene()->connect("node_removed",this,"_node_removed");
  47. if (first_enter) {
  48. //it's in propertyeditor so.. could be moved around
  49. light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
  50. light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
  51. light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
  52. light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
  53. sphere_switch->set_normal_texture(get_icon("MaterialPreviewSphereOff", "EditorIcons"));
  54. sphere_switch->set_pressed_texture(get_icon("MaterialPreviewSphere", "EditorIcons"));
  55. box_switch->set_normal_texture(get_icon("MaterialPreviewCubeOff", "EditorIcons"));
  56. box_switch->set_pressed_texture(get_icon("MaterialPreviewCube", "EditorIcons"));
  57. first_enter = false;
  58. }
  59. }
  60. if (p_what == NOTIFICATION_DRAW) {
  61. Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
  62. Size2 size = get_size();
  63. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  64. }
  65. }
  66. void MaterialEditor::_update_rotation() {
  67. Transform t;
  68. t.basis.rotate(Vector3(0, 1, 0), -rot.y);
  69. t.basis.rotate(Vector3(1, 0, 0), -rot.x);
  70. rotation->set_transform(t);
  71. }
  72. void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
  73. material = p_material;
  74. camera->set_environment(p_env);
  75. if (!material.is_null()) {
  76. sphere_instance->set_material_override(material);
  77. box_instance->set_material_override(material);
  78. } else {
  79. hide();
  80. }
  81. rot.x = Math::deg2rad(-15.0);
  82. rot.y = Math::deg2rad(30.0);
  83. _update_rotation();
  84. }
  85. void MaterialEditor::_button_pressed(Node *p_button) {
  86. if (p_button == light_1_switch) {
  87. light1->set_visible(!light_1_switch->is_pressed());
  88. }
  89. if (p_button == light_2_switch) {
  90. light2->set_visible(!light_2_switch->is_pressed());
  91. }
  92. if (p_button == box_switch) {
  93. box_instance->show();
  94. sphere_instance->hide();
  95. box_switch->set_pressed(true);
  96. sphere_switch->set_pressed(false);
  97. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false);
  98. }
  99. if (p_button == sphere_switch) {
  100. box_instance->hide();
  101. sphere_instance->show();
  102. box_switch->set_pressed(false);
  103. sphere_switch->set_pressed(true);
  104. EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true);
  105. }
  106. }
  107. void MaterialEditor::_bind_methods() {
  108. ClassDB::bind_method(D_METHOD("_gui_input"), &MaterialEditor::_gui_input);
  109. ClassDB::bind_method(D_METHOD("_button_pressed"), &MaterialEditor::_button_pressed);
  110. }
  111. MaterialEditor::MaterialEditor() {
  112. vc = memnew(ViewportContainer);
  113. vc->set_stretch(true);
  114. add_child(vc);
  115. vc->set_anchors_and_margins_preset(PRESET_WIDE);
  116. viewport = memnew(Viewport);
  117. Ref<World> world;
  118. world.instance();
  119. viewport->set_world(world); //use own world
  120. vc->add_child(viewport);
  121. viewport->set_disable_input(true);
  122. viewport->set_transparent_background(true);
  123. viewport->set_msaa(Viewport::MSAA_4X);
  124. camera = memnew(Camera);
  125. camera->set_transform(Transform(Basis(), Vector3(0, 0, 1.1)));
  126. camera->set_perspective(45, 0.1, 10);
  127. camera->make_current();
  128. viewport->add_child(camera);
  129. light1 = memnew(DirectionalLight);
  130. light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  131. viewport->add_child(light1);
  132. light2 = memnew(DirectionalLight);
  133. light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  134. light2->set_color(Color(0.7, 0.7, 0.7));
  135. viewport->add_child(light2);
  136. rotation = memnew(Spatial);
  137. viewport->add_child(rotation);
  138. sphere_instance = memnew(MeshInstance);
  139. rotation->add_child(sphere_instance);
  140. box_instance = memnew(MeshInstance);
  141. rotation->add_child(box_instance);
  142. box_instance->set_transform(Transform(Basis() * 0.25, Vector3() * 0.25));
  143. sphere_instance->set_transform(Transform(Basis() * 0.375, Vector3() * 0.375));
  144. sphere_mesh.instance();
  145. sphere_instance->set_mesh(sphere_mesh);
  146. box_mesh.instance();
  147. box_instance->set_mesh(box_mesh);
  148. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  149. HBoxContainer *hb = memnew(HBoxContainer);
  150. add_child(hb);
  151. hb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 2);
  152. VBoxContainer *vb_shape = memnew(VBoxContainer);
  153. hb->add_child(vb_shape);
  154. sphere_switch = memnew(TextureButton);
  155. sphere_switch->set_toggle_mode(true);
  156. sphere_switch->set_pressed(true);
  157. vb_shape->add_child(sphere_switch);
  158. sphere_switch->connect("pressed", this, "_button_pressed", varray(sphere_switch));
  159. box_switch = memnew(TextureButton);
  160. box_switch->set_toggle_mode(true);
  161. box_switch->set_pressed(false);
  162. vb_shape->add_child(box_switch);
  163. box_switch->connect("pressed", this, "_button_pressed", varray(box_switch));
  164. hb->add_spacer();
  165. VBoxContainer *vb_light = memnew(VBoxContainer);
  166. hb->add_child(vb_light);
  167. light_1_switch = memnew(TextureButton);
  168. light_1_switch->set_toggle_mode(true);
  169. vb_light->add_child(light_1_switch);
  170. light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
  171. light_2_switch = memnew(TextureButton);
  172. light_2_switch->set_toggle_mode(true);
  173. vb_light->add_child(light_2_switch);
  174. light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
  175. first_enter = true;
  176. if (EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_on_sphere", true)) {
  177. box_instance->hide();
  178. } else {
  179. box_instance->show();
  180. sphere_instance->hide();
  181. box_switch->set_pressed(true);
  182. sphere_switch->set_pressed(false);
  183. }
  184. }
  185. ///////////////////////
  186. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  187. Material *material = Object::cast_to<Material>(p_object);
  188. if (!material) {
  189. return false;
  190. }
  191. return material->get_shader_mode() == Shader::MODE_SPATIAL;
  192. }
  193. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  194. Material *material = Object::cast_to<Material>(p_object);
  195. if (!material) {
  196. return;
  197. }
  198. Ref<Material> m(material);
  199. MaterialEditor *editor = memnew(MaterialEditor);
  200. editor->edit(m, env);
  201. add_custom_control(editor);
  202. }
  203. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  204. env.instance();
  205. Ref<ProceduralSky> proc_sky = memnew(ProceduralSky(true));
  206. env->set_sky(proc_sky);
  207. env->set_background(Environment::BG_COLOR_SKY);
  208. }
  209. MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
  210. Ref<EditorInspectorPluginMaterial> plugin;
  211. plugin.instance();
  212. add_inspector_plugin(plugin);
  213. }
  214. String SpatialMaterialConversionPlugin::converts_to() const {
  215. return "ShaderMaterial";
  216. }
  217. bool SpatialMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  218. Ref<Material3D> mat = p_resource;
  219. return mat.is_valid();
  220. }
  221. Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  222. Ref<Material3D> mat = p_resource;
  223. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  224. Ref<ShaderMaterial> smat;
  225. smat.instance();
  226. Ref<Shader> shader;
  227. shader.instance();
  228. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  229. shader->set_code(code);
  230. smat->set_shader(shader);
  231. List<PropertyInfo> params;
  232. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  233. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  234. // Texture parameter has to be treated specially since SpatialMaterial saved it
  235. // as RID but ShaderMaterial needs Texture itself
  236. Ref<Texture> texture = mat->get_texture_by_name(E->get().name);
  237. if (texture.is_valid()) {
  238. smat->set_shader_param(E->get().name, texture);
  239. } else {
  240. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  241. smat->set_shader_param(E->get().name, value);
  242. }
  243. }
  244. smat->set_render_priority(mat->get_render_priority());
  245. smat->set_local_to_scene(mat->is_local_to_scene());
  246. smat->set_name(mat->get_name());
  247. return smat;
  248. }
  249. String ParticlesMaterialConversionPlugin::converts_to() const {
  250. return "ShaderMaterial";
  251. }
  252. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  253. Ref<ParticlesMaterial> mat = p_resource;
  254. return mat.is_valid();
  255. }
  256. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  257. Ref<ParticlesMaterial> mat = p_resource;
  258. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  259. Ref<ShaderMaterial> smat;
  260. smat.instance();
  261. Ref<Shader> shader;
  262. shader.instance();
  263. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  264. shader->set_code(code);
  265. smat->set_shader(shader);
  266. List<PropertyInfo> params;
  267. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  268. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  269. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  270. smat->set_shader_param(E->get().name, value);
  271. }
  272. smat->set_render_priority(mat->get_render_priority());
  273. smat->set_local_to_scene(mat->is_local_to_scene());
  274. smat->set_name(mat->get_name());
  275. return smat;
  276. }
  277. String CanvasItemMaterialConversionPlugin::converts_to() const {
  278. return "ShaderMaterial";
  279. }
  280. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  281. Ref<CanvasItemMaterial> mat = p_resource;
  282. return mat.is_valid();
  283. }
  284. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  285. Ref<CanvasItemMaterial> mat = p_resource;
  286. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  287. Ref<ShaderMaterial> smat;
  288. smat.instance();
  289. Ref<Shader> shader;
  290. shader.instance();
  291. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  292. shader->set_code(code);
  293. smat->set_shader(shader);
  294. List<PropertyInfo> params;
  295. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  296. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  297. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  298. smat->set_shader_param(E->get().name, value);
  299. }
  300. smat->set_render_priority(mat->get_render_priority());
  301. smat->set_local_to_scene(mat->is_local_to_scene());
  302. smat->set_name(mat->get_name());
  303. return smat;
  304. }