material.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /**************************************************************************/
  2. /* material.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/io/resource.h"
  33. #include "core/templates/self_list.h"
  34. #include "scene/resources/shader.h"
  35. #include "scene/resources/texture.h"
  36. #include "servers/rendering_server.h"
  37. class Material : public Resource {
  38. GDCLASS(Material, Resource);
  39. RES_BASE_EXTENSION("material")
  40. OBJ_SAVE_TYPE(Material);
  41. mutable RID material;
  42. Ref<Material> next_pass;
  43. int render_priority;
  44. enum {
  45. INIT_STATE_UNINITIALIZED,
  46. INIT_STATE_INITIALIZING,
  47. INIT_STATE_READY,
  48. } init_state = INIT_STATE_UNINITIALIZED;
  49. void inspect_native_shader_code();
  50. protected:
  51. _FORCE_INLINE_ void _set_material(RID p_material) const { material = p_material; }
  52. _FORCE_INLINE_ RID _get_material() const { return material; }
  53. static void _bind_methods();
  54. virtual bool _can_do_next_pass() const;
  55. virtual bool _can_use_render_priority() const;
  56. void _validate_property(PropertyInfo &p_property) const;
  57. void _mark_ready();
  58. void _mark_initialized(const Callable &p_add_to_dirty_list, const Callable &p_update_shader);
  59. bool _is_initialized() { return init_state == INIT_STATE_READY; }
  60. GDVIRTUAL0RC_REQUIRED(RID, _get_shader_rid)
  61. GDVIRTUAL0RC_REQUIRED(Shader::Mode, _get_shader_mode)
  62. GDVIRTUAL0RC(bool, _can_do_next_pass)
  63. GDVIRTUAL0RC(bool, _can_use_render_priority)
  64. public:
  65. enum {
  66. RENDER_PRIORITY_MAX = RS::MATERIAL_RENDER_PRIORITY_MAX,
  67. RENDER_PRIORITY_MIN = RS::MATERIAL_RENDER_PRIORITY_MIN,
  68. };
  69. void set_next_pass(const Ref<Material> &p_pass);
  70. Ref<Material> get_next_pass() const;
  71. void set_render_priority(int p_priority);
  72. int get_render_priority() const;
  73. virtual RID get_rid() const override;
  74. virtual RID get_shader_rid() const;
  75. virtual Shader::Mode get_shader_mode() const;
  76. virtual Ref<Resource> create_placeholder() const;
  77. Material();
  78. virtual ~Material();
  79. };
  80. class ShaderMaterial : public Material {
  81. GDCLASS(ShaderMaterial, Material);
  82. Ref<Shader> shader;
  83. mutable HashMap<StringName, StringName> remap_cache;
  84. mutable HashMap<StringName, Variant> param_cache;
  85. mutable Mutex material_rid_mutex;
  86. protected:
  87. bool _set(const StringName &p_name, const Variant &p_value);
  88. bool _get(const StringName &p_name, Variant &r_ret) const;
  89. void _get_property_list(List<PropertyInfo> *p_list) const;
  90. bool _property_can_revert(const StringName &p_name) const;
  91. bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
  92. static void _bind_methods();
  93. #ifdef TOOLS_ENABLED
  94. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  95. #endif
  96. virtual bool _can_do_next_pass() const override;
  97. virtual bool _can_use_render_priority() const override;
  98. void _shader_changed();
  99. void _check_material_rid() const;
  100. public:
  101. void set_shader(const Ref<Shader> &p_shader);
  102. Ref<Shader> get_shader() const;
  103. void set_shader_parameter(const StringName &p_param, const Variant &p_value);
  104. Variant get_shader_parameter(const StringName &p_param) const;
  105. virtual Shader::Mode get_shader_mode() const override;
  106. virtual RID get_rid() const override;
  107. virtual RID get_shader_rid() const override;
  108. ShaderMaterial();
  109. ~ShaderMaterial();
  110. };
  111. class StandardMaterial3D;
  112. class BaseMaterial3D : public Material {
  113. GDCLASS(BaseMaterial3D, Material);
  114. private:
  115. mutable Mutex material_rid_mutex;
  116. public:
  117. enum TextureParam {
  118. TEXTURE_ALBEDO,
  119. TEXTURE_METALLIC,
  120. TEXTURE_ROUGHNESS,
  121. TEXTURE_EMISSION,
  122. TEXTURE_NORMAL,
  123. TEXTURE_RIM,
  124. TEXTURE_CLEARCOAT,
  125. TEXTURE_FLOWMAP,
  126. TEXTURE_AMBIENT_OCCLUSION,
  127. TEXTURE_HEIGHTMAP,
  128. TEXTURE_SUBSURFACE_SCATTERING,
  129. TEXTURE_SUBSURFACE_TRANSMITTANCE,
  130. TEXTURE_BACKLIGHT,
  131. TEXTURE_REFRACTION,
  132. TEXTURE_DETAIL_MASK,
  133. TEXTURE_DETAIL_ALBEDO,
  134. TEXTURE_DETAIL_NORMAL,
  135. TEXTURE_ORM,
  136. TEXTURE_MAX
  137. };
  138. enum TextureFilter {
  139. TEXTURE_FILTER_NEAREST,
  140. TEXTURE_FILTER_LINEAR,
  141. TEXTURE_FILTER_NEAREST_WITH_MIPMAPS,
  142. TEXTURE_FILTER_LINEAR_WITH_MIPMAPS,
  143. TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC,
  144. TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC,
  145. TEXTURE_FILTER_MAX
  146. };
  147. enum DetailUV {
  148. DETAIL_UV_1,
  149. DETAIL_UV_2,
  150. DETAIL_UV_MAX
  151. };
  152. enum Transparency {
  153. TRANSPARENCY_DISABLED,
  154. TRANSPARENCY_ALPHA,
  155. TRANSPARENCY_ALPHA_SCISSOR,
  156. TRANSPARENCY_ALPHA_HASH,
  157. TRANSPARENCY_ALPHA_DEPTH_PRE_PASS,
  158. TRANSPARENCY_MAX,
  159. };
  160. enum AlphaAntiAliasing {
  161. ALPHA_ANTIALIASING_OFF,
  162. ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE,
  163. ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE,
  164. ALPHA_ANTIALIASING_MAX
  165. };
  166. enum ShadingMode {
  167. SHADING_MODE_UNSHADED,
  168. SHADING_MODE_PER_PIXEL,
  169. SHADING_MODE_PER_VERTEX,
  170. SHADING_MODE_MAX
  171. };
  172. enum Feature {
  173. FEATURE_EMISSION,
  174. FEATURE_NORMAL_MAPPING,
  175. FEATURE_RIM,
  176. FEATURE_CLEARCOAT,
  177. FEATURE_ANISOTROPY,
  178. FEATURE_AMBIENT_OCCLUSION,
  179. FEATURE_HEIGHT_MAPPING,
  180. FEATURE_SUBSURFACE_SCATTERING,
  181. FEATURE_SUBSURFACE_TRANSMITTANCE,
  182. FEATURE_BACKLIGHT,
  183. FEATURE_REFRACTION,
  184. FEATURE_DETAIL,
  185. FEATURE_MAX
  186. };
  187. enum BlendMode {
  188. BLEND_MODE_MIX,
  189. BLEND_MODE_ADD,
  190. BLEND_MODE_SUB,
  191. BLEND_MODE_MUL,
  192. BLEND_MODE_PREMULT_ALPHA,
  193. BLEND_MODE_MAX
  194. };
  195. enum DepthDrawMode {
  196. DEPTH_DRAW_OPAQUE_ONLY,
  197. DEPTH_DRAW_ALWAYS,
  198. DEPTH_DRAW_DISABLED,
  199. DEPTH_DRAW_MAX
  200. };
  201. enum CullMode {
  202. CULL_BACK,
  203. CULL_FRONT,
  204. CULL_DISABLED,
  205. CULL_MAX
  206. };
  207. enum Flags {
  208. FLAG_DISABLE_DEPTH_TEST,
  209. FLAG_ALBEDO_FROM_VERTEX_COLOR,
  210. FLAG_SRGB_VERTEX_COLOR,
  211. FLAG_USE_POINT_SIZE,
  212. FLAG_FIXED_SIZE,
  213. FLAG_BILLBOARD_KEEP_SCALE,
  214. FLAG_UV1_USE_TRIPLANAR,
  215. FLAG_UV2_USE_TRIPLANAR,
  216. FLAG_UV1_USE_WORLD_TRIPLANAR,
  217. FLAG_UV2_USE_WORLD_TRIPLANAR,
  218. FLAG_AO_ON_UV2,
  219. FLAG_EMISSION_ON_UV2,
  220. FLAG_ALBEDO_TEXTURE_FORCE_SRGB,
  221. FLAG_DONT_RECEIVE_SHADOWS,
  222. FLAG_DISABLE_AMBIENT_LIGHT,
  223. FLAG_USE_SHADOW_TO_OPACITY,
  224. FLAG_USE_TEXTURE_REPEAT,
  225. FLAG_INVERT_HEIGHTMAP,
  226. FLAG_SUBSURFACE_MODE_SKIN,
  227. FLAG_PARTICLE_TRAILS_MODE,
  228. FLAG_ALBEDO_TEXTURE_MSDF,
  229. FLAG_DISABLE_FOG,
  230. FLAG_MAX
  231. };
  232. enum DiffuseMode {
  233. DIFFUSE_BURLEY,
  234. DIFFUSE_LAMBERT,
  235. DIFFUSE_LAMBERT_WRAP,
  236. DIFFUSE_TOON,
  237. DIFFUSE_MAX
  238. };
  239. enum SpecularMode {
  240. SPECULAR_SCHLICK_GGX,
  241. SPECULAR_TOON,
  242. SPECULAR_DISABLED,
  243. SPECULAR_MAX
  244. };
  245. enum BillboardMode {
  246. BILLBOARD_DISABLED,
  247. BILLBOARD_ENABLED,
  248. BILLBOARD_FIXED_Y,
  249. BILLBOARD_PARTICLES,
  250. BILLBOARD_MAX
  251. };
  252. enum TextureChannel {
  253. TEXTURE_CHANNEL_RED,
  254. TEXTURE_CHANNEL_GREEN,
  255. TEXTURE_CHANNEL_BLUE,
  256. TEXTURE_CHANNEL_ALPHA,
  257. TEXTURE_CHANNEL_GRAYSCALE,
  258. TEXTURE_CHANNEL_MAX
  259. };
  260. enum EmissionOperator {
  261. EMISSION_OP_ADD,
  262. EMISSION_OP_MULTIPLY,
  263. EMISSION_OP_MAX
  264. };
  265. enum DistanceFadeMode {
  266. DISTANCE_FADE_DISABLED,
  267. DISTANCE_FADE_PIXEL_ALPHA,
  268. DISTANCE_FADE_PIXEL_DITHER,
  269. DISTANCE_FADE_OBJECT_DITHER,
  270. DISTANCE_FADE_MAX
  271. };
  272. private:
  273. struct MaterialKey {
  274. // enum values
  275. uint64_t texture_filter : get_num_bits(TEXTURE_FILTER_MAX - 1);
  276. uint64_t detail_uv : get_num_bits(DETAIL_UV_MAX - 1);
  277. uint64_t transparency : get_num_bits(TRANSPARENCY_MAX - 1);
  278. uint64_t alpha_antialiasing_mode : get_num_bits(ALPHA_ANTIALIASING_MAX - 1);
  279. uint64_t shading_mode : get_num_bits(SHADING_MODE_MAX - 1);
  280. uint64_t blend_mode : get_num_bits(BLEND_MODE_MAX - 1);
  281. uint64_t depth_draw_mode : get_num_bits(DEPTH_DRAW_MAX - 1);
  282. uint64_t cull_mode : get_num_bits(CULL_MAX - 1);
  283. uint64_t diffuse_mode : get_num_bits(DIFFUSE_MAX - 1);
  284. uint64_t specular_mode : get_num_bits(SPECULAR_MAX - 1);
  285. uint64_t billboard_mode : get_num_bits(BILLBOARD_MAX - 1);
  286. uint64_t detail_blend_mode : get_num_bits(BLEND_MODE_MAX - 1);
  287. uint64_t roughness_channel : get_num_bits(TEXTURE_CHANNEL_MAX - 1);
  288. uint64_t emission_op : get_num_bits(EMISSION_OP_MAX - 1);
  289. uint64_t distance_fade : get_num_bits(DISTANCE_FADE_MAX - 1);
  290. // booleans
  291. uint64_t invalid_key : 1;
  292. uint64_t deep_parallax : 1;
  293. uint64_t grow : 1;
  294. uint64_t proximity_fade : 1;
  295. uint64_t orm : 1;
  296. // flag bitfield
  297. uint32_t feature_mask;
  298. uint32_t flags;
  299. MaterialKey() {
  300. memset(this, 0, sizeof(MaterialKey));
  301. }
  302. static uint32_t hash(const MaterialKey &p_key) {
  303. return hash_djb2_buffer((const uint8_t *)&p_key, sizeof(MaterialKey));
  304. }
  305. bool operator==(const MaterialKey &p_key) const {
  306. return memcmp(this, &p_key, sizeof(MaterialKey)) == 0;
  307. }
  308. bool operator<(const MaterialKey &p_key) const {
  309. return memcmp(this, &p_key, sizeof(MaterialKey)) < 0;
  310. }
  311. };
  312. struct ShaderData {
  313. RID shader;
  314. int users = 0;
  315. };
  316. static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;
  317. static Mutex shader_map_mutex;
  318. MaterialKey current_key;
  319. _FORCE_INLINE_ MaterialKey _compute_key() const {
  320. MaterialKey mk;
  321. mk.detail_uv = detail_uv;
  322. mk.blend_mode = blend_mode;
  323. mk.depth_draw_mode = depth_draw_mode;
  324. mk.cull_mode = cull_mode;
  325. mk.texture_filter = texture_filter;
  326. mk.transparency = transparency;
  327. mk.shading_mode = shading_mode;
  328. mk.roughness_channel = roughness_texture_channel;
  329. mk.detail_blend_mode = detail_blend_mode;
  330. mk.diffuse_mode = diffuse_mode;
  331. mk.specular_mode = specular_mode;
  332. mk.billboard_mode = billboard_mode;
  333. mk.deep_parallax = deep_parallax;
  334. mk.grow = grow_enabled;
  335. mk.proximity_fade = proximity_fade_enabled;
  336. mk.distance_fade = distance_fade;
  337. mk.emission_op = emission_op;
  338. mk.alpha_antialiasing_mode = alpha_antialiasing_mode;
  339. mk.orm = orm;
  340. for (int i = 0; i < FEATURE_MAX; i++) {
  341. if (features[i]) {
  342. mk.feature_mask |= ((uint64_t)1 << i);
  343. }
  344. }
  345. for (int i = 0; i < FLAG_MAX; i++) {
  346. if (flags[i]) {
  347. mk.flags |= ((uint64_t)1 << i);
  348. }
  349. }
  350. return mk;
  351. }
  352. struct ShaderNames {
  353. StringName albedo;
  354. StringName specular;
  355. StringName metallic;
  356. StringName roughness;
  357. StringName emission;
  358. StringName emission_energy;
  359. StringName normal_scale;
  360. StringName rim;
  361. StringName rim_tint;
  362. StringName clearcoat;
  363. StringName clearcoat_roughness;
  364. StringName anisotropy;
  365. StringName heightmap_scale;
  366. StringName subsurface_scattering_strength;
  367. StringName transmittance_color;
  368. StringName transmittance_depth;
  369. StringName transmittance_boost;
  370. StringName backlight;
  371. StringName refraction;
  372. StringName point_size;
  373. StringName uv1_scale;
  374. StringName uv1_offset;
  375. StringName uv2_scale;
  376. StringName uv2_offset;
  377. StringName particles_anim_h_frames;
  378. StringName particles_anim_v_frames;
  379. StringName particles_anim_loop;
  380. StringName heightmap_min_layers;
  381. StringName heightmap_max_layers;
  382. StringName heightmap_flip;
  383. StringName uv1_blend_sharpness;
  384. StringName uv2_blend_sharpness;
  385. StringName grow;
  386. StringName proximity_fade_distance;
  387. StringName msdf_pixel_range;
  388. StringName msdf_outline_size;
  389. StringName distance_fade_min;
  390. StringName distance_fade_max;
  391. StringName ao_light_affect;
  392. StringName metallic_texture_channel;
  393. StringName ao_texture_channel;
  394. StringName clearcoat_texture_channel;
  395. StringName rim_texture_channel;
  396. StringName heightmap_texture_channel;
  397. StringName refraction_texture_channel;
  398. StringName texture_names[TEXTURE_MAX];
  399. StringName alpha_scissor_threshold;
  400. StringName alpha_hash_scale;
  401. StringName alpha_antialiasing_edge;
  402. StringName albedo_texture_size;
  403. };
  404. static ShaderNames *shader_names;
  405. void _mark_dirty();
  406. void _update_shader();
  407. void _check_material_rid();
  408. void _material_set_param(const StringName &p_name, const Variant &p_value);
  409. bool orm;
  410. bool dirty = true;
  411. RID shader_rid;
  412. HashMap<StringName, Variant> pending_params;
  413. Color albedo;
  414. float specular = 0.0f;
  415. float metallic = 0.0f;
  416. float roughness = 0.0f;
  417. Color emission;
  418. float emission_energy_multiplier = 1.0f;
  419. float emission_intensity = 1000.0f; // In nits, equivalent to indoor lighting.
  420. float normal_scale = 0.0f;
  421. float rim = 0.0f;
  422. float rim_tint = 0.0f;
  423. float clearcoat = 0.0f;
  424. float clearcoat_roughness = 0.0f;
  425. float anisotropy = 0.0f;
  426. float heightmap_scale = 0.0f;
  427. float subsurface_scattering_strength = 0.0f;
  428. float transmittance_amount = 0.0f;
  429. Color transmittance_color;
  430. float transmittance_depth = 0.0f;
  431. float transmittance_boost = 0.0f;
  432. Color backlight;
  433. float refraction = 0.0f;
  434. float point_size = 0.0f;
  435. float alpha_scissor_threshold = 0.0f;
  436. float alpha_hash_scale = 0.0f;
  437. float alpha_antialiasing_edge = 0.0f;
  438. bool grow_enabled = false;
  439. float ao_light_affect = 0.0f;
  440. float grow = 0.0f;
  441. int particles_anim_h_frames = 0;
  442. int particles_anim_v_frames = 0;
  443. bool particles_anim_loop = false;
  444. Transparency transparency = TRANSPARENCY_DISABLED;
  445. ShadingMode shading_mode = SHADING_MODE_PER_PIXEL;
  446. TextureFilter texture_filter = TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  447. Vector3 uv1_scale;
  448. Vector3 uv1_offset;
  449. float uv1_triplanar_sharpness = 0.0f;
  450. Vector3 uv2_scale;
  451. Vector3 uv2_offset;
  452. float uv2_triplanar_sharpness = 0.0f;
  453. DetailUV detail_uv = DETAIL_UV_1;
  454. bool deep_parallax = false;
  455. int deep_parallax_min_layers = 0;
  456. int deep_parallax_max_layers = 0;
  457. bool heightmap_parallax_flip_tangent = false;
  458. bool heightmap_parallax_flip_binormal = false;
  459. bool proximity_fade_enabled = false;
  460. float proximity_fade_distance = 0.0f;
  461. float msdf_pixel_range = 4.f;
  462. float msdf_outline_size = 0.f;
  463. DistanceFadeMode distance_fade = DISTANCE_FADE_DISABLED;
  464. float distance_fade_max_distance = 0.0f;
  465. float distance_fade_min_distance = 0.0f;
  466. BlendMode blend_mode = BLEND_MODE_MIX;
  467. BlendMode detail_blend_mode = BLEND_MODE_MIX;
  468. DepthDrawMode depth_draw_mode = DEPTH_DRAW_OPAQUE_ONLY;
  469. CullMode cull_mode = CULL_BACK;
  470. bool flags[FLAG_MAX] = {};
  471. SpecularMode specular_mode = SPECULAR_SCHLICK_GGX;
  472. DiffuseMode diffuse_mode = DIFFUSE_BURLEY;
  473. BillboardMode billboard_mode;
  474. EmissionOperator emission_op = EMISSION_OP_ADD;
  475. TextureChannel metallic_texture_channel;
  476. TextureChannel roughness_texture_channel;
  477. TextureChannel ao_texture_channel;
  478. TextureChannel refraction_texture_channel;
  479. AlphaAntiAliasing alpha_antialiasing_mode = ALPHA_ANTIALIASING_OFF;
  480. bool features[FEATURE_MAX] = {};
  481. Ref<Texture2D> textures[TEXTURE_MAX];
  482. _FORCE_INLINE_ void _validate_feature(const String &text, Feature feature, PropertyInfo &property) const;
  483. static HashMap<uint64_t, Ref<StandardMaterial3D>> materials_for_2d; //used by Sprite3D, Label3D and other stuff
  484. protected:
  485. static void _bind_methods();
  486. void _validate_property(PropertyInfo &p_property) const;
  487. virtual bool _can_do_next_pass() const override { return true; }
  488. virtual bool _can_use_render_priority() const override { return true; }
  489. public:
  490. void set_albedo(const Color &p_albedo);
  491. Color get_albedo() const;
  492. void set_specular(float p_specular);
  493. float get_specular() const;
  494. void set_metallic(float p_metallic);
  495. float get_metallic() const;
  496. void set_roughness(float p_roughness);
  497. float get_roughness() const;
  498. void set_emission(const Color &p_emission);
  499. Color get_emission() const;
  500. void set_emission_energy_multiplier(float p_emission_energy_multiplier);
  501. float get_emission_energy_multiplier() const;
  502. void set_emission_intensity(float p_emission_intensity);
  503. float get_emission_intensity() const;
  504. void set_normal_scale(float p_normal_scale);
  505. float get_normal_scale() const;
  506. void set_rim(float p_rim);
  507. float get_rim() const;
  508. void set_rim_tint(float p_rim_tint);
  509. float get_rim_tint() const;
  510. void set_ao_light_affect(float p_ao_light_affect);
  511. float get_ao_light_affect() const;
  512. void set_clearcoat(float p_clearcoat);
  513. float get_clearcoat() const;
  514. void set_clearcoat_roughness(float p_clearcoat_roughness);
  515. float get_clearcoat_roughness() const;
  516. void set_anisotropy(float p_anisotropy);
  517. float get_anisotropy() const;
  518. void set_heightmap_scale(float p_heightmap_scale);
  519. float get_heightmap_scale() const;
  520. void set_heightmap_deep_parallax(bool p_enable);
  521. bool is_heightmap_deep_parallax_enabled() const;
  522. void set_heightmap_deep_parallax_min_layers(int p_layer);
  523. int get_heightmap_deep_parallax_min_layers() const;
  524. void set_heightmap_deep_parallax_max_layers(int p_layer);
  525. int get_heightmap_deep_parallax_max_layers() const;
  526. void set_heightmap_deep_parallax_flip_tangent(bool p_flip);
  527. bool get_heightmap_deep_parallax_flip_tangent() const;
  528. void set_heightmap_deep_parallax_flip_binormal(bool p_flip);
  529. bool get_heightmap_deep_parallax_flip_binormal() const;
  530. void set_subsurface_scattering_strength(float p_subsurface_scattering_strength);
  531. float get_subsurface_scattering_strength() const;
  532. void set_transmittance_color(const Color &p_color);
  533. Color get_transmittance_color() const;
  534. void set_transmittance_depth(float p_depth);
  535. float get_transmittance_depth() const;
  536. void set_transmittance_boost(float p_boost);
  537. float get_transmittance_boost() const;
  538. void set_backlight(const Color &p_backlight);
  539. Color get_backlight() const;
  540. void set_refraction(float p_refraction);
  541. float get_refraction() const;
  542. void set_point_size(float p_point_size);
  543. float get_point_size() const;
  544. void set_transparency(Transparency p_transparency);
  545. Transparency get_transparency() const;
  546. void set_alpha_antialiasing(AlphaAntiAliasing p_alpha_aa);
  547. AlphaAntiAliasing get_alpha_antialiasing() const;
  548. void set_alpha_antialiasing_edge(float p_edge);
  549. float get_alpha_antialiasing_edge() const;
  550. void set_shading_mode(ShadingMode p_shading_mode);
  551. ShadingMode get_shading_mode() const;
  552. void set_detail_uv(DetailUV p_detail_uv);
  553. DetailUV get_detail_uv() const;
  554. void set_blend_mode(BlendMode p_mode);
  555. BlendMode get_blend_mode() const;
  556. void set_detail_blend_mode(BlendMode p_mode);
  557. BlendMode get_detail_blend_mode() const;
  558. void set_depth_draw_mode(DepthDrawMode p_mode);
  559. DepthDrawMode get_depth_draw_mode() const;
  560. void set_cull_mode(CullMode p_mode);
  561. CullMode get_cull_mode() const;
  562. void set_diffuse_mode(DiffuseMode p_mode);
  563. DiffuseMode get_diffuse_mode() const;
  564. void set_specular_mode(SpecularMode p_mode);
  565. SpecularMode get_specular_mode() const;
  566. void set_flag(Flags p_flag, bool p_enabled);
  567. bool get_flag(Flags p_flag) const;
  568. void set_texture(TextureParam p_param, const Ref<Texture2D> &p_texture);
  569. Ref<Texture2D> get_texture(TextureParam p_param) const;
  570. // Used only for shader material conversion
  571. Ref<Texture2D> get_texture_by_name(const StringName &p_name) const;
  572. void set_texture_filter(TextureFilter p_filter);
  573. TextureFilter get_texture_filter() const;
  574. void set_feature(Feature p_feature, bool p_enabled);
  575. bool get_feature(Feature p_feature) const;
  576. void set_uv1_scale(const Vector3 &p_scale);
  577. Vector3 get_uv1_scale() const;
  578. void set_uv1_offset(const Vector3 &p_offset);
  579. Vector3 get_uv1_offset() const;
  580. void set_uv1_triplanar_blend_sharpness(float p_sharpness);
  581. float get_uv1_triplanar_blend_sharpness() const;
  582. void set_uv2_scale(const Vector3 &p_scale);
  583. Vector3 get_uv2_scale() const;
  584. void set_uv2_offset(const Vector3 &p_offset);
  585. Vector3 get_uv2_offset() const;
  586. void set_uv2_triplanar_blend_sharpness(float p_sharpness);
  587. float get_uv2_triplanar_blend_sharpness() const;
  588. void set_billboard_mode(BillboardMode p_mode);
  589. BillboardMode get_billboard_mode() const;
  590. void set_particles_anim_h_frames(int p_frames);
  591. int get_particles_anim_h_frames() const;
  592. void set_particles_anim_v_frames(int p_frames);
  593. int get_particles_anim_v_frames() const;
  594. void set_particles_anim_loop(bool p_loop);
  595. bool get_particles_anim_loop() const;
  596. void set_grow_enabled(bool p_enable);
  597. bool is_grow_enabled() const;
  598. void set_grow(float p_grow);
  599. float get_grow() const;
  600. void set_alpha_scissor_threshold(float p_threshold);
  601. float get_alpha_scissor_threshold() const;
  602. void set_alpha_hash_scale(float p_scale);
  603. float get_alpha_hash_scale() const;
  604. void set_on_top_of_alpha();
  605. void set_proximity_fade_enabled(bool p_enable);
  606. bool is_proximity_fade_enabled() const;
  607. void set_proximity_fade_distance(float p_distance);
  608. float get_proximity_fade_distance() const;
  609. void set_msdf_pixel_range(float p_range);
  610. float get_msdf_pixel_range() const;
  611. void set_msdf_outline_size(float p_size);
  612. float get_msdf_outline_size() const;
  613. void set_distance_fade(DistanceFadeMode p_mode);
  614. DistanceFadeMode get_distance_fade() const;
  615. void set_distance_fade_max_distance(float p_distance);
  616. float get_distance_fade_max_distance() const;
  617. void set_distance_fade_min_distance(float p_distance);
  618. float get_distance_fade_min_distance() const;
  619. void set_emission_operator(EmissionOperator p_op);
  620. EmissionOperator get_emission_operator() const;
  621. void set_metallic_texture_channel(TextureChannel p_channel);
  622. TextureChannel get_metallic_texture_channel() const;
  623. void set_roughness_texture_channel(TextureChannel p_channel);
  624. TextureChannel get_roughness_texture_channel() const;
  625. void set_ao_texture_channel(TextureChannel p_channel);
  626. TextureChannel get_ao_texture_channel() const;
  627. void set_refraction_texture_channel(TextureChannel p_channel);
  628. TextureChannel get_refraction_texture_channel() const;
  629. static void init_shaders();
  630. static void finish_shaders();
  631. static Ref<Material> get_material_for_2d(bool p_shaded, Transparency p_transparency, bool p_double_sided, bool p_billboard = false, bool p_billboard_y = false, bool p_msdf = false, bool p_no_depth = false, bool p_fixed_size = false, TextureFilter p_filter = TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, AlphaAntiAliasing p_alpha_antialiasing_mode = ALPHA_ANTIALIASING_OFF, RID *r_shader_rid = nullptr);
  632. virtual RID get_rid() const override;
  633. virtual RID get_shader_rid() const override;
  634. virtual Shader::Mode get_shader_mode() const override;
  635. BaseMaterial3D(bool p_orm);
  636. virtual ~BaseMaterial3D();
  637. };
  638. VARIANT_ENUM_CAST(BaseMaterial3D::TextureParam)
  639. VARIANT_ENUM_CAST(BaseMaterial3D::TextureFilter)
  640. VARIANT_ENUM_CAST(BaseMaterial3D::ShadingMode)
  641. VARIANT_ENUM_CAST(BaseMaterial3D::Transparency)
  642. VARIANT_ENUM_CAST(BaseMaterial3D::AlphaAntiAliasing)
  643. VARIANT_ENUM_CAST(BaseMaterial3D::DetailUV)
  644. VARIANT_ENUM_CAST(BaseMaterial3D::Feature)
  645. VARIANT_ENUM_CAST(BaseMaterial3D::BlendMode)
  646. VARIANT_ENUM_CAST(BaseMaterial3D::DepthDrawMode)
  647. VARIANT_ENUM_CAST(BaseMaterial3D::CullMode)
  648. VARIANT_ENUM_CAST(BaseMaterial3D::Flags)
  649. VARIANT_ENUM_CAST(BaseMaterial3D::DiffuseMode)
  650. VARIANT_ENUM_CAST(BaseMaterial3D::SpecularMode)
  651. VARIANT_ENUM_CAST(BaseMaterial3D::BillboardMode)
  652. VARIANT_ENUM_CAST(BaseMaterial3D::TextureChannel)
  653. VARIANT_ENUM_CAST(BaseMaterial3D::EmissionOperator)
  654. VARIANT_ENUM_CAST(BaseMaterial3D::DistanceFadeMode)
  655. class StandardMaterial3D : public BaseMaterial3D {
  656. GDCLASS(StandardMaterial3D, BaseMaterial3D)
  657. protected:
  658. #ifndef DISABLE_DEPRECATED
  659. // Kept for compatibility from 3.x to 4.0.
  660. bool _set(const StringName &p_name, const Variant &p_value);
  661. #endif
  662. public:
  663. StandardMaterial3D() :
  664. BaseMaterial3D(false) {}
  665. };
  666. class ORMMaterial3D : public BaseMaterial3D {
  667. GDCLASS(ORMMaterial3D, BaseMaterial3D)
  668. public:
  669. ORMMaterial3D() :
  670. BaseMaterial3D(true) {}
  671. };
  672. class PlaceholderMaterial : public Material {
  673. GDCLASS(PlaceholderMaterial, Material)
  674. public:
  675. virtual RID get_shader_rid() const override { return RID(); }
  676. virtual Shader::Mode get_shader_mode() const override { return Shader::MODE_CANVAS_ITEM; }
  677. };
  678. //////////////////////
  679. #endif // MATERIAL_H