Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/canvas_item_material.h
9903 views
1
/**************************************************************************/
2
/* canvas_item_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
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
// Proper values set in constructor.
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)
151
152