1 /*************************************************************************/
2 /* light_2d.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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 #include "light_2d.h"
31 #include "servers/visual_server.h"
32
edit_set_pivot(const Point2 & p_pivot)33 void Light2D::edit_set_pivot(const Point2 &p_pivot) {
34
35 set_texture_offset(p_pivot);
36 }
37
edit_get_pivot() const38 Point2 Light2D::edit_get_pivot() const {
39
40 return get_texture_offset();
41 }
edit_has_pivot() const42 bool Light2D::edit_has_pivot() const {
43
44 return true;
45 }
46
get_item_rect() const47 Rect2 Light2D::get_item_rect() const {
48
49 if (texture.is_null())
50 return Rect2(0, 0, 1, 1);
51
52 Size2i s;
53
54 s = texture->get_size() * _scale;
55 Point2i ofs = texture_offset;
56 ofs -= s / 2;
57
58 if (s == Size2(0, 0))
59 s = Size2(1, 1);
60
61 return Rect2(ofs, s);
62 }
63
_update_light_visibility()64 void Light2D::_update_light_visibility() {
65
66 if (!is_inside_tree())
67 return;
68
69 bool editor_ok = true;
70
71 #ifdef TOOLS_ENABLED
72 if (editor_only) {
73 if (!get_tree()->is_editor_hint()) {
74 editor_ok = false;
75 } else {
76 editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
77 }
78 }
79 #else
80 if (editor_only) {
81 editor_ok = false;
82 }
83 #endif
84
85 VS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible() && editor_ok);
86 }
87
set_enabled(bool p_enabled)88 void Light2D::set_enabled(bool p_enabled) {
89
90 enabled = p_enabled;
91 _update_light_visibility();
92 }
93
is_enabled() const94 bool Light2D::is_enabled() const {
95
96 return enabled;
97 }
98
set_editor_only(bool p_editor_only)99 void Light2D::set_editor_only(bool p_editor_only) {
100
101 editor_only = p_editor_only;
102 _update_light_visibility();
103 }
104
is_editor_only() const105 bool Light2D::is_editor_only() const {
106
107 return editor_only;
108 }
109
set_texture(const Ref<Texture> & p_texture)110 void Light2D::set_texture(const Ref<Texture> &p_texture) {
111
112 texture = p_texture;
113 if (texture.is_valid())
114 VS::get_singleton()->canvas_light_set_texture(canvas_light, texture->get_rid());
115 else
116 VS::get_singleton()->canvas_light_set_texture(canvas_light, RID());
117
118 update_configuration_warning();
119 }
120
get_texture() const121 Ref<Texture> Light2D::get_texture() const {
122
123 return texture;
124 }
125
set_texture_offset(const Vector2 & p_offset)126 void Light2D::set_texture_offset(const Vector2 &p_offset) {
127
128 texture_offset = p_offset;
129 VS::get_singleton()->canvas_light_set_texture_offset(canvas_light, texture_offset);
130 item_rect_changed();
131 }
132
get_texture_offset() const133 Vector2 Light2D::get_texture_offset() const {
134
135 return texture_offset;
136 }
137
set_color(const Color & p_color)138 void Light2D::set_color(const Color &p_color) {
139
140 color = p_color;
141 VS::get_singleton()->canvas_light_set_color(canvas_light, color);
142 }
get_color() const143 Color Light2D::get_color() const {
144
145 return color;
146 }
147
set_height(float p_height)148 void Light2D::set_height(float p_height) {
149
150 height = p_height;
151 VS::get_singleton()->canvas_light_set_height(canvas_light, height);
152 }
153
get_height() const154 float Light2D::get_height() const {
155
156 return height;
157 }
158
set_energy(float p_energy)159 void Light2D::set_energy(float p_energy) {
160
161 energy = p_energy;
162 VS::get_singleton()->canvas_light_set_energy(canvas_light, energy);
163 }
164
get_energy() const165 float Light2D::get_energy() const {
166
167 return energy;
168 }
169
set_texture_scale(float p_scale)170 void Light2D::set_texture_scale(float p_scale) {
171
172 _scale = p_scale;
173 VS::get_singleton()->canvas_light_set_scale(canvas_light, _scale);
174 item_rect_changed();
175 }
176
get_texture_scale() const177 float Light2D::get_texture_scale() const {
178
179 return _scale;
180 }
181
set_z_range_min(int p_min_z)182 void Light2D::set_z_range_min(int p_min_z) {
183
184 z_min = p_min_z;
185 VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
186 }
get_z_range_min() const187 int Light2D::get_z_range_min() const {
188
189 return z_min;
190 }
191
set_z_range_max(int p_max_z)192 void Light2D::set_z_range_max(int p_max_z) {
193
194 z_max = p_max_z;
195 VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
196 }
get_z_range_max() const197 int Light2D::get_z_range_max() const {
198
199 return z_max;
200 }
201
set_layer_range_min(int p_min_layer)202 void Light2D::set_layer_range_min(int p_min_layer) {
203
204 layer_min = p_min_layer;
205 VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
206 }
get_layer_range_min() const207 int Light2D::get_layer_range_min() const {
208
209 return layer_min;
210 }
211
set_layer_range_max(int p_max_layer)212 void Light2D::set_layer_range_max(int p_max_layer) {
213
214 layer_max = p_max_layer;
215 VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
216 }
get_layer_range_max() const217 int Light2D::get_layer_range_max() const {
218
219 return layer_max;
220 }
221
set_item_mask(int p_mask)222 void Light2D::set_item_mask(int p_mask) {
223
224 item_mask = p_mask;
225 VS::get_singleton()->canvas_light_set_item_mask(canvas_light, item_mask);
226 }
227
get_item_mask() const228 int Light2D::get_item_mask() const {
229
230 return item_mask;
231 }
232
set_item_shadow_mask(int p_mask)233 void Light2D::set_item_shadow_mask(int p_mask) {
234
235 item_shadow_mask = p_mask;
236 VS::get_singleton()->canvas_light_set_item_shadow_mask(canvas_light, item_shadow_mask);
237 }
238
get_item_shadow_mask() const239 int Light2D::get_item_shadow_mask() const {
240
241 return item_shadow_mask;
242 }
243
set_mode(Mode p_mode)244 void Light2D::set_mode(Mode p_mode) {
245
246 mode = p_mode;
247 VS::get_singleton()->canvas_light_set_mode(canvas_light, VS::CanvasLightMode(p_mode));
248 }
249
get_mode() const250 Light2D::Mode Light2D::get_mode() const {
251
252 return mode;
253 }
254
set_shadow_enabled(bool p_enabled)255 void Light2D::set_shadow_enabled(bool p_enabled) {
256
257 shadow = p_enabled;
258 VS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light, shadow);
259 }
is_shadow_enabled() const260 bool Light2D::is_shadow_enabled() const {
261
262 return shadow;
263 }
264
set_shadow_buffer_size(int p_size)265 void Light2D::set_shadow_buffer_size(int p_size) {
266
267 shadow_buffer_size = p_size;
268 VS::get_singleton()->canvas_light_set_shadow_buffer_size(canvas_light, shadow_buffer_size);
269 }
270
get_shadow_buffer_size() const271 int Light2D::get_shadow_buffer_size() const {
272
273 return shadow_buffer_size;
274 }
275
set_shadow_esm_multiplier(float p_multiplier)276 void Light2D::set_shadow_esm_multiplier(float p_multiplier) {
277
278 shadow_esm_multiplier = p_multiplier;
279 VS::get_singleton()->canvas_light_set_shadow_esm_multiplier(canvas_light, p_multiplier);
280 }
281
get_shadow_esm_multiplier() const282 float Light2D::get_shadow_esm_multiplier() const {
283
284 return shadow_esm_multiplier;
285 }
286
set_shadow_color(const Color & p_shadow_color)287 void Light2D::set_shadow_color(const Color &p_shadow_color) {
288 shadow_color = p_shadow_color;
289 VS::get_singleton()->canvas_light_set_shadow_color(canvas_light, shadow_color);
290 }
291
get_shadow_color() const292 Color Light2D::get_shadow_color() const {
293 return shadow_color;
294 }
295
_notification(int p_what)296 void Light2D::_notification(int p_what) {
297
298 if (p_what == NOTIFICATION_ENTER_TREE) {
299
300 VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, get_canvas());
301 _update_light_visibility();
302 }
303
304 if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
305
306 VS::get_singleton()->canvas_light_set_transform(canvas_light, get_global_transform());
307 }
308 if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
309
310 _update_light_visibility();
311 }
312
313 if (p_what == NOTIFICATION_EXIT_TREE) {
314
315 VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, RID());
316 _update_light_visibility();
317 }
318 }
319
get_configuration_warning() const320 String Light2D::get_configuration_warning() const {
321
322 if (!texture.is_valid()) {
323 return TTR("A texture with the shape of the light must be supplied to the 'texture' property.");
324 }
325
326 return String();
327 }
328
_bind_methods()329 void Light2D::_bind_methods() {
330
331 ObjectTypeDB::bind_method(_MD("set_enabled", "enabled"), &Light2D::set_enabled);
332 ObjectTypeDB::bind_method(_MD("is_enabled"), &Light2D::is_enabled);
333
334 ObjectTypeDB::bind_method(_MD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
335 ObjectTypeDB::bind_method(_MD("is_editor_only"), &Light2D::is_editor_only);
336
337 ObjectTypeDB::bind_method(_MD("set_texture", "texture"), &Light2D::set_texture);
338 ObjectTypeDB::bind_method(_MD("get_texture"), &Light2D::get_texture);
339
340 ObjectTypeDB::bind_method(_MD("set_texture_offset", "texture_offset"), &Light2D::set_texture_offset);
341 ObjectTypeDB::bind_method(_MD("get_texture_offset"), &Light2D::get_texture_offset);
342
343 ObjectTypeDB::bind_method(_MD("set_color", "color"), &Light2D::set_color);
344 ObjectTypeDB::bind_method(_MD("get_color"), &Light2D::get_color);
345
346 ObjectTypeDB::bind_method(_MD("set_height", "height"), &Light2D::set_height);
347 ObjectTypeDB::bind_method(_MD("get_height"), &Light2D::get_height);
348
349 ObjectTypeDB::bind_method(_MD("set_energy", "energy"), &Light2D::set_energy);
350 ObjectTypeDB::bind_method(_MD("get_energy"), &Light2D::get_energy);
351
352 ObjectTypeDB::bind_method(_MD("set_texture_scale", "texture_scale"), &Light2D::set_texture_scale);
353 ObjectTypeDB::bind_method(_MD("get_texture_scale"), &Light2D::get_texture_scale);
354
355 ObjectTypeDB::bind_method(_MD("set_z_range_min", "z"), &Light2D::set_z_range_min);
356 ObjectTypeDB::bind_method(_MD("get_z_range_min"), &Light2D::get_z_range_min);
357
358 ObjectTypeDB::bind_method(_MD("set_z_range_max", "z"), &Light2D::set_z_range_max);
359 ObjectTypeDB::bind_method(_MD("get_z_range_max"), &Light2D::get_z_range_max);
360
361 ObjectTypeDB::bind_method(_MD("set_layer_range_min", "layer"), &Light2D::set_layer_range_min);
362 ObjectTypeDB::bind_method(_MD("get_layer_range_min"), &Light2D::get_layer_range_min);
363
364 ObjectTypeDB::bind_method(_MD("set_layer_range_max", "layer"), &Light2D::set_layer_range_max);
365 ObjectTypeDB::bind_method(_MD("get_layer_range_max"), &Light2D::get_layer_range_max);
366
367 ObjectTypeDB::bind_method(_MD("set_item_mask", "item_mask"), &Light2D::set_item_mask);
368 ObjectTypeDB::bind_method(_MD("get_item_mask"), &Light2D::get_item_mask);
369
370 ObjectTypeDB::bind_method(_MD("set_item_shadow_mask", "item_shadow_mask"), &Light2D::set_item_shadow_mask);
371 ObjectTypeDB::bind_method(_MD("get_item_shadow_mask"), &Light2D::get_item_shadow_mask);
372
373 ObjectTypeDB::bind_method(_MD("set_mode", "mode"), &Light2D::set_mode);
374 ObjectTypeDB::bind_method(_MD("get_mode"), &Light2D::get_mode);
375
376 ObjectTypeDB::bind_method(_MD("set_shadow_enabled", "enabled"), &Light2D::set_shadow_enabled);
377 ObjectTypeDB::bind_method(_MD("is_shadow_enabled"), &Light2D::is_shadow_enabled);
378
379 ObjectTypeDB::bind_method(_MD("set_shadow_buffer_size", "size"), &Light2D::set_shadow_buffer_size);
380 ObjectTypeDB::bind_method(_MD("get_shadow_buffer_size"), &Light2D::get_shadow_buffer_size);
381
382 ObjectTypeDB::bind_method(_MD("set_shadow_esm_multiplier", "multiplier"), &Light2D::set_shadow_esm_multiplier);
383 ObjectTypeDB::bind_method(_MD("get_shadow_esm_multiplier"), &Light2D::get_shadow_esm_multiplier);
384
385 ObjectTypeDB::bind_method(_MD("set_shadow_color", "shadow_color"), &Light2D::set_shadow_color);
386 ObjectTypeDB::bind_method(_MD("get_shadow_color"), &Light2D::get_shadow_color);
387
388 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_enabled"), _SCS("is_enabled"));
389 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), _SCS("set_editor_only"), _SCS("is_editor_only"));
390 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"), _SCS("get_texture"));
391 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), _SCS("set_texture_offset"), _SCS("get_texture_offset"));
392 ADD_PROPERTY(PropertyInfo(Variant::REAL, "scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), _SCS("set_texture_scale"), _SCS("get_texture_scale"));
393 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), _SCS("set_color"), _SCS("get_color"));
394 ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_RANGE, "0.01,100,0.01"), _SCS("set_energy"), _SCS("get_energy"));
395 ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Add,Sub,Mix,Mask"), _SCS("set_mode"), _SCS("get_mode"));
396 ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/height", PROPERTY_HINT_RANGE, "-100,100,0.1"), _SCS("set_height"), _SCS("get_height"));
397 ADD_PROPERTY(PropertyInfo(Variant::INT, "range/z_min", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), _SCS("set_z_range_min"), _SCS("get_z_range_min"));
398 ADD_PROPERTY(PropertyInfo(Variant::INT, "range/z_max", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), _SCS("set_z_range_max"), _SCS("get_z_range_max"));
399 ADD_PROPERTY(PropertyInfo(Variant::INT, "range/layer_min", PROPERTY_HINT_RANGE, "-512,512,1"), _SCS("set_layer_range_min"), _SCS("get_layer_range_min"));
400 ADD_PROPERTY(PropertyInfo(Variant::INT, "range/layer_max", PROPERTY_HINT_RANGE, "-512,512,1"), _SCS("set_layer_range_max"), _SCS("get_layer_range_max"));
401 ADD_PROPERTY(PropertyInfo(Variant::INT, "range/item_mask", PROPERTY_HINT_ALL_FLAGS), _SCS("set_item_mask"), _SCS("get_item_mask"));
402 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow/enabled"), _SCS("set_shadow_enabled"), _SCS("is_shadow_enabled"));
403 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow/color"), _SCS("set_shadow_color"), _SCS("get_shadow_color"));
404 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow/buffer_size", PROPERTY_HINT_RANGE, "32,16384,1"), _SCS("set_shadow_buffer_size"), _SCS("get_shadow_buffer_size"));
405 ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow/esm_multiplier", PROPERTY_HINT_RANGE, "1,4096,0.1"), _SCS("set_shadow_esm_multiplier"), _SCS("get_shadow_esm_multiplier"));
406 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow/item_mask", PROPERTY_HINT_ALL_FLAGS), _SCS("set_item_shadow_mask"), _SCS("get_item_shadow_mask"));
407
408 BIND_CONSTANT(MODE_ADD);
409 BIND_CONSTANT(MODE_SUB);
410 BIND_CONSTANT(MODE_MIX);
411 BIND_CONSTANT(MODE_MASK);
412 }
413
Light2D()414 Light2D::Light2D() {
415
416 canvas_light = VisualServer::get_singleton()->canvas_light_create();
417 enabled = true;
418 editor_only = false;
419 shadow = false;
420 color = Color(1, 1, 1);
421 height = 0;
422 _scale = 1.0;
423 z_min = -1024;
424 z_max = 1024;
425 layer_min = 0;
426 layer_max = 0;
427 item_mask = 1;
428 item_shadow_mask = 1;
429 mode = MODE_ADD;
430 shadow_buffer_size = 2048;
431 shadow_esm_multiplier = 80;
432 energy = 1.0;
433 shadow_color = Color(0, 0, 0, 0);
434 }
435
~Light2D()436 Light2D::~Light2D() {
437
438 VisualServer::get_singleton()->free(canvas_light);
439 }
440