-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathllightmapper_base.cpp
903 lines (715 loc) · 21.5 KB
/
llightmapper_base.cpp
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#include "core/math/plane.h"
#include "core/os/os.h"
#include "editor/editor_node.h"
#include "lconvolution.h"
#include "ldilate.h"
#include "llightmapper.h"
#include "lstitcher.h"
#include "modules/denoise/denoise_wrapper.h"
#include "scene/3d/light.h"
using namespace LM;
LightMapper_Base::BakeBeginFunc LightMapper_Base::bake_begin_function = NULL;
LightMapper_Base::BakeStepFunc LightMapper_Base::bake_step_function = NULL;
LightMapper_Base::BakeEndFunc LightMapper_Base::bake_end_function = NULL;
LightMapper_Base::LightMapper_Base() {
m_iNumRays = 1;
//m_Settings_Forward_NumRays = 16;
//m_Settings_Forward_RayPower = 0.01f;
//m_Settings_Backward_NumRays = 128;
m_Settings_Backward_RayPower = 1.0f;
m_Settings_DirectionalBouncePower = 1.0f;
m_Settings_NumPrimaryRays = 32;
m_Settings_NumAmbientBounces = 0;
m_Settings_NumAmbientBounceRays = 128;
m_Settings_NumDirectionalBounces = 0;
m_Settings_AmbientBouncePower = 0.5f;
m_Settings_Smoothness = 0.5f;
m_Settings_EmissionDensity = 1.0f;
m_Settings_Glow = 1.0f;
m_Settings_AO_Range = 2.0f;
m_Settings_AO_Samples = 256;
m_Settings_AO_CutRange = 1.5f;
m_Settings_AO_ReverseBias = 0.005f;
m_Settings_Mode = LMMODE_BACKWARD;
m_Settings_BakeMode = LMBAKEMODE_LIGHTMAP;
m_Settings_Quality = LM_QUALITY_MEDIUM;
// 0 is infinite
m_Settings_MaxLightDist = 0;
m_Settings_TexWidth = 512;
m_Settings_TexHeight = 512;
m_Settings_VoxelDensity = 20;
m_Settings_SurfaceBias = 0.005f;
m_Settings_Max_Material_Size = 256;
m_Settings_Normalize = true;
m_Settings_NormalizeBias = 4.0f;
m_Settings_Light_AO_Ratio = 0.5f;
m_Settings_Gamma = 2.2f;
m_Settings_LightmapIsHDR = false;
m_Settings_AmbientIsHDR = false;
m_Settings_CombinedIsHDR = false;
m_Logic_Process_Lightmap = true;
m_Logic_Process_AO = true;
m_Logic_Reserve_AO = true;
m_Logic_Process_Probes = true;
m_Logic_Output_Final = true;
m_Settings_UVPadding = 4; // 2
m_Settings_ProbeDensity = 64;
m_Settings_ProbeSamples = 4096;
m_Settings_NoiseThreshold = 0.1f;
m_Settings_NoiseReduction = 1.0f;
m_Settings_NoiseReductionMethod = NR_ADVANCED;
m_Settings_SeamStitching = true;
m_Settings_SeamDistanceThreshold = 0.001f;
m_Settings_SeamNormalThreshold = 45.0f;
m_Settings_VisualizeSeams = false;
m_Settings_Dilate = true;
m_Settings_Sky_BlurAmount = 0.18f;
m_Settings_Sky_Size = 256;
m_Settings_Sky_Samples = 512;
m_Settings_Sky_Brightness = 1.0f;
}
void LightMapper_Base::Base_Reset() {
m_Image_L.Reset();
m_Image_L_mirror.Reset();
m_Image_AO.Reset();
m_Image_ID_p1.Reset();
m_Image_TriIDs.Reset();
m_TriIDs.clear(true);
m_Image_Barycentric.Reset();
m_Lights.clear(true);
m_QMC.Create(0);
}
void LightMapper_Base::CalculateQualityAdjustedSettings() {
// set them initially to the same
AdjustedSettings &as = m_AdjustedSettings;
as.m_NumPrimaryRays = m_Settings_NumPrimaryRays;
//as.m_Forward_NumRays = m_Settings_Forward_NumRays;
as.m_EmissionDensity = m_Settings_EmissionDensity;
as.m_Backward_NumRays = m_Settings_Backward_NumRays;
as.m_NumAmbientBounces = m_Settings_NumAmbientBounces;
as.m_NumAmbientBounceRays = m_Settings_NumAmbientBounceRays;
as.m_NumDirectionalBounces = m_Settings_NumDirectionalBounces;
as.m_AO_Samples = m_Settings_AO_Samples;
as.m_Max_Material_Size = m_Settings_Max_Material_Size;
as.m_Sky_Samples = m_Settings_Sky_Samples;
as.m_Sky_Brightness = m_Settings_Sky_Brightness * m_Settings_Sky_Brightness;
// overrides
switch (m_Settings_Quality) {
case LM_QUALITY_LOW: {
as.m_NumPrimaryRays = 1;
as.m_Forward_NumRays = 1;
as.m_Backward_NumRays = 4;
as.m_AO_Samples = 1;
as.m_Max_Material_Size = 32;
as.m_NumAmbientBounces = 0;
as.m_NumDirectionalBounces = 0;
as.m_Sky_Samples = 64;
} break;
case LM_QUALITY_MEDIUM: {
as.m_NumPrimaryRays /= 2;
as.m_AO_Samples /= 2;
as.m_Max_Material_Size /= 4;
as.m_NumAmbientBounceRays /= 2;
as.m_Sky_Samples /= 2;
} break;
default:
// high is default
break;
case LM_QUALITY_FINAL:
as.m_NumPrimaryRays *= 2;
as.m_AO_Samples *= 2;
as.m_NumAmbientBounceRays *= 2;
as.m_Sky_Samples *= 2;
break;
}
// minimums
as.m_NumPrimaryRays = MAX(as.m_NumPrimaryRays, 1);
as.m_Forward_NumRays = (as.m_NumPrimaryRays * 16) / 32;
as.m_Backward_NumRays = (as.m_NumPrimaryRays * 128) / 32;
as.m_Forward_NumRays = MAX(as.m_Forward_NumRays, 1);
as.m_NumAmbientBounceRays = MAX(as.m_NumAmbientBounceRays, 1);
as.m_AO_Samples = MAX(as.m_AO_Samples, 1);
as.m_Max_Material_Size = MAX(as.m_Max_Material_Size, 32);
as.m_EmissionDensity = MAX(as.m_EmissionDensity, 1);
}
void LightMapper_Base::FindLight(const Node *pNode) {
const Light *pLight = Object::cast_to<const Light>(pNode);
if (!pLight)
return;
// visibility or bake mode?
if (pLight->get_bake_mode() == Light::BAKE_DISABLED)
return;
// is it visible?
// if (!pLight->is_visible_in_tree())
// return;
LLight *l = m_Lights.request();
// blank
memset(l, 0, sizeof(LLight));
l->m_pLight = pLight;
// get global transform only works if glight is in the tree
Transform trans = pLight->get_global_transform();
l->pos = trans.origin;
l->dir = -trans.basis.get_axis(2); // or possibly get_axis .. z is what we want
l->dir.normalize();
trans = pLight->get_transform();
l->scale = trans.basis.get_scale();
l->energy = pLight->get_param(Light::PARAM_ENERGY);
l->indirect_energy = pLight->get_param(Light::PARAM_INDIRECT_ENERGY);
l->range = pLight->get_param(Light::PARAM_RANGE);
l->spot_angle_radians = Math::deg2rad(pLight->get_param(Light::PARAM_SPOT_ANGLE));
l->spot_dot_max = Math::cos(l->spot_angle_radians);
l->spot_dot_max = MIN(l->spot_dot_max, 0.9999f); // just to prevent divide by zero in cone of spotlight
// the spot emanation point is used for spotlight cone culling.
// if we used the dot from the pos to cull, we would miss cases where
// the sample origin is offset by scale from pos. So we push back the pos
// in order to account for the scale 'cloud' of origins.
float radius = MAX(l->scale.x, MAX(l->scale.y, l->scale.z));
l->spot_emanation_point = l->pos - (l->dir * radius);
// pre apply intensity
l->color.Set(pLight->get_color() * l->energy);
const DirectionalLight *pDLight = Object::cast_to<DirectionalLight>(pLight);
if (pDLight)
l->type = LLight::LT_DIRECTIONAL;
const SpotLight *pSLight = Object::cast_to<SpotLight>(pLight);
if (pSLight)
l->type = LLight::LT_SPOT;
const OmniLight *pOLight = Object::cast_to<OmniLight>(pLight);
if (pOLight)
l->type = LLight::LT_OMNI;
}
void LightMapper_Base::PrepareLights() {
for (int n = 0; n < m_Lights.size(); n++) {
LLight &light = m_Lights[n];
if (light.type == LLight::LT_DIRECTIONAL)
LightToPlane(light);
}
}
void LightMapper_Base::FindLights_Recursive(const Node *pNode) {
FindLight(pNode);
int nChildren = pNode->get_child_count();
for (int n = 0; n < nChildren; n++) {
Node *pChild = pNode->get_child(n);
FindLights_Recursive(pChild);
}
}
Plane LightMapper_Base::FindContainmentPlane(const Vector3 &dir, Vector3 pts[8], float &range, float padding) {
// construct a plane with one of the points
Plane pl(pts[0], dir);
float furthest_dist = 0.0f;
int furthest = 0;
// find the furthest point
for (int n = 0; n < 8; n++) {
float d = pl.distance_to(pts[n]);
if (d < furthest_dist) {
furthest_dist = d;
furthest = n;
}
}
// reconstruct the plane based on the furthest point
pl = Plane(pts[furthest], dir);
// find the range
range = 0.0f;
for (int n = 0; n < 8; n++) {
float d = pl.distance_to(pts[n]);
if (d > range) {
range = d;
}
}
// const float padding = 8.0f;
// move plane backward a bit for luck
pl.d -= padding;
// add a boost to the range
range += padding * 2.0f;
return pl;
}
void LightMapper_Base::LightToPlane(LLight &light) {
AABB bb = m_Scene.m_Tracer.GetWorldBound_expanded();
Vector3 minmax[2];
minmax[0] = bb.position;
minmax[1] = bb.position + bb.size;
if (light.dir.y == 0.0f)
return;
// find the shift in x and z caused by y offset to top of scene
float units = bb.size.y / light.dir.y;
Vector3 offset = light.dir * -units;
// add to which side of scene
if (offset.x >= 0.0f)
minmax[1].x += offset.x;
else
minmax[0].x += offset.x;
if (offset.z >= 0.0f)
minmax[1].z += offset.z;
else
minmax[0].z += offset.z;
light.dl_plane_pt = minmax[0];
light.dl_tangent = Vector3(1, 0, 0);
light.dl_bitangent = Vector3(0, 0, 1);
light.dl_tangent_range = minmax[1].x - minmax[0].x;
light.dl_bitangent_range = minmax[1].z - minmax[0].z;
print_line("plane mins : " + String(Variant(minmax[0])));
print_line("plane maxs : " + String(Variant(minmax[1])));
return;
Vector3 pts[8];
for (int n = 0; n < 8; n++) {
// which x etc. either 0 or 1 for each axis
int wx = MIN(n & 1, 1);
int wy = MIN(n & 2, 1);
int wz = MIN(n & 4, 1);
pts[n].x = minmax[wx].x;
pts[n].y = minmax[wy].y;
pts[n].z = minmax[wz].z;
}
// new .. don't use light direction as plane normal, always use
// ceiling type plane (for sky) or from below.
// This will deal with most common cases .. for side lights,
// area light is better.
Vector3 plane_normal = light.dir;
if (light.dir.y < 0.0f) {
plane_normal = Vector3(0, -1, 0);
} else {
plane_normal = Vector3(0, 1, 0);
}
const float PLANE_PUSH = 2.0f;
float main_range;
Plane pl = FindContainmentPlane(plane_normal, pts, main_range, PLANE_PUSH);
// push it back even further for safety
//pl.d -= 2.0f;
// now create a bound on this plane
// find a good tangent
Vector3 cross[3];
cross[0] = plane_normal.cross(Vector3(1, 0, 0));
cross[1] = plane_normal.cross(Vector3(0, 1, 0));
cross[2] = plane_normal.cross(Vector3(0, 0, 1));
float lx = cross[0].length();
float ly = cross[1].length();
float lz = cross[2].length();
int best_cross = 0;
if (ly > lx) {
best_cross = 1;
if (lz > ly)
best_cross = 2;
}
if (lz > lx)
best_cross = 2;
Vector3 tangent = cross[best_cross];
tangent.normalize();
Vector3 bitangent = plane_normal.cross(tangent);
bitangent.normalize();
float tangent_range;
Plane pl_tangent = FindContainmentPlane(tangent, pts, tangent_range, 0.0f);
float bitangent_range;
Plane pl_bitangent = FindContainmentPlane(bitangent, pts, bitangent_range, 0.0f);
// find point at mins of the planes
Vector3 ptPlaneMins;
bool res = pl.intersect_3(pl_tangent, pl_bitangent, &ptPlaneMins);
assert(res);
// for flat sky, adjust the point to account for the incoming light direction
// so as not to have part of the mesh in shadow
// Vector3 offset = light.dir * -PLANE_PUSH;
// ptPlaneMins.x += offset.x;
// ptPlaneMins.z += offset.z;
// we now have a point, 2 vectors (tangent and bitangent) and ranges,
// all that is needed for a random distribution!
// Vector3 dl_plane_pt;
// Vector3 dl_plane_tangent;
// Vector3 dl_plane_bitangent;
// float dl_plane_tangent_range;
// float dl_plane_bitangent_range;
light.dl_plane_pt = ptPlaneMins;
light.dl_tangent = tangent;
light.dl_bitangent = bitangent;
light.dl_tangent_range = tangent_range;
light.dl_bitangent_range = bitangent_range;
// debug output the positions
Vector3 pA = ptPlaneMins;
Vector3 pB = ptPlaneMins + (tangent * tangent_range);
Vector3 pC = ptPlaneMins + (tangent * tangent_range) + (bitangent * bitangent_range);
Vector3 pD = ptPlaneMins + (bitangent * bitangent_range);
print_line("dir light A : " + String(Variant(pA)));
print_line("dir light B : " + String(Variant(pB)));
print_line("dir light C : " + String(Variant(pC)));
print_line("dir light D : " + String(Variant(pD)));
}
void LightMapper_Base::PrepareImageMaps() {
m_Image_ID_p1.Blank();
//m_Image_ID2_p1.Blank();
// rasterize each triangle in turn
//m_Scene.RasterizeTriangleIDs(*this, m_Image_ID_p1, m_Image_ID2_p1, m_Image_Barycentric);
m_Scene.RasterizeTriangleIDs(*this, m_Image_ID_p1, m_Image_Barycentric);
/*
// go through each texel
for (int y=0; y<m_iHeight; y++)
{
for (int x=0; x<m_iWidth; x++)
{
// use the texel centre!
// find the triangle at this UV
float u = (x + 0.5f) / (float) m_iWidth;
float v = (y + 0.5f) / (float) m_iHeight;
Vector3 &bary = *m_Image_Barycentric.Get(x, y);
*m_Image_ID_p1.Get(x, y) = m_Scene.FindTriAtUV(u, v, bary.x, bary.y, bary.z);
}
}
*/
}
void LightMapper_Base::Normalize_AO() {
int nPixels = m_Image_AO.GetNumPixels();
float fmax = 0.0f;
// first find the max
for (int n = 0; n < nPixels; n++) {
float f = *m_Image_AO.Get(n);
if (f > fmax)
fmax = f;
}
if (fmax < 0.001f) {
WARN_PRINT_ONCE("LightMapper_Base::Normalize_AO : values too small to normalize");
return;
}
// multiplier to normal is 1.0f / fmax
float mult = 1.0f / fmax;
// apply bias
//mult *= m_Settings_NormalizeBias;
// apply multiplier
for (int n = 0; n < nPixels; n++) {
float &f = *m_Image_AO.Get(n);
f *= mult;
// negate AO
f = 1.0f - f;
if (f < 0.0f)
f = 0.0f;
}
}
void LightMapper_Base::StitchSeams() {
if (!m_Settings_SeamStitching)
return;
Stitcher stitcher;
// stitch seams one mesh at a time
for (int n = 0; n < m_Scene.GetNumMeshes(); n++) {
MeshInstance *mi = m_Scene.GetMesh(n);
stitcher.StitchObjectSeams(*mi, m_Image_L, m_Settings_SeamDistanceThreshold, m_Settings_SeamNormalThreshold, m_Settings_VisualizeSeams);
}
}
void LightMapper_Base::ApplyNoiseReduction() {
switch (m_Settings_NoiseReductionMethod) {
case NR_DISABLE: {
} break;
case NR_SIMPLE: {
// simple
Convolution<FColor> conv;
conv.Run(m_Image_L, m_Settings_NoiseThreshold, m_Settings_NoiseReduction);
// Convolution<float> conv_ao;
// conv_ao.Run(m_Image_AO, m_Settings_NoiseThreshold, m_Settings_NoiseReduction);
} break;
case NR_ADVANCED: {
// use open image denoise
void *device = oidn_denoiser_init();
if (!oidn_denoise(device, (float *)m_Image_L.Get(0), m_Image_L.GetWidth(), m_Image_L.GetHeight())) {
WARN_PRINT("open image denoise error");
}
oidn_denoiser_finish(device);
} break;
}
}
void LightMapper_Base::Normalize() {
if (!m_Settings_Normalize)
return;
int nPixels = m_Image_L.GetNumPixels();
float fmax = 0.0f;
// first find the max
for (int n = 0; n < nPixels; n++) {
float f = m_Image_L.Get(n)->Max();
if (f > fmax)
fmax = f;
}
if (fmax < 0.001f) {
WARN_PRINT_ONCE("LightMapper_Base::Normalize : values too small to normalize");
return;
}
// multiplier to normal is 1.0f / fmax
float mult = 1.0f / fmax;
// apply bias
mult *= m_Settings_NormalizeBias;
// apply multiplier
for (int n = 0; n < nPixels; n++) {
FColor &col = *m_Image_L.Get(n);
col = col * mult;
}
}
bool LightMapper_Base::LoadLightmap(Image &image) {
// assert (image.get_width() == m_iWidth);
// assert (image.get_height() == m_iHeight);
Error res = image.load(m_Settings_LightmapFilename);
if (res != OK) {
ShowWarning("Loading lights EXR failed.\n\n" + m_Settings_LightmapFilename);
return false;
}
if ((image.get_width() != m_iWidth) || (image.get_height() != m_iHeight)) {
ShowWarning("Loaded Lights texture file dimensions do not match project, ignoring.");
return false;
}
image.lock();
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
m_Image_L.GetItem(x, y).Set(image.get_pixel(x, y));
}
}
image.unlock();
return true;
}
bool LightMapper_Base::LoadAO(Image &image) {
// assert (image.get_width() == m_iWidth);
// assert (image.get_height() == m_iHeight);
Error res = image.load(m_Settings_AmbientFilename);
if (res != OK) {
ShowWarning("Loading AO EXR failed.\n\n" + m_Settings_AmbientFilename);
return false;
}
if ((image.get_width() != m_iWidth) || (image.get_height() != m_iHeight)) {
ShowWarning("Loaded AO texture file dimensions do not match project, ignoring.");
return false;
}
image.lock();
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
m_Image_AO.GetItem(x, y) = image.get_pixel(x, y).r;
}
}
image.unlock();
return true;
}
void LightMapper_Base::Merge_AndWriteOutputImage_Combined(Image &image) {
// normalize lightmap on combine
Normalize();
// merge them both before applying noise reduction and seams
float gamma = 1.0f / m_Settings_Gamma;
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
float ao = 0.0f;
if (m_Image_AO.GetNumPixels())
ao = m_Image_AO.GetItem(x, y);
FColor lum = m_Image_L.GetItem(x, y);
// combined
FColor f;
switch (m_Settings_BakeMode) {
case LMBAKEMODE_LIGHTMAP: {
f = lum;
} break;
case LMBAKEMODE_AO: {
f.Set(ao);
} break;
default: {
FColor mid = lum * ao;
if (m_Settings_Light_AO_Ratio < 0.5f) {
float r = m_Settings_Light_AO_Ratio / 0.5f;
f.Set((1.0f - r) * ao);
f += mid * r;
} else {
float r = (m_Settings_Light_AO_Ratio - 0.5f) / 0.5f;
f = mid * (1.0f - r);
f += lum * r;
}
} break;
}
// gamma correction
if (!m_Settings_CombinedIsHDR) {
f.r = powf(f.r, gamma);
f.g = powf(f.g, gamma);
f.b = powf(f.b, gamma);
}
/*
Color col;
col = Color(f.r, f.g, f.b, 1);
// new... RGBM .. use a multiplier in the alpha to get increased dynamic range!
//ColorToRGBM(col);
image.set_pixel(x, y, col);
*/
// write back to L
*m_Image_L.Get(x, y) = f;
}
}
ApplyNoiseReduction();
StitchSeams();
// assuming both lightmap and AO are already dilated
// final version
image.lock();
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
FColor f = m_Image_L.GetItem(x, y);
Color col;
col = Color(f.r, f.g, f.b, 1);
// new... RGBM .. use a multiplier in the alpha to get increased dynamic range!
//ColorToRGBM(col);
image.set_pixel(x, y, col);
}
}
/*
float gamma = 1.0f / m_Settings_Gamma;
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
float ao = 0.0f;
if (m_Image_AO.GetNumPixels())
ao = m_Image_AO.GetItem(x, y);
FColor lum = m_Image_L.GetItem(x, y);
// combined
FColor f;
switch (m_Settings_BakeMode) {
case LMBAKEMODE_LIGHTMAP: {
f = lum;
} break;
case LMBAKEMODE_AO: {
f.Set(ao);
} break;
default: {
FColor mid = lum * ao;
if (m_Settings_Light_AO_Ratio < 0.5f) {
float r = m_Settings_Light_AO_Ratio / 0.5f;
f.Set((1.0f - r) * ao);
f += mid * r;
} else {
float r = (m_Settings_Light_AO_Ratio - 0.5f) / 0.5f;
f = mid * (1.0f - r);
f += lum * r;
}
} break;
}
// gamma correction
if (!m_Settings_CombinedIsHDR) {
f.r = powf(f.r, gamma);
f.g = powf(f.g, gamma);
f.b = powf(f.b, gamma);
}
Color col;
col = Color(f.r, f.g, f.b, 1);
// new... RGBM .. use a multiplier in the alpha to get increased dynamic range!
//ColorToRGBM(col);
image.set_pixel(x, y, col);
}
}
*/
image.unlock();
}
void LightMapper_Base::WriteOutputImage_AO(Image &image) {
if (!m_Image_AO.GetNumPixels())
return;
if (m_Settings_Dilate) {
Dilate<float> dilate;
dilate.DilateImage(m_Image_AO, m_Image_ID_p1, 256);
}
// final version
image.lock();
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
const float *pf = m_Image_AO.Get(x, y);
assert(pf);
float f = *pf;
// gamma correction
if (!m_Settings_AmbientIsHDR) {
float gamma = 1.0f / 2.2f;
f = powf(f, gamma);
}
Color col;
col = Color(f, f, f, 1);
// debug mark the dilated pixels
//#define MARK_AO_DILATED
#ifdef MARK_AO_DILATED
if (!m_Image_ID_p1.GetItem(x, y)) {
col = Color(1.0f, 0.33f, 0.66f, 1);
}
#endif
image.set_pixel(x, y, col);
}
}
image.unlock();
}
void LightMapper_Base::ShowWarning(String sz, bool bAlert) {
#ifdef TOOLS_ENABLED
EditorNode::get_singleton()->show_warning(TTR(sz));
WARN_PRINT(sz);
// if (bAlert)
// OS::get_singleton()->alert(sz, "WARNING");
#else
WARN_PRINT(sz);
#endif
}
void LightMapper_Base::WriteOutputImage_Lightmap(Image &image) {
if (m_Settings_Dilate) {
Dilate<FColor> dilate;
dilate.DilateImage(m_Image_L, m_Image_ID_p1, 256);
}
////
// write some debug
//#define LLIGHTMAPPER_OUTPUT_TRIIDS
#ifdef LLIGHTMAPPER_OUTPUT_TRIIDS
output_image.lock();
Color cols[1024];
for (int n = 0; n < m_Scene.GetNumTris(); n++) {
if (n == 1024)
break;
cols[n] = Color(Math::randf(), Math::randf(), Math::randf(), 1.0f);
}
cols[0] = Color(0, 0, 0, 1.0f);
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
int coln = m_Image_ID_p1.GetItem(x, y) % 1024;
output_image.set_pixel(x, y, cols[coln]);
}
}
output_image.unlock();
output_image.save_png("tri_ids.png");
#endif
// final version
image.lock();
for (int y = 0; y < m_iHeight; y++) {
for (int x = 0; x < m_iWidth; x++) {
FColor f = *m_Image_L.Get(x, y);
// gamma correction
if (!m_Settings_LightmapIsHDR) {
float gamma = 1.0f / 2.2f;
f.r = powf(f.r, gamma);
f.g = powf(f.g, gamma);
f.b = powf(f.b, gamma);
}
Color col;
col = Color(f.r, f.g, f.b, 1);
// debug mark the dilated pixels
//#define MARK_DILATED
#ifdef MARK_DILATED
if (!m_Image_ID_p1.GetItem(x, y)) {
col = Color(1.0f, 0.33f, 0.66f, 1);
}
#endif
// if (m_Image_ID_p1.GetItem(x, y))
// {
// output_image.set_pixel(x, y, Color(f, f, f, 255));
// }
// else
// {
// output_image.set_pixel(x, y, Color(0, 0, 0, 255));
// }
// visual cuts
// if (m_Image_Cuts.GetItem(x, y).num)
// {
// col = Color(1.0f, 0.33f, 0.66f, 1);
// }
// else
// {
// col = Color(0, 0, 0, 1);
// }
// visualize concave
// const MiniList_Cuts &cuts = m_Image_Cuts.GetItem(x, y);
// if (cuts.num == 2)
// {
// if (cuts.convex)
// {
// col = Color(1.0f, 0, 0, 1);
// }
// else
// {
// col = Color(0, 0, 1, 1);
// }
// }
image.set_pixel(x, y, col);
}
}
image.unlock();
}