summaryrefslogtreecommitdiff
path: root/drivers/gles2/shaders/material.glsl
blob: f2d9eaf1e5e921a1b15488250bd0e2286a9b1457 (plain)
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
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
[vertex]


#ifdef USE_GLES_OVER_GL
#define mediump
#define highp
#define roundfix( m_val ) floor( (m_val) + 0.5 )
#else
precision mediump float;
precision mediump int;
#endif



/*
from VisualServer:

ARRAY_VERTEX=0,
ARRAY_NORMAL=1,
ARRAY_TANGENT=2,
ARRAY_COLOR=3,
ARRAY_TEX_UV=4,
ARRAY_TEX_UV2=5,
ARRAY_BONES=6,
ARRAY_WEIGHTS=7,
ARRAY_INDEX=8,
*/

//hack to use uv if no uv present so it works with lightmap
#ifdef ENABLE_AMBIENT_LIGHTMAP

#ifdef USE_LIGHTMAP_ON_UV2

#ifndef ENABLE_UV2_INTERP
#define ENABLE_UV2_INTERP
#endif

#else

#ifndef ENABLE_UV_INTERP
#define ENABLE_UV_INTERP
#endif

#endif

#endif


/* INPUT ATTRIBS */

attribute highp vec4 vertex_attrib; // attrib:0
attribute vec3 normal_attrib; // attrib:1
attribute vec4 tangent_attrib; // attrib:2
attribute vec4 color_attrib; // attrib:3
attribute vec2 uv_attrib; // attrib:4
attribute vec2 uv2_attrib; // attrib:5

uniform float normal_mult;

#ifdef USE_SKELETON
attribute vec4 bone_indices; // attrib:6
attribute vec4 bone_weights; // attrib:7
uniform highp sampler2D skeleton_matrices;
uniform highp float skeltex_pixel_size;
#endif

#ifdef USE_ATTRIBUTE_INSTANCING

attribute highp vec4 instance_row0; // attrib:8
attribute highp vec4 instance_row1; // attrib:9
attribute highp vec4 instance_row2; // attrib:10
attribute highp vec4 instance_row3; // attrib:11

#endif

#ifdef USE_TEXTURE_INSTANCING

attribute highp vec3 instance_uv; // attrib:6
uniform highp sampler2D instance_matrices;

#endif

uniform highp mat4 world_transform;
uniform highp mat4 camera_inverse_transform;
uniform highp mat4 projection_transform;

#ifdef USE_UNIFORM_INSTANCING
//shittiest form of instancing (but most compatible)
uniform highp mat4 instance_transform;
#endif

/* Varyings */

varying vec3 vertex_interp;
varying vec3 normal_interp;

#if defined(ENABLE_COLOR_INTERP)
varying vec4 color_interp;
#endif

#if defined(ENABLE_UV_INTERP)
varying vec2 uv_interp;
#endif

#if defined(ENABLE_UV2_INTERP)
varying vec2 uv2_interp;
#endif

#if defined(ENABLE_VAR1_INTERP)
varying vec4 var1_interp;
#endif

#if defined(ENABLE_VAR2_INTERP)
varying vec4 var2_interp;
#endif

#if defined(ENABLE_TANGENT_INTERP)
varying vec3 tangent_interp;
varying vec3 binormal_interp;
#endif

#ifdef ENABLE_AMBIENT_OCTREE

uniform highp mat4 ambient_octree_inverse_transform;
varying highp vec3 ambient_octree_coords;

#endif

#ifdef USE_FOG

varying vec4 fog_interp;
uniform highp vec3 fog_params;
uniform vec3 fog_color_begin;
uniform vec3 fog_color_end;

#endif

#ifdef USE_VERTEX_LIGHTING

uniform vec3 light_pos;
uniform vec3 light_direction;
uniform vec3 light_attenuation;
uniform vec3 light_spot_attenuation;
uniform vec3 light_diffuse;
uniform vec3 light_specular;



#endif

varying vec4 diffuse_interp;
varying vec3 specular_interp;
//intended for static branching
//pretty much all meaningful platforms support
//static branching

uniform float time;
uniform float instance_id;

uniform vec3 ambient_light;

#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)

varying vec4 position_interp;

#endif

#ifdef LIGHT_USE_SHADOW

uniform highp mat4 shadow_matrix;
varying highp vec4 shadow_coord;
#ifdef LIGHT_USE_PSSM
uniform highp mat4 shadow_matrix2;
varying highp vec4 shadow_coord2;
#endif
#ifdef LIGHT_USE_PSSM4
uniform highp mat4 shadow_matrix3;
varying highp vec4 shadow_coord3;
uniform highp mat4 shadow_matrix4;
varying highp vec4 shadow_coord4;
#endif


#endif

#ifdef USE_SHADOW_PASS

