material.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************************************/
  2. /* material.h */
  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. #ifndef MATERIAL_H
  31. #define MATERIAL_H
  32. #include "resource.h"
  33. #include "scene/resources/shader.h"
  34. #include "scene/resources/texture.h"
  35. #include "servers/visual/shader_language.h"
  36. #include "servers/visual_server.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class Material : public Resource {
  41. OBJ_TYPE(Material, Resource);
  42. RES_BASE_EXTENSION("mtl");
  43. OBJ_SAVE_TYPE(Material);
  44. public:
  45. enum Flag {
  46. FLAG_VISIBLE = VS::MATERIAL_FLAG_VISIBLE,
  47. FLAG_DOUBLE_SIDED = VS::MATERIAL_FLAG_DOUBLE_SIDED,
  48. FLAG_INVERT_FACES = VS::MATERIAL_FLAG_INVERT_FACES,
  49. FLAG_UNSHADED = VS::MATERIAL_FLAG_UNSHADED,
  50. FLAG_ONTOP = VS::MATERIAL_FLAG_ONTOP,
  51. FLAG_LIGHTMAP_ON_UV2 = VS::MATERIAL_FLAG_LIGHTMAP_ON_UV2,
  52. FLAG_COLOR_ARRAY_SRGB = VS::MATERIAL_FLAG_COLOR_ARRAY_SRGB,
  53. FLAG_MAX = VS::MATERIAL_FLAG_MAX
  54. };
  55. enum BlendMode {
  56. BLEND_MODE_MIX = VS::MATERIAL_BLEND_MODE_MIX,
  57. BLEND_MODE_MUL = VS::MATERIAL_BLEND_MODE_MUL,
  58. BLEND_MODE_ADD = VS::MATERIAL_BLEND_MODE_ADD,
  59. BLEND_MODE_SUB = VS::MATERIAL_BLEND_MODE_SUB,
  60. BLEND_MODE_PREMULT_ALPHA = VS::MATERIAL_BLEND_MODE_PREMULT_ALPHA,
  61. };
  62. enum DepthDrawMode {
  63. DEPTH_DRAW_ALWAYS = VS::MATERIAL_DEPTH_DRAW_ALWAYS,
  64. DEPTH_DRAW_OPAQUE_ONLY = VS::MATERIAL_DEPTH_DRAW_OPAQUE_ONLY,
  65. DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA = VS::MATERIAL_DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA,
  66. DEPTH_DRAW_NEVER = VS::MATERIAL_DEPTH_DRAW_NEVER
  67. };
  68. private:
  69. BlendMode blend_mode;
  70. bool flags[VS::MATERIAL_FLAG_MAX];
  71. float line_width;
  72. DepthDrawMode depth_draw_mode;
  73. protected:
  74. RID material;
  75. static void _bind_methods();
  76. public:
  77. void set_flag(Flag p_flag, bool p_enabled);
  78. bool get_flag(Flag p_flag) const;
  79. void set_blend_mode(BlendMode p_blend_mode);
  80. BlendMode get_blend_mode() const;
  81. void set_depth_draw_mode(DepthDrawMode p_depth_draw_mode);
  82. DepthDrawMode get_depth_draw_mode() const;
  83. void set_line_width(float p_width);
  84. float get_line_width() const;
  85. virtual RID get_rid() const;
  86. Material(const RID &p_rid = RID());
  87. virtual ~Material();
  88. };
  89. VARIANT_ENUM_CAST(Material::Flag);
  90. VARIANT_ENUM_CAST(Material::DepthDrawMode);
  91. VARIANT_ENUM_CAST(Material::BlendMode);
  92. class FixedMaterial : public Material {
  93. OBJ_TYPE(FixedMaterial, Material);
  94. REVERSE_GET_PROPERTY_LIST
  95. public:
  96. enum Parameter {
  97. PARAM_DIFFUSE = VS::FIXED_MATERIAL_PARAM_DIFFUSE,
  98. PARAM_DETAIL = VS::FIXED_MATERIAL_PARAM_DETAIL,
  99. PARAM_SPECULAR = VS::FIXED_MATERIAL_PARAM_SPECULAR,
  100. PARAM_EMISSION = VS::FIXED_MATERIAL_PARAM_EMISSION,
  101. PARAM_SPECULAR_EXP = VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP,
  102. PARAM_GLOW = VS::FIXED_MATERIAL_PARAM_GLOW,
  103. PARAM_NORMAL = VS::FIXED_MATERIAL_PARAM_NORMAL,
  104. PARAM_SHADE_PARAM = VS::FIXED_MATERIAL_PARAM_SHADE_PARAM,
  105. PARAM_MAX = VS::FIXED_MATERIAL_PARAM_MAX
  106. };
  107. enum TexCoordMode {
  108. TEXCOORD_UV = VS::FIXED_MATERIAL_TEXCOORD_UV,
  109. TEXCOORD_UV_TRANSFORM = VS::FIXED_MATERIAL_TEXCOORD_UV_TRANSFORM,
  110. TEXCOORD_UV2 = VS::FIXED_MATERIAL_TEXCOORD_UV2,
  111. TEXCOORD_SPHERE = VS::FIXED_MATERIAL_TEXCOORD_SPHERE
  112. };
  113. enum FixedFlag {
  114. FLAG_USE_ALPHA = VS::FIXED_MATERIAL_FLAG_USE_ALPHA,
  115. FLAG_USE_COLOR_ARRAY = VS::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,
  116. FLAG_USE_POINT_SIZE = VS::FIXED_MATERIAL_FLAG_USE_POINT_SIZE,
  117. FLAG_DISCARD_ALPHA = VS::FIXED_MATERIAL_FLAG_DISCARD_ALPHA,
  118. FLAG_USE_XY_NORMALMAP = VS::FIXED_MATERIAL_FLAG_USE_XY_NORMALMAP,
  119. FLAG_MAX = VS::FIXED_MATERIAL_FLAG_MAX
  120. };
  121. enum LightShader {
  122. LIGHT_SHADER_LAMBERT = VS::FIXED_MATERIAL_LIGHT_SHADER_LAMBERT,
  123. LIGHT_SHADER_WRAP = VS::FIXED_MATERIAL_LIGHT_SHADER_WRAP,
  124. LIGHT_SHADER_VELVET = VS::FIXED_MATERIAL_LIGHT_SHADER_VELVET,
  125. LIGHT_SHADER_TOON = VS::FIXED_MATERIAL_LIGHT_SHADER_TOON
  126. };
  127. private:
  128. struct Node {
  129. int param;
  130. int mult;
  131. int tex;
  132. };
  133. Variant param[PARAM_MAX];
  134. Ref<Texture> texture_param[PARAM_MAX];
  135. TexCoordMode texture_texcoord[PARAM_MAX];
  136. LightShader light_shader;
  137. bool fixed_flags[FLAG_MAX];
  138. float point_size;
  139. Transform uv_transform;
  140. protected:
  141. static void _bind_methods();
  142. public:
  143. void set_fixed_flag(FixedFlag p_flag, bool p_value);
  144. bool get_fixed_flag(FixedFlag p_flag) const;
  145. void set_parameter(Parameter p_parameter, const Variant &p_value);
  146. Variant get_parameter(Parameter p_parameter) const;
  147. void set_texture(Parameter p_parameter, Ref<Texture> p_texture);
  148. Ref<Texture> get_texture(Parameter p_parameter) const;
  149. void set_texcoord_mode(Parameter p_parameter, TexCoordMode p_mode);
  150. TexCoordMode get_texcoord_mode(Parameter p_parameter) const;
  151. void set_light_shader(LightShader p_shader);
  152. LightShader get_light_shader() const;
  153. void set_uv_transform(const Transform &p_transform);
  154. Transform get_uv_transform() const;
  155. void set_point_size(float p_transform);
  156. float get_point_size() const;
  157. FixedMaterial();
  158. ~FixedMaterial();
  159. };
  160. VARIANT_ENUM_CAST(FixedMaterial::Parameter);
  161. VARIANT_ENUM_CAST(FixedMaterial::TexCoordMode);
  162. VARIANT_ENUM_CAST(FixedMaterial::FixedFlag);
  163. VARIANT_ENUM_CAST(FixedMaterial::LightShader);
  164. class ShaderMaterial : public Material {
  165. OBJ_TYPE(ShaderMaterial, Material);
  166. Ref<Shader> shader;
  167. void _shader_changed();
  168. static void _shader_parse(void *p_self, ShaderLanguage::ProgramNode *p_node);
  169. protected:
  170. bool _set(const StringName &p_name, const Variant &p_value);
  171. bool _get(const StringName &p_name, Variant &r_ret) const;
  172. void _get_property_list(List<PropertyInfo> *p_list) const;
  173. static void _bind_methods();
  174. public:
  175. void set_shader(const Ref<Shader> &p_shader);
  176. Ref<Shader> get_shader() const;
  177. void set_shader_param(const StringName &p_param, const Variant &p_value);
  178. Variant get_shader_param(const StringName &p_param) const;
  179. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  180. ShaderMaterial();
  181. };
  182. //////////////////////
  183. #endif