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
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// Copyright 2023 NVIDIA Corporation. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.

mdl 1.6;

import ::anno::*;
import ::base::*;
import ::math::*;
import ::state::*;
import ::tex::*;

export using ::base import texture_return;

// Returns the normal n in tangent space, given n is in internal space.
float3 transform_internal_to_tangent(float3 n)
{
    return
        n.x * float3(state::texture_tangent_u(0).x, state::texture_tangent_v(0).x, state::normal().x) +
        n.y * float3(state::texture_tangent_u(0).y, state::texture_tangent_v(0).y, state::normal().y) +
        n.z * float3(state::texture_tangent_u(0).z, state::texture_tangent_v(0).z, state::normal().z);
}

// 3dsmax Bitmap Transformation Matrix
uniform float4x4 max_rotation_translation_scale
(
    uniform float3 rotation = float3(0.)
        [[ anno::description("Rotation applied to every UVW coordinate") ]],
    uniform float3 translation = float3(0.)
        [[ anno::description("Offset applied to every UVW coordinate") ]],
    uniform float3 scaling = float3(1.)
        [[ anno::description("Scale applied to every UVW coordinate") ]],
    uniform bool u_mirror = false
        [[ anno::description("Mirror repeat has twice the number of textures in the same space") ]],
    uniform bool v_mirror = false
        [[ anno::description("Mirror repeat has twice the number of textures in the same space") ]]
)
[[
        anno::description("Construct transformation matrix from Euler rotation, translation and scale")
]]
{
    float4x4 scale =
        float4x4(u_mirror?scaling.x*2.:scaling.x         , 0.         , 0. , 0.,
                 0.                , v_mirror?scaling.y*2.:scaling.y  , 0. , 0.,
                 0.                , 0.                , scaling.z         , 0.,
                 u_mirror?1.:.5    , v_mirror?1.:.5    , .5                , 1.);

    float4x4 translate =
        float4x4(1.                , 0.                , 0.                , 0.,
                 0.                , 1.                , 0.                , 0.,
                 0.                , 0.                , 1.                , 0.,
                 -0.5-translation.x, -0.5-translation.y, -0.5-translation.z, 1.);


    // Euler rotation matrix  xyz order
    float3 s = math::sin(rotation);
    float3 c = math::cos(rotation);
    float4x4 rotate =
        float4x4(  c.y*c.z               , c.y*s.z               ,  -s.y   , 0.0,
                   -c.x*s.z + s.x*s.y*c.z, c.x*c.z + s.x*s.y*s.z , s.x*c.y , 0.0,
                   s.x*s.z + c.x*s.y*c.z , -s.x*c.z + c.x*s.y*s.z, c.x*c.y , 0.0,
                   0.                    , 0.                    , 0.      , 1.);
    return    scale *rotate*translate;
}

export enum vray_color_mode
[[
    anno::hidden()
]]
{
    mode_color,
    mode_temperature
};

export enum vray_color_primaries
[[
    anno::hidden()
]]
{
    primaries_none,
    primaries_sRGB,
    primaries_ACEScg
};

export enum vray_bitmap_primaries
[[
    anno::hidden()
]]
{
    bm_primaries_default,
    bm_primaries_sRGB,
    bm_primaries_ACEScg,
    bm_primaries_raw
};

export enum vray_rgb_output
[[
    anno::hidden()
]]
{
    rgb_output_rgb_color,
    rgb_output_alpha_as_grey,
    rgb_output_intensity_as_grey
};

export enum vray_mono_output
[[
    anno::hidden()
]]
{
    mono_output_rgb_intensity,
    mono_output_alpha
};

export enum vray_alpha_source
[[
    anno::hidden()
]]
{
    alpha_source_image_alpha,
    alpha_source_intensity,
    alpha_source_opaque
};