uniform highp float shadow_z_offset;
uniform highp float shadow_z_slope_scale;

#endif

#ifdef USE_DUAL_PARABOLOID
uniform highp vec2 dual_paraboloid;
varying float dp_clip;
#endif



VERTEX_SHADER_GLOBALS




void main() {
#ifdef USE_UNIFORM_INSTANCING

	highp mat4 modelview = (camera_inverse_transform * (world_transform * instance_transform));
#ifdef ENABLE_AMBIENT_OCTREE
	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * instance_transform));
#endif

#else

#ifdef USE_ATTRIBUTE_INSTANCING

	highp mat4 minst=mat4(instance_row0,instance_row1,instance_row2,instance_row3);
	highp mat4 modelview = (camera_inverse_transform * (world_transform * minst));
#ifdef ENABLE_AMBIENT_OCTREE
	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * minst));
#endif

#else

#ifdef USE_TEXTURE_INSTANCING

	highp vec2 ins_ofs=vec2(instance_uv.z,0.0);

	highp mat4 minst=mat4(
		texture2D(instance_matrices,instance_uv.xy),
		texture2D(instance_matrices,instance_uv.xy+ins_ofs),
		texture2D(instance_matrices,instance_uv.xy+ins_ofs*2.0),
		texture2D(instance_matrices,instance_uv.xy+ins_ofs*3.0)
	);

	/*highp mat4 minst=mat4(
		vec4(1.0,0.0,0.0,0.0),
		vec4(0.0,1.0,0.0,0.0),
		vec4(0.0,0.0,1.0,0.0),
		vec4(0.0,0.0,0.0,1.0)
	);*/

	highp mat4 modelview = (camera_inverse_transform * (world_transform * minst));
#ifdef ENABLE_AMBIENT_OCTREE
	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * minst));
#endif

#else
	highp mat4 modelview = (camera_inverse_transform * world_transform);
#ifdef ENABLE_AMBIENT_OCTREE
	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * world_transform);
#endif

#endif

#endif

#endif
	highp vec4 vertex_in = vertex_attrib; // vec4(vertex_attrib.xyz * data_attrib.x,1.0);
	vec3 normal_in = normal_attrib;
	normal_in*=normal_mult;
#if defined(ENABLE_TANGENT_INTERP)
	vec3 tangent_in = tangent_attrib.xyz;
	tangent_in*=normal_mult;
	float binormalf = tangent_attrib.a;
#endif

#ifdef USE_SKELETON

	{
		//skeleton transform
		highp mat4 m=mat4(texture2D(skeleton_matrices,vec2((bone_indices.x*3.0+0.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.x*3.0+1.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.x*3.0+2.0)*skeltex_pixel_size,0.0)),vec4(0.0,0.0,0.0,1.0))*bone_weights.x;
		m+=mat4(texture2D(skeleton_matrices,vec2((bone_indices.y*3.0+0.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.y*3.0+1.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.y*3.0+2.0)*skeltex_pixel_size,0.0)),vec4(0.0,0.0,0.0,1.0))*bone_weights.y;
		m+=mat4(texture2D(skeleton_matrices,vec2((bone_indices.z*3.0+0.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.z*3.0+1.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.z*3.0+2.0)*skeltex_pixel_size,0.0)),vec4(0.0,0.0,0.0,1.0))*bone_weights.z;
		m+=mat4(texture2D(skeleton_matrices,vec2((bone_indices.w*3.0+0.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.w*3.0+1.0)*skeltex_pixel_size,0.0)),texture2D(skeleton_matrices,vec2((bone_indices.w*3.0+2.0)*skeltex_pixel_size,0.0)),vec4(0.0,0.0,0.0,1.0))*bone_weights.w;

		vertex_in = vertex_in * m;
		normal_in = (vec4(normal_in,0.0) * m).xyz;
#if defined(ENABLE_TANGENT_INTERP)
		tangent_in = (vec4(tangent_in,0.0) * m).xyz;
#endif
	}

#endif

#ifdef ENABLE_AMBIENT_OCTREE

	ambient_octree_coords = (ambient_octree_transform * vertex_in).xyz;
#endif

	vertex_interp = (modelview * vertex_in).xyz;
	normal_interp = normalize((modelview * vec4(normal_in,0.0)).xyz);

