material.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*************************************************************************/
  2. /* material.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "self_list.h"
  36. #include "servers/visual/shader_language.h"
  37. #include "servers/visual_server.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class Material : public Resource {
  42. GDCLASS(Material, Resource)
  43. RES_BASE_EXTENSION("material")
  44. OBJ_SAVE_TYPE(Material)
  45. RID material;
  46. Ref<Material> next_pass;
  47. int render_priority;
  48. protected:
  49. _FORCE_INLINE_ RID _get_material() const { return material; }
  50. static void _bind_methods();
  51. virtual bool _can_do_next_pass() const { return false; }
  52. void _validate_property(PropertyInfo &property) const;
  53. public:
  54. enum {
  55. RENDER_PRIORITY_MAX = VS::MATERIAL_RENDER_PRIORITY_MAX,
  56. RENDER_PRIORITY_MIN = VS::MATERIAL_RENDER_PRIORITY_MIN,
  57. };
  58. void set_next_pass(const Ref<Material> &p_pass);
  59. Ref<Material> get_next_pass() const;
  60. void set_render_priority(int p_priority);
  61. int get_render_priority() const;
  62. virtual RID get_rid() const;
  63. Material();
  64. virtual ~Material();
  65. };
  66. class ShaderMaterial : public Material {
  67. GDCLASS(ShaderMaterial, Material);
  68. Ref<Shader> shader;
  69. protected:
  70. bool _set(const StringName &p_name, const Variant &p_value);
  71. bool _get(const StringName &p_name, Variant &r_ret) const;
  72. void _get_property_list(List<PropertyInfo> *p_list) const;
  73. static void _bind_methods();
  74. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  75. virtual bool _can_do_next_pass() const;
  76. public:
  77. void set_shader(const Ref<Shader> &p_shader);
  78. Ref<Shader> get_shader() const;
  79. void set_shader_param(const StringName &p_param, const Variant &p_value);
  80. Variant get_shader_param(const StringName &p_param) const;
  81. ShaderMaterial();
  82. ~ShaderMaterial();
  83. };
  84. class SpatialMaterial : public Material {
  85. GDCLASS(SpatialMaterial, Material)
  86. public:
  87. enum TextureParam {
  88. TEXTURE_ALBEDO,
  89. TEXTURE_METALLIC,
  90. TEXTURE_ROUGHNESS,
  91. TEXTURE_EMISSION,
  92. TEXTURE_NORMAL,
  93. TEXTURE_RIM,
  94. TEXTURE_CLEARCOAT,
  95. TEXTURE_FLOWMAP,
  96. TEXTURE_AMBIENT_OCCLUSION,
  97. TEXTURE_DEPTH,
  98. TEXTURE_SUBSURFACE_SCATTERING,
  99. TEXTURE_TRANSMISSION,
  100. TEXTURE_REFRACTION,
  101. TEXTURE_DETAIL_MASK,
  102. TEXTURE_DETAIL_ALBEDO,
  103. TEXTURE_DETAIL_NORMAL,
  104. TEXTURE_MAX
  105. };
  106. enum DetailUV {
  107. DETAIL_UV_1,
  108. DETAIL_UV_2
  109. };
  110. enum Feature {
  111. FEATURE_TRANSPARENT,
  112. FEATURE_EMISSION,
  113. FEATURE_NORMAL_MAPPING,
  114. FEATURE_RIM,
  115. FEATURE_CLEARCOAT,
  116. FEATURE_ANISOTROPY,
  117. FEATURE_AMBIENT_OCCLUSION,
  118. FEATURE_DEPTH_MAPPING,
  119. FEATURE_SUBSURACE_SCATTERING,
  120. FEATURE_TRANSMISSION,
  121. FEATURE_REFRACTION,
  122. FEATURE_DETAIL,
  123. FEATURE_MAX
  124. };
  125. enum BlendMode {
  126. BLEND_MODE_MIX,
  127. BLEND_MODE_ADD,
  128. BLEND_MODE_SUB,
  129. BLEND_MODE_MUL,
  130. };
  131. enum DepthDrawMode {
  132. DEPTH_DRAW_OPAQUE_ONLY,
  133. DEPTH_DRAW_ALWAYS,
  134. DEPTH_DRAW_DISABLED,
  135. DEPTH_DRAW_ALPHA_OPAQUE_PREPASS
  136. };
  137. enum CullMode {
  138. CULL_BACK,
  139. CULL_FRONT,
  140. CULL_DISABLED
  141. };
  142. enum Flags {
  143. FLAG_UNSHADED,
  144. FLAG_USE_VERTEX_LIGHTING,
  145. FLAG_DISABLE_DEPTH_TEST,
  146. FLAG_ALBEDO_FROM_VERTEX_COLOR,
  147. FLAG_SRGB_VERTEX_COLOR,
  148. FLAG_USE_POINT_SIZE,
  149. FLAG_FIXED_SIZE,
  150. FLAG_UV1_USE_TRIPLANAR,
  151. FLAG_UV2_USE_TRIPLANAR,
  152. FLAG_TRIPLANAR_USE_WORLD,
  153. FLAG_AO_ON_UV2,
  154. FLAG_USE_ALPHA_SCISSOR,
  155. FLAG_MAX
  156. };
  157. enum DiffuseMode {
  158. DIFFUSE_BURLEY,
  159. DIFFUSE_LAMBERT,
  160. DIFFUSE_LAMBERT_WRAP,
  161. DIFFUSE_OREN_NAYAR,
  162. DIFFUSE_TOON,
  163. };
  164. enum SpecularMode {
  165. SPECULAR_SCHLICK_GGX,
  166. SPECULAR_BLINN,
  167. SPECULAR_PHONG,
  168. SPECULAR_TOON,
  169. SPECULAR_DISABLED,
  170. };
  171. enum BillboardMode {
  172. BILLBOARD_DISABLED,
  173. BILLBOARD_ENABLED,
  174. BILLBOARD_FIXED_Y,
  175. BILLBOARD_PARTICLES,
  176. };
  177. enum TextureChannel {
  178. TEXTURE_CHANNEL_RED,
  179. TEXTURE_CHANNEL_GREEN,
  180. TEXTURE_CHANNEL_BLUE,
  181. TEXTURE_CHANNEL_ALPHA,
  182. TEXTURE_CHANNEL_GRAYSCALE
  183. };
  184. private:
  185. union MaterialKey {
  186. struct {
  187. uint64_t feature_mask : 12;
  188. uint64_t detail_uv : 1;
  189. uint64_t blend_mode : 2;
  190. uint64_t depth_draw_mode : 2;
  191. uint64_t cull_mode : 2;
  192. uint64_t flags : 12;
  193. uint64_t detail_blend_mode : 2;
  194. uint64_t diffuse_mode : 3;
  195. uint64_t specular_mode : 2;
  196. uint64_t invalid_key : 1;
  197. uint64_t deep_parallax : 1;
  198. uint64_t billboard_mode : 2;
  199. uint64_t grow : 1;
  200. uint64_t proximity_fade : 1;
  201. uint64_t distance_fade : 1;
  202. };
  203. uint64_t key;
  204. bool operator<(const MaterialKey &p_key) const {
  205. return key < p_key.key;
  206. }
  207. };
  208. struct ShaderData {
  209. RID shader;
  210. int users;
  211. };
  212. static Map<MaterialKey, ShaderData> shader_map;
  213. MaterialKey current_key;
  214. _FORCE_INLINE_ MaterialKey _compute_key() const {
  215. MaterialKey mk;
  216. mk.key = 0;
  217. for (int i = 0; i < FEATURE_MAX; i++) {
  218. if (features[i]) {
  219. mk.feature_mask |= (1 << i);
  220. }
  221. }
  222. mk.detail_uv = detail_uv;
  223. mk.blend_mode = blend_mode;
  224. mk.depth_draw_mode = depth_draw_mode;
  225. mk.cull_mode = cull_mode;
  226. for (int i = 0; i < FLAG_MAX; i++) {
  227. if (flags[i]) {
  228. mk.flags |= (1 << i);
  229. }
  230. }
  231. mk.detail_blend_mode = detail_blend_mode;
  232. mk.diffuse_mode = diffuse_mode;
  233. mk.specular_mode = specular_mode;
  234. mk.billboard_mode = billboard_mode;
  235. mk.deep_parallax = deep_parallax ? 1 : 0;
  236. mk.grow = grow_enabled;
  237. mk.proximity_fade = proximity_fade_enabled;
  238. mk.distance_fade = distance_fade_enabled;
  239. return mk;
  240. }
  241. struct ShaderNames {
  242. StringName albedo;
  243. StringName specular;
  244. StringName metallic;
  245. StringName roughness;
  246. StringName emission;
  247. StringName emission_energy;
  248. StringName normal_scale;
  249. StringName rim;
  250. StringName rim_tint;
  251. StringName clearcoat;
  252. StringName clearcoat_gloss;
  253. StringName anisotropy;
  254. StringName depth_scale;
  255. StringName subsurface_scattering_strength;
  256. StringName transmission;
  257. StringName refraction;
  258. StringName point_size;
  259. StringName uv1_scale;
  260. StringName uv1_offset;
  261. StringName uv2_scale;
  262. StringName uv2_offset;
  263. StringName particles_anim_h_frames;
  264. StringName particles_anim_v_frames;
  265. StringName particles_anim_loop;
  266. StringName depth_min_layers;
  267. StringName depth_max_layers;
  268. StringName uv1_blend_sharpness;
  269. StringName uv2_blend_sharpness;
  270. StringName grow;
  271. StringName proximity_fade_distance;
  272. StringName distance_fade_min;
  273. StringName distance_fade_max;
  274. StringName ao_light_affect;
  275. StringName metallic_texture_channel;
  276. StringName roughness_texture_channel;
  277. StringName ao_texture_channel;
  278. StringName clearcoat_texture_channel;
  279. StringName rim_texture_channel;
  280. StringName depth_texture_channel;
  281. StringName refraction_texture_channel;
  282. StringName alpha_scissor_threshold;
  283. StringName texture_names[TEXTURE_MAX];
  284. };
  285. static Mutex *material_mutex;
  286. static SelfList<SpatialMaterial>::List dirty_materials;
  287. static ShaderNames *shader_names;
  288. SelfList<SpatialMaterial> element;
  289. void _update_shader();
  290. _FORCE_INLINE_ void _queue_shader_change();
  291. _FORCE_INLINE_ bool _is_shader_dirty() const;
  292. Color albedo;
  293. float specular;
  294. float metallic;
  295. float roughness;
  296. Color emission;
  297. float emission_energy;
  298. float normal_scale;
  299. float rim;
  300. float rim_tint;
  301. float clearcoat;
  302. float clearcoat_gloss;
  303. float anisotropy;
  304. float depth_scale;
  305. float subsurface_scattering_strength;
  306. Color transmission;
  307. float refraction;
  308. float line_width;
  309. float point_size;
  310. float alpha_scissor_threshold;
  311. bool grow_enabled;
  312. float ao_light_affect;
  313. float grow;
  314. int particles_anim_h_frames;
  315. int particles_anim_v_frames;
  316. bool particles_anim_loop;
  317. Vector3 uv1_scale;
  318. Vector3 uv1_offset;
  319. float uv1_triplanar_sharpness;
  320. Vector3 uv2_scale;
  321. Vector3 uv2_offset;
  322. float uv2_triplanar_sharpness;
  323. DetailUV detail_uv;
  324. bool deep_parallax;
  325. int deep_parallax_min_layers;
  326. int deep_parallax_max_layers;
  327. bool proximity_fade_enabled;
  328. float proximity_fade_distance;
  329. bool distance_fade_enabled;
  330. float distance_fade_max_distance;
  331. float distance_fade_min_distance;
  332. BlendMode blend_mode;
  333. BlendMode detail_blend_mode;
  334. DepthDrawMode depth_draw_mode;
  335. CullMode cull_mode;
  336. bool flags[FLAG_MAX];
  337. SpecularMode specular_mode;
  338. DiffuseMode diffuse_mode;
  339. BillboardMode billboard_mode;
  340. TextureChannel metallic_texture_channel;
  341. TextureChannel roughness_texture_channel;
  342. TextureChannel ao_texture_channel;
  343. TextureChannel refraction_texture_channel;
  344. bool features[FEATURE_MAX];
  345. Ref<Texture> textures[TEXTURE_MAX];
  346. _FORCE_INLINE_ void _validate_feature(const String &text, Feature feature, PropertyInfo &property) const;
  347. enum {
  348. MAX_MATERIALS_FOR_2D = 32
  349. };
  350. static Ref<SpatialMaterial> materials_for_2d[MAX_MATERIALS_FOR_2D]; //used by Sprite3D and other stuff
  351. protected:
  352. static void _bind_methods();
  353. void _validate_property(PropertyInfo &property) const;
  354. virtual bool _can_do_next_pass() const { return true; }
  355. public:
  356. void set_albedo(const Color &p_albedo);
  357. Color get_albedo() const;
  358. void set_specular(float p_specular);
  359. float get_specular() const;
  360. void set_metallic(float p_metallic);
  361. float get_metallic() const;
  362. void set_roughness(float p_roughness);
  363. float get_roughness() const;
  364. void set_emission(const Color &p_emission);
  365. Color get_emission() const;
  366. void set_emission_energy(float p_emission_energy);
  367. float get_emission_energy() const;
  368. void set_normal_scale(float p_normal_scale);
  369. float get_normal_scale() const;
  370. void set_rim(float p_rim);
  371. float get_rim() const;
  372. void set_rim_tint(float p_rim_tint);
  373. float get_rim_tint() const;
  374. void set_ao_light_affect(float p_ao_light_affect);
  375. float get_ao_light_affect() const;
  376. void set_clearcoat(float p_clearcoat);
  377. float get_clearcoat() const;
  378. void set_clearcoat_gloss(float p_clearcoat_gloss);
  379. float get_clearcoat_gloss() const;
  380. void set_anisotropy(float p_anisotropy);
  381. float get_anisotropy() const;
  382. void set_depth_scale(float p_depth_scale);
  383. float get_depth_scale() const;
  384. void set_depth_deep_parallax(bool p_enable);
  385. bool is_depth_deep_parallax_enabled() const;
  386. void set_depth_deep_parallax_min_layers(int p_layer);
  387. int get_depth_deep_parallax_min_layers() const;
  388. void set_depth_deep_parallax_max_layers(int p_layer);
  389. int get_depth_deep_parallax_max_layers() const;
  390. void set_subsurface_scattering_strength(float p_subsurface_scattering_strength);
  391. float get_subsurface_scattering_strength() const;
  392. void set_transmission(const Color &p_transmission);
  393. Color get_transmission() const;
  394. void set_refraction(float p_refraction);
  395. float get_refraction() const;
  396. void set_line_width(float p_line_width);
  397. float get_line_width() const;
  398. void set_point_size(float p_point_size);
  399. float get_point_size() const;
  400. void set_detail_uv(DetailUV p_detail_uv);
  401. DetailUV get_detail_uv() const;
  402. void set_blend_mode(BlendMode p_mode);
  403. BlendMode get_blend_mode() const;
  404. void set_detail_blend_mode(BlendMode p_mode);
  405. BlendMode get_detail_blend_mode() const;
  406. void set_depth_draw_mode(DepthDrawMode p_mode);
  407. DepthDrawMode get_depth_draw_mode() const;
  408. void set_cull_mode(CullMode p_mode);
  409. CullMode get_cull_mode() const;
  410. void set_diffuse_mode(DiffuseMode p_mode);
  411. DiffuseMode get_diffuse_mode() const;
  412. void set_specular_mode(SpecularMode p_mode);
  413. SpecularMode get_specular_mode() const;
  414. void set_flag(Flags p_flag, bool p_enabled);
  415. bool get_flag(Flags p_flag) const;
  416. void set_texture(TextureParam p_param, const Ref<Texture> &p_texture);
  417. Ref<Texture> get_texture(TextureParam p_param) const;
  418. // Used only for shader material conversion
  419. Ref<Texture> get_texture_by_name(StringName p_name) const;
  420. void set_feature(Feature p_feature, bool p_enabled);
  421. bool get_feature(Feature p_feature) const;
  422. void set_uv1_scale(const Vector3 &p_scale);
  423. Vector3 get_uv1_scale() const;
  424. void set_uv1_offset(const Vector3 &p_offset);
  425. Vector3 get_uv1_offset() const;
  426. void set_uv1_triplanar_blend_sharpness(float p_sharpness);
  427. float get_uv1_triplanar_blend_sharpness() const;
  428. void set_uv2_scale(const Vector3 &p_scale);
  429. Vector3 get_uv2_scale() const;
  430. void set_uv2_offset(const Vector3 &p_offset);
  431. Vector3 get_uv2_offset() const;
  432. void set_uv2_triplanar_blend_sharpness(float p_sharpness);
  433. float get_uv2_triplanar_blend_sharpness() const;
  434. void set_billboard_mode(BillboardMode p_mode);
  435. BillboardMode get_billboard_mode() const;
  436. void set_particles_anim_h_frames(int p_frames);
  437. int get_particles_anim_h_frames() const;
  438. void set_particles_anim_v_frames(int p_frames);
  439. int get_particles_anim_v_frames() const;
  440. void set_particles_anim_loop(int p_frames);
  441. int get_particles_anim_loop() const;
  442. void set_grow_enabled(bool p_enable);
  443. bool is_grow_enabled() const;
  444. void set_grow(float p_grow);
  445. float get_grow() const;
  446. void set_alpha_scissor_threshold(float p_treshold);
  447. float get_alpha_scissor_threshold() const;
  448. void set_on_top_of_alpha();
  449. void set_proximity_fade(bool p_enable);
  450. bool is_proximity_fade_enabled() const;
  451. void set_proximity_fade_distance(float p_distance);
  452. float get_proximity_fade_distance() const;
  453. void set_distance_fade(bool p_enable);
  454. bool is_distance_fade_enabled() const;
  455. void set_distance_fade_max_distance(float p_distance);
  456. float get_distance_fade_max_distance() const;
  457. void set_distance_fade_min_distance(float p_distance);
  458. float get_distance_fade_min_distance() const;
  459. void set_metallic_texture_channel(TextureChannel p_channel);
  460. TextureChannel get_metallic_texture_channel() const;
  461. void set_roughness_texture_channel(TextureChannel p_channel);
  462. TextureChannel get_roughness_texture_channel() const;
  463. void set_ao_texture_channel(TextureChannel p_channel);
  464. TextureChannel get_ao_texture_channel() const;
  465. void set_refraction_texture_channel(TextureChannel p_channel);
  466. TextureChannel get_refraction_texture_channel() const;
  467. static void init_shaders();
  468. static void finish_shaders();
  469. static void flush_changes();
  470. static RID get_material_rid_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass);
  471. RID get_shader_rid() const;
  472. SpatialMaterial();
  473. virtual ~SpatialMaterial();
  474. };
  475. VARIANT_ENUM_CAST(SpatialMaterial::TextureParam)
  476. VARIANT_ENUM_CAST(SpatialMaterial::DetailUV)
  477. VARIANT_ENUM_CAST(SpatialMaterial::Feature)
  478. VARIANT_ENUM_CAST(SpatialMaterial::BlendMode)
  479. VARIANT_ENUM_CAST(SpatialMaterial::DepthDrawMode)
  480. VARIANT_ENUM_CAST(SpatialMaterial::CullMode)
  481. VARIANT_ENUM_CAST(SpatialMaterial::Flags)
  482. VARIANT_ENUM_CAST(SpatialMaterial::DiffuseMode)
  483. VARIANT_ENUM_CAST(SpatialMaterial::SpecularMode)
  484. VARIANT_ENUM_CAST(SpatialMaterial::BillboardMode)
  485. VARIANT_ENUM_CAST(SpatialMaterial::TextureChannel)
  486. //////////////////////
  487. #endif