1/**************************************************************************/
2/* light_2d.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 "light_2d.h"
32
33void Light2D::owner_changed_notify() {
34 // For cases where owner changes _after_ entering tree (as example, editor editing).
35 _update_light_visibility();
36}
37
38void Light2D::_update_light_visibility() {
39 if (!is_inside_tree()) {
40 return;
41 }
42
43 bool editor_ok = true;
44
45#ifdef TOOLS_ENABLED
46 if (editor_only) {
47 if (!Engine::get_singleton()->is_editor_hint()) {
48 editor_ok = false;
49 } else {
50 editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
51 }
52 }
53#else
54 if (editor_only) {
55 editor_ok = false;
56 }
57#endif
58
59 RS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree() && editor_ok);
60}
61
62void Light2D::set_enabled(bool p_enabled) {
63 enabled = p_enabled;
64 _update_light_visibility();
65}
66
67bool Light2D::is_enabled() const {
68 return enabled;
69}
70
71void Light2D::set_editor_only(bool p_editor_only) {
72 editor_only = p_editor_only;
73 _update_light_visibility();
74}
75
76bool Light2D::is_editor_only() const {
77 return editor_only;
78}
79
80void Light2D::set_color(const Color &p_color) {
81 color = p_color;
82 RS::get_singleton()->canvas_light_set_color(canvas_light, color);
83}
84
85Color Light2D::get_color() const {
86 return color;
87}
88
89void Light2D::set_height(real_t p_height) {
90 height = p_height;
91 RS::get_singleton()->canvas_light_set_height(canvas_light, height);
92}
93
94real_t Light2D::get_height() const {
95 return height;
96}
97
98void Light2D::set_energy(real_t p_energy) {
99 energy = p_energy;
100 RS::get_singleton()->canvas_light_set_energy(canvas_light, energy);
101}
102
103real_t Light2D::get_energy() const {
104 return energy;
105}
106
107void Light2D::set_z_range_min(int p_min_z) {
108 z_min = p_min_z;
109 RS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
110}
111
112int Light2D::get_z_range_min() const {
113 return z_min;
114}
115
116void Light2D::set_z_range_max(int p_max_z) {
117 z_max = p_max_z;
118 RS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
119}
120
121int Light2D::get_z_range_max() const {
122 return z_max;
123}
124
125void Light2D::set_layer_range_min(int p_min_layer) {
126 layer_min = p_min_layer;
127 RS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
128}
129
130int Light2D::get_layer_range_min() const {
131 return layer_min;
132}
133
134void Light2D::set_layer_range_max(int p_max_layer) {
135 layer_max = p_max_layer;
136 RS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
137}
138
139int Light2D::get_layer_range_max() const {
140 return layer_max;
141}
142
143void Light2D::set_item_cull_mask(int p_mask) {
144 item_mask = p_mask;
145 RS::get_singleton()->canvas_light_set_item_cull_mask(canvas_light, item_mask);
146}
147
148int Light2D::get_item_cull_mask() const {
149 return item_mask;
150}
151
152void Light2D::set_item_shadow_cull_mask(int p_mask) {
153 item_shadow_mask = p_mask;
154 RS::get_singleton()->canvas_light_set_item_shadow_cull_mask(canvas_light, item_shadow_mask);
155}
156
157int Light2D::get_item_shadow_cull_mask() const {
158 return item_shadow_mask;
159}
160
161void Light2D::set_shadow_enabled(bool p_enabled) {
162 shadow = p_enabled;
163 RS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light, shadow);
164 notify_property_list_changed();
165}
166
167bool Light2D::is_shadow_enabled() const {
168 return shadow;
169}
170
171void Light2D::set_shadow_filter(ShadowFilter p_filter) {
172 ERR_FAIL_INDEX(p_filter, SHADOW_FILTER_MAX);
173 shadow_filter = p_filter;
174 RS::get_singleton()->canvas_light_set_shadow_filter(canvas_light, RS::CanvasLightShadowFilter(p_filter));
175 notify_property_list_changed();
176}
177
178Light2D::ShadowFilter Light2D::get_shadow_filter() const {
179 return shadow_filter;
180}
181
182void Light2D::set_shadow_color(const Color &p_shadow_color) {
183 shadow_color = p_shadow_color;
184 RS::get_singleton()->canvas_light_set_shadow_color(canvas_light, shadow_color);
185}
186
187Color Light2D::get_shadow_color() const {
188 return shadow_color;
189}
190
191void Light2D::set_blend_mode(BlendMode p_mode) {
192 blend_mode = p_mode;
193 RS::get_singleton()->canvas_light_set_blend_mode(_get_light(), RS::CanvasLightBlendMode(p_mode));
194}
195
196Light2D::BlendMode Light2D::get_blend_mode() const {
197 return blend_mode;
198}
199
200void Light2D::_notification(int p_what) {
201 switch (p_what) {
202 case NOTIFICATION_ENTER_TREE: {
203 RS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, get_canvas());
204 _update_light_visibility();
205 } break;
206
207 case NOTIFICATION_TRANSFORM_CHANGED: {
208 RS::get_singleton()->canvas_light_set_transform(canvas_light, get_global_transform());
209 } break;
210
211 case NOTIFICATION_VISIBILITY_CHANGED: {
212 _update_light_visibility();
213 } break;
214
215 case NOTIFICATION_EXIT_TREE: {
216 RS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, RID());
217 _update_light_visibility();
218 } break;
219 }
220}
221
222void Light2D::set_shadow_smooth(real_t p_amount) {
223 shadow_smooth = p_amount;
224 RS::get_singleton()->canvas_light_set_shadow_smooth(canvas_light, shadow_smooth);
225}
226
227real_t Light2D::get_shadow_smooth() const {
228 return shadow_smooth;
229}
230
231void Light2D::_validate_property(PropertyInfo &p_property) const {
232 if (!shadow && (p_property.name == "shadow_color" || p_property.name == "shadow_filter" || p_property.name == "shadow_filter_smooth" || p_property.name == "shadow_item_cull_mask")) {
233 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
234 }
235
236 if (shadow && p_property.name == "shadow_filter_smooth" && shadow_filter == SHADOW_FILTER_NONE) {
237 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
238 }
239}
240
241void Light2D::_bind_methods() {
242 ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled);
243 ClassDB::bind_method(D_METHOD("is_enabled"), &Light2D::is_enabled);
244
245 ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
246 ClassDB::bind_method(D_METHOD("is_editor_only"), &Light2D::is_editor_only);
247
248 ClassDB::bind_method(D_METHOD("set_color", "color"), &Light2D::set_color);
249 ClassDB::bind_method(D_METHOD("get_color"), &Light2D::get_color);
250
251 ClassDB::bind_method(D_METHOD("set_energy", "energy"), &Light2D::set_energy);
252 ClassDB::bind_method(D_METHOD("get_energy"), &Light2D::get_energy);
253
254 ClassDB::bind_method(D_METHOD("set_z_range_min", "z"), &Light2D::set_z_range_min);
255 ClassDB::bind_method(D_METHOD("get_z_range_min"), &Light2D::get_z_range_min);
256
257 ClassDB::bind_method(D_METHOD("set_z_range_max", "z"), &Light2D::set_z_range_max);
258 ClassDB::bind_method(D_METHOD("get_z_range_max"), &Light2D::get_z_range_max);
259
260 ClassDB::bind_method(D_METHOD("set_layer_range_min", "layer"), &Light2D::set_layer_range_min);
261 ClassDB::bind_method(D_METHOD("get_layer_range_min"), &Light2D::get_layer_range_min);
262
263 ClassDB::bind_method(D_METHOD("set_layer_range_max", "layer"), &Light2D::set_layer_range_max);
264 ClassDB::bind_method(D_METHOD("get_layer_range_max"), &Light2D::get_layer_range_max);
265
266 ClassDB::bind_method(D_METHOD("set_item_cull_mask", "item_cull_mask"), &Light2D::set_item_cull_mask);
267 ClassDB::bind_method(D_METHOD("get_item_cull_mask"), &Light2D::get_item_cull_mask);
268
269 ClassDB::bind_method(D_METHOD("set_item_shadow_cull_mask", "item_shadow_cull_mask"), &Light2D::set_item_shadow_cull_mask);
270 ClassDB::bind_method(D_METHOD("get_item_shadow_cull_mask"), &Light2D::get_item_shadow_cull_mask);
271
272 ClassDB::bind_method(D_METHOD("set_shadow_enabled", "enabled"), &Light2D::set_shadow_enabled);
273 ClassDB::bind_method(D_METHOD("is_shadow_enabled"), &Light2D::is_shadow_enabled);
274
275 ClassDB::bind_method(D_METHOD("set_shadow_smooth", "smooth"), &Light2D::set_shadow_smooth);
276 ClassDB::bind_method(D_METHOD("get_shadow_smooth"), &Light2D::get_shadow_smooth);
277
278 ClassDB::bind_method(D_METHOD("set_shadow_filter", "filter"), &Light2D::set_shadow_filter);
279 ClassDB::bind_method(D_METHOD("get_shadow_filter"), &Light2D::get_shadow_filter);
280
281 ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light2D::set_shadow_color);
282 ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light2D::get_shadow_color);
283
284 ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &Light2D::set_blend_mode);
285 ClassDB::bind_method(D_METHOD("get_blend_mode"), &Light2D::get_blend_mode);
286
287 ClassDB::bind_method(D_METHOD("set_height", "height"), &Light2D::set_height);
288 ClassDB::bind_method(D_METHOD("get_height"), &Light2D::get_height);
289
290 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
291 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
292 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
293 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_energy", "get_energy");
294 ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Add,Subtract,Mix"), "set_blend_mode", "get_blend_mode");
295 ADD_GROUP("Range", "range_");
296 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_min", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_min", "get_z_range_min");
297 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_max", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_max", "get_z_range_max");
298 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_min", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_min", "get_layer_range_min");
299 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_max", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_max", "get_layer_range_max");
300 ADD_PROPERTY(PropertyInfo(Variant::INT, "range_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_cull_mask", "get_item_cull_mask");
301
302 ADD_GROUP("Shadow", "shadow_");
303 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow_enabled", "is_shadow_enabled");
304 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
305 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_filter", PROPERTY_HINT_ENUM, "None (Fast),PCF5 (Average),PCF13 (Slow)"), "set_shadow_filter", "get_shadow_filter");
306 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "shadow_filter_smooth", PROPERTY_HINT_RANGE, "0,64,0.1"), "set_shadow_smooth", "get_shadow_smooth");
307 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_shadow_cull_mask", "get_item_shadow_cull_mask");
308
309 BIND_ENUM_CONSTANT(SHADOW_FILTER_NONE);
310 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF5);
311 BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF13);
312
313 BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
314 BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
315 BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
316}
317
318Light2D::Light2D() {
319 canvas_light = RenderingServer::get_singleton()->canvas_light_create();
320 set_notify_transform(true);
321}
322
323Light2D::~Light2D() {
324 ERR_FAIL_NULL(RenderingServer::get_singleton());
325 RenderingServer::get_singleton()->free(canvas_light);
326}
327
328//////////////////////////////
329
330#ifdef TOOLS_ENABLED
331
332Dictionary PointLight2D::_edit_get_state() const {
333 Dictionary state = Node2D::_edit_get_state();
334 state["offset"] = get_texture_offset();
335 return state;
336}
337
338void PointLight2D::_edit_set_state(const Dictionary &p_state) {
339 Node2D::_edit_set_state(p_state);
340 set_texture_offset(p_state["offset"]);
341}
342
343void PointLight2D::_edit_set_pivot(const Point2 &p_pivot) {
344 set_position(get_transform().xform(p_pivot));
345 set_texture_offset(get_texture_offset() - p_pivot);
346}
347
348Point2 PointLight2D::_edit_get_pivot() const {
349 return Vector2();
350}
351
352bool PointLight2D::_edit_use_pivot() const {
353 return true;
354}
355
356Rect2 PointLight2D::_edit_get_rect() const {
357 if (texture.is_null()) {
358 return Rect2();
359 }
360
361 Size2 s = texture->get_size() * _scale;
362 return Rect2(texture_offset - s / 2.0, s);
363}
364
365bool PointLight2D::_edit_use_rect() const {
366 return !texture.is_null();
367}
368#endif
369
370Rect2 PointLight2D::get_anchorable_rect() const {
371 if (texture.is_null()) {
372 return Rect2();
373 }
374
375 Size2 s = texture->get_size() * _scale;
376 return Rect2(texture_offset - s / 2.0, s);
377}
378
379void PointLight2D::set_texture(const Ref<Texture2D> &p_texture) {
380 texture = p_texture;
381 if (texture.is_valid()) {
382 RS::get_singleton()->canvas_light_set_texture(_get_light(), texture->get_rid());
383 } else {
384 RS::get_singleton()->canvas_light_set_texture(_get_light(), RID());
385 }
386
387 update_configuration_warnings();
388}
389
390Ref<Texture2D> PointLight2D::get_texture() const {
391 return texture;
392}
393
394void PointLight2D::set_texture_offset(const Vector2 &p_offset) {
395 texture_offset = p_offset;
396 RS::get_singleton()->canvas_light_set_texture_offset(_get_light(), texture_offset);
397 item_rect_changed();
398}
399
400Vector2 PointLight2D::get_texture_offset() const {
401 return texture_offset;
402}
403
404PackedStringArray PointLight2D::get_configuration_warnings() const {
405 PackedStringArray warnings = Node::get_configuration_warnings();
406
407 if (!texture.is_valid()) {
408 warnings.push_back(RTR("A texture with the shape of the light must be supplied to the \"Texture\" property."));
409 }
410
411 return warnings;
412}
413
414void PointLight2D::set_texture_scale(real_t p_scale) {
415 _scale = p_scale;
416 // Avoid having 0 scale values, can lead to errors in physics and rendering.
417 if (_scale == 0) {
418 _scale = CMP_EPSILON;
419 }
420 RS::get_singleton()->canvas_light_set_texture_scale(_get_light(), _scale);
421 item_rect_changed();
422}
423
424real_t PointLight2D::get_texture_scale() const {
425 return _scale;
426}
427
428void PointLight2D::_bind_methods() {
429 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &PointLight2D::set_texture);
430 ClassDB::bind_method(D_METHOD("get_texture"), &PointLight2D::get_texture);
431
432 ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &PointLight2D::set_texture_offset);
433 ClassDB::bind_method(D_METHOD("get_texture_offset"), &PointLight2D::get_texture_offset);
434
435 ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &PointLight2D::set_texture_scale);
436 ClassDB::bind_method(D_METHOD("get_texture_scale"), &PointLight2D::get_texture_scale);
437
438 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
439 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset");
440 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), "set_texture_scale", "get_texture_scale");
441 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1024,1,or_greater,suffix:px"), "set_height", "get_height");
442}
443
444PointLight2D::PointLight2D() {
445 RS::get_singleton()->canvas_light_set_mode(_get_light(), RS::CANVAS_LIGHT_MODE_POINT);
446 set_hide_clip_children(true);
447}
448
449//////////
450
451void DirectionalLight2D::set_max_distance(real_t p_distance) {
452 max_distance = p_distance;
453 RS::get_singleton()->canvas_light_set_directional_distance(_get_light(), max_distance);
454}
455
456real_t DirectionalLight2D::get_max_distance() const {
457 return max_distance;
458}
459
460void DirectionalLight2D::_bind_methods() {
461 ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &DirectionalLight2D::set_max_distance);
462 ClassDB::bind_method(D_METHOD("get_max_distance"), &DirectionalLight2D::get_max_distance);
463
464 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_height", "get_height");
465 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "0,16384.0,1.0,or_greater,suffix:px"), "set_max_distance", "get_max_distance");
466}
467
468DirectionalLight2D::DirectionalLight2D() {
469 RS::get_singleton()->canvas_light_set_mode(_get_light(), RS::CANVAS_LIGHT_MODE_DIRECTIONAL);
470 set_max_distance(max_distance); // Update RenderingServer.
471 set_hide_clip_children(true);
472}
473