#if defined(ENABLE_COLOR_INTERP)
#ifdef USE_COLOR_ATTRIB_SRGB_TO_LINEAR

	color_interp = vec4(
		color_attrib.r<0.04045 ? color_attrib.r * (1.0 / 12.92) : pow((color_attrib.r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
		color_attrib.g<0.04045 ? color_attrib.g * (1.0 / 12.92) : pow((color_attrib.g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
		color_attrib.b<0.04045 ? color_attrib.b * (1.0 / 12.92) : pow((color_attrib.b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
		color_attrib.a
	);
#else
	color_interp = color_attrib;
#endif
#endif

#if defined(ENABLE_TANGENT_INTERP)
	tangent_interp=normalize((modelview * vec4(tangent_in,0.0)).xyz);
	binormal_interp = normalize( cross(normal_interp,tangent_interp) * binormalf );
#endif

#if defined(ENABLE_UV_INTERP)
	uv_interp = uv_attrib;
#endif
#if defined(ENABLE_UV2_INTERP)
	uv2_interp = uv2_attrib;
#endif

	float vertex_specular_exp = 40.0; //material_specular.a;



VERTEX_SHADER_CODE


#ifdef USE_DUAL_PARABOLOID
//for dual paraboloid shadow mapping
        highp vec3 vtx = vertex_interp;
        vtx.z*=dual_paraboloid.y; //side to affect
        vtx.z+=0.01;
        dp_clip=vtx.z;
        highp float len=length( vtx );
        vtx=normalize(vtx);
        vtx.xy/=1.0+vtx.z;
        vtx.z = len*dual_paraboloid.x; // it's a reciprocal(len - z_near) / (z_far - z_near);
        vtx+=normalize(vtx)*0.025;
        vtx.z = vtx.z * 2.0 - 1.0; // fit to clipspace
        vertex_interp=vtx;

        //vertex_interp.w = z_clip;

#endif

#ifdef USE_SHADOW_PASS

	float z_ofs = shadow_z_offset;
	z_ofs += (1.0-abs(normal_interp.z))*shadow_z_slope_scale;
	vertex_interp.z-=z_ofs;
#endif

#ifdef LIGHT_USE_SHADOW

        shadow_coord = shadow_matrix * vec4(vertex_interp,1.0);
	shadow_coord.xyz/=shadow_coord.w;

#ifdef LIGHT_USE_PSSM
	shadow_coord.y*=0.5;
	shadow_coord.y+=0.5;
	shadow_coord2 = shadow_matrix2 * vec4(vertex_interp,1.0);
	shadow_coord2.xyz/=shadow_coord2.w;
	shadow_coord2.y*=0.5;
#endif
#ifdef LIGHT_USE_PSSM4
	shadow_coord.x*=0.5;
	shadow_coord2.x*=0.5;

	shadow_coord3 = shadow_matrix3 * vec4(vertex_interp,1.0);
	shadow_coord3.xyz/=shadow_coord3.w;
	shadow_coord3.xy*=vec2(0.5);
	shadow_coord3.xy+=vec2(0.5);

	shadow_coord4 = shadow_matrix4 * vec4(vertex_interp,1.0);
	shadow_coord4.xyz/=shadow_coord4.w;
	shadow_coord4.xy*=vec2(0.5);
	shadow_coord4.x+=0.5;

#endif

#endif

#ifdef USE_FOG

	fog_interp.a = pow( clamp( (length(vertex_interp)-fog_params.x)/(fog_params.y-fog_params.x), 0.0, 1.0 ), fog_params.z );
	fog_interp.rgb = mix( fog_color_begin, fog_color_end, fog_interp.a );
#endif

#ifndef VERTEX_SHADER_WRITE_POSITION
//vertex shader might write a position
	gl_Position = projection_transform * vec4(vertex_interp,1.0);
#endif



#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)

    position_interp=gl_Position;

#endif


#ifdef USE_VERTEX_LIGHTING

	vec3 eye_vec = -normalize(vertex_interp);

#ifdef LIGHT_TYPE_DIRECTIONAL

	vec3 light_dir = -light_direction;
	float attenuation = light_attenuation.r;


#endif

#ifdef LIGHT_TYPE_OMNI
	vec3 light_dir = light_pos-vertex_interp;
	float radius = light_attenuation.g;
	float dist = min(length(light_dir),radius);
	light_dir=normalize(light_dir);
	float attenuation = pow( max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;

#endif

#ifdef LIGHT_TYPE_SPOT

	vec3 light_dir = light_pos-vertex_interp;
	float radius = light_attenuation.g;
	float dist = min(length(light_dir),radius);
	light_dir=normalize(light_dir);
	float attenuation = pow(  max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
	vec3 spot_dir = light_direction;
	float spot_cutoff=light_spot_attenuation.r;
	float scos = max(dot(-light_dir, spot_dir),spot_cutoff);
	float rim = (1.0 - scos) / (1.0 - spot_cutoff);
	attenuation *= 1.0 - pow( rim, light_spot_attenuation.g);


#endif

#if defined(LIGHT_TYPE_DIRECTIONAL) || defined(LIGHT_TYPE_OMNI) || defined(LIGHT_TYPE_SPOT)

	//process_shade(normal_interp,light_dir,eye_vec,vertex_specular_exp,attenuation,diffuse_interp,specular_interp);
	{
		float NdotL = max(0.0,dot( normal_interp, light_dir ));
		vec3 half_vec = normalize(light_dir + eye_vec);
		float eye_light = max(dot(normal_interp, half_vec),0.0);
		diffuse_interp.rgb=light_diffuse * NdotL * attenuation;
		diffuse_interp.a=attenuation;
		if (NdotL > 0.0) {
			specular_interp=light_specular * pow( eye_light, vertex_specular_exp ) * attenuation;
		} else {
			specular_interp=vec3(0.0);
		}
	}

#else

#ifdef SHADELESS

	diffuse_interp=vec4(vec3(1.0),0.0);
	specular_interp=vec3(0.0);
# else

	diffuse_interp=vec4(0.0);
	specular_interp=vec3(0.0);
# endif

#endif




#endif


}


[fragment]


#ifdef USE_GLES_OVER_GL
#define mediump
#define highp
#define roundfix( m_val ) floor( (m_val) + 0.5 )
#else

precision mediump float;
precision mediump int;

#endif


//hack to use uv if no uv present so it works with lightmap
#ifdef ENABLE_AMBIENT_LIGHTMAP

#ifdef USE_LIGHTMAP_ON_UV2

#ifndef ENABLE_UV2_INTERP
#define ENABLE_UV2_INTERP
#endif

#else

#ifndef ENABLE_UV_INTERP
#define ENABLE_UV_INTERP
#endif

#endif

#endif


/* Varyings */

#if defined(ENABLE_COLOR_INTERP)
varying vec4 color_interp;
#endif

#if defined(ENABLE_UV_INTERP)
varying vec2 uv_interp;
#endif

#if defined(ENABLE_UV2_INTERP)
varying vec2 uv2_interp;
#endif

#if defined(ENABLE_TANGENT_INTERP)
varying vec3 tangent_interp;
varying vec3 binormal_interp;
#endif

#if defined(ENABLE_VAR1_INTERP)
varying vec4 var1_interp;
#endif

#if defined(ENABLE_VAR2_INTERP)
varying vec4 var2_interp;
#endif

#ifdef LIGHT_USE_PSSM
uniform vec3 light_pssm_split;
#endif

varying vec3 vertex_interp;
varying vec3 normal_interp;

#ifdef USE_FOG

varying vec4 fog_interp;

#endif

/* Material Uniforms */

#ifdef USE_VERTEX_LIGHTING

varying vec4 diffuse_interp;
varying vec3 specular_interp;

#endif

#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)

varying vec4 position_interp;

#endif



uniform vec3 light_pos;
uniform vec3 light_direction;
uniform vec3 light_attenuation;
uniform vec3 light_spot_attenuation;
uniform vec3 light_diffuse;
uniform vec3 light_specular;

uniform vec3 ambient_light;


#ifdef USE_FRAGMENT_LIGHTING



# ifdef USE_DEPTH_SHADOWS
# else
# endif

#endif

uniform float const_light_mult;
uniform float time;

#ifdef ENABLE_AMBIENT_OCTREE

varying highp vec3 ambient_octree_coords;
uniform highp float ambient_octree_lattice_size;
uniform highp vec2 ambient_octree_pix_size;
uniform highp vec2 ambient_octree_light_pix_size;
uniform highp float ambient_octree_lattice_divide;
uniform highp sampler2D ambient_octree_tex;
uniform highp sampler2D ambient_octree_light_tex;
uniform float ambient_octree_multiplier;
uniform int ambient_octree_steps;

#endif

#ifdef ENABLE_AMBIENT_LIGHTMAP

uniform highp sampler2D ambient_lightmap;
uniform float ambient_lightmap_multiplier;

#endif

#ifdef ENABLE_AMBIENT_DP_SAMPLER

uniform highp sampler2D ambient_dp_sampler;
uniform float ambient_dp_sampler_multiplier;

#endif

FRAGMENT_SHADER_GLOBALS



#ifdef LIGHT_USE_SHADOW

varying highp vec4 shadow_coord;
#ifdef LIGHT_USE_PSSM
varying highp vec4 shadow_coord2;
#endif
#ifdef LIGHT_USE_PSSM4
varying highp vec4 shadow_coord3;
varying highp vec4 shadow_coord4;
#endif

uniform highp sampler2D shadow_texture;
uniform highp vec2 shadow_texel_size;

uniform float shadow_darkening;

#ifdef USE_DEPTH_SHADOWS

#define SHADOW_DEPTH(m_tex,m_uv) (texture2D((m_tex),(m_uv)).z)

#else

//#define SHADOW_DEPTH(m_tex,m_uv) dot(texture2D((m_tex),(m_uv)),highp vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1)  )
#define SHADOW_DEPTH(m_tex,m_uv) dot(texture2D((m_tex),(m_uv)),vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1)  )

#endif

#ifdef USE_SHADOW_PCF


#ifdef USE_SHADOW_PCF_HQ


float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {

	float avg=(SHADOW_DEPTH(shadow_texture,coord) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x*2.0,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x*2.0,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y*2.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y*2.0)) < refdepth ?  0.0 : 1.0);
	return avg*(1.0/13.0);
}

#else

float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {

	float avg=(SHADOW_DEPTH(shadow_texture,coord) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
	return avg*0.2;
}

#endif




/*
	16x averaging
float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {

	vec2 offset = vec2(
		lessThan(vec2(0.25),fract(gl_FragCoord.xy * 0.5))
		);
	offset.y += offset.x;  // y ^= x in floating point

	if (offset.y > 1.1)
		offset.y = 0.0;
	float avg = step( refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(-1.5, 0.5))*shadow_texel_size) );
	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(0.5, 0.5))*shadow_texel_size) );
	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(-1.5, -1.5))*shadow_texel_size) );
	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(0.5, -1.5))*shadow_texel_size) );
	return avg * 0.25;
}
*/

/*
float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {

	vec2 offset = vec2(
		lessThan(vec2(0.25),fract(gl_FragCoord.xy * 0.5))
		);
	offset.y += offset.x;  // y ^= x in floating point

	if (offset.y > 1.1)
		offset.y = 0.0;
	return step( refdepth, SHADOW_DEPTH(shadow_texture, coord+ offset*shadow_texel_size) );

}

*/
/* simple pcf4 */
//#define SAMPLE_SHADOW_TEX(m_coord,m_depth) ((step(m_depth,SHADOW_DEPTH(shadow_texture,m_coord))+step(m_depth,SHADOW_DEPTH(shadow_texture,m_coord+vec2(0.0,shadow_texel_size.y)))+step(m_depth,SHADOW_DEPTH(shadow_texture,m_coord+vec2(shadow_texel_size.x,0.0)))+step(m_depth,SHADOW_DEPTH(shadow_texture,m_coord+shadow_texel_size)))/4.0)

#endif

#ifdef USE_SHADOW_ESM

uniform float esm_multiplier;

float SAMPLE_SHADOW_TEX(vec2 p_uv,float p_depth) {

#if defined (USE_DEPTH_SHADOWS)
	//these only are used if interpolation exists
	highp float occluder = SHADOW_DEPTH(shadow_texture, p_uv);
#else
	vec2 unnormalized = p_uv/shadow_texel_size;
	vec2 fractional = fract(unnormalized);
	unnormalized = floor(unnormalized);

	vec4 exponent;
	exponent.x = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( -0.5, 0.5 )) * shadow_texel_size );
	exponent.y = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( 0.5, 0.5 )) * shadow_texel_size );
	exponent.z = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( 0.5, -0.5 )) * shadow_texel_size );
	exponent.w = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( -0.5, -0.5 )) * shadow_texel_size );

	highp float occluder = (exponent.w + (exponent.x - exponent.w) * fractional.y);
	occluder = occluder + ((exponent.z + (exponent.y - exponent.z) * fractional.y) - occluder)*fractional.x;
