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 31 #ifndef MATERIAL_H 32 #define MATERIAL_H 33 34 #include "core/resource.h" 35 #include "core/self_list.h" 36 #include "scene/resources/shader.h" 37 #include "scene/resources/texture.h" 38 #include "servers/visual/shader_language.h" 39 #include "servers/visual_server.h" 40 41 class Material : public Resource { 42 43 GDCLASS(Material, Resource); 44 RES_BASE_EXTENSION("material") 45 OBJ_SAVE_TYPE(Material); 46 47 RID material; 48 Ref<Material> next_pass; 49 int render_priority; 50 51 protected: _get_material()52 _FORCE_INLINE_ RID _get_material() const { return material; } 53 static void _bind_methods(); _can_do_next_pass()54 virtual bool _can_do_next_pass() const { return false; } 55 56 void _validate_property(PropertyInfo &property) const; 57 58 public: 59 enum { 60 RENDER_PRIORITY_MAX = VS::MATERIAL_RENDER_PRIORITY_MAX, 61 RENDER_PRIORITY_MIN = VS::MATERIAL_RENDER_PRIORITY_MIN, 62 }; 63 void set_next_pass(const Ref<Material> &p_pass); 64 Ref<Material> get_next_pass() const; 65 66 void set_render_priority(int p_priority); 67 int get_render_priority() const; 68 69 virtual RID get_rid() const; 70 71 virtual Shader::Mode get_shader_mode() const = 0; 72 Material(); 73 virtual ~Material(); 74 }; 75 76 class ShaderMaterial : public Material { 77 78 GDCLASS(ShaderMaterial, Material); 79 Ref<Shader> shader; 80 81 protected: 82 bool _set(const StringName &p_name, const Variant &p_value); 83 bool _get(const StringName &p_name, Variant &r_ret) const; 84 void _get_property_list(List<PropertyInfo> *p_list) const; 85 bool property_can_revert(const String &p_name); 86 Variant property_get_revert(const String &p_name); 87 88 static void _bind_methods(); 89 90 void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const; 91 92 virtual bool _can_do_next_pass() const; 93 94 void _shader_changed(); 95 96 public: 97 void set_shader(const Ref<Shader> &p_shader); 98 Ref<Shader> get_shader() const; 99 100 void set_shader_param(const StringName &p_param, const Variant &p_value); 101 Variant get_shader_param(const StringName &p_param) const; 102 103 virtual Shader::Mode get_shader_mode() const; 104 105 ShaderMaterial(); 106 ~ShaderMaterial(); 107 }; 108 109 class SpatialMaterial : public Material { 110 111 GDCLASS(SpatialMaterial, Material); 112 113 public: 114 enum TextureParam { 115 TEXTURE_ALBEDO, 116 TEXTURE_METALLIC, 117 TEXTURE_ROUGHNESS, 118 TEXTURE_EMISSION, 119 TEXTURE_NORMAL, 120 TEXTURE_RIM, 121 TEXTURE_CLEARCOAT, 122 TEXTURE_FLOWMAP, 123 TEXTURE_AMBIENT_OCCLUSION, 124 TEXTURE_DEPTH, 125 TEXTURE_SUBSURFACE_SCATTERING, 126 TEXTURE_TRANSMISSION, 127 TEXTURE_REFRACTION, 128 TEXTURE_DETAIL_MASK, 129 TEXTURE_DETAIL_ALBEDO, 130 TEXTURE_DETAIL_NORMAL, 131 TEXTURE_MAX 132 133 }; 134 135 enum DetailUV { 136 DETAIL_UV_1, 137 DETAIL_UV_2 138 }; 139 140 enum Feature { 141 FEATURE_TRANSPARENT, 142 FEATURE_EMISSION, 143 FEATURE_NORMAL_MAPPING, 144 FEATURE_RIM, 145 FEATURE_CLEARCOAT, 146 FEATURE_ANISOTROPY, 147 FEATURE_AMBIENT_OCCLUSION, 148 FEATURE_DEPTH_MAPPING, 149 FEATURE_SUBSURACE_SCATTERING, 150 FEATURE_TRANSMISSION, 151 FEATURE_REFRACTION, 152 FEATURE_DETAIL, 153 FEATURE_MAX 154 }; 155 156 enum BlendMode { 157 BLEND_MODE_MIX, 158 BLEND_MODE_ADD, 159 BLEND_MODE_SUB, 160 BLEND_MODE_MUL, 161 }; 162 163 enum DepthDrawMode { 164 DEPTH_DRAW_OPAQUE_ONLY, 165 DEPTH_DRAW_ALWAYS, 166 DEPTH_DRAW_DISABLED, 167 DEPTH_DRAW_ALPHA_OPAQUE_PREPASS 168 169 }; 170 171 enum CullMode { 172 CULL_BACK, 173 CULL_FRONT, 174 CULL_DISABLED 175 }; 176 177 enum Flags { 178 FLAG_UNSHADED, 179 FLAG_USE_VERTEX_LIGHTING, 180 FLAG_DISABLE_DEPTH_TEST, 181 FLAG_ALBEDO_FROM_VERTEX_COLOR, 182 FLAG_SRGB_VERTEX_COLOR, 183 FLAG_USE_POINT_SIZE, 184 FLAG_FIXED_SIZE, 185 FLAG_BILLBOARD_KEEP_SCALE, 186 FLAG_UV1_USE_TRIPLANAR, 187 FLAG_UV2_USE_TRIPLANAR, 188 FLAG_TRIPLANAR_USE_WORLD, 189 FLAG_AO_ON_UV2, 190 FLAG_EMISSION_ON_UV2, 191 FLAG_USE_ALPHA_SCISSOR, 192 FLAG_ALBEDO_TEXTURE_FORCE_SRGB, 193 FLAG_DONT_RECEIVE_SHADOWS, 194 FLAG_ENSURE_CORRECT_NORMALS, 195 FLAG_DISABLE_AMBIENT_LIGHT, 196 FLAG_USE_SHADOW_TO_OPACITY, 197 FLAG_MAX 198 }; 199 200 enum DiffuseMode { 201 DIFFUSE_BURLEY, 202 DIFFUSE_LAMBERT, 203 DIFFUSE_LAMBERT_WRAP, 204 DIFFUSE_OREN_NAYAR, 205 DIFFUSE_TOON, 206 }; 207 208 enum SpecularMode { 209 SPECULAR_SCHLICK_GGX, 210 SPECULAR_BLINN, 211 SPECULAR_PHONG, 212 SPECULAR_TOON, 213 SPECULAR_DISABLED, 214 }; 215 216 enum BillboardMode { 217 BILLBOARD_DISABLED, 218 BILLBOARD_ENABLED, 219 BILLBOARD_FIXED_Y, 220 BILLBOARD_PARTICLES, 221 }; 222 223 enum TextureChannel { 224 TEXTURE_CHANNEL_RED, 225 TEXTURE_CHANNEL_GREEN, 226 TEXTURE_CHANNEL_BLUE, 227 TEXTURE_CHANNEL_ALPHA, 228 TEXTURE_CHANNEL_GRAYSCALE 229 }; 230 231 enum EmissionOperator { 232 EMISSION_OP_ADD, 233 EMISSION_OP_MULTIPLY 234 }; 235 236 enum DistanceFadeMode { 237 DISTANCE_FADE_DISABLED, 238 DISTANCE_FADE_PIXEL_ALPHA, 239 DISTANCE_FADE_PIXEL_DITHER, 240 DISTANCE_FADE_OBJECT_DITHER, 241 }; 242 243 private: 244 union MaterialKey { 245 246 struct { 247 uint64_t feature_mask : 12; 248 uint64_t detail_uv : 1; 249 uint64_t blend_mode : 2; 250 uint64_t depth_draw_mode : 2; 251 uint64_t cull_mode : 2; 252 uint64_t flags : 19; 253 uint64_t detail_blend_mode : 2; 254 uint64_t diffuse_mode : 3; 255 uint64_t specular_mode : 3; 256 uint64_t invalid_key : 1; 257 uint64_t deep_parallax : 1; 258 uint64_t billboard_mode : 2; 259 uint64_t grow : 1; 260 uint64_t proximity_fade : 1; 261 uint64_t distance_fade : 2; 262 uint64_t emission_op : 1; 263 uint64_t texture_metallic : 1; 264 uint64_t texture_roughness : 1; 265 }; 266 267 uint64_t key; 268 269 bool operator<(const MaterialKey &p_key) const { 270 return key < p_key.key; 271 } 272 }; 273 274 struct ShaderData { 275 RID shader; 276 int users; 277 }; 278 279 static Map<MaterialKey, ShaderData> shader_map; 280 281 MaterialKey current_key; 282 _compute_key()283 _FORCE_INLINE_ MaterialKey _compute_key() const { 284 285 MaterialKey mk; 286 mk.key = 0; 287 for (int i = 0; i < FEATURE_MAX; i++) { 288 if (features[i]) { 289 mk.feature_mask |= ((uint64_t)1 << i); 290 } 291 } 292 mk.detail_uv = detail_uv; 293 mk.blend_mode = blend_mode; 294 mk.depth_draw_mode = depth_draw_mode; 295 mk.cull_mode = cull_mode; 296 for (int i = 0; i < FLAG_MAX; i++) { 297 if (flags[i]) { 298 mk.flags |= ((uint64_t)1 << i); 299 } 300 } 301 mk.detail_blend_mode = detail_blend_mode; 302 mk.diffuse_mode = diffuse_mode; 303 mk.specular_mode = specular_mode; 304 mk.billboard_mode = billboard_mode; 305 mk.deep_parallax = deep_parallax ? 1 : 0; 306 mk.grow = grow_enabled; 307 mk.proximity_fade = proximity_fade_enabled; 308 mk.distance_fade = distance_fade; 309 mk.emission_op = emission_op; 310 mk.texture_metallic = textures[TEXTURE_METALLIC].is_valid() ? 1 : 0; 311 mk.texture_roughness = textures[TEXTURE_ROUGHNESS].is_valid() ? 1 : 0; 312 313 return mk; 314 } 315 316 struct ShaderNames { 317 StringName albedo; 318 StringName specular; 319 StringName metallic; 320 StringName roughness; 321 StringName emission; 322 StringName emission_energy; 323 StringName normal_scale; 324 StringName rim; 325 StringName rim_tint; 326 StringName clearcoat; 327 StringName clearcoat_gloss; 328 StringName anisotropy; 329 StringName depth_scale; 330 StringName subsurface_scattering_strength; 331 StringName transmission; 332 StringName refraction; 333 StringName point_size; 334 StringName uv1_scale; 335 StringName uv1_offset; 336 StringName uv2_scale; 337 StringName uv2_offset; 338 StringName particles_anim_h_frames; 339 StringName particles_anim_v_frames; 340 StringName particles_anim_loop; 341 StringName depth_min_layers; 342 StringName depth_max_layers; 343 StringName depth_flip; 344 StringName uv1_blend_sharpness; 345 StringName uv2_blend_sharpness; 346 StringName grow; 347 StringName proximity_fade_distance; 348 StringName distance_fade_min; 349 StringName distance_fade_max; 350 StringName ao_light_affect; 351 352 StringName metallic_texture_channel; 353 StringName roughness_texture_channel; 354 StringName ao_texture_channel; 355 StringName clearcoat_texture_channel; 356 StringName rim_texture_channel; 357 StringName depth_texture_channel; 358 StringName refraction_texture_channel; 359 StringName alpha_scissor_threshold; 360 361 StringName texture_names[TEXTURE_MAX]; 362 }; 363 364 static Mutex *material_mutex; 365 static SelfList<SpatialMaterial>::List *dirty_materials; 366 static ShaderNames *shader_names; 367 368 SelfList<SpatialMaterial> element; 369 370 void _update_shader(); 371 _FORCE_INLINE_ void _queue_shader_change(); 372 _FORCE_INLINE_ bool _is_shader_dirty() const; 373 374 Color albedo; 375 float specular; 376 float metallic; 377 float roughness; 378 Color emission; 379 float emission_energy; 380 float normal_scale; 381 float rim; 382 float rim_tint; 383 float clearcoat; 384 float clearcoat_gloss; 385 float anisotropy; 386 float depth_scale; 387 float subsurface_scattering_strength; 388 Color transmission; 389 float refraction; 390 float line_width; 391 float point_size; 392 float alpha_scissor_threshold; 393 bool grow_enabled; 394 float ao_light_affect; 395 float grow; 396 int particles_anim_h_frames; 397 int particles_anim_v_frames; 398 bool particles_anim_loop; 399 400 Vector3 uv1_scale; 401 Vector3 uv1_offset; 402 float uv1_triplanar_sharpness; 403 404 Vector3 uv2_scale; 405 Vector3 uv2_offset; 406 float uv2_triplanar_sharpness; 407 408 DetailUV detail_uv; 409 410 bool deep_parallax; 411 int deep_parallax_min_layers; 412 int deep_parallax_max_layers; 413 bool depth_parallax_flip_tangent; 414 bool depth_parallax_flip_binormal; 415 416 bool proximity_fade_enabled; 417 float proximity_fade_distance; 418 419 DistanceFadeMode distance_fade; 420 float distance_fade_max_distance; 421 float distance_fade_min_distance; 422 423 BlendMode blend_mode; 424 BlendMode detail_blend_mode; 425 DepthDrawMode depth_draw_mode; 426 CullMode cull_mode; 427 bool flags[FLAG_MAX]; 428 SpecularMode specular_mode; 429 DiffuseMode diffuse_mode; 430 BillboardMode billboard_mode; 431 EmissionOperator emission_op; 432 433 TextureChannel metallic_texture_channel; 434 TextureChannel roughness_texture_channel; 435 TextureChannel ao_texture_channel; 436 TextureChannel refraction_texture_channel; 437 438 bool features[FEATURE_MAX]; 439 440 Ref<Texture> textures[TEXTURE_MAX]; 441 442 _FORCE_INLINE_ void _validate_feature(const String &text, Feature feature, PropertyInfo &property) const; 443 444 static const int MAX_MATERIALS_FOR_2D = 128; 445 446 static Ref<SpatialMaterial> materials_for_2d[MAX_MATERIALS_FOR_2D]; //used by Sprite3D and other stuff 447 448 void _validate_high_end(const String &text, PropertyInfo &property) const; 449 450 protected: 451 static void _bind_methods(); 452 void _validate_property(PropertyInfo &property) const; _can_do_next_pass()453 virtual bool _can_do_next_pass() const { return true; } 454 455 public: 456 void set_albedo(const Color &p_albedo); 457 Color get_albedo() const; 458 459 void set_specular(float p_specular); 460 float get_specular() const; 461 462 void set_metallic(float p_metallic); 463 float get_metallic() const; 464 465 void set_roughness(float p_roughness); 466 float get_roughness() const; 467 468 void set_emission(const Color &p_emission); 469 Color get_emission() const; 470 471 void set_emission_energy(float p_emission_energy); 472 float get_emission_energy() const; 473 474 void set_normal_scale(float p_normal_scale); 475 float get_normal_scale() const; 476 477 void set_rim(float p_rim); 478 float get_rim() const; 479 480 void set_rim_tint(float p_rim_tint); 481 float get_rim_tint() const; 482 483 void set_ao_light_affect(float p_ao_light_affect); 484 float get_ao_light_affect() const; 485 486 void set_clearcoat(float p_clearcoat); 487 float get_clearcoat() const; 488 489 void set_clearcoat_gloss(float p_clearcoat_gloss); 490 float get_clearcoat_gloss() const; 491 492 void set_anisotropy(float p_anisotropy); 493 float get_anisotropy() const; 494 495 void set_depth_scale(float p_depth_scale); 496 float get_depth_scale() const; 497 498 void set_depth_deep_parallax(bool p_enable); 499 bool is_depth_deep_parallax_enabled() const; 500 501 void set_depth_deep_parallax_min_layers(int p_layer); 502 int get_depth_deep_parallax_min_layers() const; 503 504 void set_depth_deep_parallax_max_layers(int p_layer); 505 int get_depth_deep_parallax_max_layers() const; 506 507 void set_depth_deep_parallax_flip_tangent(bool p_flip); 508 bool get_depth_deep_parallax_flip_tangent() const; 509 510 void set_depth_deep_parallax_flip_binormal(bool p_flip); 511 bool get_depth_deep_parallax_flip_binormal() const; 512 513 void set_subsurface_scattering_strength(float p_subsurface_scattering_strength); 514 float get_subsurface_scattering_strength() const; 515 516 void set_transmission(const Color &p_transmission); 517 Color get_transmission() const; 518 519 void set_refraction(float p_refraction); 520 float get_refraction() const; 521 522 void set_line_width(float p_line_width); 523 float get_line_width() const; 524 525 void set_point_size(float p_point_size); 526 float get_point_size() const; 527 528 void set_detail_uv(DetailUV p_detail_uv); 529 DetailUV get_detail_uv() const; 530 531 void set_blend_mode(BlendMode p_mode); 532 BlendMode get_blend_mode() const; 533 534 void set_detail_blend_mode(BlendMode p_mode); 535 BlendMode get_detail_blend_mode() const; 536 537 void set_depth_draw_mode(DepthDrawMode p_mode); 538 DepthDrawMode get_depth_draw_mode() const; 539 540 void set_cull_mode(CullMode p_mode); 541 CullMode get_cull_mode() const; 542 543 void set_diffuse_mode(DiffuseMode p_mode); 544 DiffuseMode get_diffuse_mode() const; 545 546 void set_specular_mode(SpecularMode p_mode); 547 SpecularMode get_specular_mode() const; 548 549 void set_flag(Flags p_flag, bool p_enabled); 550 bool get_flag(Flags p_flag) const; 551 552 void set_texture(TextureParam p_param, const Ref<Texture> &p_texture); 553 Ref<Texture> get_texture(TextureParam p_param) const; 554 // Used only for shader material conversion 555 Ref<Texture> get_texture_by_name(StringName p_name) const; 556 557 void set_feature(Feature p_feature, bool p_enabled); 558 bool get_feature(Feature p_feature) const; 559 560 void set_uv1_scale(const Vector3 &p_scale); 561 Vector3 get_uv1_scale() const; 562 563 void set_uv1_offset(const Vector3 &p_offset); 564 Vector3 get_uv1_offset() const; 565 566 void set_uv1_triplanar_blend_sharpness(float p_sharpness); 567 float get_uv1_triplanar_blend_sharpness() const; 568 569 void set_uv2_scale(const Vector3 &p_scale); 570 Vector3 get_uv2_scale() const; 571 572 void set_uv2_offset(const Vector3 &p_offset); 573 Vector3 get_uv2_offset() const; 574 575 void set_uv2_triplanar_blend_sharpness(float p_sharpness); 576 float get_uv2_triplanar_blend_sharpness() const; 577 578 void set_billboard_mode(BillboardMode p_mode); 579 BillboardMode get_billboard_mode() const; 580 581 void set_particles_anim_h_frames(int p_frames); 582 int get_particles_anim_h_frames() const; 583 void set_particles_anim_v_frames(int p_frames); 584 int get_particles_anim_v_frames() const; 585 586 void set_particles_anim_loop(bool p_loop); 587 bool get_particles_anim_loop() const; 588 589 void set_grow_enabled(bool p_enable); 590 bool is_grow_enabled() const; 591 592 void set_grow(float p_grow); 593 float get_grow() const; 594 595 void set_alpha_scissor_threshold(float p_threshold); 596 float get_alpha_scissor_threshold() const; 597 598 void set_on_top_of_alpha(); 599 600 void set_proximity_fade(bool p_enable); 601 bool is_proximity_fade_enabled() const; 602 603 void set_proximity_fade_distance(float p_distance); 604 float get_proximity_fade_distance() const; 605 606 void set_distance_fade(DistanceFadeMode p_mode); 607 DistanceFadeMode get_distance_fade() const; 608 609 void set_distance_fade_max_distance(float p_distance); 610 float get_distance_fade_max_distance() const; 611 612 void set_distance_fade_min_distance(float p_distance); 613 float get_distance_fade_min_distance() const; 614 615 void set_emission_operator(EmissionOperator p_op); 616 EmissionOperator get_emission_operator() const; 617 618 void set_metallic_texture_channel(TextureChannel p_channel); 619 TextureChannel get_metallic_texture_channel() const; 620 void set_roughness_texture_channel(TextureChannel p_channel); 621 TextureChannel get_roughness_texture_channel() const; 622 void set_ao_texture_channel(TextureChannel p_channel); 623 TextureChannel get_ao_texture_channel() const; 624 void set_refraction_texture_channel(TextureChannel p_channel); 625 TextureChannel get_refraction_texture_channel() const; 626 627 static void init_shaders(); 628 static void finish_shaders(); 629 static void flush_changes(); 630 631 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, bool p_billboard = false, bool p_billboard_y = false); 632 633 RID get_shader_rid() const; 634 635 virtual Shader::Mode get_shader_mode() const; 636 637 SpatialMaterial(); 638 virtual ~SpatialMaterial(); 639 }; 640 641 VARIANT_ENUM_CAST(SpatialMaterial::TextureParam) 642 VARIANT_ENUM_CAST(SpatialMaterial::DetailUV) 643 VARIANT_ENUM_CAST(SpatialMaterial::Feature) 644 VARIANT_ENUM_CAST(SpatialMaterial::BlendMode) 645 VARIANT_ENUM_CAST(SpatialMaterial::DepthDrawMode) 646 VARIANT_ENUM_CAST(SpatialMaterial::CullMode) 647 VARIANT_ENUM_CAST(SpatialMaterial::Flags) 648 VARIANT_ENUM_CAST(SpatialMaterial::DiffuseMode) 649 VARIANT_ENUM_CAST(SpatialMaterial::SpecularMode) 650 VARIANT_ENUM_CAST(SpatialMaterial::BillboardMode) 651 VARIANT_ENUM_CAST(SpatialMaterial::TextureChannel) 652 VARIANT_ENUM_CAST(SpatialMaterial::EmissionOperator) 653 VARIANT_ENUM_CAST(SpatialMaterial::DistanceFadeMode) 654 655 ////////////////////// 656 657 #endif 658