material_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. }
  79. if (p_button == sphere_switch) {
  80. box_instance->hide();
  81. sphere_instance->show();
  82. box_switch->set_pressed(false);
  83. sphere_switch->set_pressed(true);
  84. }
  85. }
  86. void MaterialEditor::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("_button_pressed"), &MaterialEditor::_button_pressed);
  88. }
  89. MaterialEditor::MaterialEditor() {
  90. vc = memnew(ViewportContainer);
  91. vc->set_stretch(true);
  92. add_child(vc);
  93. vc->set_anchors_and_margins_preset(PRESET_WIDE);
  94. viewport = memnew(Viewport);
  95. Ref<World> world;
  96. world.instance();
  97. viewport->set_world(world); //use own world
  98. vc->add_child(viewport);
  99. viewport->set_disable_input(true);
  100. viewport->set_transparent_background(true);
  101. viewport->set_msaa(Viewport::MSAA_4X);
  102. camera = memnew(Camera);
  103. camera->set_transform(Transform(Basis(), Vector3(0, 0, 3)));
  104. camera->set_perspective(45, 0.1, 10);
  105. camera->make_current();
  106. viewport->add_child(camera);
  107. light1 = memnew(DirectionalLight);
  108. light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  109. viewport->add_child(light1);
  110. light2 = memnew(DirectionalLight);
  111. light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  112. light2->set_color(Color(0.7, 0.7, 0.7));
  113. viewport->add_child(light2);
  114. sphere_instance = memnew(MeshInstance);
  115. viewport->add_child(sphere_instance);
  116. box_instance = memnew(MeshInstance);
  117. viewport->add_child(box_instance);
  118. Transform box_xform;
  119. box_xform.basis.rotate(Vector3(1, 0, 0), Math::deg2rad(25.0));
  120. box_xform.basis = box_xform.basis * Basis().rotated(Vector3(0, 1, 0), Math::deg2rad(-25.0));
  121. box_xform.basis.scale(Vector3(0.8, 0.8, 0.8));
  122. box_xform.origin.y = 0.2;
  123. box_instance->set_transform(box_xform);
  124. sphere_mesh.instance();
  125. sphere_instance->set_mesh(sphere_mesh);
  126. box_mesh.instance();
  127. box_instance->set_mesh(box_mesh);
  128. box_instance->hide();
  129. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  130. HBoxContainer *hb = memnew(HBoxContainer);
  131. add_child(hb);
  132. hb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 2);
  133. VBoxContainer *vb_shape = memnew(VBoxContainer);
  134. hb->add_child(vb_shape);
  135. sphere_switch = memnew(TextureButton);
  136. sphere_switch->set_toggle_mode(true);
  137. sphere_switch->set_pressed(true);
  138. vb_shape->add_child(sphere_switch);
  139. sphere_switch->connect("pressed", this, "_button_pressed", varray(sphere_switch));
  140. box_switch = memnew(TextureButton);
  141. box_switch->set_toggle_mode(true);
  142. box_switch->set_pressed(false);
  143. vb_shape->add_child(box_switch);
  144. box_switch->connect("pressed", this, "_button_pressed", varray(box_switch));
  145. hb->add_spacer();
  146. VBoxContainer *vb_light = memnew(VBoxContainer);
  147. hb->add_child(vb_light);
  148. light_1_switch = memnew(TextureButton);
  149. light_1_switch->set_toggle_mode(true);
  150. vb_light->add_child(light_1_switch);
  151. light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
  152. light_2_switch = memnew(TextureButton);
  153. light_2_switch->set_toggle_mode(true);
  154. vb_light->add_child(light_2_switch);
  155. light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
  156. first_enter = true;
  157. }
  158. ///////////////////////
  159. bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
  160. Material *material = Object::cast_to<Material>(p_object);
  161. if (!material)
  162. return false;
  163. return material->get_shader_mode() == Shader::MODE_SPATIAL;
  164. }
  165. void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
  166. Material *material = Object::cast_to<Material>(p_object);
  167. if (!material) {
  168. return;
  169. }
  170. Ref<Material> m(material);
  171. MaterialEditor *editor = memnew(MaterialEditor);
  172. editor->edit(m, env);
  173. add_custom_control(editor);
  174. }
  175. EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
  176. env.instance();
  177. Ref<ProceduralSky> proc_sky = memnew(ProceduralSky(true));
  178. env->set_sky(proc_sky);
  179. env->set_background(Environment::BG_COLOR_SKY);
  180. }
  181. MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
  182. Ref<EditorInspectorPluginMaterial> plugin;
  183. plugin.instance();
  184. add_inspector_plugin(plugin);
  185. }
  186. String SpatialMaterialConversionPlugin::converts_to() const {
  187. return "ShaderMaterial";
  188. }
  189. bool SpatialMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  190. Ref<SpatialMaterial> mat = p_resource;
  191. return mat.is_valid();
  192. }
  193. Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  194. Ref<SpatialMaterial> mat = p_resource;
  195. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  196. Ref<ShaderMaterial> smat;
  197. smat.instance();
  198. Ref<Shader> shader;
  199. shader.instance();
  200. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  201. shader->set_code(code);
  202. smat->set_shader(shader);
  203. List<PropertyInfo> params;
  204. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  205. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  206. // Texture parameter has to be treated specially since SpatialMaterial saved it
  207. // as RID but ShaderMaterial needs Texture itself
  208. Ref<Texture> texture = mat->get_texture_by_name(E->get().name);
  209. if (texture.is_valid()) {
  210. smat->set_shader_param(E->get().name, texture);
  211. } else {
  212. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  213. smat->set_shader_param(E->get().name, value);
  214. }
  215. }
  216. smat->set_render_priority(mat->get_render_priority());
  217. return smat;
  218. }
  219. String ParticlesMaterialConversionPlugin::converts_to() const {
  220. return "ShaderMaterial";
  221. }
  222. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  223. Ref<ParticlesMaterial> mat = p_resource;
  224. return mat.is_valid();
  225. }
  226. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  227. Ref<ParticlesMaterial> mat = p_resource;
  228. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  229. Ref<ShaderMaterial> smat;
  230. smat.instance();
  231. Ref<Shader> shader;
  232. shader.instance();
  233. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  234. shader->set_code(code);
  235. smat->set_shader(shader);
  236. List<PropertyInfo> params;
  237. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  238. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  239. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  240. smat->set_shader_param(E->get().name, value);
  241. }
  242. smat->set_render_priority(mat->get_render_priority());
  243. return smat;
  244. }
  245. String CanvasItemMaterialConversionPlugin::converts_to() const {
  246. return "ShaderMaterial";
  247. }
  248. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  249. Ref<CanvasItemMaterial> mat = p_resource;
  250. return mat.is_valid();
  251. }
  252. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  253. Ref<CanvasItemMaterial> mat = p_resource;
  254. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  255. Ref<ShaderMaterial> smat;
  256. smat.instance();
  257. Ref<Shader> shader;
  258. shader.instance();
  259. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  260. shader->set_code(code);
  261. smat->set_shader(shader);
  262. List<PropertyInfo> params;
  263. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  264. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  265. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  266. smat->set_shader_param(E->get().name, value);
  267. }
  268. smat->set_render_priority(mat->get_render_priority());
  269. return smat;
  270. }