1[vertex]
2
3
4#ifdef USE_GLES_OVER_GL
5#define mediump
6#define highp
7#define roundfix( m_val ) floor( (m_val) + 0.5 )
8#else
9precision mediump float;
10precision mediump int;
11#endif
12
13
14
15/*
16from VisualServer:
17
18ARRAY_VERTEX=0,
19ARRAY_NORMAL=1,
20ARRAY_TANGENT=2,
21ARRAY_COLOR=3,
22ARRAY_TEX_UV=4,
23ARRAY_TEX_UV2=5,
24ARRAY_BONES=6,
25ARRAY_WEIGHTS=7,
26ARRAY_INDEX=8,
27*/
28
29//hack to use uv if no uv present so it works with lightmap
30#ifdef ENABLE_AMBIENT_LIGHTMAP
31
32#ifdef USE_LIGHTMAP_ON_UV2
33
34#ifndef ENABLE_UV2_INTERP
35#define ENABLE_UV2_INTERP
36#endif
37
38#else
39
40#ifndef ENABLE_UV_INTERP
41#define ENABLE_UV_INTERP
42#endif
43
44#endif
45
46#endif
47
48
49/* INPUT ATTRIBS */
50
51attribute highp vec4 vertex_attrib; // attrib:0
52attribute vec3 normal_attrib; // attrib:1
53attribute vec4 tangent_attrib; // attrib:2
54attribute vec4 color_attrib; // attrib:3
55attribute vec2 uv_attrib; // attrib:4
56attribute vec2 uv2_attrib; // attrib:5
57
58uniform float normal_mult;
59
60#ifdef USE_SKELETON
61attribute vec4 bone_indices; // attrib:6
62attribute vec4 bone_weights; // attrib:7
63uniform highp sampler2D skeleton_matrices;
64uniform highp float skeltex_pixel_size;
65#endif
66
67#ifdef USE_ATTRIBUTE_INSTANCING
68
69attribute highp vec4 instance_row0; // attrib:8
70attribute highp vec4 instance_row1; // attrib:9
71attribute highp vec4 instance_row2; // attrib:10
72attribute highp vec4 instance_row3; // attrib:11
73
74#endif
75
76#ifdef USE_TEXTURE_INSTANCING
77
78attribute highp vec3 instance_uv; // attrib:6
79uniform highp sampler2D instance_matrices;
80
81#endif
82
83uniform highp mat4 world_transform;
84uniform highp mat4 camera_inverse_transform;
85uniform highp mat4 projection_transform;
86
87#ifdef USE_UNIFORM_INSTANCING
88//shittiest form of instancing (but most compatible)
89uniform highp mat4 instance_transform;
90#endif
91
92/* Varyings */
93
94varying vec3 vertex_interp;
95varying vec3 normal_interp;
96
97#if defined(ENABLE_COLOR_INTERP)
98varying vec4 color_interp;
99#endif
100
101#if defined(ENABLE_UV_INTERP)
102varying vec2 uv_interp;
103#endif
104
105#if defined(ENABLE_UV2_INTERP)
106varying vec2 uv2_interp;
107#endif
108
109#if defined(ENABLE_VAR1_INTERP)
110varying vec4 var1_interp;
111#endif
112
113#if defined(ENABLE_VAR2_INTERP)
114varying vec4 var2_interp;
115#endif
116
117#if defined(ENABLE_TANGENT_INTERP)
118varying vec3 tangent_interp;
119varying vec3 binormal_interp;
120#endif
121
122#ifdef ENABLE_AMBIENT_OCTREE
123
124uniform highp mat4 ambient_octree_inverse_transform;
125varying highp vec3 ambient_octree_coords;
126
127#endif
128
129#ifdef USE_FOG
130
131varying vec4 fog_interp;
132uniform highp vec3 fog_params;
133uniform vec3 fog_color_begin;
134uniform vec3 fog_color_end;
135
136#endif
137
138#ifdef USE_VERTEX_LIGHTING
139
140uniform vec3 light_pos;
141uniform vec3 light_direction;
142uniform vec3 light_attenuation;
143uniform vec3 light_spot_attenuation;
144uniform vec3 light_diffuse;
145uniform vec3 light_specular;
146
147
148
149#endif
150
151varying vec4 diffuse_interp;
152varying vec3 specular_interp;
153//intended for static branching
154//pretty much all meaningful platforms support
155//static branching
156
157uniform float time;
158uniform float instance_id;
159
160uniform vec3 ambient_light;
161
162#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)
163
164varying vec4 position_interp;
165
166#endif
167
168#ifdef LIGHT_USE_SHADOW
169
170uniform highp mat4 shadow_matrix;
171varying highp vec4 shadow_coord;
172#ifdef LIGHT_USE_PSSM
173uniform highp mat4 shadow_matrix2;
174varying highp vec4 shadow_coord2;
175#endif
176#ifdef LIGHT_USE_PSSM4
177uniform highp mat4 shadow_matrix3;
178varying highp vec4 shadow_coord3;
179uniform highp mat4 shadow_matrix4;
180varying highp vec4 shadow_coord4;
181#endif
182
183
184#endif
185
186#ifdef USE_SHADOW_PASS
187
188uniform highp float shadow_z_offset;
189uniform highp float shadow_z_slope_scale;
190
191#endif
192
193#ifdef USE_DUAL_PARABOLOID
194uniform highp vec2 dual_paraboloid;
195varying float dp_clip;
196#endif
197
198
199
200VERTEX_SHADER_GLOBALS
201
202
203
204
205void main() {
206#ifdef USE_UNIFORM_INSTANCING
207
208	highp mat4 modelview = (camera_inverse_transform * (world_transform * instance_transform));
209#ifdef ENABLE_AMBIENT_OCTREE
210	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * instance_transform));
211#endif
212
213#else
214
215#ifdef USE_ATTRIBUTE_INSTANCING
216
217	highp mat4 minst=mat4(instance_row0,instance_row1,instance_row2,instance_row3);
218	highp mat4 modelview = (camera_inverse_transform * (world_transform * minst));
219#ifdef ENABLE_AMBIENT_OCTREE
220	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * minst));
221#endif
222
223#else
224
225#ifdef USE_TEXTURE_INSTANCING
226
227	highp vec2 ins_ofs=vec2(instance_uv.z,0.0);
228
229	highp mat4 minst=mat4(
230		texture2D(instance_matrices,instance_uv.xy),
231		texture2D(instance_matrices,instance_uv.xy+ins_ofs),
232		texture2D(instance_matrices,instance_uv.xy+ins_ofs*2.0),
233		texture2D(instance_matrices,instance_uv.xy+ins_ofs*3.0)
234	);
235
236	/*highp mat4 minst=mat4(
237		vec4(1.0,0.0,0.0,0.0),
238		vec4(0.0,1.0,0.0,0.0),
239		vec4(0.0,0.0,1.0,0.0),
240		vec4(0.0,0.0,0.0,1.0)
241	);*/
242
243	highp mat4 modelview = (camera_inverse_transform * (world_transform * minst));
244#ifdef ENABLE_AMBIENT_OCTREE
245	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * (world_transform * minst));
246#endif
247
248#else
249	highp mat4 modelview = (camera_inverse_transform * world_transform);
250#ifdef ENABLE_AMBIENT_OCTREE
251	highp mat4 ambient_octree_transform = (ambient_octree_inverse_transform * world_transform);
252#endif
253
254#endif
255
256#endif
257
258#endif
259	highp vec4 vertex_in = vertex_attrib; // vec4(vertex_attrib.xyz * data_attrib.x,1.0);
260	vec3 normal_in = normal_attrib;
261	normal_in*=normal_mult;
262#if defined(ENABLE_TANGENT_INTERP)
263	vec3 tangent_in = tangent_attrib.xyz;
264	tangent_in*=normal_mult;
265	float binormalf = tangent_attrib.a;
266#endif
267
268#ifdef USE_SKELETON
269
270	{
271		//skeleton transform
272		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;
273		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;
274		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;
275		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;
276
277		vertex_in = vertex_in * m;
278		normal_in = (vec4(normal_in,0.0) * m).xyz;
279#if defined(ENABLE_TANGENT_INTERP)
280		tangent_in = (vec4(tangent_in,0.0) * m).xyz;
281#endif
282	}
283
284#endif
285
286#ifdef ENABLE_AMBIENT_OCTREE
287
288	ambient_octree_coords = (ambient_octree_transform * vertex_in).xyz;
289#endif
290
291	vertex_interp = (modelview * vertex_in).xyz;
292	normal_interp = normalize((modelview * vec4(normal_in,0.0)).xyz);
293
294#if defined(ENABLE_COLOR_INTERP)
295#ifdef USE_COLOR_ATTRIB_SRGB_TO_LINEAR
296
297	color_interp = vec4(
298		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),
299		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),
300		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),
301		color_attrib.a
302	);
303#else
304	color_interp = color_attrib;
305#endif
306#endif
307
308#if defined(ENABLE_TANGENT_INTERP)
309	tangent_interp=normalize((modelview * vec4(tangent_in,0.0)).xyz);
310	binormal_interp = normalize( cross(normal_interp,tangent_interp) * binormalf );
311#endif
312
313#if defined(ENABLE_UV_INTERP)
314	uv_interp = uv_attrib;
315#endif
316#if defined(ENABLE_UV2_INTERP)
317	uv2_interp = uv2_attrib;
318#endif
319
320	float vertex_specular_exp = 40.0; //material_specular.a;
321
322
323
324VERTEX_SHADER_CODE
325
326
327#ifdef USE_DUAL_PARABOLOID
328//for dual paraboloid shadow mapping
329        highp vec3 vtx = vertex_interp;
330        vtx.z*=dual_paraboloid.y; //side to affect
331        vtx.z+=0.01;
332        dp_clip=vtx.z;
333        highp float len=length( vtx );
334        vtx=normalize(vtx);
335        vtx.xy/=1.0+vtx.z;
336        vtx.z = len*dual_paraboloid.x; // it's a reciprocal(len - z_near) / (z_far - z_near);
337        vtx+=normalize(vtx)*0.025;
338        vtx.z = vtx.z * 2.0 - 1.0; // fit to clipspace
339        vertex_interp=vtx;
340
341        //vertex_interp.w = z_clip;
342
343#endif
344
345#ifdef USE_SHADOW_PASS
346
347	float z_ofs = shadow_z_offset;
348	z_ofs += (1.0-abs(normal_interp.z))*shadow_z_slope_scale;
349	vertex_interp.z-=z_ofs;
350#endif
351
352#ifdef LIGHT_USE_SHADOW
353
354        shadow_coord = shadow_matrix * vec4(vertex_interp,1.0);
355	shadow_coord.xyz/=shadow_coord.w;
356
357#ifdef LIGHT_USE_PSSM
358	shadow_coord.y*=0.5;
359	shadow_coord.y+=0.5;
360	shadow_coord2 = shadow_matrix2 * vec4(vertex_interp,1.0);
361	shadow_coord2.xyz/=shadow_coord2.w;
362	shadow_coord2.y*=0.5;
363#endif
364#ifdef LIGHT_USE_PSSM4
365	shadow_coord.x*=0.5;
366	shadow_coord2.x*=0.5;
367
368	shadow_coord3 = shadow_matrix3 * vec4(vertex_interp,1.0);
369	shadow_coord3.xyz/=shadow_coord3.w;
370	shadow_coord3.xy*=vec2(0.5);
371	shadow_coord3.xy+=vec2(0.5);
372
373	shadow_coord4 = shadow_matrix4 * vec4(vertex_interp,1.0);
374	shadow_coord4.xyz/=shadow_coord4.w;
375	shadow_coord4.xy*=vec2(0.5);
376	shadow_coord4.x+=0.5;
377
378#endif
379
380#endif
381
382#ifdef USE_FOG
383
384	fog_interp.a = pow( clamp( (length(vertex_interp)-fog_params.x)/(fog_params.y-fog_params.x), 0.0, 1.0 ), fog_params.z );
385	fog_interp.rgb = mix( fog_color_begin, fog_color_end, fog_interp.a );
386#endif
387
388#ifndef VERTEX_SHADER_WRITE_POSITION
389//vertex shader might write a position
390	gl_Position = projection_transform * vec4(vertex_interp,1.0);
391#endif
392
393
394
395#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)
396
397    position_interp=gl_Position;
398
399#endif
400
401
402#ifdef USE_VERTEX_LIGHTING
403
404	vec3 eye_vec = -normalize(vertex_interp);
405
406#ifdef LIGHT_TYPE_DIRECTIONAL
407
408	vec3 light_dir = -light_direction;
409	float attenuation = light_attenuation.r;
410
411
412#endif
413
414#ifdef LIGHT_TYPE_OMNI
415	vec3 light_dir = light_pos-vertex_interp;
416	float radius = light_attenuation.g;
417	float dist = min(length(light_dir),radius);
418	light_dir=normalize(light_dir);
419	float attenuation = pow( max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
420
421#endif
422
423#ifdef LIGHT_TYPE_SPOT
424
425	vec3 light_dir = light_pos-vertex_interp;
426	float radius = light_attenuation.g;
427	float dist = min(length(light_dir),radius);
428	light_dir=normalize(light_dir);
429	float attenuation = pow(  max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
430	vec3 spot_dir = light_direction;
431	float spot_cutoff=light_spot_attenuation.r;
432	float scos = max(dot(-light_dir, spot_dir),spot_cutoff);
433	float rim = (1.0 - scos) / (1.0 - spot_cutoff);
434	attenuation *= 1.0 - pow( rim, light_spot_attenuation.g);
435
436
437#endif
438
439#if defined(LIGHT_TYPE_DIRECTIONAL) || defined(LIGHT_TYPE_OMNI) || defined(LIGHT_TYPE_SPOT)
440
441	//process_shade(normal_interp,light_dir,eye_vec,vertex_specular_exp,attenuation,diffuse_interp,specular_interp);
442	{
443		float NdotL = max(0.0,dot( normal_interp, light_dir ));
444		vec3 half_vec = normalize(light_dir + eye_vec);
445		float eye_light = max(dot(normal_interp, half_vec),0.0);
446		diffuse_interp.rgb=light_diffuse * NdotL * attenuation;
447		diffuse_interp.a=attenuation;
448		if (NdotL > 0.0) {
449			specular_interp=light_specular * pow( eye_light, vertex_specular_exp ) * attenuation;
450		} else {
451			specular_interp=vec3(0.0);
452		}
453	}
454
455#else
456
457#ifdef SHADELESS
458
459	diffuse_interp=vec4(vec3(1.0),0.0);
460	specular_interp=vec3(0.0);
461# else
462
463	diffuse_interp=vec4(0.0);
464	specular_interp=vec3(0.0);
465# endif
466
467#endif
468
469
470
471
472#endif
473
474
475}
476
477
478[fragment]
479
480
481#ifdef USE_GLES_OVER_GL
482#define mediump
483#define highp
484#define roundfix( m_val ) floor( (m_val) + 0.5 )
485#else
486
487precision mediump float;
488precision mediump int;
489
490#endif
491
492
493//hack to use uv if no uv present so it works with lightmap
494#ifdef ENABLE_AMBIENT_LIGHTMAP
495
496#ifdef USE_LIGHTMAP_ON_UV2
497
498#ifndef ENABLE_UV2_INTERP
499#define ENABLE_UV2_INTERP
500#endif
501
502#else
503
504#ifndef ENABLE_UV_INTERP
505#define ENABLE_UV_INTERP
506#endif
507
508#endif
509
510#endif
511
512
513/* Varyings */
514
515#if defined(ENABLE_COLOR_INTERP)
516varying vec4 color_interp;
517#endif
518
519#if defined(ENABLE_UV_INTERP)
520varying vec2 uv_interp;
521#endif
522
523#if defined(ENABLE_UV2_INTERP)
524varying vec2 uv2_interp;
525#endif
526
527#if defined(ENABLE_TANGENT_INTERP)
528varying vec3 tangent_interp;
529varying vec3 binormal_interp;
530#endif
531
532#if defined(ENABLE_VAR1_INTERP)
533varying vec4 var1_interp;
534#endif
535
536#if defined(ENABLE_VAR2_INTERP)
537varying vec4 var2_interp;
538#endif
539
540#ifdef LIGHT_USE_PSSM
541uniform vec3 light_pssm_split;
542#endif
543
544varying vec3 vertex_interp;
545varying vec3 normal_interp;
546
547#ifdef USE_FOG
548
549varying vec4 fog_interp;
550
551#endif
552
553/* Material Uniforms */
554
555#ifdef USE_VERTEX_LIGHTING
556
557varying vec4 diffuse_interp;
558varying vec3 specular_interp;
559
560#endif
561
562#if !defined(USE_DEPTH_SHADOWS) && defined(USE_SHADOW_PASS)
563
564varying vec4 position_interp;
565
566#endif
567
568
569
570uniform vec3 light_pos;
571uniform vec3 light_direction;
572uniform vec3 light_attenuation;
573uniform vec3 light_spot_attenuation;
574uniform vec3 light_diffuse;
575uniform vec3 light_specular;
576
577uniform vec3 ambient_light;
578
579
580#ifdef USE_FRAGMENT_LIGHTING
581
582
583
584# ifdef USE_DEPTH_SHADOWS
585# else
586# endif
587
588#endif
589
590uniform float const_light_mult;
591uniform float time;
592
593#ifdef ENABLE_AMBIENT_OCTREE
594
595varying highp vec3 ambient_octree_coords;
596uniform highp float ambient_octree_lattice_size;
597uniform highp vec2 ambient_octree_pix_size;
598uniform highp vec2 ambient_octree_light_pix_size;
599uniform highp float ambient_octree_lattice_divide;
600uniform highp sampler2D ambient_octree_tex;
601uniform highp sampler2D ambient_octree_light_tex;
602uniform float ambient_octree_multiplier;
603uniform int ambient_octree_steps;
604
605#endif
606
607#ifdef ENABLE_AMBIENT_LIGHTMAP
608
609uniform highp sampler2D ambient_lightmap;
610uniform float ambient_lightmap_multiplier;
611
612#endif
613
614#ifdef ENABLE_AMBIENT_DP_SAMPLER
615
616uniform highp sampler2D ambient_dp_sampler;
617uniform float ambient_dp_sampler_multiplier;
618
619#endif
620
621#ifdef ENABLE_AMBIENT_COLOR
622
623uniform vec3 ambient_color;
624
625#endif
626
627FRAGMENT_SHADER_GLOBALS
628
629
630
631#ifdef LIGHT_USE_SHADOW
632
633varying highp vec4 shadow_coord;
634#ifdef LIGHT_USE_PSSM
635varying highp vec4 shadow_coord2;
636#endif
637#ifdef LIGHT_USE_PSSM4
638varying highp vec4 shadow_coord3;
639varying highp vec4 shadow_coord4;
640#endif
641
642uniform highp sampler2D shadow_texture;
643uniform highp vec2 shadow_texel_size;
644
645uniform float shadow_darkening;
646
647#ifdef USE_DEPTH_SHADOWS
648
649#define SHADOW_DEPTH(m_tex,m_uv) (texture2D((m_tex),(m_uv)).z)
650
651#else
652
653//#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)  )
654#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)  )
655
656#endif
657
658#ifdef USE_SHADOW_PCF
659
660
661#ifdef USE_SHADOW_PCF_HQ
662
663
664float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {
665
666	float avg=(SHADOW_DEPTH(shadow_texture,coord) < refdepth ?  0.0 : 1.0);
667	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
668	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
669	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
670	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
671	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
672	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
673	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
674	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
675	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x*2.0,0.0)) < refdepth ?  0.0 : 1.0);
676	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x*2.0,0.0)) < refdepth ?  0.0 : 1.0);
677	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y*2.0)) < refdepth ?  0.0 : 1.0);
678	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y*2.0)) < refdepth ?  0.0 : 1.0);
679	return avg*(1.0/13.0);
680}
681
682#else
683
684float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {
685
686	float avg=(SHADOW_DEPTH(shadow_texture,coord) < refdepth ?  0.0 : 1.0);
687	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
688	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(-shadow_texel_size.x,0.0)) < refdepth ?  0.0 : 1.0);
689	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
690	avg+=(SHADOW_DEPTH(shadow_texture,coord+vec2(0.0,-shadow_texel_size.y)) < refdepth ?  0.0 : 1.0);
691	return avg*0.2;
692}
693
694#endif
695
696
697
698
699/*
700	16x averaging
701float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {
702
703	vec2 offset = vec2(
704		lessThan(vec2(0.25),fract(gl_FragCoord.xy * 0.5))
705		);
706	offset.y += offset.x;  // y ^= x in floating point
707
708	if (offset.y > 1.1)
709		offset.y = 0.0;
710	float avg = step( refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(-1.5, 0.5))*shadow_texel_size) );
711	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(0.5, 0.5))*shadow_texel_size) );
712	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(-1.5, -1.5))*shadow_texel_size) );
713	avg+=step(refdepth, SHADOW_DEPTH(shadow_texture, coord+ (offset + vec2(0.5, -1.5))*shadow_texel_size) );
714	return avg * 0.25;
715}
716*/
717
718/*
719float SAMPLE_SHADOW_TEX( highp vec2 coord, highp float refdepth) {
720
721	vec2 offset = vec2(
722		lessThan(vec2(0.25),fract(gl_FragCoord.xy * 0.5))
723		);
724	offset.y += offset.x;  // y ^= x in floating point
725
726	if (offset.y > 1.1)
727		offset.y = 0.0;
728	return step( refdepth, SHADOW_DEPTH(shadow_texture, coord+ offset*shadow_texel_size) );
729
730}
731
732*/
733/* simple pcf4 */
734//#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)
735
736#endif
737
738#ifdef USE_SHADOW_ESM
739
740uniform float esm_multiplier;
741
742float SAMPLE_SHADOW_TEX(vec2 p_uv,float p_depth) {
743
744#if defined (USE_DEPTH_SHADOWS)
745	//these only are used if interpolation exists
746	highp float occluder = SHADOW_DEPTH(shadow_texture, p_uv);
747#else
748	vec2 unnormalized = p_uv/shadow_texel_size;
749	vec2 fractional = fract(unnormalized);
750	unnormalized = floor(unnormalized);
751
752	vec4 exponent;
753	exponent.x = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( -0.5, 0.5 )) * shadow_texel_size );
754	exponent.y = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( 0.5, 0.5 )) * shadow_texel_size );
755	exponent.z = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( 0.5, -0.5 )) * shadow_texel_size );
756	exponent.w = SHADOW_DEPTH(shadow_texture, (unnormalized + vec2( -0.5, -0.5 )) * shadow_texel_size );
757
758	highp float occluder = (exponent.w + (exponent.x - exponent.w) * fractional.y);
759	occluder = occluder + ((exponent.z + (exponent.y - exponent.z) * fractional.y) - occluder)*fractional.x;
760#endif
761	return clamp(exp(esm_multiplier* ( occluder - p_depth )),0.0,1.0);
762
763}
764
765
766#endif
767
768#if !defined(USE_SHADOW_PCF) && !defined(USE_SHADOW_ESM)
769
770#define SAMPLE_SHADOW_TEX(m_coord,m_depth) (SHADOW_DEPTH(shadow_texture,m_coord) < m_depth ?  0.0 : 1.0)
771
772#endif
773
774
775#endif
776
777#ifdef USE_DUAL_PARABOLOID
778
779varying float dp_clip;
780
781#endif
782
783uniform highp mat4 camera_inverse_transform;
784
785#if defined(ENABLE_TEXSCREEN)
786
787uniform vec2 texscreen_screen_mult;
788uniform vec4 texscreen_screen_clamp;
789uniform sampler2D texscreen_tex;
790
791#endif
792
793#if defined(ENABLE_SCREEN_UV)
794
795uniform vec2 screen_uv_mult;
796
797#endif
798
799void main() {
800
801#ifdef USE_DUAL_PARABOLOID
802        if (dp_clip<0.0)
803            discard;
804#endif
805
806	//lay out everything, whathever is unused is optimized away anyway
807        vec3 vertex = vertex_interp;
808	vec4 diffuse = vec4(0.9,0.9,0.9,1.0);
809	vec3 specular = vec3(0.0,0.0,0.0);
810	vec3 emission = vec3(0.0,0.0,0.0);
811	float specular_exp=1.0;
812	float glow=0.0;
813	float shade_param=0.0;
814#ifdef DISABLE_FRONT_FACING
815	float side=float(1)*2.0-1.0;
816#else
817	float side=float(gl_FrontFacing)*2.0-1.0;
818#endif
819#if defined(ENABLE_TANGENT_INTERP)
820	vec3 binormal = normalize(binormal_interp)*side;
821	vec3 tangent = normalize(tangent_interp)*side;
822#endif
823//	vec3 normal = abs(normalize(normal_interp))*side;
824	vec3 normal = normalize(normal_interp)*side;
825#if defined(ENABLE_SCREEN_UV)
826	vec2 screen_uv = gl_FragCoord.xy*screen_uv_mult;
827#endif
828
829#if defined(ENABLE_UV_INTERP)
830	vec2 uv = uv_interp;
831#endif
832
833#if defined(ENABLE_UV2_INTERP)
834	vec2 uv2 = uv2_interp;
835#endif
836
837#if defined(ENABLE_COLOR_INTERP)
838	vec4 color = color_interp;
839#endif
840
841#if defined(ENABLE_NORMALMAP)
842
843	vec3 normalmap = vec3(0.0);
844#endif
845
846	float normaldepth=1.0;
847
848
849
850#if defined(ENABLE_DISCARD)
851	bool discard_=false;
852#endif
853
854{
855
856
857FRAGMENT_SHADER_CODE
858
859}
860
861#if defined(ENABLE_NORMALMAP)
862
863	normal = normalize( mix(normal_interp,tangent_interp * normalmap.x + binormal_interp * normalmap.y + normal_interp * normalmap.z,normaldepth) ) * side;
864
865#endif
866
867#if defined(ENABLE_DISCARD)
868	if (discard_) {
869	//easy to eliminate dead code
870		discard;
871	}
872#endif
873
874#ifdef ENABLE_CLIP_ALPHA
875	if (diffuse.a<0.99) {
876		//used for doublepass and shadowmapping
877		discard;
878	}
879#endif
880
881	float shadow_attenuation = 1.0;
882
883#ifdef ENABLE_AMBIENT_LIGHTMAP
884
885	vec3 ambientmap_color = vec3(0.0,0.0,0.0);
886	vec2 ambientmap_uv = vec2(0.0,0.0);
887
888#ifdef USE_LIGHTMAP_ON_UV2
889
890	ambientmap_uv = uv2_interp;
891
892#else
893
894	ambientmap_uv = uv_interp;
895
896#endif
897
898	vec4 amcol = texture2D(ambient_lightmap,ambientmap_uv);
899	shadow_attenuation=amcol.a;
900	ambientmap_color = amcol.rgb;
901	ambientmap_color*=ambient_lightmap_multiplier;
902	ambientmap_color*=diffuse.rgb;
903
904
905
906#endif
907
908
909#ifdef ENABLE_AMBIENT_OCTREE
910
911	vec3 ambientmap_color = vec3(0.0,0.0,0.0);
912
913
914	{
915
916		//read position from initial lattice grid
917		highp vec3 lattice_pos = floor(ambient_octree_coords*ambient_octree_lattice_size);
918		highp vec2 octant_uv = highp vec2(lattice_pos.x+ambient_octree_lattice_size*lattice_pos.z,lattice_pos.y);
919		octant_uv=(octant_uv*highp vec2(2.0,4.0)+highp vec2(0.0,4.0));
920		highp float ld = 1.0/ambient_octree_lattice_size;
921
922
923		//go down the octree
924
925		for(int i=0;i<ambient_octree_steps;i++) {
926
927
928			highp vec3 sub=mod(ambient_octree_coords,ld);
929			ld*=0.5;
930			highp vec3 s = step(ld,sub);
931			octant_uv+=s.xy;
932			octant_uv.y+=s.z*2.0;
933			octant_uv=(octant_uv+0.5)*ambient_octree_pix_size;
934			highp vec4 new_uv = texture2D(ambient_octree_tex,octant_uv);
935			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;
936
937		}
938
939		//sample color
940		octant_uv=(octant_uv+0.5)*ambient_octree_light_pix_size;
941		highp vec3 sub=(mod(ambient_octree_coords,ld)/ld);
942		octant_uv.xy+=sub.xy*ambient_octree_light_pix_size.xy;
943		vec3 col_up=texture2D(ambient_octree_light_tex,octant_uv).rgb;
944		octant_uv.y+=ambient_octree_light_pix_size.y*2.0;
945		vec3 col_down=texture2D(ambient_octree_light_tex,octant_uv).rgb;
946		ambientmap_color=mix(col_up,col_down,sub.z)*ambient_octree_multiplier;
947
948		ambientmap_color*=diffuse.rgb;
949
950	}
951
952#endif
953
954
955
956#ifdef ENABLE_AMBIENT_DP_SAMPLER
957
958	vec3 ambientmap_color = vec3(0.0,0.0,0.0);
959
960	{
961
962		vec3 dp_normal = normalize((vec4(normal,0) * camera_inverse_transform).xyz);
963		vec2 ambient_uv = (dp_normal.xy / (1.0+abs(dp_normal.z)))*0.5+0.5; //dual paraboloid
964		ambient_uv.y*=0.5;
965		if (dp_normal.z<0) {
966
967			ambient_uv.y=(0.5-ambient_uv.y)+0.5;
968
969		}
970
971		ambientmap_color = texture2D(ambient_dp_sampler,ambient_uv ).rgb * ambient_dp_sampler_multiplier;
972		ambientmap_color*=diffuse.rgb;
973	}
974
975#endif
976
977
978
979
980#ifdef LIGHT_USE_SHADOW
981#ifdef LIGHT_TYPE_DIRECTIONAL
982
983	float shadow_fade_exponent=5.0;  //hardcoded for now
984	float shadow_fade=pow(length(vertex_interp)/light_attenuation.g,shadow_fade_exponent);
985
986// optimization - skip shadows outside visible range
987	if(shadow_fade<1.0){
988
989#ifdef LIGHT_USE_PSSM
990
991
992//	if (vertex_interp.z > light_pssm_split) {
993#if 0
994	highp vec3 splane = vec3(0.0,0.0,0.0);
995
996	if (gl_FragCoord.w > light_pssm_split.x) {
997
998		splane = shadow_coord.xyz;
999		splane.y+=1.0;
1000	} else {
1001		splane = shadow_coord2.xyz;
1002	}
1003	splane.y*=0.5;
1004	shadow_attenuation=SAMPLE_SHADOW_TEX(splane.xy,splane.z);
1005
1006#else
1007/*
1008	float sa_a = SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
1009	float sa_b = SAMPLE_SHADOW_TEX(shadow_coord2.xy,shadow_coord2.z);
1010	if (gl_FragCoord.w > light_pssm_split.x) {
1011		shadow_attenuation=sa_a;
1012	} else {
1013		shadow_attenuation=sa_b;
1014	}
1015*/
1016
1017	vec2 pssm_coord;
1018	float pssm_z;
1019
1020#if defined(LIGHT_USE_PSSM) && defined(USE_SHADOW_ESM)
1021#define USE_PSSM_BLEND
1022	float pssm_blend;
1023	vec2 pssm_coord_2;
1024	float pssm_z_2;
1025	vec3 light_pssm_split_inv = 1.0/light_pssm_split;
1026	float w_inv = 1.0/gl_FragCoord.w;
1027#endif
1028
1029#ifdef LIGHT_USE_PSSM4
1030
1031
1032	if (gl_FragCoord.w > light_pssm_split.y) {
1033
1034		if (gl_FragCoord.w > light_pssm_split.x) {
1035			pssm_coord=shadow_coord.xy;
1036			pssm_z=shadow_coord.z;
1037#if defined(USE_PSSM_BLEND)
1038			pssm_coord_2=shadow_coord2.xy;
1039			pssm_z_2=shadow_coord2.z;
1040			pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv);
1041#endif
1042
1043		} else {
1044			pssm_coord=shadow_coord2.xy;
1045			pssm_z=shadow_coord2.z;
1046#if defined(USE_PSSM_BLEND)
1047			pssm_coord_2=shadow_coord3.xy;
1048			pssm_z_2=shadow_coord3.z;
1049			pssm_blend=smoothstep(light_pssm_split_inv.x,light_pssm_split_inv.y,w_inv);
1050#endif
1051
1052		}
1053	} else {
1054
1055
1056		if (gl_FragCoord.w > light_pssm_split.z) {
1057			pssm_coord=shadow_coord3.xy;
1058			pssm_z=shadow_coord3.z;
1059#if defined(USE_PSSM_BLEND)
1060			pssm_coord_2=shadow_coord4.xy;
1061			pssm_z_2=shadow_coord4.z;
1062			pssm_blend=smoothstep(light_pssm_split_inv.y,light_pssm_split_inv.z,w_inv);
1063#endif
1064
1065		} else {
1066			pssm_coord=shadow_coord4.xy;
1067			pssm_z=shadow_coord4.z;
1068#if defined(USE_PSSM_BLEND)
1069			pssm_coord_2=shadow_coord4.xy;
1070			pssm_z_2=shadow_coord4.z;
1071			pssm_blend=0.0;
1072#endif
1073
1074		}
1075	}
1076
1077#else
1078
1079	if (gl_FragCoord.w > light_pssm_split.x) {
1080		pssm_coord=shadow_coord.xy;
1081		pssm_z=shadow_coord.z;
1082#if defined(USE_PSSM_BLEND)
1083		pssm_coord_2=shadow_coord2.xy;
1084		pssm_z_2=shadow_coord2.z;
1085		pssm_blend=smoothstep(0.0,light_pssm_split_inv.x,w_inv);
1086#endif
1087
1088	} else {
1089		pssm_coord=shadow_coord2.xy;
1090		pssm_z=shadow_coord2.z;
1091#if defined(USE_PSSM_BLEND)
1092		pssm_coord_2=shadow_coord2.xy;
1093		pssm_z_2=shadow_coord2.z;
1094		pssm_blend=0.0;
1095#endif
1096
1097	}
1098
1099#endif
1100
1101	//one one sample
1102	shadow_attenuation=SAMPLE_SHADOW_TEX(pssm_coord,pssm_z);
1103#if defined(USE_PSSM_BLEND)
1104	shadow_attenuation=mix(shadow_attenuation,SAMPLE_SHADOW_TEX(pssm_coord_2,pssm_z_2),pssm_blend);
1105#endif
1106
1107
1108#endif
1109
1110#else
1111
1112	shadow_attenuation=SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
1113#endif
1114
1115	shadow_attenuation=mix(shadow_attenuation,1.0,shadow_fade);
1116	}else{
1117	shadow_attenuation=1.0;
1118	};
1119
1120#endif
1121
1122#ifdef LIGHT_TYPE_OMNI
1123
1124        vec3 splane=shadow_coord.xyz;///shadow_coord.w;
1125        float shadow_len=length(splane);
1126        splane=normalize(splane);
1127        float vofs=0.0;
1128
1129        if (splane.z>=0.0) {
1130
1131                splane.z+=1.0;
1132        } else {
1133
1134                splane.z=1.0 - splane.z;
1135                vofs=0.5;
1136        }
1137        splane.xy/=splane.z;
1138        splane.xy=splane.xy * 0.5 + 0.5;
1139	float lradius = light_attenuation.g;
1140        splane.z = shadow_len / lradius;
1141        splane.y=clamp(splane.y,0.0,1.0)*0.5+vofs;
1142
1143        shadow_attenuation=SAMPLE_SHADOW_TEX(splane.xy,splane.z);
1144#endif
1145
1146#ifdef LIGHT_TYPE_SPOT
1147
1148	shadow_attenuation=SAMPLE_SHADOW_TEX(shadow_coord.xy,shadow_coord.z);
1149#endif
1150
1151	shadow_attenuation=mix(shadow_attenuation,1.0,shadow_darkening);
1152#endif
1153
1154
1155#ifdef USE_FRAGMENT_LIGHTING
1156
1157	vec3 eye_vec = -normalize(vertex);
1158
1159#ifdef LIGHT_TYPE_DIRECTIONAL
1160
1161	vec3 light_dir = -light_direction;
1162	float attenuation = light_attenuation.r;
1163
1164
1165#endif
1166
1167#ifdef LIGHT_TYPE_OMNI
1168
1169	vec3 light_dir = light_pos-vertex;
1170	float radius = light_attenuation.g;
1171	float dist = min(length(light_dir),radius);
1172	light_dir=normalize(light_dir);
1173	float attenuation = pow( max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
1174
1175#endif
1176
1177
1178#ifdef LIGHT_TYPE_SPOT
1179
1180	vec3 light_dir = light_pos-vertex;
1181	float radius = light_attenuation.g;
1182	float dist = min(length(light_dir),radius);
1183	light_dir=normalize(light_dir);
1184	float attenuation = pow(  max(1.0 - dist/radius, 0.0), light_attenuation.b ) * light_attenuation.r;
1185	vec3 spot_dir = light_direction;
1186	float spot_cutoff=light_spot_attenuation.r;
1187	float scos = max(dot(-light_dir, spot_dir),spot_cutoff);
1188	float rim = (1.0 - scos) / (1.0 - spot_cutoff);
1189	attenuation *= 1.0 - pow( rim, light_spot_attenuation.g);
1190
1191#endif
1192
1193# if defined(LIGHT_TYPE_DIRECTIONAL) || defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
1194
1195	{
1196
1197		vec3 mdiffuse = diffuse.rgb;
1198		vec3 light;
1199
1200#if defined(USE_OUTPUT_SHADOW_COLOR)
1201		vec3 shadow_color=vec3(0.0,0.0,0.0);
1202#endif
1203
1204#if defined(USE_LIGHT_SHADER_CODE)
1205//light is written by the light shader
1206{
1207
1208LIGHT_SHADER_CODE
1209
1210}
1211#else
1212//traditional lambert + blinn
1213		float NdotL = max(0.0,dot( normal, light_dir ));
1214		vec3 half_vec = normalize(light_dir + eye_vec);
1215		float eye_light = max(dot(normal, half_vec),0.0);
1216
1217		light = light_diffuse * mdiffuse * NdotL;
1218		if (NdotL > 0.0) {
1219			light+=specular * light_specular * pow( eye_light, specular_exp );
1220		}
1221#endif
1222		diffuse.rgb = const_light_mult * ambient_light *diffuse.rgb + light * attenuation * shadow_attenuation;
1223
1224#if defined(USE_OUTPUT_SHADOW_COLOR)
1225		diffuse.rgb += light * shadow_color * attenuation * (1.0 - shadow_attenuation);
1226#endif
1227
1228#ifdef USE_FOG
1229
1230		diffuse.rgb = mix(diffuse.rgb,fog_interp.rgb,fog_interp.a);
1231
1232# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
1233		diffuse.rgb = mix(mix(vec3(0.0),diffuse.rgb,attenuation),diffuse.rgb,const_light_mult);
1234# endif
1235
1236
1237#endif
1238
1239
1240	}
1241
1242
1243# endif
1244
1245# if !defined(LIGHT_TYPE_DIRECTIONAL) && !defined(LIGHT_TYPE_OMNI) && !defined (LIGHT_TYPE_SPOT)
1246//none
1247#ifndef SHADELESS
1248	diffuse.rgb=ambient_light *diffuse.rgb;
1249#endif
1250
1251# endif
1252
1253	diffuse.rgb+=const_light_mult*emission;
1254
1255#endif
1256
1257
1258
1259
1260#ifdef USE_VERTEX_LIGHTING
1261
1262	vec3 ambient = const_light_mult*ambient_light*diffuse.rgb;
1263# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
1264//	ambient*=diffuse_interp.a; //attenuation affects ambient too
1265
1266# endif
1267
1268//	diffuse.rgb=(diffuse.rgb * diffuse_interp.rgb + specular * specular_interp)*shadow_attenuation + ambient;
1269//	diffuse.rgb+=emission * const_light_mult;
1270	diffuse.rgb=(diffuse.rgb * diffuse_interp.rgb + specular * specular_interp)*shadow_attenuation + ambient;
1271	diffuse.rgb+=emission * const_light_mult;
1272
1273#ifdef USE_FOG
1274
1275	diffuse.rgb = mix(diffuse.rgb,fog_interp.rgb,fog_interp.a);
1276
1277# if defined(LIGHT_TYPE_OMNI) || defined (LIGHT_TYPE_SPOT)
1278	diffuse.rgb = mix(mix(vec3(0.0),diffuse.rgb,diffuse_interp.a),diffuse.rgb,const_light_mult);
1279# endif
1280
1281#endif
1282
1283#endif
1284
1285
1286#if defined(ENABLE_AMBIENT_OCTREE) || defined(ENABLE_AMBIENT_LIGHTMAP) || defined(ENABLE_AMBIENT_DP_SAMPLER)
1287#if defined(ENABLE_AMBIENT_COLOR)
1288	ambientmap_color*=ambient_color;
1289#endif
1290	diffuse.rgb+=ambientmap_color;
1291#endif
1292
1293
1294#ifdef USE_SHADOW_PASS
1295
1296#ifdef USE_DEPTH_SHADOWS
1297
1298        //do nothing, depth is just written
1299#else
1300        // pack depth to rgba
1301        //highp float bias = 0.0005;
1302	highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0;//bias;
1303        highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0));
1304        comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
1305        gl_FragColor = comp;
1306
1307#endif
1308
1309#else
1310
1311
1312
1313#ifdef USE_GLOW
1314
1315	diffuse.a=glow;
1316#endif
1317
1318#ifdef USE_8BIT_HDR
1319	diffuse.rgb*=0.25;
1320#endif
1321
1322	gl_FragColor = diffuse;
1323#endif
1324}
1325
1326
1327