// VRayBitmap map
export texture_return VRayBitmap
(
    // UVW Coordinates
    uniform int mapChannel=1
    [[
        anno::in_group("Coordinates"),
        anno::display_name("Map channel")
    ]],
    uniform float U_Offset = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Offset")
    ]],
    uniform float V_Offset = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Offset")
    ]],
    uniform float U_Tiling = 1.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Tiling")
    ]],
    uniform float V_Tiling = 1.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Tiling")
    ]],
    uniform bool U_Tile=true //it is actually an error if both mirror and tile are true
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Tile")
    ]],
    uniform bool U_Mirror=false
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Mirror")
    ]],
    uniform bool V_Tile=true
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Tile")
    ]],
    uniform bool V_Mirror=false
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Mirror")
    ]],
    uniform float U_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Angle")
    ]],
    uniform float V_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Angle")
    ]],
    uniform float W_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("W Angle")
    ]],
    uniform int UVW_Type = 0 //enum?
    [[
        anno::in_group("Coordinates"),
        anno::display_name("UVW Type"),
        anno::unused(),
        anno::hidden()
    ]],

    // Bitmap
    uniform texture_2d filename = texture_2d()
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Bitmap")
    ]],
    uniform float multiplier = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Overall multiplier")
    ]],
    uniform vray_rgb_output rgb_output = rgb_output_rgb_color
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("RGB output")
    ]],
    uniform vray_mono_output mono_output = mono_output_rgb_intensity
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Mono output")
    ]],
    uniform vray_alpha_source alpha_source = alpha_source_image_alpha
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Alpha source")
    ]],
    uniform vray_bitmap_primaries rgb_primaries = bm_primaries_raw
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("RGB primaries"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform bool crop_enable = false
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Crop")
    ]],
    uniform float crop_u = 0.f //u+w and v+h are always <=1.0
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("U Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_w = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("W Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_v = 0.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("V Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_h = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("H Crop"),
        anno::hard_range(0.f, 1.f)
    ]],

    // Output
    uniform float bump_amount = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("Bump amount"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform bool clamp = false
    [[
        anno::in_group("Output"),
        anno::display_name("Clamp"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform bool invert = false
    [[
        anno::in_group("Output"),
        anno::display_name("Invert")
    ]],
    uniform bool alphaFromRGB = false
    [[
        anno::in_group("Output"),
        anno::display_name("Alpha from RGB intensity"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform float output_amount = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("Output amount")
    ]],
    uniform float rgb_level = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("RGB level")
    ]],
    uniform float rgb_offset = 0.f
    [[
        anno::in_group("Output"),
        anno::display_name("RGB Offset")
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    uniform float clip_u = crop_enable ? crop_u : 0.f;
    uniform float clip_w = crop_enable ? crop_w : 1.f;
    uniform float clip_v = crop_enable ? crop_v : 0.f;
    uniform float clip_h = crop_enable ? crop_h : 1.f;

    texture_return bitmap_sample = base::file_texture
    (
        texture     : filename,
        mono_source : (alpha_source == alpha_source_image_alpha) ? base::mono_alpha : base::mono_average,
        crop_u      : float2(clip_u, clip_w + clip_u),
        crop_v      : float2(1.f - clip_v - clip_h, 1.f - clip_v),
        uvw         : base::transform_coordinate
        (
            transform   : max_rotation_translation_scale
            (
                scaling     : float3(U_Tiling, V_Tiling, 1.f),
                rotation    : float3(U_angle, V_angle, W_angle)/180.f*math::PI ,
                translation : float3(U_Offset, V_Offset, 0.f), u_mirror : U_Mirror, v_mirror : V_Mirror
            ),
            coordinate  : base::coordinate_source(texture_space: mapChannel-1)
        ),
        wrap_u      : U_Tile ? tex::wrap_repeat : U_Mirror ? tex::wrap_mirrored_repeat : tex::wrap_clip,
        wrap_v      : V_Tile ? tex::wrap_repeat : V_Mirror ? tex::wrap_mirrored_repeat : tex::wrap_clip
    );

    bitmap_sample.tint = invert ?
        bitmap_sample.tint * (-rgb_level * output_amount * multiplier ) + (1.f - rgb_offset) :
        bitmap_sample.tint * (rgb_level * output_amount * multiplier) + rgb_offset;

    float alpha = (alpha_source != alpha_source_opaque) ? bitmap_sample.mono : 1.f;
    bitmap_sample.tint = bitmap_sample.tint*alpha;

    // determinate mono output
    if(mono_output == mono_output_alpha)
        bitmap_sample.mono = alpha;
    else
        bitmap_sample.mono = math::average(bitmap_sample.tint);

    // determinate rgb output
    if(rgb_output == rgb_output_alpha_as_grey)
        bitmap_sample.tint = color(alpha);
    else if(rgb_output == rgb_output_intensity_as_grey)
        bitmap_sample.tint = color(math::average(bitmap_sample.tint));

    return bitmap_sample;
}
// VRayBitmap Map for Bump
export float3 VRayBitmap_bump
(
    // UVW Coordinates
    uniform int mapChannel=1
    [[
        anno::in_group("Coordinates"),
        anno::display_name("Map channel")
    ]],
    uniform bool U_Tile=true
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Tile")
    ]],
    uniform bool U_Mirror=false
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Mirror")
    ]],
    uniform bool V_Tile=true
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Tile")
    ]],
    uniform bool V_Mirror=false
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Mirror")
    ]],
    uniform float U_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Angle")
    ]],
    uniform float V_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Angle")
    ]],
    uniform float W_angle = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("W Angle")
    ]],
    uniform float U_Offset = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Offset")
    ]],
    uniform float V_Offset = 0.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Offset")
    ]],
    uniform float U_Tiling = 1.f
    [[
        anno::in_group("Coordinates"),
        anno::display_name("U Tiling")
    ]],
    uniform float V_Tiling = 1.
    [[
        anno::in_group("Coordinates"),
        anno::display_name("V Tiling")
    ]],
    uniform int UVW_Type = 0 //enum?
    [[
        anno::in_group("Coordinates"),
        anno::display_name("UVW Type"),
        anno::unused()
    ]],

    // Bitmap
    uniform texture_2d filename = texture_2d()
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Bitmap")
    ]],
    uniform bool crop_enable = false
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Crop")
    ]],
    uniform float crop_u = 0.f //u+w and v+h are always <=1.0
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("U Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_w = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("W Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_v = 0.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("V Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float crop_h = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("H Crop"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float factor = 1.f
    [[
        anno::in_group("Bitmap parameters"),
        anno::display_name("Bump amount")
    ]],

    // Output
    uniform float bump_amount = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("Bump output")
    ]],
    uniform bool clamp = false
    [[
        anno::in_group("Output"),
        anno::display_name("Clamp"),
        anno::unused()
    ]],
    uniform bool invert = false
    [[
        anno::in_group("Output"),
        anno::display_name("Invert")
    ]],
    uniform bool alphaFromRGB = false
    [[
        anno::in_group("Output"),
        anno::display_name("Alpha from RGB intensity"),
        anno::unused()
    ]],
    uniform float output_amount = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("Output amount"),
        anno::unused()
    ]],
    uniform float rgb_level = 1.f
    [[
        anno::in_group("Output"),
        anno::display_name("RGB level"),
        anno::unused()
    ]],
    uniform float rgb_offset = 0.f
    [[
        anno::in_group("Output"),
        anno::display_name("RGB Offset"),
        anno::unused()
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    uniform float clip_u = crop_enable ? crop_u : 0.f;
    uniform float clip_w = crop_enable ? crop_w : 1.f;
    uniform float clip_v = crop_enable ? crop_v : 0.f;
    uniform float clip_h = crop_enable ? crop_h : 1.f;

    return base::file_bump_texture
    (
        texture : filename,
        crop_u  : float2(clip_u, clip_w + clip_u),
        crop_v  : float2(1.f - clip_v - clip_h, 1.f - clip_v),
        uvw     : base::transform_coordinate
        (
            transform   : max_rotation_translation_scale
            (
                scaling     : float3(U_Tiling, V_Tiling, 1.f),
                rotation    : float3(U_angle, V_angle,W_angle)/(180.*math::PI),
                translation : float3(U_Offset, V_Offset, 0.f), u_mirror : U_Mirror, v_mirror : V_Mirror
            ),
            coordinate  : base::coordinate_source(texture_space: mapChannel - 1)
        ),
        wrap_u  : U_Tile ? tex::wrap_repeat : U_Mirror ? tex::wrap_mirrored_repeat : tex::wrap_clip,
        wrap_v  : V_Tile ? tex::wrap_repeat : V_Mirror ? tex::wrap_mirrored_repeat : tex::wrap_clip,
        factor  : invert ? -10.f*factor*bump_amount : 10.f*factor*bump_amount
    );
}

// VRayBump2Normal Map for Bump
export enum vray_bump2normal_mode
[[
    anno::hidden()
]]
{
    b2n_tangent_space,
    b2n_local_XYZ,
    b2n_screen_space,
    b2n_world_space
};

export texture_return VRayBump2Normal
(
    float3 bump_map = state::normal()
    [[
        anno::in_group("Parameters"),
        anno::display_name("Bump map")
    ]],
    uniform float bump_map_mult = 100.f
    [[
        anno::in_group("Parameters"),
        anno::display_name("Bump map multiplier"),
        anno::hard_range(0.f, 100.f)
    ]],
    uniform vray_bump2normal_mode mode = b2n_tangent_space
    [[
        anno::in_group("Parameters"),
        anno::display_name("Mode"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform int map_channel=1
    [[
        anno::in_group("Coordinates"),
        anno::display_name("Map channel"),
        anno::unused(),
        anno::hidden()
    ]]    
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    texture_return normal_tex;
    
    float3 normal_tangent_space = 
        transform_internal_to_tangent(math::normalize(state::normal() + (bump_map - state::normal())*(bump_map_mult*0.01f)));

    normal_tex.tint = color( (normal_tangent_space + float3(1.f))*0.5f );
    normal_tex.mono = 1.f;
    return normal_tex;
}

// VRayColor Map
export texture_return VRayColor
(
    uniform vray_color_mode color_mode = mode_color
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Color mode")
    ]],
    uniform float temperature = 6500.f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Temperature"),
        anno::hard_range(350.f, 1000000000.f)
    ]],
    uniform float red = 0.5f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Red"),
        anno::soft_range(0.f, 1.f)
    ]],
    uniform float green = 0.5f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Green"),
        anno::soft_range(0.f, 1.f)
    ]],
    uniform float blue = 0.5f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Blue"),
        anno::soft_range(0.f, 1.f)
    ]],
    uniform float rgb_multiplier = 1.f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("RGB Multiplier")
    ]],
    uniform float alpha = 1.f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Alpha"),
        anno::hard_range(0.f, 1.f)
    ]],
    uniform float color_gamma = 1.0f
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("Color gamma"),
        anno::hard_range(0.05f, 20.f)
    ]],
    uniform vray_color_primaries rgb_primaries = primaries_none
    [[
        anno::in_group("VRayColor Parameters"),
        anno::display_name("RGB primaries"),
        anno::unused(),
        anno::hidden()
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    float3 rgb(red, green, blue);

    // using temperature?. Then convert from Kelvin to RGB
    if(color_mode == mode_temperature)
    {
        float k = temperature*0.01f;

        // red
        if (temperature < 6600.f)
        {
            rgb.x = 1.f;
        }
        else
        {
            float r = k - 55.f;
            r = (351.9769f + 0.1142f * r - 40.2537f * math::log(r))/255.f;
            rgb.x = math::clamp(r, 0.f, 1.f);
        }

        // green
        if (temperature < 6600.f)
        {
            float g = k - 2.f;
            g = (-155.2549f - 0.4460f * g + 104.4922f * math::log(g))/255.f;
            rgb.y = math::clamp(g, 0.f, 1.f);

        }
        else
        {
            float g = k - 50.f;
            g = (325.4494f + 0.0794f * g - 28.0853f * math::log(g))/255.f;
            rgb.y = math::clamp(g, 0.f, 1.f);
        }

        // blue
        if (temperature >= 6600.f)
        {
            rgb.z = 1.f;
        }
        else if (temperature <= 2000.f)
        {
            rgb.z = 0.f;
        }
        else
        {
            float b = k - 10.f;
            b = (-254.7694f + 0.8274f * blue + 115.6799f * math::log(b))/255.f;
            rgb.z = math::clamp(b, 0.f, 1.f);
        }
    }

    // apply gamma
    if(color_gamma != 1.f)
        rgb = math::pow(rgb, 1.f/color_gamma);

    // apply multiplier
    rgb *= rgb_multiplier;

    // fix ranges and return
    rgb = math::clamp(rgb, 0.f, 1.f);
    return texture_return(color(rgb), alpha);
}

// VRayColor Map for Bump
export float3 VRayColor_bump
(
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    return state::normal();
}

// VRayColor2Bump Map for Bump
export float3 VRayColor2Bump_bump
(
    float3 map = state::normal()
    [[
        anno::in_group("Parameters"),
        anno::display_name("Bump map")
    ]],
    uniform float height = 2.0f
    [[
        anno::in_group("Parameters"),
        anno::display_name("Height"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform float bump_delta_scale = 1.0f
    [[
        anno::in_group("Parameters"),
        anno::display_name("Bump delta scale"),
        anno::unused(),
        anno::hidden(),
        anno::hard_range(0.f, 1.f)
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    return map;
}

// VRay Dirt Map
export texture_return VRayDirt
(
    float radius = 10.f
    [[
        anno::in_group("VRayDirt Parameters"),
        anno::display_name("Radius"),
        anno::unused(),
        anno::hidden()
    ]],
    color occluded_color = color(0.f)
    [[
        anno::in_group("VRayDirt Parameters"),
        anno::display_name("Occluded color"),
        anno::unused(),
        anno::hidden()
    ]],
    color unoccluded_color = color(1.f)
    [[
        anno::in_group("VRayDirt Parameters"),
        anno::display_name("Unoccluded color")
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    return texture_return(unoccluded_color, 1.f);
}

// VRay Edges Tex Map
export texture_return VRayEdgesTex
(
    color edges_color = color(1.f)
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Color")
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    return texture_return(edges_color, 1.f);
}

// VRay Edges Tex Map for bump
export float3 VRayEdgesTex_bump
(
    color edges_color = color(1.f)
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Color"),
        anno::unused(),
        anno::hidden()
    ]],
    uniform float radius = 0.1f
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Radius"),
        anno::hard_range(0.f, 100.f)

    ]],
    uniform bool same_object_only = true
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Consider same object only")
    ]],
    uniform int corners_mode = 0
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Corners"),
        anno::unused(),
        anno::hidden()
    ]],
    float multiplier = 1.f
    [[
        anno::in_group("VRayEdgesTex params"),
        anno::display_name("Multiplier"),
        anno::unused(),
        anno::hidden()
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    return state::rounded_corner_normal(radius*state::meters_per_scene_unit(), !same_object_only);
}

// VRay Normap Map for bump
export float3 VRayNormalMap_bump
(
    texture_return normal_map = texture_return()
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Normal map")
    ]],
    float3 bump_map = state::normal()
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Bump map")
    ]],
    uniform float normal_amount = 1.f
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Normal amount"),
        anno::hard_range(-1000.f, 1000.f)
    ]],
    uniform float bump_amount = 1.f
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Bump amount"),
        anno::hard_range(-1000.f, 1000.f)
    ]],
    uniform bool flip_red = false
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Flip red")
    ]],
    uniform bool flip_green = false
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Flip green")
    ]],
    uniform bool swap_rg = false
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Swap red and green")
    ]],
    uniform int map_channel = 1
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Map channel"),
        anno::hidden(),
        anno::unused()
    ]],
    uniform float map_rotation = 0.f
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Map rotation"),
        anno::hard_range(0.f, 360.f),
        anno::hidden(),
        anno::unused()
    ]],
    uniform float normal_map_amount = 1.0f
    [[
        anno::in_group("VRayNormalMap Parameters"),
        anno::display_name("Map amount"),
        anno::hard_range(-10.f, 10.f)
    ]]
)
[[
    anno::author("NVIDIA Corporation")
]]
{
    float3 tangent_space_normal = float3(normal_map.tint);

    if(flip_red)
        tangent_space_normal.x = 1.f - tangent_space_normal.x;

    if(flip_green)
        tangent_space_normal.y = 1.f - tangent_space_normal.y;

    if(swap_rg)
    {
        float tmp = tangent_space_normal.x;
        tangent_space_normal.x = tangent_space_normal.y;
        tangent_space_normal.y = tmp;
    }

    float normal_factor = normal_amount*(normal_map_amount);
    tangent_space_normal = (tangent_space_normal - float3(0.5f))*(2.f*normal_factor);

    float3 mapped_normal = math::normalize(
        state::texture_tangent_u(0) * tangent_space_normal.x +
        state::texture_tangent_v(0) * tangent_space_normal.y +
        state::normal() * (tangent_space_normal.z + (1.f - normal_factor)));

    float3 bumped_normal = math::normalize(math::lerp(state::normal(), bump_map, bump_amount*normal_map_amount));

    return math::normalize(mapped_normal + bumped_normal);
}