#endif
	return clamp(exp(esm_multiplier* ( occluder - p_depth )),0.0,1.0);

}


#endif

#if !defined(USE_SHADOW_PCF) && !defined(USE_SHADOW_ESM)

#define SAMPLE_SHADOW_TEX(m_coord,m_depth) (SHADOW_DEPTH(shadow_texture,m_coord) < m_depth ?  0.0 : 1.0)

#endif


#endif

#ifdef USE_DUAL_PARABOLOID

varying float dp_clip;

#endif

uniform highp mat4 camera_inverse_transform;

#if defined(ENABLE_TEXSCREEN)

uniform vec2 texscreen_screen_mult;
uniform sampler2D texscreen_tex;

#endif

#if defined(ENABLE_SCREEN_UV)

uniform vec2 screen_uv_mult;

#endif

void main() {

#ifdef USE_DUAL_PARABOLOID
        if (dp_clip<0.0)
            discard;
#endif

	//lay out everything, whathever is unused is optimized away anyway
        vec3 vertex = vertex_interp;
	vec4 diffuse = vec4(0.9,0.9,0.9,1.0);
	vec3 specular = vec3(0.0,0.0,0.0);
	vec3 emission = vec3(0.0,0.0,0.0);
	float specular_exp=1.0;
	float glow=0.0;
	float shade_param=0.0;
	float side=float(gl_FrontFacing)*2.0-1.0;
#if defined(ENABLE_TANGENT_INTERP)
	vec3 binormal = normalize(binormal_interp)*side;
	vec3 tangent = normalize(tangent_interp)*side;
#endif
//	vec3 normal = abs(normalize(normal_interp))*side;
	vec3 normal = normalize(normal_interp)*side;
#if defined(ENABLE_SCREEN_UV)
	vec2 screen_uv = gl_FragCoord.xy*screen_uv_mult;
#endif

#if defined(ENABLE_UV_INTERP)
	vec2 uv = uv_interp;
#endif

#if defined(ENABLE_UV2_INTERP)
	vec2 uv2 = uv2_interp;
#endif

#if defined(ENABLE_COLOR_INTERP)
	vec4 color = color_interp;
#endif

#if defined(ENABLE_NORMALMAP)

	vec3 normalmap = vec3(0.0);
#endif

	float normaldepth=1.0;



#if defined(ENABLE_DISCARD)
	bool discard_=false;
#endif

{


FRAGMENT_SHADER_CODE

}

#if defined(ENABLE_NORMALMAP)

	normal = normalize( mix(normal_interp,tangent_interp * normalmap.x + binormal_interp * normalmap.y + normal_interp * normalmap.z,normaldepth) ) * side;

#endif

#if defined(ENABLE_DISCARD)
	if (discard_) {
	//easy to eliminate dead code
		discard;
	}
#endif

#ifdef ENABLE_CLIP_ALPHA
	if (diffuse.a<0.99) {
		//used for doublepass and shadowmapping
		discard;
	}
#endif

	float shadow_attenuation = 1.0;

#ifdef ENABLE_AMBIENT_LIGHTMAP

	vec3 ambientmap_color = vec3(0.0,0.0,0.0);
	vec2 ambientmap_uv = vec2(0.0,0.0);

#ifdef USE_LIGHTMAP_ON_UV2

	ambientmap_uv = uv2_interp;

#else

	ambientmap_uv = uv_interp;

#endif

	vec4 amcol = texture2D(ambient_lightmap,ambientmap_uv);
	shadow_attenuation=amcol.a;
	ambientmap_color = amcol.rgb;
	ambientmap_color*=ambient_lightmap_multiplier;
	ambientmap_color*=diffuse.rgb;



#endif


#ifdef ENABLE_AMBIENT_OCTREE

	vec3 ambientmap_color = vec3(0.0,0.0,0.0);


	{

		//read position from initial lattice grid
		highp vec3 lattice_pos = floor(ambient_octree_coords*ambient_octree_lattice_size);
		highp vec2 octant_uv = highp vec2(lattice_pos.x+ambient_octree_lattice_size*lattice_pos.z,lattice_pos.y);
		octant_uv=(octant_uv*highp vec2(2.0,4.0)+highp vec2(0.0,4.0));
		highp float ld = 1.0/ambient_octree_lattice_size;


		//go down the octree

		for(int i=0;i<ambient_octree_steps;i++) {


			highp vec3 sub=mod(ambient_octree_coords,ld);
			ld*=0.5;
			highp vec3 s = step(ld,sub);
			octant_uv+=s.xy;
			octant_uv.y+=s.z*2.0;
			octant_uv=(octant_uv+0.5)*ambient_octree_pix_size;
			highp vec4 new_uv = texture2D(ambient_octree_tex,octant_uv);
			octant_uv=floor(highp vec2( dot(new_uv.xy,highp vec2(65280.0,255.0)),  dot(new_uv.zw,highp vec2(65280.0,255.0)) )+0.5);//+ambient_octree_pix_size*0.5;

		}

		//sample color
		octant_uv=(octant_uv+0.5)*ambient_octree_light_pix_size;
		highp vec3 sub=(mod(ambient_octree_coords,ld)/ld);
		octant_uv.xy+=sub.xy*ambient_octree_light_pix_size.xy;
		vec3 col_up=texture2D(ambient_octree_light_tex,octant_uv).rgb;
		octant_uv.y+=ambient_octree_light_pix_size.y*2.0;
		vec3 col_down=texture2D(ambient_octree_light_tex,octant_uv).rgb;
		ambientmap_color=mix(col_up,col_down,sub.z)*ambient_octree_multiplier;

		ambientmap_color*=diffuse.rgb;

	}

#endif



#ifdef ENABLE_AMBIENT_DP_SAMPLER

	vec3 ambientmap_color = vec3(0.0,0.0,0.0);

	{

		vec3 dp_normal = normalize((vec4(normal,0) * camera_inverse_transform).xyz);
		vec2 ambient_uv = (dp_normal.xy / (1.0+abs(dp_normal.z)))*0.5+0.5; //dual paraboloid
		ambient_uv.y*=0.5;
		if (dp_normal.z<0) {

			ambient_uv.y=(0.5-ambient_uv.y)+0.5;

		}

		ambientmap_color = texture2D(ambient_dp_sampler,ambient_uv ).rgb * ambient_dp_sampler_multiplier;
		ambientmap_color*=diffuse.rgb;
	}

#endif




#ifdef LIGHT_USE_SHADOW
#ifdef LIGHT_TYPE_DIRECTIONAL

#ifdef LIGHT_USE_PSSM


//	if (vertex_interp.z > light_pssm_split) {
#if 0
	highp vec3 splane = vec3(0.0,0.0,0.0);

	if (gl_FragCoord.w > light_pssm_split.x) {

		splane = shadow_coord.xyz;
		splane.y+=1.0;
	} else {
		splane = shadow_coord2.xyz;
	}
	splane.y*=0.5;
	shadow_attenuation=SAMPLE_SHADOW_TEX(splane.xy,splane.z);

#else
/*
	float sa_a = SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
	float sa_b = SAMPLE_SHADOW_TEX(shadow_coord2.xy,shadow_coord2.z);
	if (gl_FragCoord.w > light_pssm_split.x) {
		shadow_attenuation=sa_a;
	} else {
		shadow_attenuation=sa_b;
	}
*/

	vec2 pssm_coord;
	float pssm_z;

#if defined(LIGHT_USE_PSSM) && defined(USE_SHADOW_ESM)
#define USE_PSSM_BLEND
	float pssm_blend;
	vec2 pssm_coord_2;
	float pssm_z_2;
	vec3 light_pssm_split_inv = 1.0/light_pssm_split;
	float w_inv = 1.0/gl_FragCoord.w;
#endif

#ifdef LIGHT_USE_PSSM4


	if (gl_FragCoord.w > light_pssm_split.y) {

		if (gl_FragCoord.w > light_pssm_split.x) {
			pssm_coord=shadow_coord.xy;
			pssm_z=shadow_coord.z;
#if defined(USE_PSSM_BLEND)
			pssm_coord_2=shadow_coord2.xy;
			pssm_z_2=shadow_coord2.z;
			pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv);
#endif

		} else {
			pssm_coord=shadow_coord2.xy;
			pssm_z=shadow_coord2.z;
#if defined(USE_PSSM_BLEND)
			pssm_coord_2=shadow_coord3.xy;
			pssm_z_2=shadow_coord3.z;
			pssm_blend=smoothstep(light_pssm_split_inv.x,light_pssm_split_inv.y,w_inv);
#endif

		}
	} else {


		if (gl_FragCoord.w > light_pssm_split.z) {
			pssm_coord=shadow_coord3.xy;
			pssm_z=shadow_coord3.z;
#if defined(USE_PSSM_BLEND)
			pssm_coord_2=shadow_coord4.xy;
			pssm_z_2=shadow_coord4.z;
			pssm_blend=smoothstep(light_pssm_split_inv.y,light_pssm_split_inv.z,w_inv);
#endif

		} else {
			pssm_coord=shadow_coord4.xy;
			pssm_z=shadow_coord4.z;
#if defined(USE_PSSM_BLEND)
			pssm_coord_2=shadow_coord4.xy;
			pssm_z_2=shadow_coord4.z;
			pssm_blend=0.0;
#endif

		}
	}

