material_editor_plugin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "scene/resources/particles_material.h"
  32. String SpatialMaterialConversionPlugin::converts_to() const {
  33. return "ShaderMaterial";
  34. }
  35. bool SpatialMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  36. Ref<SpatialMaterial> mat = p_resource;
  37. return mat.is_valid();
  38. }
  39. Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  40. Ref<SpatialMaterial> mat = p_resource;
  41. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  42. Ref<ShaderMaterial> smat;
  43. smat.instance();
  44. Ref<Shader> shader;
  45. shader.instance();
  46. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  47. shader->set_code(code);
  48. smat->set_shader(shader);
  49. List<PropertyInfo> params;
  50. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  51. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  52. // Texture parameter has to be treated specially since SpatialMaterial saved it
  53. // as RID but ShaderMaterial needs Texture itself
  54. Ref<Texture> texture = mat->get_texture_by_name(E->get().name);
  55. if (texture.is_valid()) {
  56. smat->set_shader_param(E->get().name, texture);
  57. } else {
  58. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  59. smat->set_shader_param(E->get().name, value);
  60. }
  61. }
  62. smat->set_render_priority(mat->get_render_priority());
  63. return smat;
  64. }
  65. String ParticlesMaterialConversionPlugin::converts_to() const {
  66. return "ShaderMaterial";
  67. }
  68. bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  69. Ref<ParticlesMaterial> mat = p_resource;
  70. return mat.is_valid();
  71. }
  72. Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  73. Ref<ParticlesMaterial> mat = p_resource;
  74. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  75. Ref<ShaderMaterial> smat;
  76. smat.instance();
  77. Ref<Shader> shader;
  78. shader.instance();
  79. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  80. shader->set_code(code);
  81. smat->set_shader(shader);
  82. List<PropertyInfo> params;
  83. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  84. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  85. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  86. smat->set_shader_param(E->get().name, value);
  87. }
  88. smat->set_render_priority(mat->get_render_priority());
  89. return smat;
  90. }
  91. String CanvasItemMaterialConversionPlugin::converts_to() const {
  92. return "ShaderMaterial";
  93. }
  94. bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  95. Ref<CanvasItemMaterial> mat = p_resource;
  96. return mat.is_valid();
  97. }
  98. Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  99. Ref<CanvasItemMaterial> mat = p_resource;
  100. ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
  101. Ref<ShaderMaterial> smat;
  102. smat.instance();
  103. Ref<Shader> shader;
  104. shader.instance();
  105. String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
  106. shader->set_code(code);
  107. smat->set_shader(shader);
  108. List<PropertyInfo> params;
  109. VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
  110. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  111. Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
  112. smat->set_shader_param(E->get().name, value);
  113. }
  114. smat->set_render_priority(mat->get_render_priority());
  115. return smat;
  116. }