1 /*************************************************************************/
2 /* light_2d.cpp */
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 #include "light_2d.h"
32
33 #include "core/engine.h"
34 #include "servers/visual_server.h"
35
36 #ifdef TOOLS_ENABLED
_edit_get_state() const37 Dictionary Light2D::_edit_get_state() const {
38 Dictionary state = Node2D::_edit_get_state();
39 state["offset"] = get_texture_offset();
40 return state;
41 }
42
_edit_set_state(const Dictionary & p_state)43 void Light2D::_edit_set_state(const Dictionary &p_state) {
44 Node2D::_edit_set_state(p_state);
45 set_texture_offset(p_state["offset"]);
46 }
47
_edit_set_pivot(const Point2 & p_pivot)48 void Light2D::_edit_set_pivot(const Point2 &p_pivot) {
49 set_position(get_transform().xform(p_pivot));
50 set_texture_offset(get_texture_offset() - p_pivot);
51 }
52
_edit_get_pivot() const53 Point2 Light2D::_edit_get_pivot() const {
54 return Vector2();
55 }
56
_edit_use_pivot() const57 bool Light2D::_edit_use_pivot() const {
58 return true;
59 }
60
_edit_get_rect() const61 Rect2 Light2D::_edit_get_rect() const {
62 if (texture.is_null())
63 return Rect2();
64
65 Size2 s = texture->get_size() * _scale;
66 return Rect2(texture_offset - s / 2.0, s);
67 }
68
_edit_use_rect() const69 bool Light2D::_edit_use_rect() const {
70 return !texture.is_null();
71 }
72 #endif
73
get_anchorable_rect() const74 Rect2 Light2D::get_anchorable_rect() const {
75 if (texture.is_null())
76 return Rect2();
77
78 Size2 s = texture->get_size() * _scale;
79 return Rect2(texture_offset - s / 2.0, s);
80 }
81
_update_light_visibility()82 void Light2D::_update_light_visibility() {
83
84 if (!is_inside_tree())
85 return;
86
87 bool editor_ok = true;
88
89 #ifdef TOOLS_ENABLED
90 if (editor_only) {
91 if (!Engine::get_singleton()->is_editor_hint()) {
92 editor_ok = false;
93 } else {
94 editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
95 }
96 }
97 #else
98 if (editor_only) {
99 editor_ok = false;
100 }
101 #endif
102
103 VS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree() && editor_ok);
104 }
105
set_enabled(bool p_enabled)106 void Light2D::set_enabled(bool p_enabled) {
107
108 enabled = p_enabled;
109 _update_light_visibility();
110 }
111
is_enabled() const112 bool Light2D::is_enabled() const {
113
114 return enabled;
115 }
116
set_editor_only(bool p_editor_only)117 void Light2D::set_editor_only(bool p_editor_only) {
118
119 editor_only = p_editor_only;
120 _update_light_visibility();
121 }
122
is_editor_only() const123 bool Light2D::is_editor_only() const {
124
125 return editor_only;
126 }
127
set_texture(const Ref<Texture> & p_texture)128 void Light2D::set_texture(const Ref<Texture> &p_texture) {
129
130 texture = p_texture;
131 if (texture.is_valid())
132 VS::get_singleton()->canvas_light_set_texture(canvas_light, texture->get_rid());
133 else
134 VS::get_singleton()->canvas_light_set_texture(canvas_light, RID());
135
136 update_configuration_warning();
137 }
138
get_texture() const139 Ref<Texture> Light2D::get_texture() const {
140
141 return texture;
142 }
143
set_texture_offset(const Vector2 & p_offset)144 void Light2D::set_texture_offset(const Vector2 &p_offset) {
145
146 texture_offset = p_offset;
147 VS::get_singleton()->canvas_light_set_texture_offset(canvas_light, texture_offset);
148 item_rect_changed();
149 _change_notify("offset");
150 }
151
get_texture_offset() const152 Vector2 Light2D::get_texture_offset() const {
153
154 return texture_offset;
155 }
156
set_color(const Color & p_color)157 void Light2D::set_color(const Color &p_color) {
158
159 color = p_color;
160 VS::get_singleton()->canvas_light_set_color(canvas_light, color);
161 }
get_color() const162 Color Light2D::get_color() const {
163
164 return color;
165 }
166
set_height(float p_height)167 void Light2D::set_height(float p_height) {
168
169 height = p_height;
170 VS::get_singleton()->canvas_light_set_height(canvas_light, height);
171 }
172
get_height() const173 float Light2D::get_height() const {
174
175 return height;
176 }
177
set_energy(float p_energy)178 void Light2D::set_energy(float p_energy) {
179
180 energy = p_energy;
181 VS::get_singleton()->canvas_light_set_energy(canvas_light, energy);
182 }
183
get_energy() const184 float Light2D::get_energy() const {
185
186 return energy;
187 }
188
set_texture_scale(float p_scale)189 void Light2D::set_texture_scale(float p_scale) {
190
191 _scale = p_scale;
192 // Avoid having 0 scale values, can lead to errors in physics and rendering.
193 if (_scale == 0) {
194 _scale = CMP_EPSILON;
195 }
196 VS::get_singleton()->canvas_light_set_scale(canvas_light, _scale);
197 item_rect_changed();
198 }
199
get_texture_scale() const200 float Light2D::get_texture_scale() const {
201
202 return _scale;
203 }
204
set_z_range_min(int p_min_z)205 void Light2D::set_z_range_min(int p_min_z) {
206
207 z_min = p_min_z;
208 VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
209 }
get_z_range_min() const210 int Light2D::get_z_range_min() const {
211
212 return z_min;
213 }
214
set_z_range_max(int p_max_z)215 void Light2D::set_z_range_max(int p_max_z) {
216
217 z_max = p_max_z;
218 VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
219 }
get_z_range_max() const220 int Light2D::get_z_range_max() const {
221
222 return z_max;
223 }
224
set_layer_range_min(int p_min_layer)225 void Light2D::set_layer_range_min(int p_min_layer) {
226
227 layer_min = p_min_layer;
228 VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
229 }
get_layer_range_min() const230 int Light2D::get_layer_range_min() const {
231
232 return layer_min;
233 }
234
set_layer_range_max(int p_max_layer)235 void Light2D::set_layer_range_max(int p_max_layer) {
236
237 layer_max = p_max_layer;
238 VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
239 }
get_layer_range_max() const240 int Light2D::get_layer_range_max() const {
241
242 return layer_max;
243 }
244
set_item_cull_mask(int p_mask)245 void Light2D::set_item_cull_mask(int p_mask) {
246
247 item_mask = p_mask;
248 VS::get_singleton()->canvas_light_set_item_cull_mask(canvas_light, item_mask);
249 }
250
get_item_cull_mask() const251 int Light2D::get_item_cull_mask() const {
252
253 return item_mask;
254 }
255
set_item_shadow_cull_mask(int p_mask)256 void Light2D::set_item_shadow_cull_mask(int p_mask) {
257
258 item_shadow_mask = p_mask;
259 VS::get_singleton()->canvas_light_set_item_shadow_cull_mask(canvas_light, item_shadow_mask);
260 }
261
get_item_shadow_cull_mask() const262 int Light2D::get_item_shadow_cull_mask() const {
263
264 return item_shadow_mask;
265 }
266
set_mode(Mode p_mode)267 void Light2D::set_mode(Mode p_mode) {
268
269 mode = p_mode;
270 VS::get_singleton()->canvas_light_set_mode(canvas_light, VS::CanvasLightMode(p_mode));
271 }
272
get_mode() const273 Light2D::Mode Light2D::get_mode() const {
274
275 return mode;
276 }
277
set_shadow_enabled(bool p_enabled)278 void Light2D::set_shadow_enabled(bool p_enabled) {
279
280 shadow = p_enabled;
281 VS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light, shadow);
282 }
is_shadow_enabled() const283 bool Light2D::is_shadow_enabled() const {
284
285 return shadow;
286 }
287
set_shadow_buffer_size(int p_size)288 void Light2D::set_shadow_buffer_size(int p_size) {
289
290 shadow_buffer_size = p_size;
291 VS::get_singleton()->canvas_light_set_shadow_buffer_size(canvas_light, shadow_buffer_size);
292 }
293
get_shadow_buffer_size() const294 int Light2D::get_shadow_buffer_size() const {
295
296 return shadow_buffer_size;
297 }
298
set_shadow_gradient_length(float p_multiplier)299 void Light2D::set_shadow_gradient_length(float p_multiplier) {
300
301 shadow_gradient_length = p_multiplier;
302 VS::get_singleton()->canvas_light_set_shadow_gradient_length(canvas_light, p_multiplier);
303 }
304
get_shadow_gradient_length() const305 float Light2D::get_shadow_gradient_length() const {
306
307 return shadow_gradient_length;
308 }
309
set_shadow_filter(ShadowFilter p_filter)310 void Light2D::set_shadow_filter(ShadowFilter p_filter) {
311 shadow_filter = p_filter;
312 VS::get_singleton()->canvas_light_set_shadow_filter(canvas_light, VS::CanvasLightShadowFilter(p_filter));
313 }
314
get_shadow_filter() const315 Light2D::ShadowFilter Light2D::get_shadow_filter() const {
316
317 return shadow_filter;
318 }
319
set_shadow_color(const Color & p_shadow_color)320 void Light2D::set_shadow_color(const Color &p_shadow_color) {
321 shadow_color = p_shadow_color;
322 VS::get_singleton()->canvas_light_set_shadow_color(canvas_light, shadow_color);
323 }
324
get_shadow_color() const325 Color Light2D::get_shadow_color() const {
326 return shadow_color;
327 }
328
_notification(int p_what)329 void Light2D::_notification(int p_what) {
330
331 if (p_what == NOTIFICATION_ENTER_TREE) {
332
333 VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, get_canvas());
334 _update_light_visibility();
335 }
336
337 if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
338
339 VS::get_singleton()->canvas_light_set_transform(canvas_light, get_global_transform());
340 }
341 if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
342
343 _update_light_visibility();
344 }
345
346 if (p_what == NOTIFICATION_EXIT_TREE) {
347
348 VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, RID());
349 _update_light_visibility();
350 }
351 }
352
get_configuration_warning() const353 String Light2D::get_configuration_warning() const {
354
355 if (!texture.is_valid()) {
356 return TTR("A texture with the shape of the light must be supplied to the \"Texture\" property.");
357 }
358
359 return String();
360 }
361
set_shadow_smooth(float p_amount)362 void Light2D::set_shadow_smooth(float p_amount) {
363
364 shadow_smooth = p_amount;
365 VS::get_singleton()->canvas_light_set_shadow_smooth(canvas_light, shadow_smooth);
366 }
367
get_shadow_smooth() const368 float Light2D::get_shadow_smooth() const {
369
370 return shadow_smooth;
371 }
372
_bind_methods()373 void Light2D::_bind_methods() {
374
375 ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled);
376 ClassDB::bind_method(D_METHOD("is_enabled"), &Light2D::is_enabled);
377
378 ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
379 ClassDB::bind_method(D_METHOD("is_editor_only"), &Light2D::is_editor_only);
380
381 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Light2D::set_texture);
382 ClassDB::bind_method(D_METHOD("get_texture"), &Light2D::get_texture);
383
384 ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Light2D::set_texture_offset);
385 ClassDB::bind_method(D_METHOD("get_texture_offset"), &Light2D::get_texture_offset);
386
387 ClassDB::bind_method(D_METHOD("set_color", "color"), &Light2D::set_color);
388 ClassDB::bind_method(D_METHOD("get_color"), &Light2D::get_color);
389
390 ClassDB::bind_method(D_METHOD("set_height", "height"), &Light2D::set_height);
391 ClassDB::bind_method(D_METHOD("get_height"), &Light2D::get_height);
392
393 ClassDB::bind_method(D_METHOD("set_energy", "energy"), &Light2D::set_energy);
394 ClassDB::bind_method(D_METHOD("get_energy"), &Light2D::get_energy);
395
396 ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Light2D::set_texture_scale);
397 ClassDB::bind_method(D_METHOD("get_texture_scale"), &Light2D::get_texture_scale);
398
399 ClassDB::bind_method(D_METHOD("set_z_range_min", "z"), &Light2D::set_z_range_min);
400 ClassDB::bind_method(D_METHOD("get_z_range_min"), &Light2D::get_z_range_min);
401
402 ClassDB::bind_method(D_METHOD("set_z_range_max", "z"), &Light2D::set_z_range_max);
403 ClassDB::bind_method(D_METHOD("get_z_range_max"), &Light2D::get_z_range_max);
404
405 ClassDB::bind_method(D_METHOD("set_layer_range_min", "layer"), &Light2D::set_layer_range_min);
406 ClassDB::bind_method(D_METHOD("get_layer_range_min"), &Light2D::get_layer_range_min);
407
408 ClassDB::bind_method(D_METHOD("set_layer_range_max", "layer"), &Light2D::set_layer_range_max);
409 ClassDB::bind_method(D_METHOD("get_layer_range_max"), &Light2D::get_layer_range_max);
410
411 ClassDB::bind_method(D_METHOD("set_item_cull_mask", "item_cull_mask"), &Light2D::set_item_cull_mask);
412 ClassDB::bind_method(D_METHOD("get_item_cull_mask"), &Light2D::get_item_cull_mask);
413
414 ClassDB::bind_method(D_METHOD("set_item_shadow_cull_mask", "item_shadow_cull_mask"), &Light2D::set_item_shadow_cull_mask);
415 ClassDB::bind_method(D_METHOD("get_item_shadow_cull_mask"), &Light2D::get_item_shadow_cull_mask);
416
417 ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Light2D::set_mode);
418 ClassDB::bind_method(D_METHOD("get_mode"), &Light2D::get_mode);
419
420 ClassDB::bind_method(D_METHOD("set_shadow_enabled", "enabled"), &Light2D::set_shadow_enabled);
421 ClassDB::bind_method(D_METHOD("is_shadow_enabled"), &Light2D::is_shadow_enabled);
422
423 ClassDB::bind_method(D_METHOD("set_shadow_buffer_size", "size"), &Light2D::set_shadow_buffer_size);
424 ClassDB::bind_method(D_METHOD("get_shadow_buffer_size"), &Light2D::get_shadow_buffer_size);
425
426 ClassDB::bind_method(D_METHOD("set_shadow_smooth", "smooth"), &Light2D::set_shadow_smooth);
427 ClassDB::bind_method(D_METHOD("get_shadow_smooth"), &Light2D::get_shadow_smooth);
428
429 ClassDB::bind_method(D_METHOD("set_shadow_gradient_length", "multiplier"), &Light2D::set_shadow_gradient_length);
430 ClassDB::bind_method(D_METHOD("get_shadow_gradient_length"), &Light2D::get_shadow_gradient_length);
431
432 ClassDB::bind_method(D_METHOD("set_shadow_filter", "filter"), &Light2D::set_shadow_filter);
433 ClassDB::bind_method(D_METHOD("get_shadow_filter"), &Light2D::get_shadow_filter);
434
435 ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light2D::set_shadow_color);
436 ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light2D::get_shadow_color);
437
438 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
439 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
440 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
441 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_texture_offset", "get_texture_offset");
442 ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), "set_texture_scale", "get_texture_scale");
443 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
444 ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_energy", "get_energy");
445 ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Add,Sub,Mix,Mask"), "set_mode", "get_mode");
446 ADD_GROUP("Range", "range_");
447 ADD_PROPERTY(PropertyInfo(Variant::REAL, "range_height", PROPERTY_HINT_RANGE, "-2048,2048,0.1,or_lesser,or_greater"), "set_height", "get_height");
448 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_min", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_min", "get_z_range_min");
449 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_max", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_max", "get_z_range_max");
450 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_min", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_min", "get_layer_range_min");
451 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_max", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_max", "get_layer_range_max");
452 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_cull_mask", "get_item_cull_mask");
453
454 ADD_GROUP("Shadow", "shadow_");
455 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow_enabled", "is_shadow_enabled");
456 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
457 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_buffer_size", PROPERTY_HINT_RANGE, "32,16384,1"), "set_shadow_buffer_size", "get_shadow_buffer_size");
458 ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_gradient_length", PROPERTY_HINT_RANGE, "0,4096,0.1"), "set_shadow_gradient_length", "get_shadow_gradient_length");
459 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_filter", PROPERTY_HINT_ENUM, "None,PCF3,PCF5,PCF7,PCF9,PCF13"), "set_shadow_filter", "get_shadow_filter");
460 ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_filter_smooth", PROPERTY_HINT_RANGE, "0,64,0.1"), "set_shadow_smooth", "get_shadow_smooth");
461 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_shadow_cull_mask", "get_item_shadow_cull_mask");
462
463 BIND_ENUM_CONSTANT(MODE_ADD);
464 BIND_ENUM_CONSTANT(MODE_SUB);
465 BIND_ENUM_CONSTANT(MODE_MIX);
466 BIND_ENUM_CONSTANT(MODE_MASK);
467
468 BIND_ENUM_CONSTANT(SHADOW_FILTER_NONE);
469 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF3);
470 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF5);
471 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF7);
472 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF9);
473 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF13);
474 }
475
Light2D()476 Light2D::Light2D() {
477
478 canvas_light = VisualServer::get_singleton()->canvas_light_create();
479 enabled = true;
480 editor_only = false;
481 shadow = false;
482 color = Color(1, 1, 1);
483 height = 0;
484 _scale = 1.0;
485 z_min = -1024;
486 z_max = 1024;
487 layer_min = 0;
488 layer_max = 0;
489 item_mask = 1;
490 item_shadow_mask = 1;
491 mode = MODE_ADD;
492 shadow_buffer_size = 2048;
493 shadow_gradient_length = 0;
494 energy = 1.0;
495 shadow_color = Color(0, 0, 0, 0);
496 shadow_filter = SHADOW_FILTER_NONE;
497 shadow_smooth = 0;
498
499 set_notify_transform(true);
500 }
501
~Light2D()502 Light2D::~Light2D() {
503
504 VisualServer::get_singleton()->free(canvas_light);
505 }
506