#else

	if (gl_FragCoord.w > light_pssm_split.x) {
		pssm_coord=shadow_coord.xy;
		pssm_z=shadow_coord.z;
#if defined(USE_PSSM_BLEND)
		pssm_coord_2=shadow_coord2.xy;
		pssm_z_2=shadow_coord2.z;
		pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv);
#endif

	} else {
		pssm_coord=shadow_coord2.xy;
		pssm_z=shadow_coord2.z;
#if defined(USE_PSSM_BLEND)
		pssm_coord_2=shadow_coord2.xy;
		pssm_z_2=shadow_coord2.z;
		pssm_blend=0.0;
#endif

	}

#endif

	//one one sample
	shadow_attenuation=SAMPLE_SHADOW_TEX(pssm_coord,pssm_z);
#if defined(USE_PSSM_BLEND)
	shadow_attenuation=mix(shadow_attenuation,SAMPLE_SHADOW_TEX(pssm_coord_2,pssm_z_2),pssm_blend);
#endif


#endif

#else

	shadow_attenuation=SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
#endif
#endif

#ifdef LIGHT_TYPE_OMNI

        vec3 splane=shadow_coord.xyz;///shadow_coord.w;
        float shadow_len=length(splane);
        splane=normalize(splane);
        float vofs=0.0;

        if (splane.z>=0.0) {

                splane.z+=1.0;
        } else {

                splane.z=1.0 - splane.z;
                vofs=0.5;
        }
        splane.xy/=splane.z;
        splane.xy=splane.xy * 0.5 + 0.5;
	float lradius = light_attenuation.g;
        splane.z = shadow_len / lradius;
        splane.y=clamp(splane.y,0.0,1.0)*0.5+vofs;

        shadow_attenuation=SAMPLE_SHADOW_TEX(splane.xy,splane.z);
