material_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #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. }
  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. }
  86. }
  87. void MaterialEditor::_bind_methods() {
  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(SubViewport);
  95. Ref<World3D> 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(Camera3D);
  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(DirectionalLight3D);
  108. light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  109. viewport->add_child(light1);
  110. light2 = memnew(DirectionalLight3D);
  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(MeshInstance3D);
  115. viewport->add_child(sphere_instance);
  116. box_instance = memnew(MeshInstance3D);
  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", callable_mp(this, &MaterialEditor::_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", callable_mp(this, &MaterialEditor::_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", callable_mp(this, &MaterialEditor::_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", callable_mp(this, &MaterialEditor::_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<Sky> sky = memnew(Sky());
  178. env->set_sky(sky);
  179. env->set_background(Environment::BG_COLOR);
  180. env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
  181. env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
  182. }
  183. MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
  184. Ref<EditorInspectorPluginMaterial> plugin;
  185. plugin.instance();
  186. add_inspector_plugin(plugin);
  187. }
  188. String StandardMaterial3DConversionPlugin::converts_to() const {
  189. return "ShaderMaterial";
  190. }
  191. bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  192. Ref<StandardMaterial3D> mat = p_resource;
  193. return mat.is_valid();
  194. }
  195. Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  196. Ref<StandardMaterial3D> mat = p_resource;
  197. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  198. Ref<ShaderMaterial> smat;
  199. smat.instance();
  200. Ref<Shader> shader;
  201. shader.instance();
  202. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  203. shader->set_code(code);
  204. smat->set_shader(shader);
  205. List<PropertyInfo> params;
  206. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  207. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  208. // Texture parameter has to be treated specially since StandardMaterial3D saved it
  209. // as RID but ShaderMaterial needs Texture itself
  210. Ref<Texture2D> texture = mat->get_texture_by_name(E->get().name);
  211. if (texture.is_valid()) {
  212. smat->set_shader_param(E->get().name, texture);
  213. } else {
  214. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  215. smat->set_shader_param(E->get().name, value);
  216. }
  217. }
  218. smat->set_render_priority(mat->get_render_priority());
  219. return smat;
  220. }
  221. String ParticlesMaterialConversionPlugin::converts_to() const {
  222. return "ShaderMaterial";
  223. }
  224. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  225. Ref<ParticlesMaterial> mat = p_resource;
  226. return mat.is_valid();
  227. }
  228. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  229. Ref<ParticlesMaterial> mat = p_resource;
  230. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  231. Ref<ShaderMaterial> smat;
  232. smat.instance();
  233. Ref<Shader> shader;
  234. shader.instance();
  235. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  236. shader->set_code(code);
  237. smat->set_shader(shader);
  238. List<PropertyInfo> params;
  239. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  240. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  241. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  242. smat->set_shader_param(E->get().name, value);
  243. }
  244. smat->set_render_priority(mat->get_render_priority());
  245. return smat;
  246. }
  247. String CanvasItemMaterialConversionPlugin::converts_to() const {
  248. return "ShaderMaterial";
  249. }
  250. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  251. Ref<CanvasItemMaterial> mat = p_resource;
  252. return mat.is_valid();
  253. }
  254. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  255. Ref<CanvasItemMaterial> mat = p_resource;
  256. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  257. Ref<ShaderMaterial> smat;
  258. smat.instance();
  259. Ref<Shader> shader;
  260. shader.instance();
  261. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  262. shader->set_code(code);
  263. smat->set_shader(shader);
  264. List<PropertyInfo> params;
  265. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  266. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  267. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  268. smat->set_shader_param(E->get().name, value);
  269. }
  270. smat->set_render_priority(mat->get_render_priority());
  271. return smat;
  272. }
  273. String ProceduralSkyMaterialConversionPlugin::converts_to() const {
  274. return "ShaderMaterial";
  275. }
  276. bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  277. Ref<ProceduralSkyMaterial> mat = p_resource;
  278. return mat.is_valid();
  279. }
  280. Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  281. Ref<ProceduralSkyMaterial> mat = p_resource;
  282. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  283. Ref<ShaderMaterial> smat;
  284. smat.instance();
  285. Ref<Shader> shader;
  286. shader.instance();
  287. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  288. shader->set_code(code);
  289. smat->set_shader(shader);
  290. List<PropertyInfo> params;
  291. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  292. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  293. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  294. smat->set_shader_param(E->get().name, value);
  295. }
  296. smat->set_render_priority(mat->get_render_priority());
  297. return smat;
  298. }
  299. String PanoramaSkyMaterialConversionPlugin::converts_to() const {
  300. return "ShaderMaterial";
  301. }
  302. bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  303. Ref<PanoramaSkyMaterial> mat = p_resource;
  304. return mat.is_valid();
  305. }
  306. Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  307. Ref<PanoramaSkyMaterial> mat = p_resource;
  308. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  309. Ref<ShaderMaterial> smat;
  310. smat.instance();
  311. Ref<Shader> shader;
  312. shader.instance();
  313. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  314. shader->set_code(code);
  315. smat->set_shader(shader);
  316. List<PropertyInfo> params;
  317. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  318. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  319. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  320. smat->set_shader_param(E->get().name, value);
  321. }
  322. smat->set_render_priority(mat->get_render_priority());
  323. return smat;
  324. }
  325. String PhysicalSkyMaterialConversionPlugin::converts_to() const {
  326. return "ShaderMaterial";
  327. }
  328. bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  329. Ref<PhysicalSkyMaterial> mat = p_resource;
  330. return mat.is_valid();
  331. }
  332. Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  333. Ref<PhysicalSkyMaterial> mat = p_resource;
  334. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  335. Ref<ShaderMaterial> smat;
  336. smat.instance();
  337. Ref<Shader> shader;
  338. shader.instance();
  339. String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
  340. shader->set_code(code);
  341. smat->set_shader(shader);
  342. List<PropertyInfo> params;
  343. RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  344. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  345. Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  346. smat->set_shader_param(E->get().name, value);
  347. }
  348. smat->set_render_priority(mat->get_render_priority());
  349. return smat;
  350. }