1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
/*************************************************************************/
/* sky_box.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "sky_box.h"
#include "core/io/image_loader.h"
void Sky::set_radiance_size(RadianceSize p_size) {
ERR_FAIL_INDEX(p_size, RADIANCE_SIZE_MAX);
radiance_size = p_size;
_radiance_changed();
}
Sky::RadianceSize Sky::get_radiance_size() const {
return radiance_size;
}
void Sky::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_radiance_size", "size"), &Sky::set_radiance_size);
ClassDB::bind_method(D_METHOD("get_radiance_size"), &Sky::get_radiance_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "radiance_size", PROPERTY_HINT_ENUM, "32,64,128,256,512,1024,2048"), "set_radiance_size", "get_radiance_size");
BIND_ENUM_CONSTANT(RADIANCE_SIZE_32);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_64);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_128);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_256);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_512);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_1024);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_2048);
BIND_ENUM_CONSTANT(RADIANCE_SIZE_MAX);
}
Sky::Sky() {
radiance_size = RADIANCE_SIZE_512;
}
/////////////////////////////////////////
void PanoramaSky::_radiance_changed() {
if (panorama.is_valid()) {
static const int size[RADIANCE_SIZE_MAX] = {
32, 64, 128, 256, 512, 1024, 2048
};
VS::get_singleton()->sky_set_texture(sky, panorama->get_rid(), size[get_radiance_size()]);
}
}
void PanoramaSky::set_panorama(const Ref<Texture> &p_panorama) {
panorama = p_panorama;
if (panorama.is_valid()) {
_radiance_changed();
} else {
VS::get_singleton()->sky_set_texture(sky, RID(), 0);
}
}
Ref<Texture> PanoramaSky::get_panorama() const {
return panorama;
}
RID PanoramaSky::get_rid() const {
return sky;
}
void PanoramaSky::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_panorama", "texture"), &PanoramaSky::set_panorama);
ClassDB::bind_method(D_METHOD("get_panorama"), &PanoramaSky::get_panorama);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "panorama", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_panorama", "get_panorama");
}
PanoramaSky::PanoramaSky() {
sky = VS::get_singleton()->sky_create();
}
PanoramaSky::~PanoramaSky() {
VS::get_singleton()->free(sky);
}
//////////////////////////////////
void ProceduralSky::_radiance_changed() {
if (update_queued)
return; //do nothing yet
static const int size[RADIANCE_SIZE_MAX] = {
32, 64, 128, 256, 512, 1024, 2048
};
VS::get_singleton()->sky_set_texture(sky, texture, size[get_radiance_size()]);
}
Ref<Image> ProceduralSky::_generate_sky() {
update_queued = false;
PoolVector<uint8_t> imgdata;
static const int size[TEXTURE_SIZE_MAX] = {
256, 512, 1024, 2048, 4096
};
int w = size[texture_size];
int h = w / 2;
imgdata.resize(w * h * 4); //RGBE
{
PoolVector<uint8_t>::Write dataw = imgdata.write();
uint32_t *ptr = (uint32_t *)dataw.ptr();
Color sky_top_linear = sky_top_color.to_linear();
Color sky_horizon_linear = sky_horizon_color.to_linear();
Color ground_bottom_linear = ground_bottom_color.to_linear();
Color ground_horizon_linear = ground_horizon_color.to_linear();
//Color sun_linear = sun_color.to_linear();
Vector3 sun(0, 0, -1);
sun = Basis(Vector3(1, 0, 0), Math::deg2rad(sun_latitude)).xform(sun);
sun = Basis(Vector3(0, 1, 0), Math::deg2rad(sun_longitude)).xform(sun);
sun.normalize();
for (int i = 0; i < w; i++) {
float u = float(i) / (w - 1);
float phi = u * 2.0 * Math_PI;
for (int j = 0; j < h; j++) {
float v = float(j) / (h - 1);
float theta = v * Math_PI;
Vector3 normal(
Math::sin(phi) * Math::sin(theta) * -1.0,
Math::cos(theta),
Math::cos(phi) * Math::sin(theta) * -1.0);
normal.normalize();
float v_angle = Math::acos(CLAMP(normal.y, -1.0, 1.0));
Color color;
if (normal.y < 0) {
//ground
float c = (v_angle - (Math_PI * 0.5)) / (Math_PI * 0.5);
color = ground_horizon_linear.linear_interpolate(ground_bottom_linear, Math::ease(c, ground_curve));
} else {
float c = v_angle / (Math_PI * 0.5);
color = sky_horizon_linear.linear_interpolate(sky_top_linear, Math::ease(1.0 - c, sky_curve));
float sun_angle = Math::rad2deg(Math::acos(CLAMP(sun.dot(normal), -1.0, 1.0)));
if (sun_angle < sun_angle_min) {
color = color.blend(sun_color);
} else if (sun_angle < sun_angle_max) {
float c2 = (sun_angle - sun_angle_min) / (sun_angle_max - sun_angle_min);
c2 = Math::ease(c2, sun_curve);
color = color.blend(sun_color).linear_interpolate(color, c2);
}
}
ptr[j * w + i] = color.to_rgbe9995();
}
}
}
Ref<Image> image;
image.instance();
image->create(w, h, false, Image::FORMAT_RGBE9995, imgdata);
return image;
}
void ProceduralSky::set_sky_top_color(const Color &p_sky_top) {
sky_top_color = p_sky_top;
_queue_update();
}
Color ProceduralSky::get_sky_top_color() const {
return sky_top_color;
}
void ProceduralSky::set_sky_horizon_color(const Color &p_sky_horizon) {
sky_horizon_color = p_sky_horizon;
_queue_update();
}
Color ProceduralSky::get_sky_horizon_color() const {
return sky_horizon_color;
}
void ProceduralSky::set_sky_curve(float p_curve) {
sky_curve = p_curve;
_queue_update();
}
float ProceduralSky::get_sky_curve() const {
return sky_curve;
}
void ProceduralSky::set_sky_energy(float p_energy) {
sky_energy = p_energy;
_queue_update();
}
float ProceduralSky::get_sky_energy() const {
return sky_energy;
}
void ProceduralSky::set_ground_bottom_color(const Color &p_ground_bottom) {
ground_bottom_color = p_ground_bottom;
_queue_update();
}
Color ProceduralSky::get_ground_bottom_color() const {
return ground_bottom_color;
}
void ProceduralSky::set_ground_horizon_color(const Color &p_ground_horizon) {
ground_horizon_color = p_ground_horizon;
_queue_update();
}
Color ProceduralSky::get_ground_horizon_color() const {
return ground_horizon_color;
}
void ProceduralSky::set_ground_curve(float p_curve) {
ground_curve = p_curve;
_queue_update();
}
float ProceduralSky::get_ground_curve() const {
return ground_curve;
}
void ProceduralSky::set_ground_energy(float p_energy) {
ground_energy = p_energy;
_queue_update();
}
float ProceduralSky::get_ground_energy() const {
return ground_energy;
}
void ProceduralSky::set_sun_color(const Color &p_sun) {
sun_color = p_sun;
_queue_update();
}
Color ProceduralSky::get_sun_color() const {
return sun_color;
}
void ProceduralSky::set_sun_latitude(float p_angle) {
sun_latitude = p_angle;
_queue_update();
}
float ProceduralSky::get_sun_latitude() const {
return sun_latitude;
}
void ProceduralSky::set_sun_longitude(float p_angle) {
sun_longitude = p_angle;
_queue_update();
}
float ProceduralSky::get_sun_longitude() const {
return sun_longitude;
}
void ProceduralSky::set_sun_angle_min(float p_angle) {
sun_angle_min = p_angle;
_queue_update();
}
float ProceduralSky::get_sun_angle_min() const {
return sun_angle_min;
}
void ProceduralSky::set_sun_angle_max(float p_angle) {
sun_angle_max = p_angle;
_queue_update();
}
float ProceduralSky::get_sun_angle_max() const {
return sun_angle_max;
}
void ProceduralSky::set_sun_curve(float p_curve) {
sun_curve = p_curve;
_queue_update();
}
float ProceduralSky::get_sun_curve() const {
return sun_curve;
}
void ProceduralSky::set_sun_energy(float p_energy) {
sun_energy = p_energy;
_queue_update();
}
float ProceduralSky::get_sun_energy() const {
return sun_energy;
}
void ProceduralSky::set_texture_size(TextureSize p_size) {
ERR_FAIL_INDEX(p_size, TEXTURE_SIZE_MAX);
texture_size = p_size;
_queue_update();
}
ProceduralSky::TextureSize ProceduralSky::get_texture_size() const {
return texture_size;
}
RID ProceduralSky::get_rid() const {
return sky;
}
void ProceduralSky::_update_sky() {
bool use_thread = true;
if (first_time) {
use_thread = false;
first_time = false;
}
#ifdef NO_THREADS
use_thread = false;
#endif
if (use_thread) {
if (!sky_thread) {
sky_thread = Thread::create(_thread_function, this);
regen_queued = false;
} else {
regen_queued = true;
}
} else {
Ref<Image> image = _generate_sky();
VS::get_singleton()->texture_allocate(texture, image->get_width(), image->get_height(), 0, Image::FORMAT_RGBE9995, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
VS::get_singleton()->texture_set_data(texture, image);
_radiance_changed();
}
}
void ProceduralSky::_queue_update() {
if (update_queued)
return;
update_queued = true;
call_deferred("_update_sky");
}
void ProceduralSky::_thread_done(const Ref<Image> &p_image) {
VS::get_singleton()->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, Image::FORMAT_RGBE9995, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
VS::get_singleton()->texture_set_data(texture, p_image);
_radiance_changed();
Thread::wait_to_finish(sky_thread);
memdelete(sky_thread);
sky_thread = NULL;
if (regen_queued) {
sky_thread = Thread::create(_thread_function, this);
regen_queued = false;
}
}
void ProceduralSky::_thread_function(void *p_ud) {
ProceduralSky *psky = (ProceduralSky *)p_ud;
psky->call_deferred("_thread_done", psky->_generate_sky());
}
void ProceduralSky::_bind_methods() {
ClassDB::bind_method(D_METHOD("_update_sky"), &ProceduralSky::_update_sky);
ClassDB::bind_method(D_METHOD("set_sky_top_color", "color"), &ProceduralSky::set_sky_top_color);
ClassDB::bind_method(D_METHOD("get_sky_top_color"), &ProceduralSky::get_sky_top_color);
ClassDB::bind_method(D_METHOD("set_sky_horizon_color", "color"), &ProceduralSky::set_sky_horizon_color);
ClassDB::bind_method(D_METHOD("get_sky_horizon_color"), &ProceduralSky::get_sky_horizon_color);
ClassDB::bind_method(D_METHOD("set_sky_curve", "curve"), &ProceduralSky::set_sky_curve);
ClassDB::bind_method(D_METHOD("get_sky_curve"), &ProceduralSky::get_sky_curve);
ClassDB::bind_method(D_METHOD("set_sky_energy", "energy"), &ProceduralSky::set_sky_energy);
ClassDB::bind_method(D_METHOD("get_sky_energy"), &ProceduralSky::get_sky_energy);
ClassDB::bind_method(D_METHOD("set_ground_bottom_color", "color"), &ProceduralSky::set_ground_bottom_color);
ClassDB::bind_method(D_METHOD("get_ground_bottom_color"), &ProceduralSky::get_ground_bottom_color);
ClassDB::bind_method(D_METHOD("set_ground_horizon_color", "color"), &ProceduralSky::set_ground_horizon_color);
ClassDB::bind_method(D_METHOD("get_ground_horizon_color"), &ProceduralSky::get_ground_horizon_color);
ClassDB::bind_method(D_METHOD("set_ground_curve", "curve"), &ProceduralSky::set_ground_curve);
ClassDB::bind_method(D_METHOD("get_ground_curve"), &ProceduralSky::get_ground_curve);
ClassDB::bind_method(D_METHOD("set_ground_energy", "energy"), &ProceduralSky::set_ground_energy);
ClassDB::bind_method(D_METHOD("get_ground_energy"), &ProceduralSky::get_ground_energy);
ClassDB::bind_method(D_METHOD("set_sun_color", "color"), &ProceduralSky::set_sun_color);
ClassDB::bind_method(D_METHOD("get_sun_color"), &ProceduralSky::get_sun_color);
ClassDB::bind_method(D_METHOD("set_sun_latitude", "degrees"), &ProceduralSky::set_sun_latitude);
ClassDB::bind_method(D_METHOD("get_sun_latitude"), &ProceduralSky::get_sun_latitude);
ClassDB::bind_method(D_METHOD("set_sun_longitude", "degrees"), &ProceduralSky::set_sun_longitude);
ClassDB::bind_method(D_METHOD("get_sun_longitude"), &ProceduralSky::get_sun_longitude);
ClassDB::bind_method(D_METHOD("set_sun_angle_min", "degrees"), &ProceduralSky::set_sun_angle_min);
ClassDB::bind_method(D_METHOD("get_sun_angle_min"), &ProceduralSky::get_sun_angle_min);
ClassDB::bind_method(D_METHOD("set_sun_angle_max", "degrees"), &ProceduralSky::set_sun_angle_max);
ClassDB::bind_method(D_METHOD("get_sun_angle_max"), &ProceduralSky::get_sun_angle_max);
ClassDB::bind_method(D_METHOD("set_sun_curve", "curve"), &ProceduralSky::set_sun_curve);
ClassDB::bind_method(D_METHOD("get_sun_curve"), &ProceduralSky::get_sun_curve);
ClassDB::bind_method(D_METHOD("set_sun_energy", "energy"), &ProceduralSky::set_sun_energy);
ClassDB::bind_method(D_METHOD("get_sun_energy"), &ProceduralSky::get_sun_energy);
ClassDB::bind_method(D_METHOD("set_texture_size", "size"), &ProceduralSky::set_texture_size);
ClassDB::bind_method(D_METHOD("get_texture_size"), &ProceduralSky::get_texture_size);
ClassDB::bind_method(D_METHOD("_thread_done", "image"), &ProceduralSky::_thread_done);
ADD_GROUP("Sky", "sky_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_top_color"), "set_sky_top_color", "get_sky_top_color");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_horizon_color"), "set_sky_horizon_color", "get_sky_horizon_color");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sky_curve", PROPERTY_HINT_EXP_EASING), "set_sky_curve", "get_sky_curve");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sky_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_sky_energy", "get_sky_energy");
ADD_GROUP("Ground", "ground_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_bottom_color"), "set_ground_bottom_color", "get_ground_bottom_color");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_horizon_color"), "set_ground_horizon_color", "get_ground_horizon_color");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ground_curve", PROPERTY_HINT_EXP_EASING), "set_ground_curve", "get_ground_curve");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ground_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_ground_energy", "get_ground_energy");
ADD_GROUP("Sun", "sun_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sun_color"), "set_sun_color", "get_sun_color");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_latitude", PROPERTY_HINT_RANGE, "-180,180,0.01"), "set_sun_latitude", "get_sun_latitude");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_longitude", PROPERTY_HINT_RANGE, "-180,180,0.01"), "set_sun_longitude", "get_sun_longitude");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_angle_min", PROPERTY_HINT_RANGE, "0,360,0.01"), "set_sun_angle_min", "get_sun_angle_min");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_angle_max", PROPERTY_HINT_RANGE, "0,360,0.01"), "set_sun_angle_max", "get_sun_angle_max");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_curve", PROPERTY_HINT_EXP_EASING), "set_sun_curve", "get_sun_curve");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "sun_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_sun_energy", "get_sun_energy");
ADD_GROUP("Texture", "texture_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_size", PROPERTY_HINT_ENUM, "256,512,1024,2048,4096"), "set_texture_size", "get_texture_size");
BIND_ENUM_CONSTANT(TEXTURE_SIZE_256);
BIND_ENUM_CONSTANT(TEXTURE_SIZE_512);
BIND_ENUM_CONSTANT(TEXTURE_SIZE_1024);
BIND_ENUM_CONSTANT(TEXTURE_SIZE_2048);
BIND_ENUM_CONSTANT(TEXTURE_SIZE_4096);
BIND_ENUM_CONSTANT(TEXTURE_SIZE_MAX);
}
ProceduralSky::ProceduralSky() {
sky = VS::get_singleton()->sky_create();
texture = VS::get_singleton()->texture_create();
update_queued = false;
sky_top_color = Color::hex(0xa5d6f1ff);
sky_horizon_color = Color::hex(0xd6eafaff);
sky_curve = 0.09;
sky_energy = 1;
ground_bottom_color = Color::hex(0x282f36ff);
ground_horizon_color = Color::hex(0x6c655fff);
ground_curve = 0.02;
ground_energy = 1;
sun_color = Color(1, 1, 1);
sun_latitude = 35;
sun_longitude = 0;
sun_angle_min = 1;
sun_angle_max = 100;
sun_curve = 0.05;
sun_energy = 16;
texture_size = TEXTURE_SIZE_1024;
sky_thread = NULL;
regen_queued = false;
first_time = true;
_queue_update();
}
ProceduralSky::~ProceduralSky() {
if (sky_thread) {
Thread::wait_to_finish(sky_thread);
memdelete(sky_thread);
sky_thread = NULL;
}
VS::get_singleton()->free(sky);
VS::get_singleton()->free(texture);
}
|