#endif

#ifdef LIGHT_TYPE_SPOT

	shadow_attenuation=SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
#endif

	shadow_attenuation=mix(shadow_attenuation,1.0,shadow_darkening);
#endif


#ifdef USE_FRAGMENT_LIGHTING

	vec3 eye_vec = -normalize(vertex);

#ifdef LIGHT_TYPE_DIRECTIONAL

	vec3 light_dir = -light_direction;
	float attenuation = light_attenuation.r;


#endif

#ifdef LIGHT_TYPE_OMNI

	vec3 light_dir = light_pos-vertex;
	float radius = light_attenuation.g;
	float dist = min(length(light_dir),radius);
	light_dir=normalize(light_dir);
	float attenuation = pow( max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;

#endif


#ifdef LIGHT_TYPE_SPOT

	vec3 light_dir = light_pos-vertex;
	float radius = light_attenuation.g;
	float dist = min(length(light_dir),radius);
	light_dir=normalize(light_dir);
	float attenuation = pow(  max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
	vec3 spot_dir = light_direction;
	float spot_cutoff=light_spot_attenuation.r;
	float scos = max(dot(-light_dir, spot_dir),spot_cutoff);
	float rim = (1.0 - scos) / (1.0 - spot_cutoff);
	attenuation *= 1.0 - pow( rim, light_spot_attenuation.g);

#endif

# if defined(LIGHT_TYPE_DIRECTIONAL) || defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)

	{

		vec3 mdiffuse = diffuse.rgb;
		vec3 light;

#if defined(USE_LIGHT_SHADER_CODE)
//light is written by the light shader
{

LIGHT_SHADER_CODE

}
#else
//traditional lambert + blinn
		float NdotL = max(0.0,dot( normal, light_dir ));
		vec3 half_vec = normalize(light_dir + eye_vec);
		float eye_light = max(dot(normal, half_vec),0.0);

		light = light_diffuse * mdiffuse * NdotL;
		if (NdotL > 0.0) {
			light+=specular * light_specular * pow( eye_light, specular_exp );
		}
#endif
		diffuse.rgb = const_light_mult * ambient_light *diffuse.rgb + light * attenuation * shadow_attenuation;

#ifdef USE_FOG

		diffuse.rgb = mix(diffuse.rgb,fog_interp.rgb,fog_interp.a);

# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
		diffuse.rgb = mix(mix(vec3(0.0),diffuse.rgb,attenuation),diffuse.rgb,const_light_mult);
# endif


#endif


	}


# endif

# if !defined(LIGHT_TYPE_DIRECTIONAL) && !defined(LIGHT_TYPE_OMNI) && !defined (LIGHT_TYPE_SPOT)
//none
#ifndef SHADELESS
	diffuse.rgb=ambient_light *diffuse.rgb;
#endif

# endif

	diffuse.rgb+=const_light_mult*emission;

#endif




#ifdef USE_VERTEX_LIGHTING

	vec3 ambient = const_light_mult*ambient_light*diffuse.rgb;
# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
	ambient*=diffuse_interp.a; //attenuation affects ambient too

# endif

//	diffuse.rgb=(diffuse.rgb * diffuse_interp.rgb + specular * specular_interp)*shadow_attenuation + ambient;
//	diffuse.rgb+=emission * const_light_mult;
	diffuse.rgb=(diffuse.rgb * diffuse_interp.rgb + specular * specular_interp)*shadow_attenuation + ambient;
	diffuse.rgb+=emission * const_light_mult;

#ifdef USE_FOG

	diffuse.rgb = mix(diffuse.rgb,fog_interp.rgb,fog_interp.a);

# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
	diffuse.rgb = mix(mix(vec3(0.0),diffuse.rgb,diffuse_interp.a),diffuse.rgb,const_light_mult);
# endif

#endif

#endif


#if defined(ENABLE_AMBIENT_OCTREE) || defined(ENABLE_AMBIENT_LIGHTMAP) || defined(ENABLE_AMBIENT_DP_SAMPLER)

	diffuse.rgb+=ambientmap_color;
#endif


#ifdef USE_SHADOW_PASS

#ifdef USE_DEPTH_SHADOWS

        //do nothing, depth is just written
#else
        // pack depth to rgba
        //highp float bias = 0.0005;
	highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0;//bias;
        highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0));
        comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
        gl_FragColor = comp;

#endif

#else



#ifdef USE_GLOW

	diffuse.a=glow;
#endif

#ifdef USE_8BIT_HDR
	diffuse.rgb*=0.25;
#endif

	gl_FragColor = diffuse;
#endif
}