Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/canvas_item_material.cpp
9896 views
1
/**************************************************************************/
2
/* canvas_item_material.cpp */
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
#include "canvas_item_material.h"
32
33
#include "core/version.h"
34
35
Mutex CanvasItemMaterial::material_mutex;
36
SelfList<CanvasItemMaterial>::List CanvasItemMaterial::dirty_materials;
37
HashMap<CanvasItemMaterial::MaterialKey, CanvasItemMaterial::ShaderData, CanvasItemMaterial::MaterialKey> CanvasItemMaterial::shader_map;
38
CanvasItemMaterial::ShaderNames *CanvasItemMaterial::shader_names = nullptr;
39
40
void CanvasItemMaterial::init_shaders() {
41
shader_names = memnew(ShaderNames);
42
43
shader_names->particles_anim_h_frames = "particles_anim_h_frames";
44
shader_names->particles_anim_v_frames = "particles_anim_v_frames";
45
shader_names->particles_anim_loop = "particles_anim_loop";
46
}
47
48
void CanvasItemMaterial::finish_shaders() {
49
dirty_materials.clear();
50
51
memdelete(shader_names);
52
shader_names = nullptr;
53
}
54
55
void CanvasItemMaterial::_update_shader() {
56
MaterialKey mk = _compute_key();
57
if (mk.key == current_key.key) {
58
return; //no update required in the end
59
}
60
61
if (shader_map.has(current_key)) {
62
shader_map[current_key].users--;
63
if (shader_map[current_key].users == 0) {
64
//deallocate shader, as it's no longer in use
65
RS::get_singleton()->free(shader_map[current_key].shader);
66
shader_map.erase(current_key);
67
}
68
}
69
70
current_key = mk;
71
72
if (shader_map.has(mk)) {
73
RS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
74
shader_map[mk].users++;
75
return;
76
}
77
78
//must create a shader!
79
80
// Add a comment to describe the shader origin (useful when converting to ShaderMaterial).
81
String code = "// NOTE: Shader automatically converted from " GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG "'s CanvasItemMaterial.\n\n";
82
83
code += "shader_type canvas_item;\nrender_mode ";
84
switch (blend_mode) {
85
case BLEND_MODE_MIX:
86
code += "blend_mix";
87
break;
88
case BLEND_MODE_ADD:
89
code += "blend_add";
90
break;
91
case BLEND_MODE_SUB:
92
code += "blend_sub";
93
break;
94
case BLEND_MODE_MUL:
95
code += "blend_mul";
96
break;
97
case BLEND_MODE_PREMULT_ALPHA:
98
code += "blend_premul_alpha";
99
break;
100
case BLEND_MODE_DISABLED:
101
code += "blend_disabled";
102
break;
103
}
104
105
switch (light_mode) {
106
case LIGHT_MODE_NORMAL:
107
break;
108
case LIGHT_MODE_UNSHADED:
109
code += ",unshaded";
110
break;
111
case LIGHT_MODE_LIGHT_ONLY:
112
code += ",light_only";
113
break;
114
}
115
116
code += ";\n";
117
118
if (particles_animation) {
119
code += "uniform int particles_anim_h_frames;\n";
120
code += "uniform int particles_anim_v_frames;\n";
121
code += "uniform bool particles_anim_loop;\n\n";
122
123
code += "void vertex() {\n";
124
code += " float h_frames = float(particles_anim_h_frames);\n";
125
code += " float v_frames = float(particles_anim_v_frames);\n";
126
code += " VERTEX.xy /= vec2(h_frames, v_frames);\n";
127
code += " float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n";
128
code += " float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n";
129
code += " if (!particles_anim_loop) {\n";
130
code += " particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n";
131
code += " } else {\n";
132
code += " particle_frame = mod(particle_frame, particle_total_frames);\n";
133
code += " }";
134
code += " UV /= vec2(h_frames, v_frames);\n";
135
code += " UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);\n";
136
code += "}\n";
137
}
138
139
ShaderData shader_data;
140
shader_data.shader = RS::get_singleton()->shader_create();
141
shader_data.users = 1;
142
143
RS::get_singleton()->shader_set_code(shader_data.shader, code);
144
145
shader_map[mk] = shader_data;
146
147
RS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
148
}
149
150
void CanvasItemMaterial::flush_changes() {
151
MutexLock lock(material_mutex);
152
153
while (dirty_materials.first()) {
154
dirty_materials.first()->self()->_update_shader();
155
dirty_materials.first()->remove_from_list();
156
}
157
}
158
159
void CanvasItemMaterial::_queue_shader_change() {
160
if (!_is_initialized()) {
161
return;
162
}
163
164
MutexLock lock(material_mutex);
165
166
if (!element.in_list()) {
167
dirty_materials.add(&element);
168
}
169
}
170
171
void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
172
blend_mode = p_blend_mode;
173
_queue_shader_change();
174
}
175
176
CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
177
return blend_mode;
178
}
179
180
void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
181
light_mode = p_light_mode;
182
_queue_shader_change();
183
}
184
185
CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
186
return light_mode;
187
}
188
189
void CanvasItemMaterial::set_particles_animation(bool p_particles_anim) {
190
particles_animation = p_particles_anim;
191
_queue_shader_change();
192
notify_property_list_changed();
193
}
194
195
bool CanvasItemMaterial::get_particles_animation() const {
196
return particles_animation;
197
}
198
199
void CanvasItemMaterial::set_particles_anim_h_frames(int p_frames) {
200
particles_anim_h_frames = p_frames;
201
RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
202
}
203
204
int CanvasItemMaterial::get_particles_anim_h_frames() const {
205
return particles_anim_h_frames;
206
}
207
208
void CanvasItemMaterial::set_particles_anim_v_frames(int p_frames) {
209
particles_anim_v_frames = p_frames;
210
RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
211
}
212
213
int CanvasItemMaterial::get_particles_anim_v_frames() const {
214
return particles_anim_v_frames;
215
}
216
217
void CanvasItemMaterial::set_particles_anim_loop(bool p_loop) {
218
particles_anim_loop = p_loop;
219
RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
220
}
221
222
bool CanvasItemMaterial::get_particles_anim_loop() const {
223
return particles_anim_loop;
224
}
225
226
void CanvasItemMaterial::_validate_property(PropertyInfo &p_property) const {
227
if (p_property.name.begins_with("particles_anim_") && !particles_animation) {
228
p_property.usage = PROPERTY_USAGE_NONE;
229
}
230
}
231
232
RID CanvasItemMaterial::get_shader_rid() const {
233
ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
234
return shader_map[current_key].shader;
235
}
236
237
Shader::Mode CanvasItemMaterial::get_shader_mode() const {
238
return Shader::MODE_CANVAS_ITEM;
239
}
240
241
void CanvasItemMaterial::_bind_methods() {
242
ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
243
ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
244
245
ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
246
ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
247
248
ClassDB::bind_method(D_METHOD("set_particles_animation", "particles_anim"), &CanvasItemMaterial::set_particles_animation);
249
ClassDB::bind_method(D_METHOD("get_particles_animation"), &CanvasItemMaterial::get_particles_animation);
250
251
ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &CanvasItemMaterial::set_particles_anim_h_frames);
252
ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &CanvasItemMaterial::get_particles_anim_h_frames);
253
254
ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &CanvasItemMaterial::set_particles_anim_v_frames);
255
ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &CanvasItemMaterial::get_particles_anim_v_frames);
256
257
ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &CanvasItemMaterial::set_particles_anim_loop);
258
ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &CanvasItemMaterial::get_particles_anim_loop);
259
260
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply,Premultiplied Alpha"), "set_blend_mode", "get_blend_mode");
261
ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
262
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_animation"), "set_particles_animation", "get_particles_animation");
263
264
ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
265
ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
266
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
267
268
BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
269
BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
270
BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
271
BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
272
BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
273
274
BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
275
BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
276
BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
277
}
278
279
CanvasItemMaterial::CanvasItemMaterial() :
280
element(this) {
281
_set_material(RS::get_singleton()->material_create());
282
283
set_particles_anim_h_frames(1);
284
set_particles_anim_v_frames(1);
285
set_particles_anim_loop(false);
286
287
current_key.invalid_key = 1;
288
289
_mark_initialized(callable_mp(this, &CanvasItemMaterial::_queue_shader_change), callable_mp(this, &CanvasItemMaterial::_update_shader));
290
}
291
292
CanvasItemMaterial::~CanvasItemMaterial() {
293
MutexLock lock(material_mutex);
294
295
ERR_FAIL_NULL(RenderingServer::get_singleton());
296
297
if (shader_map.has(current_key)) {
298
shader_map[current_key].users--;
299
if (shader_map[current_key].users == 0) {
300
//deallocate shader, as it's no longer in use
301
RS::get_singleton()->free(shader_map[current_key].shader);
302
shader_map.erase(current_key);
303
}
304
305
RS::get_singleton()->material_set_shader(_get_material(), RID());
306
}
307
}
308
309