| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/resources/material.h" |
| 34 |
|
| 35 |
class CanvasItemMaterial : public Material { |
| 36 |
GDCLASS(CanvasItemMaterial, Material); |
| 37 |
|
| 38 |
public: |
| 39 |
enum BlendMode { |
| 40 |
BLEND_MODE_MIX, |
| 41 |
BLEND_MODE_ADD, |
| 42 |
BLEND_MODE_SUB, |
| 43 |
BLEND_MODE_MUL, |
| 44 |
BLEND_MODE_PREMULT_ALPHA, |
| 45 |
BLEND_MODE_DISABLED |
| 46 |
}; |
| 47 |
|
| 48 |
enum LightMode { |
| 49 |
LIGHT_MODE_NORMAL, |
| 50 |
LIGHT_MODE_UNSHADED, |
| 51 |
LIGHT_MODE_LIGHT_ONLY |
| 52 |
}; |
| 53 |
|
| 54 |
private: |
| 55 |
union MaterialKey { |
| 56 |
struct { |
| 57 |
uint32_t blend_mode : 4; |
| 58 |
uint32_t light_mode : 4; |
| 59 |
uint32_t particles_animation : 1; |
| 60 |
uint32_t invalid_key : 1; |
| 61 |
}; |
| 62 |
|
| 63 |
uint32_t key = 0; |
| 64 |
|
| 65 |
static uint32_t hash(const MaterialKey &p_key) { |
| 66 |
return hash_murmur3_one_32(p_key.key); |
| 67 |
} |
| 68 |
bool operator==(const MaterialKey &p_key) const { |
| 69 |
return key == p_key.key; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
struct ShaderNames { |
| 74 |
StringName particles_anim_h_frames; |
| 75 |
StringName particles_anim_v_frames; |
| 76 |
StringName particles_anim_loop; |
| 77 |
}; |
| 78 |
|
| 79 |
static ShaderNames *shader_names; |
| 80 |
|
| 81 |
struct ShaderData { |
| 82 |
RID shader; |
| 83 |
int users = 0; |
| 84 |
}; |
| 85 |
|
| 86 |
static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map; |
| 87 |
|
| 88 |
MaterialKey current_key; |
| 89 |
|
| 90 |
_FORCE_INLINE_ MaterialKey _compute_key() const { |
| 91 |
MaterialKey mk; |
| 92 |
mk.key = 0; |
| 93 |
mk.blend_mode = blend_mode; |
| 94 |
mk.light_mode = light_mode; |
| 95 |
mk.particles_animation = particles_animation; |
| 96 |
return mk; |
| 97 |
} |
| 98 |
|
| 99 |
static Mutex material_mutex; |
| 100 |
static SelfList<CanvasItemMaterial>::List dirty_materials; |
| 101 |
SelfList<CanvasItemMaterial> element; |
| 102 |
|
| 103 |
void _update_shader(); |
| 104 |
_FORCE_INLINE_ void _queue_shader_change(); |
| 105 |
|
| 106 |
BlendMode blend_mode = BLEND_MODE_MIX; |
| 107 |
LightMode light_mode = LIGHT_MODE_NORMAL; |
| 108 |
bool particles_animation = false; |
| 109 |
|
| 110 |
|
| 111 |
int particles_anim_h_frames = 0; |
| 112 |
int particles_anim_v_frames = 0; |
| 113 |
bool particles_anim_loop = false; |
| 114 |
|
| 115 |
protected: |
| 116 |
static void _bind_methods(); |
| 117 |
void _validate_property(PropertyInfo &p_property) const; |
| 118 |
|
| 119 |
public: |
| 120 |
void set_blend_mode(BlendMode p_blend_mode); |
| 121 |
BlendMode get_blend_mode() const; |
| 122 |
|
| 123 |
void set_light_mode(LightMode p_light_mode); |
| 124 |
LightMode get_light_mode() const; |
| 125 |
|
| 126 |
void set_particles_animation(bool p_particles_anim); |
| 127 |
bool get_particles_animation() const; |
| 128 |
|
| 129 |
void set_particles_anim_h_frames(int p_frames); |
| 130 |
int get_particles_anim_h_frames() const; |
| 131 |
void set_particles_anim_v_frames(int p_frames); |
| 132 |
int get_particles_anim_v_frames() const; |
| 133 |
|
| 134 |
void set_particles_anim_loop(bool p_loop); |
| 135 |
bool get_particles_anim_loop() const; |
| 136 |
|
| 137 |
static void init_shaders(); |
| 138 |
static void finish_shaders(); |
| 139 |
static void flush_changes(); |
| 140 |
|
| 141 |
virtual RID get_shader_rid() const override; |
| 142 |
|
| 143 |
virtual Shader::Mode get_shader_mode() const override; |
| 144 |
|
| 145 |
CanvasItemMaterial(); |
| 146 |
virtual ~CanvasItemMaterial(); |
| 147 |
}; |
| 148 |
|
| 149 |
VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode) |
| 150 |
VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode) |