Jump to content

Too many uniforms for simple mesh


ozRocker
 Share

Recommended Posts

I've discovered that this playground doesn't run on iPhone 6+ https://playground.babylonjs.com/#BDG7ME#28

I get this error:

[Error] WebGL: ERROR: too many uniforms
compileShader
i (babylon.js:4:31855)
t (babylon.js:4:31705)
createShaderProgram (babylon.js:6:18019)
_prepareEffect (babylon.js:1:10164)
(anonymous function) (babylon.js:1:4037)
promiseReactionJob
[Error] BJS - [14:06:47]: Unable to compile effect:
_ErrorEnabled (babylon.js:4:8244)
_prepareEffect (babylon.js:1:10998)
(anonymous function) (babylon.js:1:4037)
promiseReactionJob
[Error] BJS - [14:06:47]: Uniforms:  world, view, viewProjection, vEyePosition, vLightsType, vAmbientColor, vDiffuseColor, vSpecularColor, vEmissiveColor, vFogInfos, vFogColor, pointSize, vDiffuseInfos, vAmbientInfos, vOpacityInfos, vReflectionInfos, vEmissiveInfos, vSpecularInfos, vBumpInfos, vLightmapInfos, vRefractionInfos, mBones, vClipPlane, diffuseMatrix, ambientMatrix, opacityMatrix, reflectionMatrix, emissiveMatrix, specularMatrix, bumpMatrix, normalMatrix, lightmapMatrix, refractionMatrix, diffuseLeftColor, diffuseRightColor, opacityParts, reflectionLeftColor, reflectionRightColor, emissiveLeftColor, emissiveRightColor, refractionLeftColor, refractionRightColor, vReflectionPosition, vReflectionSize, logarithmicDepthConstant, vTangentSpaceParams, vLightData0, vLightDiffuse0, vLightSpecular0, vLightDirection0, vLightGround0, lightMatrix0, shadowsInfo0, depthValues0, morphTargetInfluences, diffuseSampler, ambientSampler, opacitySampler, reflectionCubeSampler, reflection2DSampler, emissiveSampler, specularSampler, bumpSampler, lightmapSampler, refractionCubeSampler, refraction2DSampler, shadowSampler0
_ErrorEnabled (babylon.js:4:8244)
_prepareEffect (babylon.js:1:11041)
(anonymous function) (babylon.js:1:4037)
promiseReactionJob

I think the max number of uniforms on iPhone 6+ is 64.  My mesh has a skeleton but there's only 24 bones.  Are the 3 morph targets pushing it over the edge?  Is there something I can do to reduce uniforms?

Link to comment
Share on other sites

I know this is a shame. You can turn off GPU skeleton with mesh.computeBonesUsingShaders = false

Can you also give me the list of defines? It should be dumped in the console as well. This will help me pointing you out to some optimizations

Link to comment
Share on other sites

5 hours ago, Deltakosh said:

I know this is a shame. You can turn off GPU skeleton with mesh.computeBonesUsingShaders = false

Can you also give me the list of defines? It should be dumped in the console as well. This will help me pointing you out to some optimizations

I tried using CPU for bones but that doesn't work.  There is a problem with the morph target https://playground.babylonjs.com/#BDG7ME#30

The list of uniforms is from the error message above:

[Error] BJS - [14:06:47]: Uniforms:  world, view, viewProjection, vEyePosition, vLightsType, vAmbientColor, vDiffuseColor, vSpecularColor, vEmissiveColor, vFogInfos, vFogColor, pointSize, vDiffuseInfos, vAmbientInfos, vOpacityInfos, vReflectionInfos, vEmissiveInfos, vSpecularInfos, vBumpInfos, vLightmapInfos, vRefractionInfos, mBones, vClipPlane, diffuseMatrix, ambientMatrix, opacityMatrix, reflectionMatrix, emissiveMatrix, specularMatrix, bumpMatrix, normalMatrix, lightmapMatrix, refractionMatrix, diffuseLeftColor, diffuseRightColor, opacityParts, reflectionLeftColor, reflectionRightColor, emissiveLeftColor, emissiveRightColor, refractionLeftColor, refractionRightColor, vReflectionPosition, vReflectionSize, logarithmicDepthConstant, vTangentSpaceParams, vLightData0, vLightDiffuse0, vLightSpecular0, vLightDirection0, vLightGround0, lightMatrix0, shadowsInfo0, depthValues0, morphTargetInfluences, diffuseSampler, ambientSampler, opacitySampler, reflectionCubeSampler, reflection2DSampler, emissiveSampler, specularSampler, bumpSampler, lightmapSampler, refractionCubeSampler, refraction2DSampler, shadowSampler0
 

Is there a way to remove some of those?  I'm not using bump/normal maps for this

Link to comment
Share on other sites

Not the uniform but the defines (they filter the uniforms. The list you get defines all the uniforms unfiltered)

Morph target consumes a lot of uniforms with the "morphTargetInfluences"

Link to comment
Share on other sites

Technically, I think the minimum # of uniforms that must be supported is 128, which is what iOS uses.  Each bone matrix takes 4, so 24 * 4 - 128 leaves 32 for everything else.  One thing you might do is reduce your max simultaneous lights for the material.  In the Blender exporter, it is in the Mesh section, but it is applied on the materials for the mesh.

Link to comment
Share on other sites

10 minutes ago, JCPalmer said:

Technically, I think the minimum # of uniforms that must be supported is 128, which is what iOS uses.  Each bone matrix takes 4, so 24 * 4 - 128 leaves 32 for everything else.  One thing you might do is reduce your max simultaneous lights for the material.  In the Blender exporter, it is in the Mesh section, but it is applied on the materials for the mesh.

I don't have any lights in the .babylon file.  Its just "lights":[].  Or am I looking in the wrong place?

I only have a point light which is in the playground.

Link to comment
Share on other sites

Since you only have one light in the scene, export all meshes using this armature with max simultaneous lights set to 1.  You can also dump shadows, if you can live without them.  I do not know a mechanical way to get the number of uniforms used.  Probably would not help.  You are over.  You really need to know where to cut.

maxLights.jpg.01e366b4f1617086cfd4829dd6da8aa9.jpg

Link to comment
Share on other sites

7 hours ago, Deltakosh said:

Can you dump here the entire error message? It will contain the defines that I can use to determine which uniforms are used :)

[Error] WebGL: ERROR: too many uniforms
	compileShader
	i (babylon.js:4:31855)
	t (babylon.js:4:31705)
	createShaderProgram (babylon.js:6:18019)
	_prepareEffect (babylon.js:1:10164)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Unable to compile effect:
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:10998)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Uniforms:  world, view, viewProjection, vEyePosition, vLightsType, vAmbientColor, vDiffuseColor, vSpecularColor, vEmissiveColor, vFogInfos, vFogColor, pointSize, vDiffuseInfos, vAmbientInfos, vOpacityInfos, vReflectionInfos, vEmissiveInfos, vSpecularInfos, vBumpInfos, vLightmapInfos, vRefractionInfos, mBones, vClipPlane, diffuseMatrix, ambientMatrix, opacityMatrix, reflectionMatrix, emissiveMatrix, specularMatrix, bumpMatrix, normalMatrix, lightmapMatrix, refractionMatrix, diffuseLeftColor, diffuseRightColor, opacityParts, reflectionLeftColor, reflectionRightColor, emissiveLeftColor, emissiveRightColor, refractionLeftColor, refractionRightColor, vReflectionPosition, vReflectionSize, logarithmicDepthConstant, vTangentSpaceParams, vLightData0, vLightDiffuse0, vLightSpecular0, vLightDirection0, vLightGround0, lightMatrix0, shadowsInfo0, depthValues0, morphTargetInfluences, diffuseSampler, ambientSampler, opacitySampler, reflectionCubeSampler, reflection2DSampler, emissiveSampler, specularSampler, bumpSampler, lightmapSampler, refractionCubeSampler, refraction2DSampler, shadowSampler0
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11041)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Attributes:  position, normal, matricesIndices, matricesWeights, position0
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11121)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Vertex shader: default
1	#define DIFFUSEDIRECTUV 0
2	#define AMBIENTDIRECTUV 0
3	#define OPACITYDIRECTUV 0
4	#define EMISSIVEDIRECTUV 0
5	#define SPECULARDIRECTUV 0
6	#define BUMPDIRECTUV 0
7	#define SPECULARTERM
8	#define NORMAL
9	#define NUM_BONE_INFLUENCERS 4
10	#define BonesPerMesh 33
11	#define LIGHTMAPDIRECTUV 0
12	#define MORPHTARGETS
13	#define NUM_MORPH_INFLUENCERS 1
14	#define VIGNETTEBLENDMODEMULTIPLY
15	#define SAMPLER3DGREENDEPTH
16	#define SAMPLER3DBGRMAP
17	#define LIGHT0
18	#define POINTLIGHT0
19	
20	#define SHADER_NAME vertex:default
21	precision highp float;
22	
23	uniform mat4 viewProjection;
24	uniform mat4 view;
25	#ifdef DIFFUSE
26	uniform mat4 diffuseMatrix;
27	uniform vec2 vDiffuseInfos;
28	#endif
29	#ifdef AMBIENT
30	uniform mat4 ambientMatrix;
31	uniform vec2 vAmbientInfos;
32	#endif
33	#ifdef OPACITY
34	uniform mat4 opacityMatrix;
35	uniform vec2 vOpacityInfos;
36	#endif
37	#ifdef EMISSIVE
38	uniform vec2 vEmissiveInfos;
39	uniform mat4 emissiveMatrix;
40	#endif
41	#ifdef LIGHTMAP
42	uniform vec2 vLightmapInfos;
43	uniform mat4 lightmapMatrix;
44	#endif
45	#if defined(SPECULAR) && defined(SPECULARTERM)
46	uniform vec2 vSpecularInfos;
47	uniform mat4 specularMatrix;
48	#endif
49	#ifdef BUMP
50	uniform vec3 vBumpInfos;
51	uniform mat4 bumpMatrix;
52	#endif
53	#ifdef POINTSIZE
54	uniform float pointSize;
55	#endif
56	
57	
58	#define CUSTOM_VERTEX_BEGIN
59	attribute vec3 position;
60	#ifdef NORMAL
61	attribute vec3 normal;
62	#endif
63	#ifdef TANGENT
64	attribute vec4 tangent;
65	#endif
66	#ifdef UV1
67	attribute vec2 uv;
68	#endif
69	#ifdef UV2
70	attribute vec2 uv2;
71	#endif
72	#ifdef VERTEXCOLOR
73	attribute vec4 color;
74	#endif
75	const float PI=3.1415926535897932384626433832795;
76	const float LinearEncodePowerApprox=2.2;
77	const float GammaEncodePowerApprox=1.0/LinearEncodePowerApprox;
78	const vec3 LuminanceEncodeApprox=vec3(0.2126,0.7152,0.0722);
79	mat3 transposeMat3(mat3 inMatrix) {
80	vec3 i0=inMatrix[0];
81	vec3 i1=inMatrix[1];
82	vec3 i2=inMatrix[2];
83	mat3 outMatrix=mat3(
84	vec3(i0.x,i1.x,i2.x),
85	vec3(i0.y,i1.y,i2.y),
86	vec3(i0.z,i1.z,i2.z)
87	);
88	return outMatrix;
89	}
90	
91	mat3 inverseMat3(mat3 inMatrix) {
92	float a00=inMatrix[0][0],a01=inMatrix[0][1],a02=inMatrix[0][2];
93	float a10=inMatrix[1][0],a11=inMatrix[1][1],a12=inMatrix[1][2];
94	float a20=inMatrix[2][0],a21=inMatrix[2][1],a22=inMatrix[2][2];
95	float b01=a22*a11-a12*a21;
96	float b11=-a22*a10+a12*a20;
97	float b21=a21*a10-a11*a20;
98	float det=a00*b01+a01*b11+a02*b21;
99	return mat3(b01,(-a22*a01+a02*a21),(a12*a01-a02*a11),
100	b11,(a22*a00-a02*a20),(-a12*a00+a02*a10),
101	b21,(-a21*a00+a01*a20),(a11*a00-a01*a10))/det;
102	}
103	float computeFallOff(float value,vec2 clipSpace,float frustumEdgeFalloff)
104	{
105	float mask=smoothstep(1.0-frustumEdgeFalloff,1.0,clamp(dot(clipSpace,clipSpace),0.,1.));
106	return mix(value,1.0,mask);
107	}
108	vec3 applyEaseInOut(vec3 x){
109	return x*x*(3.0-2.0*x);
110	}
111	vec3 toLinearSpace(vec3 color)
112	{
113	return pow(color,vec3(LinearEncodePowerApprox));
114	}
115	vec3 toGammaSpace(vec3 color)
116	{
117	return pow(color,vec3(GammaEncodePowerApprox));
118	}
119	float square(float value)
120	{
121	return value*value;
122	}
123	float getLuminance(vec3 color)
124	{
125	return clamp(dot(color,LuminanceEncodeApprox),0.,1.);
126	}
127	
128	float getRand(vec2 seed) {
129	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233)))*43758.5453);
130	}
131	vec3 dither(vec2 seed,vec3 color) {
132	float rand=getRand(seed);
133	color+=mix(-0.5/255.0,0.5/255.0,rand);
134	color=max(color,0.0);
135	return color;
136	}
137	#if NUM_BONE_INFLUENCERS>0
138	uniform mat4 mBones[BonesPerMesh];
139	attribute vec4 matricesIndices;
140	attribute vec4 matricesWeights;
141	#if NUM_BONE_INFLUENCERS>4
142	attribute vec4 matricesIndicesExtra;
143	attribute vec4 matricesWeightsExtra;
144	#endif
145	#endif
146	
147	#ifdef INSTANCES
148	attribute vec4 world0;
149	attribute vec4 world1;
150	attribute vec4 world2;
151	attribute vec4 world3;
152	#else
153	uniform mat4 world;
154	#endif
155	#ifdef MAINUV1
156	varying vec2 vMainUV1;
157	#endif
158	#ifdef MAINUV2
159	varying vec2 vMainUV2;
160	#endif
161	#if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
162	varying vec2 vDiffuseUV;
163	#endif
164	#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
165	varying vec2 vAmbientUV;
166	#endif
167	#if defined(OPACITY) && OPACITYDIRECTUV == 0
168	varying vec2 vOpacityUV;
169	#endif
170	#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
171	varying vec2 vEmissiveUV;
172	#endif
173	#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
174	varying vec2 vLightmapUV;
175	#endif
176	#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
177	varying vec2 vSpecularUV;
178	#endif
179	#if defined(BUMP) && BUMPDIRECTUV == 0
180	varying vec2 vBumpUV;
181	#endif
182	
183	varying vec3 vPositionW;
184	#ifdef NORMAL
185	varying vec3 vNormalW;
186	#endif
187	#ifdef VERTEXCOLOR
188	varying vec4 vColor;
189	#endif
190	#if defined(BUMP) || defined(PARALLAX)
191	#if defined(TANGENT) && defined(NORMAL) 
192	varying mat3 vTBN;
193	#endif
194	#endif
195	
196	#ifdef CLIPPLANE
197	uniform vec4 vClipPlane;
198	varying float fClipDistance;
199	#endif
200	#ifdef FOG
201	varying vec3 vFogDistance;
202	#endif
203	#ifdef LIGHT0
204	uniform vec4 vLightData0;
205	uniform vec4 vLightDiffuse0;
206	#ifdef SPECULARTERM
207	uniform vec3 vLightSpecular0;
208	#else
209	vec3 vLightSpecular0=vec3(0.);
210	#endif
211	#ifdef SHADOW0
212	#if defined(SHADOWCUBE0)
213	uniform samplerCube shadowSampler0;
214	#else
215	varying vec4 vPositionFromLight0;
216	varying float vDepthMetric0;
217	uniform sampler2D shadowSampler0;
218	uniform mat4 lightMatrix0;
219	#endif
220	uniform vec4 shadowsInfo0;
221	uniform vec2 depthValues0;
222	#endif
223	#ifdef SPOTLIGHT0
224	uniform vec4 vLightDirection0;
225	#endif
226	#ifdef HEMILIGHT0
227	uniform vec3 vLightGround0;
228	#endif
229	#ifdef PROJECTEDLIGHTTEXTURE0
230	uniform mat4 textureProjectionMatrix0;
231	uniform sampler2D projectionLightSampler0;
232	#endif
233	#endif
234	#ifdef LIGHT1
235	uniform vec4 vLightData1;
236	uniform vec4 vLightDiffuse1;
237	#ifdef SPECULARTERM
238	uniform vec3 vLightSpecular1;
239	#else
240	vec3 vLightSpecular1=vec3(0.);
241	#endif
242	#ifdef SHADOW1
243	#if defined(SHADOWCUBE1)
244	uniform samplerCube shadowSampler1;
245	#else
246	varying vec4 vPositionFromLight1;
247	varying float vDepthMetric1;
248	uniform sampler2D shadowSampler1;
249	uniform mat4 lightMatrix1;
250	#endif
251	uniform vec4 shadowsInfo1;
252	uniform vec2 depthValues1;
253	#endif
254	#ifdef SPOTLIGHT1
255	uniform vec4 vLightDirection1;
256	#endif
257	#ifdef HEMILIGHT1
258	uniform vec3 vLightGround1;
259	#endif
260	#ifdef PROJECTEDLIGHTTEXTURE1
261	uniform mat4 textureProjectionMatrix1;
262	uniform sampler2D projectionLightSampler1;
263	#endif
264	#endif
265	#ifdef LIGHT2
266	uniform vec4 vLightData2;
267	uniform vec4 vLightDiffuse2;
268	#ifdef SPECULARTERM
269	uniform vec3 vLightSpecular2;
270	#else
271	vec3 vLightSpecular2=vec3(0.);
272	#endif
273	#ifdef SHADOW2
274	#if defined(SHADOWCUBE2)
275	uniform samplerCube shadowSampler2;
276	#else
277	varying vec4 vPositionFromLight2;
278	varying float vDepthMetric2;
279	uniform sampler2D shadowSampler2;
280	uniform mat4 lightMatrix2;
281	#endif
282	uniform vec4 shadowsInfo2;
283	uniform vec2 depthValues2;
284	#endif
285	#ifdef SPOTLIGHT2
286	uniform vec4 vLightDirection2;
287	#endif
288	#ifdef HEMILIGHT2
289	uniform vec3 vLightGround2;
290	#endif
291	#ifdef PROJECTEDLIGHTTEXTURE2
292	uniform mat4 textureProjectionMatrix2;
293	uniform sampler2D projectionLightSampler2;
294	#endif
295	#endif
296	#ifdef LIGHT3
297	uniform vec4 vLightData3;
298	uniform vec4 vLightDiffuse3;
299	#ifdef SPECULARTERM
300	uniform vec3 vLightSpecular3;
301	#else
302	vec3 vLightSpecular3=vec3(0.);
303	#endif
304	#ifdef SHADOW3
305	#if defined(SHADOWCUBE3)
306	uniform samplerCube shadowSampler3;
307	#else
308	varying vec4 vPositionFromLight3;
309	varying float vDepthMetric3;
310	uniform sampler2D shadowSampler3;
311	uniform mat4 lightMatrix3;
312	#endif
313	uniform vec4 shadowsInfo3;
314	uniform vec2 depthValues3;
315	#endif
316	#ifdef SPOTLIGHT3
317	uniform vec4 vLightDirection3;
318	#endif
319	#ifdef HEMILIGHT3
320	uniform vec3 vLightGround3;
321	#endif
322	#ifdef PROJECTEDLIGHTTEXTURE3
323	uniform mat4 textureProjectionMatrix3;
324	uniform sampler2D projectionLightSampler3;
325	#endif
326	#endif
327	
328	#ifdef MORPHTARGETS
329	uniform float morphTargetInfluences[NUM_MORPH_INFLUENCERS];
330	#endif
331	#ifdef MORPHTARGETS
332	attribute vec3 position0;
333	#ifdef MORPHTARGETS_NORMAL
334	attribute vec3 normal0;
335	#endif
336	#ifdef MORPHTARGETS_TANGENT
337	attribute vec3 tangent0;
338	#endif
339	#endif
340	
341	#ifdef REFLECTIONMAP_SKYBOX
342	varying vec3 vPositionUVW;
343	#endif
344	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
345	varying vec3 vDirectionW;
346	#endif
347	#ifdef LOGARITHMICDEPTH
348	uniform float logarithmicDepthConstant;
349	varying float vFragmentDepth;
350	#endif
351	#define CUSTOM_VERTEX_DEFINITIONS
352	void main(void) {
353	#define CUSTOM_VERTEX_MAIN_BEGIN
354	vec3 positionUpdated=position;
355	#ifdef NORMAL 
356	vec3 normalUpdated=normal;
357	#endif
358	#ifdef TANGENT
359	vec4 tangentUpdated=tangent;
360	#endif
361	#ifdef MORPHTARGETS
362	positionUpdated+=(position0-position)*morphTargetInfluences[0];
363	#ifdef MORPHTARGETS_NORMAL
364	normalUpdated+=(normal0-normal)*morphTargetInfluences[0];
365	#endif
366	#ifdef MORPHTARGETS_TANGENT
367	tangentUpdated.xyz+=(tangent0-tangent.xyz)*morphTargetInfluences[0];
368	#endif
369	#endif
370	
371	#ifdef REFLECTIONMAP_SKYBOX
372	vPositionUVW=positionUpdated;
373	#endif 
374	#define CUSTOM_VERTEX_UPDATE_POSITION
375	#define CUSTOM_VERTEX_UPDATE_NORMAL
376	#ifdef INSTANCES
377	mat4 finalWorld=mat4(world0,world1,world2,world3);
378	#else
379	mat4 finalWorld=world;
380	#endif
381	#if NUM_BONE_INFLUENCERS>0
382	mat4 influence;
383	influence=mBones[int(matricesIndices[0])]*matricesWeights[0];
384	#if NUM_BONE_INFLUENCERS>1
385	influence+=mBones[int(matricesIndices[1])]*matricesWeights[1];
386	#endif 
387	#if NUM_BONE_INFLUENCERS>2
388	influence+=mBones[int(matricesIndices[2])]*matricesWeights[2];
389	#endif 
390	#if NUM_BONE_INFLUENCERS>3
391	influence+=mBones[int(matricesIndices[3])]*matricesWeights[3];
392	#endif 
393	#if NUM_BONE_INFLUENCERS>4
394	influence+=mBones[int(matricesIndicesExtra[0])]*matricesWeightsExtra[0];
395	#endif 
396	#if NUM_BONE_INFLUENCERS>5
397	influence+=mBones[int(matricesIndicesExtra[1])]*matricesWeightsExtra[1];
398	#endif 
399	#if NUM_BONE_INFLUENCERS>6
400	influence+=mBones[int(matricesIndicesExtra[2])]*matricesWeightsExtra[2];
401	#endif 
402	#if NUM_BONE_INFLUENCERS>7
403	influence+=mBones[int(matricesIndicesExtra[3])]*matricesWeightsExtra[3];
404	#endif 
405	finalWorld=finalWorld*influence;
406	#endif
407	gl_Position=viewProjection*finalWorld*vec4(positionUpdated,1.0);
408	vec4 worldPos=finalWorld*vec4(positionUpdated,1.0);
409	vPositionW=vec3(worldPos);
410	#ifdef NORMAL
411	mat3 normalWorld=mat3(finalWorld);
412	#ifdef NONUNIFORMSCALING
413	normalWorld=transposeMat3(inverseMat3(normalWorld));
414	#endif
415	vNormalW=normalize(normalWorld*normalUpdated);
416	#endif
417	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
418	vDirectionW=normalize(vec3(finalWorld*vec4(positionUpdated,0.0)));
419	#endif
420	
421	#ifndef UV1
422	vec2 uv=vec2(0.,0.);
423	#endif
424	#ifndef UV2
425	vec2 uv2=vec2(0.,0.);
426	#endif
427	#ifdef MAINUV1
428	vMainUV1=uv;
429	#endif
430	#ifdef MAINUV2
431	vMainUV2=uv2;
432	#endif
433	#if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
434	if (vDiffuseInfos.x == 0.)
435	{
436	vDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));
437	}
438	else
439	{
440	vDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));
441	}
442	#endif
443	#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
444	if (vAmbientInfos.x == 0.)
445	{
446	vAmbientUV=vec2(ambientMatrix*vec4(uv,1.0,0.0));
447	}
448	else
449	{
450	vAmbientUV=vec2(ambientMatrix*vec4(uv2,1.0,0.0));
451	}
452	#endif
453	#if defined(OPACITY) && OPACITYDIRECTUV == 0
454	if (vOpacityInfos.x == 0.)
455	{
456	vOpacityUV=vec2(opacityMatrix*vec4(uv,1.0,0.0));
457	}
458	else
459	{
460	vOpacityUV=vec2(opacityMatrix*vec4(uv2,1.0,0.0));
461	}
462	#endif
463	#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
464	if (vEmissiveInfos.x == 0.)
465	{
466	vEmissiveUV=vec2(emissiveMatrix*vec4(uv,1.0,0.0));
467	}
468	else
469	{
470	vEmissiveUV=vec2(emissiveMatrix*vec4(uv2,1.0,0.0));
471	}
472	#endif
473	#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
474	if (vLightmapInfos.x == 0.)
475	{
476	vLightmapUV=vec2(lightmapMatrix*vec4(uv,1.0,0.0));
477	}
478	else
479	{
480	vLightmapUV=vec2(lightmapMatrix*vec4(uv2,1.0,0.0));
481	}
482	#endif
483	#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
484	if (vSpecularInfos.x == 0.)
485	{
486	vSpecularUV=vec2(specularMatrix*vec4(uv,1.0,0.0));
487	}
488	else
489	{
490	vSpecularUV=vec2(specularMatrix*vec4(uv2,1.0,0.0));
491	}
492	#endif
493	#if defined(BUMP) && BUMPDIRECTUV == 0
494	if (vBumpInfos.x == 0.)
495	{
496	vBumpUV=vec2(bumpMatrix*vec4(uv,1.0,0.0));
497	}
498	else
499	{
500	vBumpUV=vec2(bumpMatrix*vec4(uv2,1.0,0.0));
501	}
502	#endif
503	#if defined(BUMP) || defined(PARALLAX)
504	#if defined(TANGENT) && defined(NORMAL)
505	vec3 tbnNormal=normalize(normalUpdated);
506	vec3 tbnTangent=normalize(tangentUpdated.xyz);
507	vec3 tbnBitangent=cross(tbnNormal,tbnTangent)*tangentUpdated.w;
508	vTBN=mat3(finalWorld)*mat3(tbnTangent,tbnBitangent,tbnNormal);
509	#endif
510	#endif
511	#ifdef CLIPPLANE
512	fClipDistance=dot(worldPos,vClipPlane);
513	#endif
514	#ifdef FOG
515	vFogDistance=(view*worldPos).xyz;
516	#endif
517	#ifdef SHADOWS
518	#if defined(SHADOW0) && !defined(SHADOWCUBE0)
519	vPositionFromLight0=lightMatrix0*worldPos;
520	vDepthMetric0=((vPositionFromLight0.z+depthValues0.x)/(depthValues0.y));
521	#endif
522	#endif
523	#ifdef SHADOWS
524	#if defined(SHADOW1) && !defined(SHADOWCUBE1)
525	vPositionFromLight1=lightMatrix1*worldPos;
526	vDepthMetric1=((vPositionFromLight1.z+depthValues1.x)/(depthValues1.y));
527	#endif
528	#endif
529	#ifdef SHADOWS
530	#if defined(SHADOW2) && !defined(SHADOWCUBE2)
531	vPositionFromLight2=lightMatrix2*worldPos;
532	vDepthMetric2=((vPositionFromLight2.z+depthValues2.x)/(depthValues2.y));
533	#endif
534	#endif
535	#ifdef SHADOWS
536	#if defined(SHADOW3) && !defined(SHADOWCUBE3)
537	vPositionFromLight3=lightMatrix3*worldPos;
538	vDepthMetric3=((vPositionFromLight3.z+depthValues3.x)/(depthValues3.y));
539	#endif
540	#endif
541	
542	#ifdef VERTEXCOLOR
543	
544	vColor=color;
545	#endif
546	#ifdef POINTSIZE
547	gl_PointSize=pointSize;
548	#endif
549	#ifdef LOGARITHMICDEPTH
550	vFragmentDepth=1.0+gl_Position.w;
551	gl_Position.z=log2(max(0.000001,vFragmentDepth))*logarithmicDepthConstant;
552	#endif
553	#define CUSTOM_VERTEX_MAIN_END
554	}
555	
	_ErrorEnabled (babylon.js:4:8244)
	_dumpShadersSource (babylon.js:1:6720)
	_prepareEffect (babylon.js:1:11195)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Fragment shader: default
1	#define DIFFUSEDIRECTUV 0
2	#define AMBIENTDIRECTUV 0
3	#define OPACITYDIRECTUV 0
4	#define EMISSIVEDIRECTUV 0
5	#define SPECULARDIRECTUV 0
6	#define BUMPDIRECTUV 0
7	#define SPECULARTERM
8	#define NORMAL
9	#define NUM_BONE_INFLUENCERS 4
10	#define BonesPerMesh 33
11	#define LIGHTMAPDIRECTUV 0
12	#define MORPHTARGETS
13	#define NUM_MORPH_INFLUENCERS 1
14	#define VIGNETTEBLENDMODEMULTIPLY
15	#define SAMPLER3DGREENDEPTH
16	#define SAMPLER3DBGRMAP
17	#define LIGHT0
18	#define POINTLIGHT0
19	
20	#define SHADER_NAME fragment:default
21	precision highp float;
22	uniform vec4 vDiffuseColor;
23	#ifdef SPECULARTERM
24	uniform vec4 vSpecularColor;
25	#endif
26	uniform vec3 vEmissiveColor;
27	
28	#ifdef DIFFUSE
29	uniform vec2 vDiffuseInfos;
30	#endif
31	#ifdef AMBIENT
32	uniform vec2 vAmbientInfos;
33	#endif
34	#ifdef OPACITY 
35	uniform vec2 vOpacityInfos;
36	#endif
37	#ifdef EMISSIVE
38	uniform vec2 vEmissiveInfos;
39	#endif
40	#ifdef LIGHTMAP
41	uniform vec2 vLightmapInfos;
42	#endif
43	#ifdef BUMP
44	uniform vec3 vBumpInfos;
45	uniform vec2 vTangentSpaceParams;
46	#endif
47	#if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
48	uniform mat4 view;
49	#endif
50	#ifdef REFRACTION
51	uniform vec4 vRefractionInfos;
52	#ifndef REFRACTIONMAP_3D
53	uniform mat4 refractionMatrix;
54	#endif
55	#ifdef REFRACTIONFRESNEL
56	uniform vec4 refractionLeftColor;
57	uniform vec4 refractionRightColor;
58	#endif
59	#endif
60	#if defined(SPECULAR) && defined(SPECULARTERM)
61	uniform vec2 vSpecularInfos;
62	#endif
63	#ifdef DIFFUSEFRESNEL
64	uniform vec4 diffuseLeftColor;
65	uniform vec4 diffuseRightColor;
66	#endif
67	#ifdef OPACITYFRESNEL
68	uniform vec4 opacityParts;
69	#endif
70	#ifdef EMISSIVEFRESNEL
71	uniform vec4 emissiveLeftColor;
72	uniform vec4 emissiveRightColor;
73	#endif
74	
75	#ifdef REFLECTION
76	uniform vec2 vReflectionInfos;
77	#ifdef REFLECTIONMAP_SKYBOX
78	#else
79	#if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
80	uniform mat4 reflectionMatrix;
81	#endif
82	#if defined(USE_LOCAL_REFLECTIONMAP_CUBIC) && defined(REFLECTIONMAP_CUBIC)
83	uniform vec3 vReflectionPosition;
84	uniform vec3 vReflectionSize; 
85	#endif
86	#endif
87	#ifdef REFLECTIONFRESNEL
88	uniform vec4 reflectionLeftColor;
89	uniform vec4 reflectionRightColor;
90	#endif
91	#endif
92	#if defined(BUMP) || !defined(NORMAL)
93	#extension GL_OES_standard_derivatives : enable
94	#endif
95	#define CUSTOM_FRAGMENT_BEGIN
96	#ifdef LOGARITHMICDEPTH
97	#extension GL_EXT_frag_depth : enable
98	#endif
99	
100	#define RECIPROCAL_PI2 0.15915494
101	uniform vec3 vEyePosition;
102	uniform vec3 vAmbientColor;
103	
104	varying vec3 vPositionW;
105	#ifdef NORMAL
106	varying vec3 vNormalW;
107	#endif
108	#ifdef VERTEXCOLOR
109	varying vec4 vColor;
110	#endif
111	#ifdef MAINUV1
112	varying vec2 vMainUV1;
113	#endif
114	#ifdef MAINUV2
115	varying vec2 vMainUV2;
116	#endif
117	
118	const float PI=3.1415926535897932384626433832795;
119	const float LinearEncodePowerApprox=2.2;
120	const float GammaEncodePowerApprox=1.0/LinearEncodePowerApprox;
121	const vec3 LuminanceEncodeApprox=vec3(0.2126,0.7152,0.0722);
122	mat3 transposeMat3(mat3 inMatrix) {
123	vec3 i0=inMatrix[0];
124	vec3 i1=inMatrix[1];
125	vec3 i2=inMatrix[2];
126	mat3 outMatrix=mat3(
127	vec3(i0.x,i1.x,i2.x),
128	vec3(i0.y,i1.y,i2.y),
129	vec3(i0.z,i1.z,i2.z)
130	);
131	return outMatrix;
132	}
133	
134	mat3 inverseMat3(mat3 inMatrix) {
135	float a00=inMatrix[0][0],a01=inMatrix[0][1],a02=inMatrix[0][2];
136	float a10=inMatrix[1][0],a11=inMatrix[1][1],a12=inMatrix[1][2];
137	float a20=inMatrix[2][0],a21=inMatrix[2][1],a22=inMatrix[2][2];
138	float b01=a22*a11-a12*a21;
139	float b11=-a22*a10+a12*a20;
140	float b21=a21*a10-a11*a20;
141	float det=a00*b01+a01*b11+a02*b21;
142	return mat3(b01,(-a22*a01+a02*a21),(a12*a01-a02*a11),
143	b11,(a22*a00-a02*a20),(-a12*a00+a02*a10),
144	b21,(-a21*a00+a01*a20),(a11*a00-a01*a10))/det;
145	}
146	float computeFallOff(float value,vec2 clipSpace,float frustumEdgeFalloff)
147	{
148	float mask=smoothstep(1.0-frustumEdgeFalloff,1.0,clamp(dot(clipSpace,clipSpace),0.,1.));
149	return mix(value,1.0,mask);
150	}
151	vec3 applyEaseInOut(vec3 x){
152	return x*x*(3.0-2.0*x);
153	}
154	vec3 toLinearSpace(vec3 color)
155	{
156	return pow(color,vec3(LinearEncodePowerApprox));
157	}
158	vec3 toGammaSpace(vec3 color)
159	{
160	return pow(color,vec3(GammaEncodePowerApprox));
161	}
162	float square(float value)
163	{
164	return value*value;
165	}
166	float getLuminance(vec3 color)
167	{
168	return clamp(dot(color,LuminanceEncodeApprox),0.,1.);
169	}
170	
171	float getRand(vec2 seed) {
172	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233)))*43758.5453);
173	}
174	vec3 dither(vec2 seed,vec3 color) {
175	float rand=getRand(seed);
176	color+=mix(-0.5/255.0,0.5/255.0,rand);
177	color=max(color,0.0);
178	return color;
179	}
180	
181	#ifdef LIGHT0
182	uniform vec4 vLightData0;
183	uniform vec4 vLightDiffuse0;
184	#ifdef SPECULARTERM
185	uniform vec3 vLightSpecular0;
186	#else
187	vec3 vLightSpecular0=vec3(0.);
188	#endif
189	#ifdef SHADOW0
190	#if defined(SHADOWCUBE0)
191	uniform samplerCube shadowSampler0;
192	#else
193	varying vec4 vPositionFromLight0;
194	varying float vDepthMetric0;
195	uniform sampler2D shadowSampler0;
196	uniform mat4 lightMatrix0;
197	#endif
198	uniform vec4 shadowsInfo0;
199	uniform vec2 depthValues0;
200	#endif
201	#ifdef SPOTLIGHT0
202	uniform vec4 vLightDirection0;
203	#endif
204	#ifdef HEMILIGHT0
205	uniform vec3 vLightGround0;
206	#endif
207	#ifdef PROJECTEDLIGHTTEXTURE0
208	uniform mat4 textureProjectionMatrix0;
209	uniform sampler2D projectionLightSampler0;
210	#endif
211	#endif
212	#ifdef LIGHT1
213	uniform vec4 vLightData1;
214	uniform vec4 vLightDiffuse1;
215	#ifdef SPECULARTERM
216	uniform vec3 vLightSpecular1;
217	#else
218	vec3 vLightSpecular1=vec3(0.);
219	#endif
220	#ifdef SHADOW1
221	#if defined(SHADOWCUBE1)
222	uniform samplerCube shadowSampler1;
223	#else
224	varying vec4 vPositionFromLight1;
225	varying float vDepthMetric1;
226	uniform sampler2D shadowSampler1;
227	uniform mat4 lightMatrix1;
228	#endif
229	uniform vec4 shadowsInfo1;
230	uniform vec2 depthValues1;
231	#endif
232	#ifdef SPOTLIGHT1
233	uniform vec4 vLightDirection1;
234	#endif
235	#ifdef HEMILIGHT1
236	uniform vec3 vLightGround1;
237	#endif
238	#ifdef PROJECTEDLIGHTTEXTURE1
239	uniform mat4 textureProjectionMatrix1;
240	uniform sampler2D projectionLightSampler1;
241	#endif
242	#endif
243	#ifdef LIGHT2
244	uniform vec4 vLightData2;
245	uniform vec4 vLightDiffuse2;
246	#ifdef SPECULARTERM
247	uniform vec3 vLightSpecular2;
248	#else
249	vec3 vLightSpecular2=vec3(0.);
250	#endif
251	#ifdef SHADOW2
252	#if defined(SHADOWCUBE2)
253	uniform samplerCube shadowSampler2;
254	#else
255	varying vec4 vPositionFromLight2;
256	varying float vDepthMetric2;
257	uniform sampler2D shadowSampler2;
258	uniform mat4 lightMatrix2;
259	#endif
260	uniform vec4 shadowsInfo2;
261	uniform vec2 depthValues2;
262	#endif
263	#ifdef SPOTLIGHT2
264	uniform vec4 vLightDirection2;
265	#endif
266	#ifdef HEMILIGHT2
267	uniform vec3 vLightGround2;
268	#endif
269	#ifdef PROJECTEDLIGHTTEXTURE2
270	uniform mat4 textureProjectionMatrix2;
271	uniform sampler2D projectionLightSampler2;
272	#endif
273	#endif
274	#ifdef LIGHT3
275	uniform vec4 vLightData3;
276	uniform vec4 vLightDiffuse3;
277	#ifdef SPECULARTERM
278	uniform vec3 vLightSpecular3;
279	#else
280	vec3 vLightSpecular3=vec3(0.);
281	#endif
282	#ifdef SHADOW3
283	#if defined(SHADOWCUBE3)
284	uniform samplerCube shadowSampler3;
285	#else
286	varying vec4 vPositionFromLight3;
287	varying float vDepthMetric3;
288	uniform sampler2D shadowSampler3;
289	uniform mat4 lightMatrix3;
290	#endif
291	uniform vec4 shadowsInfo3;
292	uniform vec2 depthValues3;
293	#endif
294	#ifdef SPOTLIGHT3
295	uniform vec4 vLightDirection3;
296	#endif
297	#ifdef HEMILIGHT3
298	uniform vec3 vLightGround3;
299	#endif
300	#ifdef PROJECTEDLIGHTTEXTURE3
301	uniform mat4 textureProjectionMatrix3;
302	uniform sampler2D projectionLightSampler3;
303	#endif
304	#endif
305	
306	
307	struct lightingInfo
308	{
309	vec3 diffuse;
310	#ifdef SPECULARTERM
311	vec3 specular;
312	#endif
313	#ifdef NDOTL
314	float ndl;
315	#endif
316	};
317	lightingInfo computeLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,float range,float glossiness) {
318	lightingInfo result;
319	vec3 lightVectorW;
320	float attenuation=1.0;
321	if (lightData.w == 0.)
322	{
323	vec3 direction=lightData.xyz-vPositionW;
324	attenuation=max(0.,1.0-length(direction)/range);
325	lightVectorW=normalize(direction);
326	}
327	else
328	{
329	lightVectorW=normalize(-lightData.xyz);
330	}
331	
332	float ndl=max(0.,dot(vNormal,lightVectorW));
333	#ifdef NDOTL
334	result.ndl=ndl;
335	#endif
336	result.diffuse=ndl*diffuseColor*attenuation;
337	#ifdef SPECULARTERM
338	
339	vec3 angleW=normalize(viewDirectionW+lightVectorW);
340	float specComp=max(0.,dot(vNormal,angleW));
341	specComp=pow(specComp,max(1.,glossiness));
342	result.specular=specComp*specularColor*attenuation;
343	#endif
344	return result;
345	}
346	lightingInfo computeSpotLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec4 lightDirection,vec3 diffuseColor,vec3 specularColor,float range,float glossiness) {
347	lightingInfo result;
348	vec3 direction=lightData.xyz-vPositionW;
349	vec3 lightVectorW=normalize(direction);
350	float attenuation=max(0.,1.0-length(direction)/range);
351	
352	float cosAngle=max(0.,dot(lightDirection.xyz,-lightVectorW));
353	if (cosAngle>=lightDirection.w)
354	{
355	cosAngle=max(0.,pow(cosAngle,lightData.w));
356	attenuation*=cosAngle;
357	
358	float ndl=max(0.,dot(vNormal,lightVectorW));
359	#ifdef NDOTL
360	result.ndl=ndl;
361	#endif
362	result.diffuse=ndl*diffuseColor*attenuation;
363	#ifdef SPECULARTERM
364	
365	vec3 angleW=normalize(viewDirectionW+lightVectorW);
366	float specComp=max(0.,dot(vNormal,angleW));
367	specComp=pow(specComp,max(1.,glossiness));
368	result.specular=specComp*specularColor*attenuation;
369	#endif
370	return result;
371	}
372	result.diffuse=vec3(0.);
373	#ifdef SPECULARTERM
374	result.specular=vec3(0.);
375	#endif
376	#ifdef NDOTL
377	result.ndl=0.;
378	#endif
379	return result;
380	}
381	lightingInfo computeHemisphericLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,vec3 groundColor,float glossiness) {
382	lightingInfo result;
383	
384	float ndl=dot(vNormal,lightData.xyz)*0.5+0.5;
385	#ifdef NDOTL
386	result.ndl=ndl;
387	#endif
388	result.diffuse=mix(groundColor,diffuseColor,ndl);
389	#ifdef SPECULARTERM
390	
391	vec3 angleW=normalize(viewDirectionW+lightData.xyz);
392	float specComp=max(0.,dot(vNormal,angleW));
393	specComp=pow(specComp,max(1.,glossiness));
394	result.specular=specComp*specularColor;
395	#endif
396	return result;
397	}
398	vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler,mat4 textureProjectionMatrix){
399	vec4 strq=textureProjectionMatrix*vec4(vPositionW,1.0);
400	strq/=strq.w;
401	vec3 textureColor=texture2D(projectionLightSampler,strq.xy).rgb;
402	#ifdef PBR
403	textureColor=toLinearSpace(textureColor);
404	#endif
405	return textureColor;
406	}
407	#ifdef SHADOWS
408	#ifndef SHADOWFLOAT
409	float unpack(vec4 color)
410	{
411	const vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);
412	return dot(color,bit_shift);
413	}
414	#endif
415	float computeShadowCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,vec2 depthValues)
416	{
417	vec3 directionToLight=vPositionW-lightPosition;
418	float depth=length(directionToLight);
419	depth=(depth+depthValues.x)/(depthValues.y);
420	depth=clamp(depth,0.,1.0);
421	directionToLight=normalize(directionToLight);
422	directionToLight.y=-directionToLight.y;
423	#ifndef SHADOWFLOAT
424	float shadow=unpack(textureCube(shadowSampler,directionToLight));
425	#else
426	float shadow=textureCube(shadowSampler,directionToLight).x;
427	#endif
428	if (depth>shadow)
429	{
430	return darkness;
431	}
432	return 1.0;
433	}
434	float computeShadowWithPCFCube(vec3 lightPosition,samplerCube shadowSampler,float mapSize,float darkness,vec2 depthValues)
435	{
436	vec3 directionToLight=vPositionW-lightPosition;
437	float depth=length(directionToLight);
438	depth=(depth+depthValues.x)/(depthValues.y);
439	depth=clamp(depth,0.,1.0);
440	directionToLight=normalize(directionToLight);
441	directionToLight.y=-directionToLight.y;
442	float visibility=1.;
443	vec3 poissonDisk[4];
444	poissonDisk[0]=vec3(-1.0,1.0,-1.0);
445	poissonDisk[1]=vec3(1.0,-1.0,-1.0);
446	poissonDisk[2]=vec3(-1.0,-1.0,-1.0);
447	poissonDisk[3]=vec3(1.0,-1.0,1.0);
448	
449	#ifndef SHADOWFLOAT
450	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize))<depth) visibility-=0.25;
451	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize))<depth) visibility-=0.25;
452	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize))<depth) visibility-=0.25;
453	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize))<depth) visibility-=0.25;
454	#else
455	if (textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize).x<depth) visibility-=0.25;
456	if (textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize).x<depth) visibility-=0.25;
457	if (textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize).x<depth) visibility-=0.25;
458	if (textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize).x<depth) visibility-=0.25;
459	#endif
460	return min(1.0,visibility+darkness);
461	}
462	float computeShadowWithESMCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
463	{
464	vec3 directionToLight=vPositionW-lightPosition;
465	float depth=length(directionToLight);
466	depth=(depth+depthValues.x)/(depthValues.y);
467	float shadowPixelDepth=clamp(depth,0.,1.0);
468	directionToLight=normalize(directionToLight);
469	directionToLight.y=-directionToLight.y;
470	#ifndef SHADOWFLOAT
471	float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
472	#else
473	float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
474	#endif
475	float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness); 
476	return esm;
477	}
478	float computeShadowWithCloseESMCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
479	{
480	vec3 directionToLight=vPositionW-lightPosition;
481	float depth=length(directionToLight);
482	depth=(depth+depthValues.x)/(depthValues.y);
483	float shadowPixelDepth=clamp(depth,0.,1.0);
484	directionToLight=normalize(directionToLight);
485	directionToLight.y=-directionToLight.y;
486	#ifndef SHADOWFLOAT
487	float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
488	#else
489	float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
490	#endif
491	float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);
492	return esm;
493	}
494	float computeShadow(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float frustumEdgeFalloff)
495	{
496	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
497	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
498	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
499	{
500	return 1.0;
501	}
502	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
503	#ifndef SHADOWFLOAT
504	float shadow=unpack(texture2D(shadowSampler,uv));
505	#else
506	float shadow=texture2D(shadowSampler,uv).x;
507	#endif
508	if (shadowPixelDepth>shadow)
509	{
510	return computeFallOff(darkness,clipSpace.xy,frustumEdgeFalloff);
511	}
512	return 1.;
513	}
514	float computeShadowWithPCF(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float mapSize,float darkness,float frustumEdgeFalloff)
515	{
516	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
517	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
518	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
519	{
520	return 1.0;
521	}
522	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
523	float visibility=1.;
524	vec2 poissonDisk[4];
525	poissonDisk[0]=vec2(-0.94201624,-0.39906216);
526	poissonDisk[1]=vec2(0.94558609,-0.76890725);
527	poissonDisk[2]=vec2(-0.094184101,-0.92938870);
528	poissonDisk[3]=vec2(0.34495938,0.29387760);
529	
530	#ifndef SHADOWFLOAT
531	if (unpack(texture2D(shadowSampler,uv+poissonDisk[0]*mapSize))<shadowPixelDepth) visibility-=0.25;
532	if (unpack(texture2D(shadowSampler,uv+poissonDisk[1]*mapSize))<shadowPixelDepth) visibility-=0.25;
533	if (unpack(texture2D(shadowSampler,uv+poissonDisk[2]*mapSize))<shadowPixelDepth) visibility-=0.25;
534	if (unpack(texture2D(shadowSampler,uv+poissonDisk[3]*mapSize))<shadowPixelDepth) visibility-=0.25;
535	#else
536	if (texture2D(shadowSampler,uv+poissonDisk[0]*mapSize).x<shadowPixelDepth) visibility-=0.25;
537	if (texture2D(shadowSampler,uv+poissonDisk[1]*mapSize).x<shadowPixelDepth) visibility-=0.25;
538	if (texture2D(shadowSampler,uv+poissonDisk[2]*mapSize).x<shadowPixelDepth) visibility-=0.25;
539	if (texture2D(shadowSampler,uv+poissonDisk[3]*mapSize).x<shadowPixelDepth) visibility-=0.25;
540	#endif
541	return computeFallOff(min(1.0,visibility+darkness),clipSpace.xy,frustumEdgeFalloff);
542	}
543	float computeShadowWithESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
544	{
545	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
546	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
547	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
548	{
549	return 1.0;
550	}
551	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
552	#ifndef SHADOWFLOAT
553	float shadowMapSample=unpack(texture2D(shadowSampler,uv));
554	#else
555	float shadowMapSample=texture2D(shadowSampler,uv).x;
556	#endif
557	float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness);
558	return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);
559	}
560	float computeShadowWithCloseESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
561	{
562	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
563	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
564	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
565	{
566	return 1.0;
567	}
568	float shadowPixelDepth=clamp(depthMetric,0.,1.0); 
569	#ifndef SHADOWFLOAT
570	float shadowMapSample=unpack(texture2D(shadowSampler,uv));
571	#else
572	float shadowMapSample=texture2D(shadowSampler,uv).x;
573	#endif
574	float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);
575	return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);
576	}
577	#endif
578	
579	
580	#ifdef DIFFUSE
581	#if DIFFUSEDIRECTUV == 1
582	#define vDiffuseUV vMainUV1
583	#elif DIFFUSEDIRECTUV == 2
584	#define vDiffuseUV vMainUV2
585	#else
586	varying vec2 vDiffuseUV;
587	#endif
588	uniform sampler2D diffuseSampler;
589	#endif
590	#ifdef AMBIENT
591	#if AMBIENTDIRECTUV == 1
592	#define vAmbientUV vMainUV1
593	#elif AMBIENTDIRECTUV == 2
594	#define vAmbientUV vMainUV2
595	#else
596	varying vec2 vAmbientUV;
597	#endif
598	uniform sampler2D ambientSampler;
599	#endif
600	#ifdef OPACITY 
601	#if OPACITYDIRECTUV == 1
602	#define vOpacityUV vMainUV1
603	#elif OPACITYDIRECTUV == 2
604	#define vOpacityUV vMainUV2
605	#else
606	varying vec2 vOpacityUV;
607	#endif
608	uniform sampler2D opacitySampler;
609	#endif
610	#ifdef EMISSIVE
611	#if EMISSIVEDIRECTUV == 1
612	#define vEmissiveUV vMainUV1
613	#elif EMISSIVEDIRECTUV == 2
614	#define vEmissiveUV vMainUV2
615	#else
616	varying vec2 vEmissiveUV;
617	#endif
618	uniform sampler2D emissiveSampler;
619	#endif
620	#ifdef LIGHTMAP
621	#if LIGHTMAPDIRECTUV == 1
622	#define vLightmapUV vMainUV1
623	#elif LIGHTMAPDIRECTUV == 2
624	#define vLightmapUV vMainUV2
625	#else
626	varying vec2 vLightmapUV;
627	#endif
628	uniform sampler2D lightmapSampler;
629	#endif
630	#ifdef REFRACTION
631	#ifdef REFRACTIONMAP_3D
632	uniform samplerCube refractionCubeSampler;
633	#else
634	uniform sampler2D refraction2DSampler;
635	#endif
636	#endif
637	#if defined(SPECULAR) && defined(SPECULARTERM)
638	#if SPECULARDIRECTUV == 1
639	#define vSpecularUV vMainUV1
640	#elif SPECULARDIRECTUV == 2
641	#define vSpecularUV vMainUV2
642	#else
643	varying vec2 vSpecularUV;
644	#endif
645	uniform sampler2D specularSampler;
646	#endif
647	
648	#ifdef FRESNEL
649	float computeFresnelTerm(vec3 viewDirection,vec3 worldNormal,float bias,float power)
650	{
651	float fresnelTerm=pow(bias+abs(dot(viewDirection,worldNormal)),power);
652	return clamp(fresnelTerm,0.,1.);
653	}
654	#endif
655	
656	#ifdef REFLECTION
657	#ifdef REFLECTIONMAP_3D
658	uniform samplerCube reflectionCubeSampler;
659	#else
660	uniform sampler2D reflection2DSampler;
661	#endif
662	#ifdef REFLECTIONMAP_SKYBOX
663	varying vec3 vPositionUVW;
664	#else
665	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
666	varying vec3 vDirectionW;
667	#endif
668	#endif
669	#ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
670	vec3 parallaxCorrectNormal( vec3 vertexPos,vec3 origVec,vec3 cubeSize,vec3 cubePos ) {
671	
672	vec3 invOrigVec=vec3(1.0,1.0,1.0)/origVec;
673	vec3 halfSize=cubeSize*0.5;
674	vec3 intersecAtMaxPlane=(cubePos+halfSize-vertexPos)*invOrigVec;
675	vec3 intersecAtMinPlane=(cubePos-halfSize-vertexPos)*invOrigVec;
676	
677	vec3 largestIntersec=max(intersecAtMaxPlane,intersecAtMinPlane);
678	
679	float distance=min(min(largestIntersec.x,largestIntersec.y),largestIntersec.z);
680	
681	vec3 intersectPositionWS=vertexPos+origVec*distance;
682	
683	return intersectPositionWS-cubePos;
684	}
685	#endif
686	vec3 computeReflectionCoords(vec4 worldPos,vec3 worldNormal)
687	{
688	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
689	vec3 direction=vDirectionW;
690	float t=clamp(direction.y*-0.5+0.5,0.,1.0);
691	float s=atan(direction.z,direction.x)*RECIPROCAL_PI2+0.5;
692	#ifdef REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED
693	return vec3(1.0-s,t,0);
694	#else
695	return vec3(s,t,0);
696	#endif
697	#endif
698	#ifdef REFLECTIONMAP_EQUIRECTANGULAR
699	vec3 cameraToVertex=normalize(worldPos.xyz-vEyePosition.xyz);
700	vec3 r=reflect(cameraToVertex,worldNormal);
701	float t=clamp(r.y*-0.5+0.5,0.,1.0);
702	float s=atan(r.z,r.x)*RECIPROCAL_PI2+0.5;
703	return vec3(s,t,0);
704	#endif
705	#ifdef REFLECTIONMAP_SPHERICAL
706	vec3 viewDir=normalize(vec3(view*worldPos));
707	vec3 viewNormal=normalize(vec3(view*vec4(worldNormal,0.0)));
708	vec3 r=reflect(viewDir,viewNormal);
709	r.z=r.z-1.0;
710	float m=2.0*length(r);
711	return vec3(r.x/m+0.5,1.0-r.y/m-0.5,0);
712	#endif
713	#ifdef REFLECTIONMAP_PLANAR
714	vec3 viewDir=worldPos.xyz-vEyePosition.xyz;
715	vec3 coords=normalize(reflect(viewDir,worldNormal));
716	return vec3(reflectionMatrix*vec4(coords,1));
717	#endif
718	#ifdef REFLECTIONMAP_CUBIC
719	vec3 viewDir=normalize(worldPos.xyz-vEyePosition.xyz);
720	
721	vec3 coords=reflect(viewDir,worldNormal);
722	#ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
723	coords=parallaxCorrectNormal(worldPos.xyz,coords,vReflectionSize,vReflectionPosition);
724	#endif
725	coords=vec3(reflectionMatrix*vec4(coords,0));
726	#ifdef INVERTCUBICMAP
727	coords.y*=-1.0;
728	#endif
729	return coords;
730	#endif
731	#ifdef REFLECTIONMAP_PROJECTION
732	return vec3(reflectionMatrix*(view*worldPos));
733	#endif
734	#ifdef REFLECTIONMAP_SKYBOX
735	return vPositionUVW;
736	#endif
737	#ifdef REFLECTIONMAP_EXPLICIT
738	return vec3(0,0,0);
739	#endif
740	}
741	#endif
742	#ifdef EXPOSURE
743	uniform float exposureLinear;
744	#endif
745	#ifdef CONTRAST
746	uniform float contrast;
747	#endif
748	#ifdef VIGNETTE
749	uniform vec2 vInverseScreenSize;
750	uniform vec4 vignetteSettings1;
751	uniform vec4 vignetteSettings2;
752	#endif
753	#ifdef COLORCURVES
754	uniform vec4 vCameraColorCurveNegative;
755	uniform vec4 vCameraColorCurveNeutral;
756	uniform vec4 vCameraColorCurvePositive;
757	#endif
758	#ifdef COLORGRADING
759	#ifdef COLORGRADING3D
760	uniform highp sampler3D txColorTransform;
761	#else
762	uniform sampler2D txColorTransform;
763	#endif
764	uniform vec4 colorTransformSettings;
765	#endif
766	#if defined(COLORGRADING) && !defined(COLORGRADING3D)
767	
768	vec3 sampleTexture3D(sampler2D colorTransform,vec3 color,vec2 sampler3dSetting)
769	{
770	float sliceSize=2.0*sampler3dSetting.x; 
771	#ifdef SAMPLER3DGREENDEPTH
772	float sliceContinuous=(color.g-sampler3dSetting.x)*sampler3dSetting.y;
773	#else
774	float sliceContinuous=(color.b-sampler3dSetting.x)*sampler3dSetting.y;
775	#endif
776	float sliceInteger=floor(sliceContinuous);
777	
778	
779	float sliceFraction=sliceContinuous-sliceInteger;
780	#ifdef SAMPLER3DGREENDEPTH
781	vec2 sliceUV=color.rb;
782	#else
783	vec2 sliceUV=color.rg;
784	#endif
785	sliceUV.x*=sliceSize;
786	sliceUV.x+=sliceInteger*sliceSize;
787	sliceUV=clamp(sliceUV,0.,1.);
788	vec4 slice0Color=texture2D(colorTransform,sliceUV);
789	sliceUV.x+=sliceSize;
790	sliceUV=clamp(sliceUV,0.,1.);
791	vec4 slice1Color=texture2D(colorTransform,sliceUV);
792	vec3 result=mix(slice0Color.rgb,slice1Color.rgb,sliceFraction);
793	#ifdef SAMPLER3DBGRMAP
794	color.rgb=result.rgb;
795	#else
796	color.rgb=result.bgr;
797	#endif
798	return color;
799	}
800	#endif
801	vec4 applyImageProcessing(vec4 result) {
802	#ifdef EXPOSURE
803	result.rgb*=exposureLinear;
804	#endif
805	#ifdef VIGNETTE
806	
807	vec2 viewportXY=gl_FragCoord.xy*vInverseScreenSize;
808	viewportXY=viewportXY*2.0-1.0;
809	vec3 vignetteXY1=vec3(viewportXY*vignetteSettings1.xy+vignetteSettings1.zw,1.0);
810	float vignetteTerm=dot(vignetteXY1,vignetteXY1);
811	float vignette=pow(vignetteTerm,vignetteSettings2.w);
812	
813	vec3 vignetteColor=vignetteSettings2.rgb;
814	#ifdef VIGNETTEBLENDMODEMULTIPLY
815	vec3 vignetteColorMultiplier=mix(vignetteColor,vec3(1,1,1),vignette);
816	result.rgb*=vignetteColorMultiplier;
817	#endif
818	#ifdef VIGNETTEBLENDMODEOPAQUE
819	result.rgb=mix(vignetteColor,result.rgb,vignette);
820	#endif
821	#endif
822	#ifdef TONEMAPPING
823	const float tonemappingCalibration=1.590579;
824	result.rgb=1.0-exp2(-tonemappingCalibration*result.rgb);
825	#endif
826	
827	result.rgb=toGammaSpace(result.rgb);
828	result.rgb=clamp(result.rgb,0.0,1.0);
829	#ifdef CONTRAST
830	
831	vec3 resultHighContrast=applyEaseInOut(result.rgb);
832	if (contrast<1.0) {
833	
834	result.rgb=mix(vec3(0.5,0.5,0.5),result.rgb,contrast);
835	} else {
836	
837	result.rgb=mix(result.rgb,resultHighContrast,contrast-1.0);
838	}
839	#endif
840	
841	#ifdef COLORGRADING
842	vec3 colorTransformInput=result.rgb*colorTransformSettings.xxx+colorTransformSettings.yyy;
843	#ifdef COLORGRADING3D
844	vec3 colorTransformOutput=texture(txColorTransform,colorTransformInput).rgb;
845	#else
846	vec3 colorTransformOutput=sampleTexture3D(txColorTransform,colorTransformInput,colorTransformSettings.yz).rgb;
847	#endif
848	result.rgb=mix(result.rgb,colorTransformOutput,colorTransformSettings.www);
849	#endif
850	#ifdef COLORCURVES
851	
852	float luma=getLuminance(result.rgb);
853	vec2 curveMix=clamp(vec2(luma*3.0-1.5,luma*-3.0+1.5),vec2(0.0),vec2(1.0));
854	vec4 colorCurve=vCameraColorCurveNeutral+curveMix.x*vCameraColorCurvePositive-curveMix.y*vCameraColorCurveNegative;
855	result.rgb*=colorCurve.rgb;
856	result.rgb=mix(vec3(luma),result.rgb,colorCurve.a);
857	#endif
858	return result;
859	}
860	#ifdef BUMP
861	#if BUMPDIRECTUV == 1
862	#define vBumpUV vMainUV1
863	#elif BUMPDIRECTUV == 2
864	#define vBumpUV vMainUV2
865	#else
866	varying vec2 vBumpUV;
867	#endif
868	uniform sampler2D bumpSampler;
869	#if defined(TANGENT) && defined(NORMAL) 
870	varying mat3 vTBN;
871	#endif
872	#ifdef OBJECTSPACE_NORMALMAP
873	uniform mat4 normalMatrix;
874	#endif
875	
876	mat3 cotangent_frame(vec3 normal,vec3 p,vec2 uv)
877	{
878	
879	uv=gl_FrontFacing ? uv : -uv;
880	
881	vec3 dp1=dFdx(p);
882	vec3 dp2=dFdy(p);
883	vec2 duv1=dFdx(uv);
884	vec2 duv2=dFdy(uv);
885	
886	vec3 dp2perp=cross(dp2,normal);
887	vec3 dp1perp=cross(normal,dp1);
888	vec3 tangent=dp2perp*duv1.x+dp1perp*duv2.x;
889	vec3 bitangent=dp2perp*duv1.y+dp1perp*duv2.y;
890	
891	tangent*=vTangentSpaceParams.x;
892	bitangent*=vTangentSpaceParams.y;
893	
894	float invmax=inversesqrt(max(dot(tangent,tangent),dot(bitangent,bitangent)));
895	return mat3(tangent*invmax,bitangent*invmax,normal);
896	}
897	vec3 perturbNormal(mat3 cotangentFrame,vec2 uv)
898	{
899	vec3 map=texture2D(bumpSampler,uv).xyz;
900	map=map*2.0-1.0;
901	#ifdef NORMALXYSCALE
902	map=normalize(map*vec3(vBumpInfos.y,vBumpInfos.y,1.0));
903	#endif
904	return normalize(cotangentFrame*map);
905	}
906	#ifdef PARALLAX
907	const float minSamples=4.;
908	const float maxSamples=15.;
909	const int iMaxSamples=15;
910	
911	vec2 parallaxOcclusion(vec3 vViewDirCoT,vec3 vNormalCoT,vec2 texCoord,float parallaxScale) {
912	float parallaxLimit=length(vViewDirCoT.xy)/vViewDirCoT.z;
913	parallaxLimit*=parallaxScale;
914	vec2 vOffsetDir=normalize(vViewDirCoT.xy);
915	vec2 vMaxOffset=vOffsetDir*parallaxLimit;
916	float numSamples=maxSamples+(dot(vViewDirCoT,vNormalCoT)*(minSamples-maxSamples));
917	float stepSize=1.0/numSamples;
918	
919	float currRayHeight=1.0;
920	vec2 vCurrOffset=vec2(0,0);
921	vec2 vLastOffset=vec2(0,0);
922	float lastSampledHeight=1.0;
923	float currSampledHeight=1.0;
924	for (int i=0; i<iMaxSamples; i++)
925	{
926	currSampledHeight=texture2D(bumpSampler,vBumpUV+vCurrOffset).w;
927	
928	if (currSampledHeight>currRayHeight)
929	{
930	float delta1=currSampledHeight-currRayHeight;
931	float delta2=(currRayHeight+stepSize)-lastSampledHeight;
932	float ratio=delta1/(delta1+delta2);
933	vCurrOffset=(ratio)* vLastOffset+(1.0-ratio)*vCurrOffset;
934	
935	break;
936	}
937	else
938	{
939	currRayHeight-=stepSize;
940	vLastOffset=vCurrOffset;
941	vCurrOffset+=stepSize*vMaxOffset;
942	lastSampledHeight=currSampledHeight;
943	}
944	}
945	return vCurrOffset;
946	}
947	vec2 parallaxOffset(vec3 viewDir,float heightScale)
948	{
949	
950	float height=texture2D(bumpSampler,vBumpUV).w;
951	vec2 texCoordOffset=heightScale*viewDir.xy*height;
952	return -texCoordOffset;
953	}
954	#endif
955	#endif
956	#ifdef CLIPPLANE
957	varying float fClipDistance;
958	#endif
959	#ifdef LOGARITHMICDEPTH
960	uniform float logarithmicDepthConstant;
961	varying float vFragmentDepth;
962	#endif
963	#ifdef FOG
964	#define FOGMODE_NONE 0.
965	#define FOGMODE_EXP 1.
966	#define FOGMODE_EXP2 2.
967	#define FOGMODE_LINEAR 3.
968	#define E 2.71828
969	uniform vec4 vFogInfos;
970	uniform vec3 vFogColor;
971	varying vec3 vFogDistance;
972	float CalcFogFactor()
973	{
974	float fogCoeff=1.0;
975	float fogStart=vFogInfos.y;
976	float fogEnd=vFogInfos.z;
977	float fogDensity=vFogInfos.w;
978	float fogDistance=length(vFogDistance);
979	if (FOGMODE_LINEAR == vFogInfos.x)
980	{
981	fogCoeff=(fogEnd-fogDistance)/(fogEnd-fogStart);
982	}
983	else if (FOGMODE_EXP == vFogInfos.x)
984	{
985	fogCoeff=1.0/pow(E,fogDistance*fogDensity);
986	}
987	else if (FOGMODE_EXP2 == vFogInfos.x)
988	{
989	fogCoeff=1.0/pow(E,fogDistance*fogDistance*fogDensity*fogDensity);
990	}
991	return clamp(fogCoeff,0.0,1.0);
992	}
993	#endif
994	#define CUSTOM_FRAGMENT_DEFINITIONS
995	void main(void) {
996	#define CUSTOM_FRAGMENT_MAIN_BEGIN
997	#ifdef CLIPPLANE
998	if (fClipDistance>0.0)
999	{
1000	discard;
1001	}
1002	#endif
1003	vec3 viewDirectionW=normalize(vEyePosition-vPositionW);
1004	
1005	vec4 baseColor=vec4(1.,1.,1.,1.);
1006	vec3 diffuseColor=vDiffuseColor.rgb;
1007	
1008	float alpha=vDiffuseColor.a;
1009	
1010	#ifdef NORMAL
1011	vec3 normalW=normalize(vNormalW);
1012	#else
1013	vec3 normalW=normalize(-cross(dFdx(vPositionW),dFdy(vPositionW)));
1014	#endif
1015	vec2 uvOffset=vec2(0.0,0.0);
1016	#if defined(BUMP) || defined(PARALLAX)
1017	#ifdef NORMALXYSCALE
1018	float normalScale=1.0;
1019	#else 
1020	float normalScale=vBumpInfos.y;
1021	#endif
1022	#if defined(TANGENT) && defined(NORMAL)
1023	mat3 TBN=vTBN;
1024	#else
1025	mat3 TBN=cotangent_frame(normalW*normalScale,vPositionW,vBumpUV);
1026	#endif
1027	#endif
1028	#ifdef PARALLAX
1029	mat3 invTBN=transposeMat3(TBN);
1030	#ifdef PARALLAXOCCLUSION
1031	uvOffset=parallaxOcclusion(invTBN*-viewDirectionW,invTBN*normalW,vBumpUV,vBumpInfos.z);
1032	#else
1033	uvOffset=parallaxOffset(invTBN*viewDirectionW,vBumpInfos.z);
1034	#endif
1035	#endif
1036	#ifdef BUMP
1037	#ifdef OBJECTSPACE_NORMALMAP
1038	normalW=normalize(texture2D(bumpSampler,vBumpUV).xyz*2.0-1.0);
1039	normalW=normalize(mat3(normalMatrix)*normalW); 
1040	#else
1041	normalW=perturbNormal(TBN,vBumpUV+uvOffset);
1042	#endif
1043	#endif
1044	#ifdef TWOSIDEDLIGHTING
1045	normalW=gl_FrontFacing ? normalW : -normalW;
1046	#endif
1047	#ifdef DIFFUSE
1048	baseColor=texture2D(diffuseSampler,vDiffuseUV+uvOffset);
1049	#ifdef ALPHATEST
1050	if (baseColor.a<0.4)
1051	discard;
1052	#endif
1053	#ifdef ALPHAFROMDIFFUSE
1054	alpha*=baseColor.a;
1055	#endif
1056	#define CUSTOM_FRAGMENT_UPDATE_ALPHA
1057	baseColor.rgb*=vDiffuseInfos.y;
1058	#endif
1059	#ifdef DEPTHPREPASS
1060	gl_FragColor=vec4(0.,0.,0.,1.0);
1061	return;
1062	#endif
1063	#ifdef VERTEXCOLOR
1064	baseColor.rgb*=vColor.rgb;
1065	#endif
1066	#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE
1067	
1068	vec3 baseAmbientColor=vec3(1.,1.,1.);
1069	#ifdef AMBIENT
1070	baseAmbientColor=texture2D(ambientSampler,vAmbientUV+uvOffset).rgb*vAmbientInfos.y;
1071	#endif
1072	#define CUSTOM_FRAGMENT_BEFORE_LIGHTS
1073	
1074	#ifdef SPECULARTERM
1075	float glossiness=vSpecularColor.a;
1076	vec3 specularColor=vSpecularColor.rgb;
1077	#ifdef SPECULAR
1078	vec4 specularMapColor=texture2D(specularSampler,vSpecularUV+uvOffset);
1079	specularColor=specularMapColor.rgb;
1080	#ifdef GLOSSINESS
1081	glossiness=glossiness*specularMapColor.a;
1082	#endif
1083	#endif
1084	#else
1085	float glossiness=0.;
1086	#endif
1087	
1088	vec3 diffuseBase=vec3(0.,0.,0.);
1089	lightingInfo info;
1090	#ifdef SPECULARTERM
1091	vec3 specularBase=vec3(0.,0.,0.);
1092	#endif
1093	float shadow=1.;
1094	#ifdef LIGHTMAP
1095	vec3 lightmapColor=texture2D(lightmapSampler,vLightmapUV+uvOffset).rgb*vLightmapInfos.y;
1096	#endif
1097	#ifdef LIGHT0
1098	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED0) && defined(LIGHTMAPNOSPECULAR0))
1099	
1100	#else
1101	#ifdef PBR
1102	#ifdef SPOTLIGHT0
1103	info=computeSpotLighting(viewDirectionW,normalW,vLightData0,vLightDirection0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1104	#endif
1105	#ifdef HEMILIGHT0
1106	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightGround0,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1107	#endif
1108	#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
1109	info=computeLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1110	#endif
1111	#else
1112	#ifdef SPOTLIGHT0
1113	info=computeSpotLighting(viewDirectionW,normalW,vLightData0,vLightDirection0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness);
1114	#endif
1115	#ifdef HEMILIGHT0
1116	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightGround0,glossiness);
1117	#endif
1118	#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
1119	info=computeLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness);
1120	#endif
1121	#endif
1122	#ifdef PROJECTEDLIGHTTEXTURE0
1123	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler0,textureProjectionMatrix0);
1124	#endif
1125	#endif
1126	#ifdef SHADOW0
1127	#ifdef SHADOWCLOSEESM0
1128	#if defined(SHADOWCUBE0)
1129	shadow=computeShadowWithCloseESMCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,depthValues0);
1130	#else
1131	shadow=computeShadowWithCloseESM(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,shadowsInfo0.w);
1132	#endif
1133	#else
1134	#ifdef SHADOWESM0
1135	#if defined(SHADOWCUBE0)
1136	shadow=computeShadowWithESMCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,depthValues0);
1137	#else
1138	shadow=computeShadowWithESM(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,shadowsInfo0.w);
1139	#endif
1140	#else 
1141	#ifdef SHADOWPCF0
1142	#if defined(SHADOWCUBE0)
1143	shadow=computeShadowWithPCFCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.y,shadowsInfo0.x,depthValues0);
1144	#else
1145	shadow=computeShadowWithPCF(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.y,shadowsInfo0.x,shadowsInfo0.w);
1146	#endif
1147	#else
1148	#if defined(SHADOWCUBE0)
1149	shadow=computeShadowCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,depthValues0);
1150	#else
1151	shadow=computeShadow(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.w);
1152	#endif
1153	#endif
1154	#endif
1155	#endif
1156	#ifdef SHADOWONLY
1157	#ifndef SHADOWINUSE
1158	#define SHADOWINUSE
1159	#endif
1160	globalShadow+=shadow;
1161	shadowLightCount+=1.0;
1162	#endif
1163	#else
1164	shadow=1.;
1165	#endif
1166	#ifndef SHADOWONLY
1167	#ifdef CUSTOMUSERLIGHTING
1168	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1169	#ifdef SPECULARTERM
1170	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1171	#endif
1172	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED0)
1173	diffuseBase+=lightmapColor*shadow;
1174	#ifdef SPECULARTERM
1175	#ifndef LIGHTMAPNOSPECULAR0
1176	specularBase+=info.specular*shadow*lightmapColor;
1177	#endif
1178	#endif
1179	#else
1180	diffuseBase+=info.diffuse*shadow;
1181	#ifdef SPECULARTERM
1182	specularBase+=info.specular*shadow;
1183	#endif
1184	#endif
1185	#endif
1186	#endif
1187	#ifdef LIGHT1
1188	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED1) && defined(LIGHTMAPNOSPECULAR1))
1189	
1190	#else
1191	#ifdef PBR
1192	#ifdef SPOTLIGHT1
1193	info=computeSpotLighting(viewDirectionW,normalW,vLightData1,vLightDirection1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1194	#endif
1195	#ifdef HEMILIGHT1
1196	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightGround1,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1197	#endif
1198	#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
1199	info=computeLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1200	#endif
1201	#else
1202	#ifdef SPOTLIGHT1
1203	info=computeSpotLighting(viewDirectionW,normalW,vLightData1,vLightDirection1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness);
1204	#endif
1205	#ifdef HEMILIGHT1
1206	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightGround1,glossiness);
1207	#endif
1208	#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
1209	info=computeLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness);
1210	#endif
1211	#endif
1212	#ifdef PROJECTEDLIGHTTEXTURE1
1213	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler1,textureProjectionMatrix1);
1214	#endif
1215	#endif
1216	#ifdef SHADOW1
1217	#ifdef SHADOWCLOSEESM1
1218	#if defined(SHADOWCUBE1)
1219	shadow=computeShadowWithCloseESMCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,depthValues1);
1220	#else
1221	shadow=computeShadowWithCloseESM(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,shadowsInfo1.w);
1222	#endif
1223	#else
1224	#ifdef SHADOWESM1
1225	#if defined(SHADOWCUBE1)
1226	shadow=computeShadowWithESMCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,depthValues1);
1227	#else
1228	shadow=computeShadowWithESM(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,shadowsInfo1.w);
1229	#endif
1230	#else 
1231	#ifdef SHADOWPCF1
1232	#if defined(SHADOWCUBE1)
1233	shadow=computeShadowWithPCFCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.y,shadowsInfo1.x,depthValues1);
1234	#else
1235	shadow=computeShadowWithPCF(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.y,shadowsInfo1.x,shadowsInfo1.w);
1236	#endif
1237	#else
1238	#if defined(SHADOWCUBE1)
1239	shadow=computeShadowCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,depthValues1);
1240	#else
1241	shadow=computeShadow(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.w);
1242	#endif
1243	#endif
1244	#endif
1245	#endif
1246	#ifdef SHADOWONLY
1247	#ifndef SHADOWINUSE
1248	#define SHADOWINUSE
1249	#endif
1250	globalShadow+=shadow;
1251	shadowLightCount+=1.0;
1252	#endif
1253	#else
1254	shadow=1.;
1255	#endif
1256	#ifndef SHADOWONLY
1257	#ifdef CUSTOMUSERLIGHTING
1258	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1259	#ifdef SPECULARTERM
1260	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1261	#endif
1262	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED1)
1263	diffuseBase+=lightmapColor*shadow;
1264	#ifdef SPECULARTERM
1265	#ifndef LIGHTMAPNOSPECULAR1
1266	specularBase+=info.specular*shadow*lightmapColor;
1267	#endif
1268	#endif
1269	#else
1270	diffuseBase+=info.diffuse*shadow;
1271	#ifdef SPECULARTERM
1272	specularBase+=info.specular*shadow;
1273	#endif
1274	#endif
1275	#endif
1276	#endif
1277	#ifdef LIGHT2
1278	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED2) && defined(LIGHTMAPNOSPECULAR2))
1279	
1280	#else
1281	#ifdef PBR
1282	#ifdef SPOTLIGHT2
1283	info=computeSpotLighting(viewDirectionW,normalW,vLightData2,vLightDirection2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1284	#endif
1285	#ifdef HEMILIGHT2
1286	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightGround2,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1287	#endif
1288	#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
1289	info=computeLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1290	#endif
1291	#else
1292	#ifdef SPOTLIGHT2
1293	info=computeSpotLighting(viewDirectionW,normalW,vLightData2,vLightDirection2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness);
1294	#endif
1295	#ifdef HEMILIGHT2
1296	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightGround2,glossiness);
1297	#endif
1298	#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
1299	info=computeLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness);
1300	#endif
1301	#endif
1302	#ifdef PROJECTEDLIGHTTEXTURE2
1303	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler2,textureProjectionMatrix2);
1304	#endif
1305	#endif
1306	#ifdef SHADOW2
1307	#ifdef SHADOWCLOSEESM2
1308	#if defined(SHADOWCUBE2)
1309	shadow=computeShadowWithCloseESMCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,depthValues2);
1310	#else
1311	shadow=computeShadowWithCloseESM(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,shadowsInfo2.w);
1312	#endif
1313	#else
1314	#ifdef SHADOWESM2
1315	#if defined(SHADOWCUBE2)
1316	shadow=computeShadowWithESMCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,depthValues2);
1317	#else
1318	shadow=computeShadowWithESM(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,shadowsInfo2.w);
1319	#endif
1320	#else 
1321	#ifdef SHADOWPCF2
1322	#if defined(SHADOWCUBE2)
1323	shadow=computeShadowWithPCFCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.y,shadowsInfo2.x,depthValues2);
1324	#else
1325	shadow=computeShadowWithPCF(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.y,shadowsInfo2.x,shadowsInfo2.w);
1326	#endif
1327	#else
1328	#if defined(SHADOWCUBE2)
1329	shadow=computeShadowCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,depthValues2);
1330	#else
1331	shadow=computeShadow(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.w);
1332	#endif
1333	#endif
1334	#endif
1335	#endif
1336	#ifdef SHADOWONLY
1337	#ifndef SHADOWINUSE
1338	#define SHADOWINUSE
1339	#endif
1340	globalShadow+=shadow;
1341	shadowLightCount+=1.0;
1342	#endif
1343	#else
1344	shadow=1.;
1345	#endif
1346	#ifndef SHADOWONLY
1347	#ifdef CUSTOMUSERLIGHTING
1348	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1349	#ifdef SPECULARTERM
1350	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1351	#endif
1352	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED2)
1353	diffuseBase+=lightmapColor*shadow;
1354	#ifdef SPECULARTERM
1355	#ifndef LIGHTMAPNOSPECULAR2
1356	specularBase+=info.specular*shadow*lightmapColor;
1357	#endif
1358	#endif
1359	#else
1360	diffuseBase+=info.diffuse*shadow;
1361	#ifdef SPECULARTERM
1362	specularBase+=info.specular*shadow;
1363	#endif
1364	#endif
1365	#endif
1366	#endif
1367	#ifdef LIGHT3
1368	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED3) && defined(LIGHTMAPNOSPECULAR3))
1369	
1370	#else
1371	#ifdef PBR
1372	#ifdef SPOTLIGHT3
1373	info=computeSpotLighting(viewDirectionW,normalW,vLightData3,vLightDirection3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1374	#endif
1375	#ifdef HEMILIGHT3
1376	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightGround3,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1377	#endif
1378	#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
1379	info=computeLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1380	#endif
1381	#else
1382	#ifdef SPOTLIGHT3
1383	info=computeSpotLighting(viewDirectionW,normalW,vLightData3,vLightDirection3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness);
1384	#endif
1385	#ifdef HEMILIGHT3
1386	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightGround3,glossiness);
1387	#endif
1388	#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
1389	info=computeLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness);
1390	#endif
1391	#endif
1392	#ifdef PROJECTEDLIGHTTEXTURE3
1393	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler3,textureProjectionMatrix3);
1394	#endif
1395	#endif
1396	#ifdef SHADOW3
1397	#ifdef SHADOWCLOSEESM3
1398	#if defined(SHADOWCUBE3)
1399	shadow=computeShadowWithCloseESMCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,depthValues3);
1400	#else
1401	shadow=computeShadowWithCloseESM(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,shadowsInfo3.w);
1402	#endif
1403	#else
1404	#ifdef SHADOWESM3
1405	#if defined(SHADOWCUBE3)
1406	shadow=computeShadowWithESMCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,depthValues3);
1407	#else
1408	shadow=computeShadowWithESM(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,shadowsInfo3.w);
1409	#endif
1410	#else 
1411	#ifdef SHADOWPCF3
1412	#if defined(SHADOWCUBE3)
1413	shadow=computeShadowWithPCFCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.y,shadowsInfo3.x,depthValues3);
1414	#else
1415	shadow=computeShadowWithPCF(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.y,shadowsInfo3.x,shadowsInfo3.w);
1416	#endif
1417	#else
1418	#if defined(SHADOWCUBE3)
1419	shadow=computeShadowCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,depthValues3);
1420	#else
1421	shadow=computeShadow(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.w);
1422	#endif
1423	#endif
1424	#endif
1425	#endif
1426	#ifdef SHADOWONLY
1427	#ifndef SHADOWINUSE
1428	#define SHADOWINUSE
1429	#endif
1430	globalShadow+=shadow;
1431	shadowLightCount+=1.0;
1432	#endif
1433	#else
1434	shadow=1.;
1435	#endif
1436	#ifndef SHADOWONLY
1437	#ifdef CUSTOMUSERLIGHTING
1438	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1439	#ifdef SPECULARTERM
1440	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1441	#endif
1442	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED3)
1443	diffuseBase+=lightmapColor*shadow;
1444	#ifdef SPECULARTERM
1445	#ifndef LIGHTMAPNOSPECULAR3
1446	specularBase+=info.specular*shadow*lightmapColor;
1447	#endif
1448	#endif
1449	#else
1450	diffuseBase+=info.diffuse*shadow;
1451	#ifdef SPECULARTERM
1452	specularBase+=info.specular*shadow;
1453	#endif
1454	#endif
1455	#endif
1456	#endif
1457	
1458	
1459	vec3 refractionColor=vec3(0.,0.,0.);
1460	#ifdef REFRACTION
1461	vec3 refractionVector=normalize(refract(-viewDirectionW,normalW,vRefractionInfos.y));
1462	#ifdef REFRACTIONMAP_3D
1463	refractionVector.y=refractionVector.y*vRefractionInfos.w;
1464	if (dot(refractionVector,viewDirectionW)<1.0)
1465	{
1466	refractionColor=textureCube(refractionCubeSampler,refractionVector).rgb*vRefractionInfos.x;
1467	}
1468	#else
1469	vec3 vRefractionUVW=vec3(refractionMatrix*(view*vec4(vPositionW+refractionVector*vRefractionInfos.z,1.0)));
1470	vec2 refractionCoords=vRefractionUVW.xy/vRefractionUVW.z;
1471	refractionCoords.y=1.0-refractionCoords.y;
1472	refractionColor=texture2D(refraction2DSampler,refractionCoords).rgb*vRefractionInfos.x;
1473	#endif
1474	#endif
1475	
1476	vec3 reflectionColor=vec3(0.,0.,0.);
1477	#ifdef REFLECTION
1478	vec3 vReflectionUVW=computeReflectionCoords(vec4(vPositionW,1.0),normalW);
1479	#ifdef REFLECTIONMAP_3D
1480	#ifdef ROUGHNESS
1481	float bias=vReflectionInfos.y;
1482	#ifdef SPECULARTERM
1483	#ifdef SPECULAR
1484	#ifdef GLOSSINESS
1485	bias*=(1.0-specularMapColor.a);
1486	#endif
1487	#endif
1488	#endif
1489	reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW,bias).rgb*vReflectionInfos.x;
1490	#else
1491	reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW).rgb*vReflectionInfos.x;
1492	#endif
1493	#else
1494	vec2 coords=vReflectionUVW.xy;
1495	#ifdef REFLECTIONMAP_PROJECTION
1496	coords/=vReflectionUVW.z;
1497	#endif
1498	coords.y=1.0-coords.y;
1499	reflectionColor=texture2D(reflection2DSampler,coords).rgb*vReflectionInfos.x;
1500	#endif
1501	#ifdef REFLECTIONFRESNEL
1502	float reflectionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,reflectionRightColor.a,reflectionLeftColor.a);
1503	#ifdef REFLECTIONFRESNELFROMSPECULAR
1504	#ifdef SPECULARTERM
1505	reflectionColor*=specularColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1506	#else
1507	reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1508	#endif
1509	#else
1510	reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1511	#endif
1512	#endif
1513	#endif
1514	#ifdef REFRACTIONFRESNEL
1515	float refractionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,refractionRightColor.a,refractionLeftColor.a);
1516	refractionColor*=refractionLeftColor.rgb*(1.0-refractionFresnelTerm)+refractionFresnelTerm*refractionRightColor.rgb;
1517	#endif
1518	#ifdef OPACITY
1519	vec4 opacityMap=texture2D(opacitySampler,vOpacityUV+uvOffset);
1520	#ifdef OPACITYRGB
1521	opacityMap.rgb=opacityMap.rgb*vec3(0.3,0.59,0.11);
1522	alpha*=(opacityMap.x+opacityMap.y+opacityMap.z)* vOpacityInfos.y;
1523	#else
1524	alpha*=opacityMap.a*vOpacityInfos.y;
1525	#endif
1526	#endif
1527	#ifdef VERTEXALPHA
1528	alpha*=vColor.a;
1529	#endif
1530	#ifdef OPACITYFRESNEL
1531	float opacityFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,opacityParts.z,opacityParts.w);
1532	alpha+=opacityParts.x*(1.0-opacityFresnelTerm)+opacityFresnelTerm*opacityParts.y;
1533	#endif
1534	
1535	vec3 emissiveColor=vEmissiveColor;
1536	#ifdef EMISSIVE
1537	emissiveColor+=texture2D(emissiveSampler,vEmissiveUV+uvOffset).rgb*vEmissiveInfos.y;
1538	#endif
1539	#ifdef EMISSIVEFRESNEL
1540	float emissiveFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,emissiveRightColor.a,emissiveLeftColor.a);
1541	emissiveColor*=emissiveLeftColor.rgb*(1.0-emissiveFresnelTerm)+emissiveFresnelTerm*emissiveRightColor.rgb;
1542	#endif
1543	
1544	#ifdef DIFFUSEFRESNEL
1545	float diffuseFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,diffuseRightColor.a,diffuseLeftColor.a);
1546	diffuseBase*=diffuseLeftColor.rgb*(1.0-diffuseFresnelTerm)+diffuseFresnelTerm*diffuseRightColor.rgb;
1547	#endif
1548	
1549	#ifdef EMISSIVEASILLUMINATION
1550	vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1551	#else
1552	#ifdef LINKEMISSIVEWITHDIFFUSE
1553	vec3 finalDiffuse=clamp((diffuseBase+emissiveColor)*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1554	#else
1555	vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+emissiveColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1556	#endif
1557	#endif
1558	#ifdef SPECULARTERM
1559	vec3 finalSpecular=specularBase*specularColor;
1560	#ifdef SPECULAROVERALPHA
1561	alpha=clamp(alpha+dot(finalSpecular,vec3(0.3,0.59,0.11)),0.,1.);
1562	#endif
1563	#else
1564	vec3 finalSpecular=vec3(0.0);
1565	#endif
1566	#ifdef REFLECTIONOVERALPHA
1567	alpha=clamp(alpha+dot(reflectionColor,vec3(0.3,0.59,0.11)),0.,1.);
1568	#endif
1569	
1570	#ifdef EMISSIVEASILLUMINATION
1571	vec4 color=vec4(clamp(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+emissiveColor+refractionColor,0.0,1.0),alpha);
1572	#else
1573	vec4 color=vec4(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+refractionColor,alpha);
1574	#endif
1575	
1576	#ifdef LIGHTMAP
1577	#ifndef LIGHTMAPEXCLUDED
1578	#ifdef USELIGHTMAPASSHADOWMAP
1579	color.rgb*=lightmapColor;
1580	#else
1581	color.rgb+=lightmapColor;
1582	#endif
1583	#endif
1584	#endif
1585	#define CUSTOM_FRAGMENT_BEFORE_FOG
1586	color.rgb=max(color.rgb,0.);
1587	#ifdef LOGARITHMICDEPTH
1588	gl_FragDepthEXT=log2(vFragmentDepth)*logarithmicDepthConstant*0.5;
1589	#endif
1590	#ifdef FOG
1591	float fog=CalcFogFactor();
1592	color.rgb=fog*color.rgb+(1.0-fog)*vFogColor;
1593	#endif
1594	
1595	
1596	#ifdef IMAGEPROCESSINGPOSTPROCESS
1597	color.rgb=toLinearSpace(color.rgb);
1598	#else
1599	#ifdef IMAGEPROCESSING
1600	color.rgb=toLinearSpace(color.rgb);
1601	color=applyImageProcessing(color);
1602	#endif
1603	#endif
1604	#ifdef PREMULTIPLYALPHA
1605	
1606	color.rgb*=color.a;
1607	#endif
1608	#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
1609	gl_FragColor=color;
1610	}
1611	
	_ErrorEnabled (babylon.js:4:8244)
	_dumpShadersSource (babylon.js:1:6765)
	_prepareEffect (babylon.js:1:11195)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Error: ERROR: too many uniforms

	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11260)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Trying next fallback.
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11469)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] WebGL: ERROR: too many uniforms
	compileShader
	i (babylon.js:4:31855)
	t (babylon.js:4:31705)
	createShaderProgram (babylon.js:6:18019)
	_prepareEffect (babylon.js:1:10164)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Unable to compile effect:
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:10998)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Uniforms:  world, view, viewProjection, vEyePosition, vLightsType, vAmbientColor, vDiffuseColor, vSpecularColor, vEmissiveColor, vFogInfos, vFogColor, pointSize, vDiffuseInfos, vAmbientInfos, vOpacityInfos, vReflectionInfos, vEmissiveInfos, vSpecularInfos, vBumpInfos, vLightmapInfos, vRefractionInfos, mBones, vClipPlane, diffuseMatrix, ambientMatrix, opacityMatrix, reflectionMatrix, emissiveMatrix, specularMatrix, bumpMatrix, normalMatrix, lightmapMatrix, refractionMatrix, diffuseLeftColor, diffuseRightColor, opacityParts, reflectionLeftColor, reflectionRightColor, emissiveLeftColor, emissiveRightColor, refractionLeftColor, refractionRightColor, vReflectionPosition, vReflectionSize, logarithmicDepthConstant, vTangentSpaceParams, vLightData0, vLightDiffuse0, vLightSpecular0, vLightDirection0, vLightGround0, lightMatrix0, shadowsInfo0, depthValues0, diffuseSampler, ambientSampler, opacitySampler, reflectionCubeSampler, reflection2DSampler, emissiveSampler, specularSampler, bumpSampler, lightmapSampler, refractionCubeSampler, refraction2DSampler, shadowSampler0
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11041)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Attributes:  position, normal, matricesIndices, matricesWeights, matricesIndicesExtra, matricesWeightsExtra
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11121)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Vertex shader: default
1	#define DIFFUSEDIRECTUV 0
2	#define AMBIENTDIRECTUV 0
3	#define OPACITYDIRECTUV 0
4	#define EMISSIVEDIRECTUV 0
5	#define SPECULARDIRECTUV 0
6	#define BUMPDIRECTUV 0
7	#define SPECULARTERM
8	#define NORMAL
9	#define NUM_BONE_INFLUENCERS 8
10	#define BonesPerMesh 33
11	#define LIGHTMAPDIRECTUV 0
12	#define NUM_MORPH_INFLUENCERS 0
13	#define VIGNETTEBLENDMODEMULTIPLY
14	#define SAMPLER3DGREENDEPTH
15	#define SAMPLER3DBGRMAP
16	#define LIGHT0
17	#define POINTLIGHT0
18	
19	#define SHADER_NAME vertex:default
20	precision highp float;
21	
22	uniform mat4 viewProjection;
23	uniform mat4 view;
24	#ifdef DIFFUSE
25	uniform mat4 diffuseMatrix;
26	uniform vec2 vDiffuseInfos;
27	#endif
28	#ifdef AMBIENT
29	uniform mat4 ambientMatrix;
30	uniform vec2 vAmbientInfos;
31	#endif
32	#ifdef OPACITY
33	uniform mat4 opacityMatrix;
34	uniform vec2 vOpacityInfos;
35	#endif
36	#ifdef EMISSIVE
37	uniform vec2 vEmissiveInfos;
38	uniform mat4 emissiveMatrix;
39	#endif
40	#ifdef LIGHTMAP
41	uniform vec2 vLightmapInfos;
42	uniform mat4 lightmapMatrix;
43	#endif
44	#if defined(SPECULAR) && defined(SPECULARTERM)
45	uniform vec2 vSpecularInfos;
46	uniform mat4 specularMatrix;
47	#endif
48	#ifdef BUMP
49	uniform vec3 vBumpInfos;
50	uniform mat4 bumpMatrix;
51	#endif
52	#ifdef POINTSIZE
53	uniform float pointSize;
54	#endif
55	
56	
57	#define CUSTOM_VERTEX_BEGIN
58	attribute vec3 position;
59	#ifdef NORMAL
60	attribute vec3 normal;
61	#endif
62	#ifdef TANGENT
63	attribute vec4 tangent;
64	#endif
65	#ifdef UV1
66	attribute vec2 uv;
67	#endif
68	#ifdef UV2
69	attribute vec2 uv2;
70	#endif
71	#ifdef VERTEXCOLOR
72	attribute vec4 color;
73	#endif
74	const float PI=3.1415926535897932384626433832795;
75	const float LinearEncodePowerApprox=2.2;
76	const float GammaEncodePowerApprox=1.0/LinearEncodePowerApprox;
77	const vec3 LuminanceEncodeApprox=vec3(0.2126,0.7152,0.0722);
78	mat3 transposeMat3(mat3 inMatrix) {
79	vec3 i0=inMatrix[0];
80	vec3 i1=inMatrix[1];
81	vec3 i2=inMatrix[2];
82	mat3 outMatrix=mat3(
83	vec3(i0.x,i1.x,i2.x),
84	vec3(i0.y,i1.y,i2.y),
85	vec3(i0.z,i1.z,i2.z)
86	);
87	return outMatrix;
88	}
89	
90	mat3 inverseMat3(mat3 inMatrix) {
91	float a00=inMatrix[0][0],a01=inMatrix[0][1],a02=inMatrix[0][2];
92	float a10=inMatrix[1][0],a11=inMatrix[1][1],a12=inMatrix[1][2];
93	float a20=inMatrix[2][0],a21=inMatrix[2][1],a22=inMatrix[2][2];
94	float b01=a22*a11-a12*a21;
95	float b11=-a22*a10+a12*a20;
96	float b21=a21*a10-a11*a20;
97	float det=a00*b01+a01*b11+a02*b21;
98	return mat3(b01,(-a22*a01+a02*a21),(a12*a01-a02*a11),
99	b11,(a22*a00-a02*a20),(-a12*a00+a02*a10),
100	b21,(-a21*a00+a01*a20),(a11*a00-a01*a10))/det;
101	}
102	float computeFallOff(float value,vec2 clipSpace,float frustumEdgeFalloff)
103	{
104	float mask=smoothstep(1.0-frustumEdgeFalloff,1.0,clamp(dot(clipSpace,clipSpace),0.,1.));
105	return mix(value,1.0,mask);
106	}
107	vec3 applyEaseInOut(vec3 x){
108	return x*x*(3.0-2.0*x);
109	}
110	vec3 toLinearSpace(vec3 color)
111	{
112	return pow(color,vec3(LinearEncodePowerApprox));
113	}
114	vec3 toGammaSpace(vec3 color)
115	{
116	return pow(color,vec3(GammaEncodePowerApprox));
117	}
118	float square(float value)
119	{
120	return value*value;
121	}
122	float getLuminance(vec3 color)
123	{
124	return clamp(dot(color,LuminanceEncodeApprox),0.,1.);
125	}
126	
127	float getRand(vec2 seed) {
128	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233)))*43758.5453);
129	}
130	vec3 dither(vec2 seed,vec3 color) {
131	float rand=getRand(seed);
132	color+=mix(-0.5/255.0,0.5/255.0,rand);
133	color=max(color,0.0);
134	return color;
135	}
136	#if NUM_BONE_INFLUENCERS>0
137	uniform mat4 mBones[BonesPerMesh];
138	attribute vec4 matricesIndices;
139	attribute vec4 matricesWeights;
140	#if NUM_BONE_INFLUENCERS>4
141	attribute vec4 matricesIndicesExtra;
142	attribute vec4 matricesWeightsExtra;
143	#endif
144	#endif
145	
146	#ifdef INSTANCES
147	attribute vec4 world0;
148	attribute vec4 world1;
149	attribute vec4 world2;
150	attribute vec4 world3;
151	#else
152	uniform mat4 world;
153	#endif
154	#ifdef MAINUV1
155	varying vec2 vMainUV1;
156	#endif
157	#ifdef MAINUV2
158	varying vec2 vMainUV2;
159	#endif
160	#if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
161	varying vec2 vDiffuseUV;
162	#endif
163	#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
164	varying vec2 vAmbientUV;
165	#endif
166	#if defined(OPACITY) && OPACITYDIRECTUV == 0
167	varying vec2 vOpacityUV;
168	#endif
169	#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
170	varying vec2 vEmissiveUV;
171	#endif
172	#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
173	varying vec2 vLightmapUV;
174	#endif
175	#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
176	varying vec2 vSpecularUV;
177	#endif
178	#if defined(BUMP) && BUMPDIRECTUV == 0
179	varying vec2 vBumpUV;
180	#endif
181	
182	varying vec3 vPositionW;
183	#ifdef NORMAL
184	varying vec3 vNormalW;
185	#endif
186	#ifdef VERTEXCOLOR
187	varying vec4 vColor;
188	#endif
189	#if defined(BUMP) || defined(PARALLAX)
190	#if defined(TANGENT) && defined(NORMAL) 
191	varying mat3 vTBN;
192	#endif
193	#endif
194	
195	#ifdef CLIPPLANE
196	uniform vec4 vClipPlane;
197	varying float fClipDistance;
198	#endif
199	#ifdef FOG
200	varying vec3 vFogDistance;
201	#endif
202	#ifdef LIGHT0
203	uniform vec4 vLightData0;
204	uniform vec4 vLightDiffuse0;
205	#ifdef SPECULARTERM
206	uniform vec3 vLightSpecular0;
207	#else
208	vec3 vLightSpecular0=vec3(0.);
209	#endif
210	#ifdef SHADOW0
211	#if defined(SHADOWCUBE0)
212	uniform samplerCube shadowSampler0;
213	#else
214	varying vec4 vPositionFromLight0;
215	varying float vDepthMetric0;
216	uniform sampler2D shadowSampler0;
217	uniform mat4 lightMatrix0;
218	#endif
219	uniform vec4 shadowsInfo0;
220	uniform vec2 depthValues0;
221	#endif
222	#ifdef SPOTLIGHT0
223	uniform vec4 vLightDirection0;
224	#endif
225	#ifdef HEMILIGHT0
226	uniform vec3 vLightGround0;
227	#endif
228	#ifdef PROJECTEDLIGHTTEXTURE0
229	uniform mat4 textureProjectionMatrix0;
230	uniform sampler2D projectionLightSampler0;
231	#endif
232	#endif
233	#ifdef LIGHT1
234	uniform vec4 vLightData1;
235	uniform vec4 vLightDiffuse1;
236	#ifdef SPECULARTERM
237	uniform vec3 vLightSpecular1;
238	#else
239	vec3 vLightSpecular1=vec3(0.);
240	#endif
241	#ifdef SHADOW1
242	#if defined(SHADOWCUBE1)
243	uniform samplerCube shadowSampler1;
244	#else
245	varying vec4 vPositionFromLight1;
246	varying float vDepthMetric1;
247	uniform sampler2D shadowSampler1;
248	uniform mat4 lightMatrix1;
249	#endif
250	uniform vec4 shadowsInfo1;
251	uniform vec2 depthValues1;
252	#endif
253	#ifdef SPOTLIGHT1
254	uniform vec4 vLightDirection1;
255	#endif
256	#ifdef HEMILIGHT1
257	uniform vec3 vLightGround1;
258	#endif
259	#ifdef PROJECTEDLIGHTTEXTURE1
260	uniform mat4 textureProjectionMatrix1;
261	uniform sampler2D projectionLightSampler1;
262	#endif
263	#endif
264	#ifdef LIGHT2
265	uniform vec4 vLightData2;
266	uniform vec4 vLightDiffuse2;
267	#ifdef SPECULARTERM
268	uniform vec3 vLightSpecular2;
269	#else
270	vec3 vLightSpecular2=vec3(0.);
271	#endif
272	#ifdef SHADOW2
273	#if defined(SHADOWCUBE2)
274	uniform samplerCube shadowSampler2;
275	#else
276	varying vec4 vPositionFromLight2;
277	varying float vDepthMetric2;
278	uniform sampler2D shadowSampler2;
279	uniform mat4 lightMatrix2;
280	#endif
281	uniform vec4 shadowsInfo2;
282	uniform vec2 depthValues2;
283	#endif
284	#ifdef SPOTLIGHT2
285	uniform vec4 vLightDirection2;
286	#endif
287	#ifdef HEMILIGHT2
288	uniform vec3 vLightGround2;
289	#endif
290	#ifdef PROJECTEDLIGHTTEXTURE2
291	uniform mat4 textureProjectionMatrix2;
292	uniform sampler2D projectionLightSampler2;
293	#endif
294	#endif
295	#ifdef LIGHT3
296	uniform vec4 vLightData3;
297	uniform vec4 vLightDiffuse3;
298	#ifdef SPECULARTERM
299	uniform vec3 vLightSpecular3;
300	#else
301	vec3 vLightSpecular3=vec3(0.);
302	#endif
303	#ifdef SHADOW3
304	#if defined(SHADOWCUBE3)
305	uniform samplerCube shadowSampler3;
306	#else
307	varying vec4 vPositionFromLight3;
308	varying float vDepthMetric3;
309	uniform sampler2D shadowSampler3;
310	uniform mat4 lightMatrix3;
311	#endif
312	uniform vec4 shadowsInfo3;
313	uniform vec2 depthValues3;
314	#endif
315	#ifdef SPOTLIGHT3
316	uniform vec4 vLightDirection3;
317	#endif
318	#ifdef HEMILIGHT3
319	uniform vec3 vLightGround3;
320	#endif
321	#ifdef PROJECTEDLIGHTTEXTURE3
322	uniform mat4 textureProjectionMatrix3;
323	uniform sampler2D projectionLightSampler3;
324	#endif
325	#endif
326	
327	#ifdef MORPHTARGETS
328	uniform float morphTargetInfluences[NUM_MORPH_INFLUENCERS];
329	#endif
330	
331	#ifdef REFLECTIONMAP_SKYBOX
332	varying vec3 vPositionUVW;
333	#endif
334	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
335	varying vec3 vDirectionW;
336	#endif
337	#ifdef LOGARITHMICDEPTH
338	uniform float logarithmicDepthConstant;
339	varying float vFragmentDepth;
340	#endif
341	#define CUSTOM_VERTEX_DEFINITIONS
342	void main(void) {
343	#define CUSTOM_VERTEX_MAIN_BEGIN
344	vec3 positionUpdated=position;
345	#ifdef NORMAL 
346	vec3 normalUpdated=normal;
347	#endif
348	#ifdef TANGENT
349	vec4 tangentUpdated=tangent;
350	#endif
351	
352	#ifdef REFLECTIONMAP_SKYBOX
353	vPositionUVW=positionUpdated;
354	#endif 
355	#define CUSTOM_VERTEX_UPDATE_POSITION
356	#define CUSTOM_VERTEX_UPDATE_NORMAL
357	#ifdef INSTANCES
358	mat4 finalWorld=mat4(world0,world1,world2,world3);
359	#else
360	mat4 finalWorld=world;
361	#endif
362	#if NUM_BONE_INFLUENCERS>0
363	mat4 influence;
364	influence=mBones[int(matricesIndices[0])]*matricesWeights[0];
365	#if NUM_BONE_INFLUENCERS>1
366	influence+=mBones[int(matricesIndices[1])]*matricesWeights[1];
367	#endif 
368	#if NUM_BONE_INFLUENCERS>2
369	influence+=mBones[int(matricesIndices[2])]*matricesWeights[2];
370	#endif 
371	#if NUM_BONE_INFLUENCERS>3
372	influence+=mBones[int(matricesIndices[3])]*matricesWeights[3];
373	#endif 
374	#if NUM_BONE_INFLUENCERS>4
375	influence+=mBones[int(matricesIndicesExtra[0])]*matricesWeightsExtra[0];
376	#endif 
377	#if NUM_BONE_INFLUENCERS>5
378	influence+=mBones[int(matricesIndicesExtra[1])]*matricesWeightsExtra[1];
379	#endif 
380	#if NUM_BONE_INFLUENCERS>6
381	influence+=mBones[int(matricesIndicesExtra[2])]*matricesWeightsExtra[2];
382	#endif 
383	#if NUM_BONE_INFLUENCERS>7
384	influence+=mBones[int(matricesIndicesExtra[3])]*matricesWeightsExtra[3];
385	#endif 
386	finalWorld=finalWorld*influence;
387	#endif
388	gl_Position=viewProjection*finalWorld*vec4(positionUpdated,1.0);
389	vec4 worldPos=finalWorld*vec4(positionUpdated,1.0);
390	vPositionW=vec3(worldPos);
391	#ifdef NORMAL
392	mat3 normalWorld=mat3(finalWorld);
393	#ifdef NONUNIFORMSCALING
394	normalWorld=transposeMat3(inverseMat3(normalWorld));
395	#endif
396	vNormalW=normalize(normalWorld*normalUpdated);
397	#endif
398	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
399	vDirectionW=normalize(vec3(finalWorld*vec4(positionUpdated,0.0)));
400	#endif
401	
402	#ifndef UV1
403	vec2 uv=vec2(0.,0.);
404	#endif
405	#ifndef UV2
406	vec2 uv2=vec2(0.,0.);
407	#endif
408	#ifdef MAINUV1
409	vMainUV1=uv;
410	#endif
411	#ifdef MAINUV2
412	vMainUV2=uv2;
413	#endif
414	#if defined(DIFFUSE) && DIFFUSEDIRECTUV == 0
415	if (vDiffuseInfos.x == 0.)
416	{
417	vDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));
418	}
419	else
420	{
421	vDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));
422	}
423	#endif
424	#if defined(AMBIENT) && AMBIENTDIRECTUV == 0
425	if (vAmbientInfos.x == 0.)
426	{
427	vAmbientUV=vec2(ambientMatrix*vec4(uv,1.0,0.0));
428	}
429	else
430	{
431	vAmbientUV=vec2(ambientMatrix*vec4(uv2,1.0,0.0));
432	}
433	#endif
434	#if defined(OPACITY) && OPACITYDIRECTUV == 0
435	if (vOpacityInfos.x == 0.)
436	{
437	vOpacityUV=vec2(opacityMatrix*vec4(uv,1.0,0.0));
438	}
439	else
440	{
441	vOpacityUV=vec2(opacityMatrix*vec4(uv2,1.0,0.0));
442	}
443	#endif
444	#if defined(EMISSIVE) && EMISSIVEDIRECTUV == 0
445	if (vEmissiveInfos.x == 0.)
446	{
447	vEmissiveUV=vec2(emissiveMatrix*vec4(uv,1.0,0.0));
448	}
449	else
450	{
451	vEmissiveUV=vec2(emissiveMatrix*vec4(uv2,1.0,0.0));
452	}
453	#endif
454	#if defined(LIGHTMAP) && LIGHTMAPDIRECTUV == 0
455	if (vLightmapInfos.x == 0.)
456	{
457	vLightmapUV=vec2(lightmapMatrix*vec4(uv,1.0,0.0));
458	}
459	else
460	{
461	vLightmapUV=vec2(lightmapMatrix*vec4(uv2,1.0,0.0));
462	}
463	#endif
464	#if defined(SPECULAR) && defined(SPECULARTERM) && SPECULARDIRECTUV == 0
465	if (vSpecularInfos.x == 0.)
466	{
467	vSpecularUV=vec2(specularMatrix*vec4(uv,1.0,0.0));
468	}
469	else
470	{
471	vSpecularUV=vec2(specularMatrix*vec4(uv2,1.0,0.0));
472	}
473	#endif
474	#if defined(BUMP) && BUMPDIRECTUV == 0
475	if (vBumpInfos.x == 0.)
476	{
477	vBumpUV=vec2(bumpMatrix*vec4(uv,1.0,0.0));
478	}
479	else
480	{
481	vBumpUV=vec2(bumpMatrix*vec4(uv2,1.0,0.0));
482	}
483	#endif
484	#if defined(BUMP) || defined(PARALLAX)
485	#if defined(TANGENT) && defined(NORMAL)
486	vec3 tbnNormal=normalize(normalUpdated);
487	vec3 tbnTangent=normalize(tangentUpdated.xyz);
488	vec3 tbnBitangent=cross(tbnNormal,tbnTangent)*tangentUpdated.w;
489	vTBN=mat3(finalWorld)*mat3(tbnTangent,tbnBitangent,tbnNormal);
490	#endif
491	#endif
492	#ifdef CLIPPLANE
493	fClipDistance=dot(worldPos,vClipPlane);
494	#endif
495	#ifdef FOG
496	vFogDistance=(view*worldPos).xyz;
497	#endif
498	#ifdef SHADOWS
499	#if defined(SHADOW0) && !defined(SHADOWCUBE0)
500	vPositionFromLight0=lightMatrix0*worldPos;
501	vDepthMetric0=((vPositionFromLight0.z+depthValues0.x)/(depthValues0.y));
502	#endif
503	#endif
504	#ifdef SHADOWS
505	#if defined(SHADOW1) && !defined(SHADOWCUBE1)
506	vPositionFromLight1=lightMatrix1*worldPos;
507	vDepthMetric1=((vPositionFromLight1.z+depthValues1.x)/(depthValues1.y));
508	#endif
509	#endif
510	#ifdef SHADOWS
511	#if defined(SHADOW2) && !defined(SHADOWCUBE2)
512	vPositionFromLight2=lightMatrix2*worldPos;
513	vDepthMetric2=((vPositionFromLight2.z+depthValues2.x)/(depthValues2.y));
514	#endif
515	#endif
516	#ifdef SHADOWS
517	#if defined(SHADOW3) && !defined(SHADOWCUBE3)
518	vPositionFromLight3=lightMatrix3*worldPos;
519	vDepthMetric3=((vPositionFromLight3.z+depthValues3.x)/(depthValues3.y));
520	#endif
521	#endif
522	
523	#ifdef VERTEXCOLOR
524	
525	vColor=color;
526	#endif
527	#ifdef POINTSIZE
528	gl_PointSize=pointSize;
529	#endif
530	#ifdef LOGARITHMICDEPTH
531	vFragmentDepth=1.0+gl_Position.w;
532	gl_Position.z=log2(max(0.000001,vFragmentDepth))*logarithmicDepthConstant;
533	#endif
534	#define CUSTOM_VERTEX_MAIN_END
535	}
536	
	_ErrorEnabled (babylon.js:4:8244)
	_dumpShadersSource (babylon.js:1:6720)
	_prepareEffect (babylon.js:1:11195)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Fragment shader: default
1	#define DIFFUSEDIRECTUV 0
2	#define AMBIENTDIRECTUV 0
3	#define OPACITYDIRECTUV 0
4	#define EMISSIVEDIRECTUV 0
5	#define SPECULARDIRECTUV 0
6	#define BUMPDIRECTUV 0
7	#define SPECULARTERM
8	#define NORMAL
9	#define NUM_BONE_INFLUENCERS 8
10	#define BonesPerMesh 33
11	#define LIGHTMAPDIRECTUV 0
12	#define NUM_MORPH_INFLUENCERS 0
13	#define VIGNETTEBLENDMODEMULTIPLY
14	#define SAMPLER3DGREENDEPTH
15	#define SAMPLER3DBGRMAP
16	#define LIGHT0
17	#define POINTLIGHT0
18	
19	#define SHADER_NAME fragment:default
20	precision highp float;
21	uniform vec4 vDiffuseColor;
22	#ifdef SPECULARTERM
23	uniform vec4 vSpecularColor;
24	#endif
25	uniform vec3 vEmissiveColor;
26	
27	#ifdef DIFFUSE
28	uniform vec2 vDiffuseInfos;
29	#endif
30	#ifdef AMBIENT
31	uniform vec2 vAmbientInfos;
32	#endif
33	#ifdef OPACITY 
34	uniform vec2 vOpacityInfos;
35	#endif
36	#ifdef EMISSIVE
37	uniform vec2 vEmissiveInfos;
38	#endif
39	#ifdef LIGHTMAP
40	uniform vec2 vLightmapInfos;
41	#endif
42	#ifdef BUMP
43	uniform vec3 vBumpInfos;
44	uniform vec2 vTangentSpaceParams;
45	#endif
46	#if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
47	uniform mat4 view;
48	#endif
49	#ifdef REFRACTION
50	uniform vec4 vRefractionInfos;
51	#ifndef REFRACTIONMAP_3D
52	uniform mat4 refractionMatrix;
53	#endif
54	#ifdef REFRACTIONFRESNEL
55	uniform vec4 refractionLeftColor;
56	uniform vec4 refractionRightColor;
57	#endif
58	#endif
59	#if defined(SPECULAR) && defined(SPECULARTERM)
60	uniform vec2 vSpecularInfos;
61	#endif
62	#ifdef DIFFUSEFRESNEL
63	uniform vec4 diffuseLeftColor;
64	uniform vec4 diffuseRightColor;
65	#endif
66	#ifdef OPACITYFRESNEL
67	uniform vec4 opacityParts;
68	#endif
69	#ifdef EMISSIVEFRESNEL
70	uniform vec4 emissiveLeftColor;
71	uniform vec4 emissiveRightColor;
72	#endif
73	
74	#ifdef REFLECTION
75	uniform vec2 vReflectionInfos;
76	#ifdef REFLECTIONMAP_SKYBOX
77	#else
78	#if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
79	uniform mat4 reflectionMatrix;
80	#endif
81	#if defined(USE_LOCAL_REFLECTIONMAP_CUBIC) && defined(REFLECTIONMAP_CUBIC)
82	uniform vec3 vReflectionPosition;
83	uniform vec3 vReflectionSize; 
84	#endif
85	#endif
86	#ifdef REFLECTIONFRESNEL
87	uniform vec4 reflectionLeftColor;
88	uniform vec4 reflectionRightColor;
89	#endif
90	#endif
91	#if defined(BUMP) || !defined(NORMAL)
92	#extension GL_OES_standard_derivatives : enable
93	#endif
94	#define CUSTOM_FRAGMENT_BEGIN
95	#ifdef LOGARITHMICDEPTH
96	#extension GL_EXT_frag_depth : enable
97	#endif
98	
99	#define RECIPROCAL_PI2 0.15915494
100	uniform vec3 vEyePosition;
101	uniform vec3 vAmbientColor;
102	
103	varying vec3 vPositionW;
104	#ifdef NORMAL
105	varying vec3 vNormalW;
106	#endif
107	#ifdef VERTEXCOLOR
108	varying vec4 vColor;
109	#endif
110	#ifdef MAINUV1
111	varying vec2 vMainUV1;
112	#endif
113	#ifdef MAINUV2
114	varying vec2 vMainUV2;
115	#endif
116	
117	const float PI=3.1415926535897932384626433832795;
118	const float LinearEncodePowerApprox=2.2;
119	const float GammaEncodePowerApprox=1.0/LinearEncodePowerApprox;
120	const vec3 LuminanceEncodeApprox=vec3(0.2126,0.7152,0.0722);
121	mat3 transposeMat3(mat3 inMatrix) {
122	vec3 i0=inMatrix[0];
123	vec3 i1=inMatrix[1];
124	vec3 i2=inMatrix[2];
125	mat3 outMatrix=mat3(
126	vec3(i0.x,i1.x,i2.x),
127	vec3(i0.y,i1.y,i2.y),
128	vec3(i0.z,i1.z,i2.z)
129	);
130	return outMatrix;
131	}
132	
133	mat3 inverseMat3(mat3 inMatrix) {
134	float a00=inMatrix[0][0],a01=inMatrix[0][1],a02=inMatrix[0][2];
135	float a10=inMatrix[1][0],a11=inMatrix[1][1],a12=inMatrix[1][2];
136	float a20=inMatrix[2][0],a21=inMatrix[2][1],a22=inMatrix[2][2];
137	float b01=a22*a11-a12*a21;
138	float b11=-a22*a10+a12*a20;
139	float b21=a21*a10-a11*a20;
140	float det=a00*b01+a01*b11+a02*b21;
141	return mat3(b01,(-a22*a01+a02*a21),(a12*a01-a02*a11),
142	b11,(a22*a00-a02*a20),(-a12*a00+a02*a10),
143	b21,(-a21*a00+a01*a20),(a11*a00-a01*a10))/det;
144	}
145	float computeFallOff(float value,vec2 clipSpace,float frustumEdgeFalloff)
146	{
147	float mask=smoothstep(1.0-frustumEdgeFalloff,1.0,clamp(dot(clipSpace,clipSpace),0.,1.));
148	return mix(value,1.0,mask);
149	}
150	vec3 applyEaseInOut(vec3 x){
151	return x*x*(3.0-2.0*x);
152	}
153	vec3 toLinearSpace(vec3 color)
154	{
155	return pow(color,vec3(LinearEncodePowerApprox));
156	}
157	vec3 toGammaSpace(vec3 color)
158	{
159	return pow(color,vec3(GammaEncodePowerApprox));
160	}
161	float square(float value)
162	{
163	return value*value;
164	}
165	float getLuminance(vec3 color)
166	{
167	return clamp(dot(color,LuminanceEncodeApprox),0.,1.);
168	}
169	
170	float getRand(vec2 seed) {
171	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233)))*43758.5453);
172	}
173	vec3 dither(vec2 seed,vec3 color) {
174	float rand=getRand(seed);
175	color+=mix(-0.5/255.0,0.5/255.0,rand);
176	color=max(color,0.0);
177	return color;
178	}
179	
180	#ifdef LIGHT0
181	uniform vec4 vLightData0;
182	uniform vec4 vLightDiffuse0;
183	#ifdef SPECULARTERM
184	uniform vec3 vLightSpecular0;
185	#else
186	vec3 vLightSpecular0=vec3(0.);
187	#endif
188	#ifdef SHADOW0
189	#if defined(SHADOWCUBE0)
190	uniform samplerCube shadowSampler0;
191	#else
192	varying vec4 vPositionFromLight0;
193	varying float vDepthMetric0;
194	uniform sampler2D shadowSampler0;
195	uniform mat4 lightMatrix0;
196	#endif
197	uniform vec4 shadowsInfo0;
198	uniform vec2 depthValues0;
199	#endif
200	#ifdef SPOTLIGHT0
201	uniform vec4 vLightDirection0;
202	#endif
203	#ifdef HEMILIGHT0
204	uniform vec3 vLightGround0;
205	#endif
206	#ifdef PROJECTEDLIGHTTEXTURE0
207	uniform mat4 textureProjectionMatrix0;
208	uniform sampler2D projectionLightSampler0;
209	#endif
210	#endif
211	#ifdef LIGHT1
212	uniform vec4 vLightData1;
213	uniform vec4 vLightDiffuse1;
214	#ifdef SPECULARTERM
215	uniform vec3 vLightSpecular1;
216	#else
217	vec3 vLightSpecular1=vec3(0.);
218	#endif
219	#ifdef SHADOW1
220	#if defined(SHADOWCUBE1)
221	uniform samplerCube shadowSampler1;
222	#else
223	varying vec4 vPositionFromLight1;
224	varying float vDepthMetric1;
225	uniform sampler2D shadowSampler1;
226	uniform mat4 lightMatrix1;
227	#endif
228	uniform vec4 shadowsInfo1;
229	uniform vec2 depthValues1;
230	#endif
231	#ifdef SPOTLIGHT1
232	uniform vec4 vLightDirection1;
233	#endif
234	#ifdef HEMILIGHT1
235	uniform vec3 vLightGround1;
236	#endif
237	#ifdef PROJECTEDLIGHTTEXTURE1
238	uniform mat4 textureProjectionMatrix1;
239	uniform sampler2D projectionLightSampler1;
240	#endif
241	#endif
242	#ifdef LIGHT2
243	uniform vec4 vLightData2;
244	uniform vec4 vLightDiffuse2;
245	#ifdef SPECULARTERM
246	uniform vec3 vLightSpecular2;
247	#else
248	vec3 vLightSpecular2=vec3(0.);
249	#endif
250	#ifdef SHADOW2
251	#if defined(SHADOWCUBE2)
252	uniform samplerCube shadowSampler2;
253	#else
254	varying vec4 vPositionFromLight2;
255	varying float vDepthMetric2;
256	uniform sampler2D shadowSampler2;
257	uniform mat4 lightMatrix2;
258	#endif
259	uniform vec4 shadowsInfo2;
260	uniform vec2 depthValues2;
261	#endif
262	#ifdef SPOTLIGHT2
263	uniform vec4 vLightDirection2;
264	#endif
265	#ifdef HEMILIGHT2
266	uniform vec3 vLightGround2;
267	#endif
268	#ifdef PROJECTEDLIGHTTEXTURE2
269	uniform mat4 textureProjectionMatrix2;
270	uniform sampler2D projectionLightSampler2;
271	#endif
272	#endif
273	#ifdef LIGHT3
274	uniform vec4 vLightData3;
275	uniform vec4 vLightDiffuse3;
276	#ifdef SPECULARTERM
277	uniform vec3 vLightSpecular3;
278	#else
279	vec3 vLightSpecular3=vec3(0.);
280	#endif
281	#ifdef SHADOW3
282	#if defined(SHADOWCUBE3)
283	uniform samplerCube shadowSampler3;
284	#else
285	varying vec4 vPositionFromLight3;
286	varying float vDepthMetric3;
287	uniform sampler2D shadowSampler3;
288	uniform mat4 lightMatrix3;
289	#endif
290	uniform vec4 shadowsInfo3;
291	uniform vec2 depthValues3;
292	#endif
293	#ifdef SPOTLIGHT3
294	uniform vec4 vLightDirection3;
295	#endif
296	#ifdef HEMILIGHT3
297	uniform vec3 vLightGround3;
298	#endif
299	#ifdef PROJECTEDLIGHTTEXTURE3
300	uniform mat4 textureProjectionMatrix3;
301	uniform sampler2D projectionLightSampler3;
302	#endif
303	#endif
304	
305	
306	struct lightingInfo
307	{
308	vec3 diffuse;
309	#ifdef SPECULARTERM
310	vec3 specular;
311	#endif
312	#ifdef NDOTL
313	float ndl;
314	#endif
315	};
316	lightingInfo computeLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,float range,float glossiness) {
317	lightingInfo result;
318	vec3 lightVectorW;
319	float attenuation=1.0;
320	if (lightData.w == 0.)
321	{
322	vec3 direction=lightData.xyz-vPositionW;
323	attenuation=max(0.,1.0-length(direction)/range);
324	lightVectorW=normalize(direction);
325	}
326	else
327	{
328	lightVectorW=normalize(-lightData.xyz);
329	}
330	
331	float ndl=max(0.,dot(vNormal,lightVectorW));
332	#ifdef NDOTL
333	result.ndl=ndl;
334	#endif
335	result.diffuse=ndl*diffuseColor*attenuation;
336	#ifdef SPECULARTERM
337	
338	vec3 angleW=normalize(viewDirectionW+lightVectorW);
339	float specComp=max(0.,dot(vNormal,angleW));
340	specComp=pow(specComp,max(1.,glossiness));
341	result.specular=specComp*specularColor*attenuation;
342	#endif
343	return result;
344	}
345	lightingInfo computeSpotLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec4 lightDirection,vec3 diffuseColor,vec3 specularColor,float range,float glossiness) {
346	lightingInfo result;
347	vec3 direction=lightData.xyz-vPositionW;
348	vec3 lightVectorW=normalize(direction);
349	float attenuation=max(0.,1.0-length(direction)/range);
350	
351	float cosAngle=max(0.,dot(lightDirection.xyz,-lightVectorW));
352	if (cosAngle>=lightDirection.w)
353	{
354	cosAngle=max(0.,pow(cosAngle,lightData.w));
355	attenuation*=cosAngle;
356	
357	float ndl=max(0.,dot(vNormal,lightVectorW));
358	#ifdef NDOTL
359	result.ndl=ndl;
360	#endif
361	result.diffuse=ndl*diffuseColor*attenuation;
362	#ifdef SPECULARTERM
363	
364	vec3 angleW=normalize(viewDirectionW+lightVectorW);
365	float specComp=max(0.,dot(vNormal,angleW));
366	specComp=pow(specComp,max(1.,glossiness));
367	result.specular=specComp*specularColor*attenuation;
368	#endif
369	return result;
370	}
371	result.diffuse=vec3(0.);
372	#ifdef SPECULARTERM
373	result.specular=vec3(0.);
374	#endif
375	#ifdef NDOTL
376	result.ndl=0.;
377	#endif
378	return result;
379	}
380	lightingInfo computeHemisphericLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,vec3 groundColor,float glossiness) {
381	lightingInfo result;
382	
383	float ndl=dot(vNormal,lightData.xyz)*0.5+0.5;
384	#ifdef NDOTL
385	result.ndl=ndl;
386	#endif
387	result.diffuse=mix(groundColor,diffuseColor,ndl);
388	#ifdef SPECULARTERM
389	
390	vec3 angleW=normalize(viewDirectionW+lightData.xyz);
391	float specComp=max(0.,dot(vNormal,angleW));
392	specComp=pow(specComp,max(1.,glossiness));
393	result.specular=specComp*specularColor;
394	#endif
395	return result;
396	}
397	vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler,mat4 textureProjectionMatrix){
398	vec4 strq=textureProjectionMatrix*vec4(vPositionW,1.0);
399	strq/=strq.w;
400	vec3 textureColor=texture2D(projectionLightSampler,strq.xy).rgb;
401	#ifdef PBR
402	textureColor=toLinearSpace(textureColor);
403	#endif
404	return textureColor;
405	}
406	#ifdef SHADOWS
407	#ifndef SHADOWFLOAT
408	float unpack(vec4 color)
409	{
410	const vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);
411	return dot(color,bit_shift);
412	}
413	#endif
414	float computeShadowCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,vec2 depthValues)
415	{
416	vec3 directionToLight=vPositionW-lightPosition;
417	float depth=length(directionToLight);
418	depth=(depth+depthValues.x)/(depthValues.y);
419	depth=clamp(depth,0.,1.0);
420	directionToLight=normalize(directionToLight);
421	directionToLight.y=-directionToLight.y;
422	#ifndef SHADOWFLOAT
423	float shadow=unpack(textureCube(shadowSampler,directionToLight));
424	#else
425	float shadow=textureCube(shadowSampler,directionToLight).x;
426	#endif
427	if (depth>shadow)
428	{
429	return darkness;
430	}
431	return 1.0;
432	}
433	float computeShadowWithPCFCube(vec3 lightPosition,samplerCube shadowSampler,float mapSize,float darkness,vec2 depthValues)
434	{
435	vec3 directionToLight=vPositionW-lightPosition;
436	float depth=length(directionToLight);
437	depth=(depth+depthValues.x)/(depthValues.y);
438	depth=clamp(depth,0.,1.0);
439	directionToLight=normalize(directionToLight);
440	directionToLight.y=-directionToLight.y;
441	float visibility=1.;
442	vec3 poissonDisk[4];
443	poissonDisk[0]=vec3(-1.0,1.0,-1.0);
444	poissonDisk[1]=vec3(1.0,-1.0,-1.0);
445	poissonDisk[2]=vec3(-1.0,-1.0,-1.0);
446	poissonDisk[3]=vec3(1.0,-1.0,1.0);
447	
448	#ifndef SHADOWFLOAT
449	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize))<depth) visibility-=0.25;
450	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize))<depth) visibility-=0.25;
451	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize))<depth) visibility-=0.25;
452	if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize))<depth) visibility-=0.25;
453	#else
454	if (textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize).x<depth) visibility-=0.25;
455	if (textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize).x<depth) visibility-=0.25;
456	if (textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize).x<depth) visibility-=0.25;
457	if (textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize).x<depth) visibility-=0.25;
458	#endif
459	return min(1.0,visibility+darkness);
460	}
461	float computeShadowWithESMCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
462	{
463	vec3 directionToLight=vPositionW-lightPosition;
464	float depth=length(directionToLight);
465	depth=(depth+depthValues.x)/(depthValues.y);
466	float shadowPixelDepth=clamp(depth,0.,1.0);
467	directionToLight=normalize(directionToLight);
468	directionToLight.y=-directionToLight.y;
469	#ifndef SHADOWFLOAT
470	float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
471	#else
472	float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
473	#endif
474	float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness); 
475	return esm;
476	}
477	float computeShadowWithCloseESMCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
478	{
479	vec3 directionToLight=vPositionW-lightPosition;
480	float depth=length(directionToLight);
481	depth=(depth+depthValues.x)/(depthValues.y);
482	float shadowPixelDepth=clamp(depth,0.,1.0);
483	directionToLight=normalize(directionToLight);
484	directionToLight.y=-directionToLight.y;
485	#ifndef SHADOWFLOAT
486	float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
487	#else
488	float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
489	#endif
490	float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);
491	return esm;
492	}
493	float computeShadow(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float frustumEdgeFalloff)
494	{
495	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
496	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
497	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
498	{
499	return 1.0;
500	}
501	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
502	#ifndef SHADOWFLOAT
503	float shadow=unpack(texture2D(shadowSampler,uv));
504	#else
505	float shadow=texture2D(shadowSampler,uv).x;
506	#endif
507	if (shadowPixelDepth>shadow)
508	{
509	return computeFallOff(darkness,clipSpace.xy,frustumEdgeFalloff);
510	}
511	return 1.;
512	}
513	float computeShadowWithPCF(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float mapSize,float darkness,float frustumEdgeFalloff)
514	{
515	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
516	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
517	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
518	{
519	return 1.0;
520	}
521	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
522	float visibility=1.;
523	vec2 poissonDisk[4];
524	poissonDisk[0]=vec2(-0.94201624,-0.39906216);
525	poissonDisk[1]=vec2(0.94558609,-0.76890725);
526	poissonDisk[2]=vec2(-0.094184101,-0.92938870);
527	poissonDisk[3]=vec2(0.34495938,0.29387760);
528	
529	#ifndef SHADOWFLOAT
530	if (unpack(texture2D(shadowSampler,uv+poissonDisk[0]*mapSize))<shadowPixelDepth) visibility-=0.25;
531	if (unpack(texture2D(shadowSampler,uv+poissonDisk[1]*mapSize))<shadowPixelDepth) visibility-=0.25;
532	if (unpack(texture2D(shadowSampler,uv+poissonDisk[2]*mapSize))<shadowPixelDepth) visibility-=0.25;
533	if (unpack(texture2D(shadowSampler,uv+poissonDisk[3]*mapSize))<shadowPixelDepth) visibility-=0.25;
534	#else
535	if (texture2D(shadowSampler,uv+poissonDisk[0]*mapSize).x<shadowPixelDepth) visibility-=0.25;
536	if (texture2D(shadowSampler,uv+poissonDisk[1]*mapSize).x<shadowPixelDepth) visibility-=0.25;
537	if (texture2D(shadowSampler,uv+poissonDisk[2]*mapSize).x<shadowPixelDepth) visibility-=0.25;
538	if (texture2D(shadowSampler,uv+poissonDisk[3]*mapSize).x<shadowPixelDepth) visibility-=0.25;
539	#endif
540	return computeFallOff(min(1.0,visibility+darkness),clipSpace.xy,frustumEdgeFalloff);
541	}
542	float computeShadowWithESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
543	{
544	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
545	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
546	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
547	{
548	return 1.0;
549	}
550	float shadowPixelDepth=clamp(depthMetric,0.,1.0);
551	#ifndef SHADOWFLOAT
552	float shadowMapSample=unpack(texture2D(shadowSampler,uv));
553	#else
554	float shadowMapSample=texture2D(shadowSampler,uv).x;
555	#endif
556	float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness);
557	return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);
558	}
559	float computeShadowWithCloseESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
560	{
561	vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;
562	vec2 uv=0.5*clipSpace.xy+vec2(0.5);
563	if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
564	{
565	return 1.0;
566	}
567	float shadowPixelDepth=clamp(depthMetric,0.,1.0); 
568	#ifndef SHADOWFLOAT
569	float shadowMapSample=unpack(texture2D(shadowSampler,uv));
570	#else
571	float shadowMapSample=texture2D(shadowSampler,uv).x;
572	#endif
573	float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);
574	return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);
575	}
576	#endif
577	
578	
579	#ifdef DIFFUSE
580	#if DIFFUSEDIRECTUV == 1
581	#define vDiffuseUV vMainUV1
582	#elif DIFFUSEDIRECTUV == 2
583	#define vDiffuseUV vMainUV2
584	#else
585	varying vec2 vDiffuseUV;
586	#endif
587	uniform sampler2D diffuseSampler;
588	#endif
589	#ifdef AMBIENT
590	#if AMBIENTDIRECTUV == 1
591	#define vAmbientUV vMainUV1
592	#elif AMBIENTDIRECTUV == 2
593	#define vAmbientUV vMainUV2
594	#else
595	varying vec2 vAmbientUV;
596	#endif
597	uniform sampler2D ambientSampler;
598	#endif
599	#ifdef OPACITY 
600	#if OPACITYDIRECTUV == 1
601	#define vOpacityUV vMainUV1
602	#elif OPACITYDIRECTUV == 2
603	#define vOpacityUV vMainUV2
604	#else
605	varying vec2 vOpacityUV;
606	#endif
607	uniform sampler2D opacitySampler;
608	#endif
609	#ifdef EMISSIVE
610	#if EMISSIVEDIRECTUV == 1
611	#define vEmissiveUV vMainUV1
612	#elif EMISSIVEDIRECTUV == 2
613	#define vEmissiveUV vMainUV2
614	#else
615	varying vec2 vEmissiveUV;
616	#endif
617	uniform sampler2D emissiveSampler;
618	#endif
619	#ifdef LIGHTMAP
620	#if LIGHTMAPDIRECTUV == 1
621	#define vLightmapUV vMainUV1
622	#elif LIGHTMAPDIRECTUV == 2
623	#define vLightmapUV vMainUV2
624	#else
625	varying vec2 vLightmapUV;
626	#endif
627	uniform sampler2D lightmapSampler;
628	#endif
629	#ifdef REFRACTION
630	#ifdef REFRACTIONMAP_3D
631	uniform samplerCube refractionCubeSampler;
632	#else
633	uniform sampler2D refraction2DSampler;
634	#endif
635	#endif
636	#if defined(SPECULAR) && defined(SPECULARTERM)
637	#if SPECULARDIRECTUV == 1
638	#define vSpecularUV vMainUV1
639	#elif SPECULARDIRECTUV == 2
640	#define vSpecularUV vMainUV2
641	#else
642	varying vec2 vSpecularUV;
643	#endif
644	uniform sampler2D specularSampler;
645	#endif
646	
647	#ifdef FRESNEL
648	float computeFresnelTerm(vec3 viewDirection,vec3 worldNormal,float bias,float power)
649	{
650	float fresnelTerm=pow(bias+abs(dot(viewDirection,worldNormal)),power);
651	return clamp(fresnelTerm,0.,1.);
652	}
653	#endif
654	
655	#ifdef REFLECTION
656	#ifdef REFLECTIONMAP_3D
657	uniform samplerCube reflectionCubeSampler;
658	#else
659	uniform sampler2D reflection2DSampler;
660	#endif
661	#ifdef REFLECTIONMAP_SKYBOX
662	varying vec3 vPositionUVW;
663	#else
664	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
665	varying vec3 vDirectionW;
666	#endif
667	#endif
668	#ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
669	vec3 parallaxCorrectNormal( vec3 vertexPos,vec3 origVec,vec3 cubeSize,vec3 cubePos ) {
670	
671	vec3 invOrigVec=vec3(1.0,1.0,1.0)/origVec;
672	vec3 halfSize=cubeSize*0.5;
673	vec3 intersecAtMaxPlane=(cubePos+halfSize-vertexPos)*invOrigVec;
674	vec3 intersecAtMinPlane=(cubePos-halfSize-vertexPos)*invOrigVec;
675	
676	vec3 largestIntersec=max(intersecAtMaxPlane,intersecAtMinPlane);
677	
678	float distance=min(min(largestIntersec.x,largestIntersec.y),largestIntersec.z);
679	
680	vec3 intersectPositionWS=vertexPos+origVec*distance;
681	
682	return intersectPositionWS-cubePos;
683	}
684	#endif
685	vec3 computeReflectionCoords(vec4 worldPos,vec3 worldNormal)
686	{
687	#if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
688	vec3 direction=vDirectionW;
689	float t=clamp(direction.y*-0.5+0.5,0.,1.0);
690	float s=atan(direction.z,direction.x)*RECIPROCAL_PI2+0.5;
691	#ifdef REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED
692	return vec3(1.0-s,t,0);
693	#else
694	return vec3(s,t,0);
695	#endif
696	#endif
697	#ifdef REFLECTIONMAP_EQUIRECTANGULAR
698	vec3 cameraToVertex=normalize(worldPos.xyz-vEyePosition.xyz);
699	vec3 r=reflect(cameraToVertex,worldNormal);
700	float t=clamp(r.y*-0.5+0.5,0.,1.0);
701	float s=atan(r.z,r.x)*RECIPROCAL_PI2+0.5;
702	return vec3(s,t,0);
703	#endif
704	#ifdef REFLECTIONMAP_SPHERICAL
705	vec3 viewDir=normalize(vec3(view*worldPos));
706	vec3 viewNormal=normalize(vec3(view*vec4(worldNormal,0.0)));
707	vec3 r=reflect(viewDir,viewNormal);
708	r.z=r.z-1.0;
709	float m=2.0*length(r);
710	return vec3(r.x/m+0.5,1.0-r.y/m-0.5,0);
711	#endif
712	#ifdef REFLECTIONMAP_PLANAR
713	vec3 viewDir=worldPos.xyz-vEyePosition.xyz;
714	vec3 coords=normalize(reflect(viewDir,worldNormal));
715	return vec3(reflectionMatrix*vec4(coords,1));
716	#endif
717	#ifdef REFLECTIONMAP_CUBIC
718	vec3 viewDir=normalize(worldPos.xyz-vEyePosition.xyz);
719	
720	vec3 coords=reflect(viewDir,worldNormal);
721	#ifdef USE_LOCAL_REFLECTIONMAP_CUBIC
722	coords=parallaxCorrectNormal(worldPos.xyz,coords,vReflectionSize,vReflectionPosition);
723	#endif
724	coords=vec3(reflectionMatrix*vec4(coords,0));
725	#ifdef INVERTCUBICMAP
726	coords.y*=-1.0;
727	#endif
728	return coords;
729	#endif
730	#ifdef REFLECTIONMAP_PROJECTION
731	return vec3(reflectionMatrix*(view*worldPos));
732	#endif
733	#ifdef REFLECTIONMAP_SKYBOX
734	return vPositionUVW;
735	#endif
736	#ifdef REFLECTIONMAP_EXPLICIT
737	return vec3(0,0,0);
738	#endif
739	}
740	#endif
741	#ifdef EXPOSURE
742	uniform float exposureLinear;
743	#endif
744	#ifdef CONTRAST
745	uniform float contrast;
746	#endif
747	#ifdef VIGNETTE
748	uniform vec2 vInverseScreenSize;
749	uniform vec4 vignetteSettings1;
750	uniform vec4 vignetteSettings2;
751	#endif
752	#ifdef COLORCURVES
753	uniform vec4 vCameraColorCurveNegative;
754	uniform vec4 vCameraColorCurveNeutral;
755	uniform vec4 vCameraColorCurvePositive;
756	#endif
757	#ifdef COLORGRADING
758	#ifdef COLORGRADING3D
759	uniform highp sampler3D txColorTransform;
760	#else
761	uniform sampler2D txColorTransform;
762	#endif
763	uniform vec4 colorTransformSettings;
764	#endif
765	#if defined(COLORGRADING) && !defined(COLORGRADING3D)
766	
767	vec3 sampleTexture3D(sampler2D colorTransform,vec3 color,vec2 sampler3dSetting)
768	{
769	float sliceSize=2.0*sampler3dSetting.x; 
770	#ifdef SAMPLER3DGREENDEPTH
771	float sliceContinuous=(color.g-sampler3dSetting.x)*sampler3dSetting.y;
772	#else
773	float sliceContinuous=(color.b-sampler3dSetting.x)*sampler3dSetting.y;
774	#endif
775	float sliceInteger=floor(sliceContinuous);
776	
777	
778	float sliceFraction=sliceContinuous-sliceInteger;
779	#ifdef SAMPLER3DGREENDEPTH
780	vec2 sliceUV=color.rb;
781	#else
782	vec2 sliceUV=color.rg;
783	#endif
784	sliceUV.x*=sliceSize;
785	sliceUV.x+=sliceInteger*sliceSize;
786	sliceUV=clamp(sliceUV,0.,1.);
787	vec4 slice0Color=texture2D(colorTransform,sliceUV);
788	sliceUV.x+=sliceSize;
789	sliceUV=clamp(sliceUV,0.,1.);
790	vec4 slice1Color=texture2D(colorTransform,sliceUV);
791	vec3 result=mix(slice0Color.rgb,slice1Color.rgb,sliceFraction);
792	#ifdef SAMPLER3DBGRMAP
793	color.rgb=result.rgb;
794	#else
795	color.rgb=result.bgr;
796	#endif
797	return color;
798	}
799	#endif
800	vec4 applyImageProcessing(vec4 result) {
801	#ifdef EXPOSURE
802	result.rgb*=exposureLinear;
803	#endif
804	#ifdef VIGNETTE
805	
806	vec2 viewportXY=gl_FragCoord.xy*vInverseScreenSize;
807	viewportXY=viewportXY*2.0-1.0;
808	vec3 vignetteXY1=vec3(viewportXY*vignetteSettings1.xy+vignetteSettings1.zw,1.0);
809	float vignetteTerm=dot(vignetteXY1,vignetteXY1);
810	float vignette=pow(vignetteTerm,vignetteSettings2.w);
811	
812	vec3 vignetteColor=vignetteSettings2.rgb;
813	#ifdef VIGNETTEBLENDMODEMULTIPLY
814	vec3 vignetteColorMultiplier=mix(vignetteColor,vec3(1,1,1),vignette);
815	result.rgb*=vignetteColorMultiplier;
816	#endif
817	#ifdef VIGNETTEBLENDMODEOPAQUE
818	result.rgb=mix(vignetteColor,result.rgb,vignette);
819	#endif
820	#endif
821	#ifdef TONEMAPPING
822	const float tonemappingCalibration=1.590579;
823	result.rgb=1.0-exp2(-tonemappingCalibration*result.rgb);
824	#endif
825	
826	result.rgb=toGammaSpace(result.rgb);
827	result.rgb=clamp(result.rgb,0.0,1.0);
828	#ifdef CONTRAST
829	
830	vec3 resultHighContrast=applyEaseInOut(result.rgb);
831	if (contrast<1.0) {
832	
833	result.rgb=mix(vec3(0.5,0.5,0.5),result.rgb,contrast);
834	} else {
835	
836	result.rgb=mix(result.rgb,resultHighContrast,contrast-1.0);
837	}
838	#endif
839	
840	#ifdef COLORGRADING
841	vec3 colorTransformInput=result.rgb*colorTransformSettings.xxx+colorTransformSettings.yyy;
842	#ifdef COLORGRADING3D
843	vec3 colorTransformOutput=texture(txColorTransform,colorTransformInput).rgb;
844	#else
845	vec3 colorTransformOutput=sampleTexture3D(txColorTransform,colorTransformInput,colorTransformSettings.yz).rgb;
846	#endif
847	result.rgb=mix(result.rgb,colorTransformOutput,colorTransformSettings.www);
848	#endif
849	#ifdef COLORCURVES
850	
851	float luma=getLuminance(result.rgb);
852	vec2 curveMix=clamp(vec2(luma*3.0-1.5,luma*-3.0+1.5),vec2(0.0),vec2(1.0));
853	vec4 colorCurve=vCameraColorCurveNeutral+curveMix.x*vCameraColorCurvePositive-curveMix.y*vCameraColorCurveNegative;
854	result.rgb*=colorCurve.rgb;
855	result.rgb=mix(vec3(luma),result.rgb,colorCurve.a);
856	#endif
857	return result;
858	}
859	#ifdef BUMP
860	#if BUMPDIRECTUV == 1
861	#define vBumpUV vMainUV1
862	#elif BUMPDIRECTUV == 2
863	#define vBumpUV vMainUV2
864	#else
865	varying vec2 vBumpUV;
866	#endif
867	uniform sampler2D bumpSampler;
868	#if defined(TANGENT) && defined(NORMAL) 
869	varying mat3 vTBN;
870	#endif
871	#ifdef OBJECTSPACE_NORMALMAP
872	uniform mat4 normalMatrix;
873	#endif
874	
875	mat3 cotangent_frame(vec3 normal,vec3 p,vec2 uv)
876	{
877	
878	uv=gl_FrontFacing ? uv : -uv;
879	
880	vec3 dp1=dFdx(p);
881	vec3 dp2=dFdy(p);
882	vec2 duv1=dFdx(uv);
883	vec2 duv2=dFdy(uv);
884	
885	vec3 dp2perp=cross(dp2,normal);
886	vec3 dp1perp=cross(normal,dp1);
887	vec3 tangent=dp2perp*duv1.x+dp1perp*duv2.x;
888	vec3 bitangent=dp2perp*duv1.y+dp1perp*duv2.y;
889	
890	tangent*=vTangentSpaceParams.x;
891	bitangent*=vTangentSpaceParams.y;
892	
893	float invmax=inversesqrt(max(dot(tangent,tangent),dot(bitangent,bitangent)));
894	return mat3(tangent*invmax,bitangent*invmax,normal);
895	}
896	vec3 perturbNormal(mat3 cotangentFrame,vec2 uv)
897	{
898	vec3 map=texture2D(bumpSampler,uv).xyz;
899	map=map*2.0-1.0;
900	#ifdef NORMALXYSCALE
901	map=normalize(map*vec3(vBumpInfos.y,vBumpInfos.y,1.0));
902	#endif
903	return normalize(cotangentFrame*map);
904	}
905	#ifdef PARALLAX
906	const float minSamples=4.;
907	const float maxSamples=15.;
908	const int iMaxSamples=15;
909	
910	vec2 parallaxOcclusion(vec3 vViewDirCoT,vec3 vNormalCoT,vec2 texCoord,float parallaxScale) {
911	float parallaxLimit=length(vViewDirCoT.xy)/vViewDirCoT.z;
912	parallaxLimit*=parallaxScale;
913	vec2 vOffsetDir=normalize(vViewDirCoT.xy);
914	vec2 vMaxOffset=vOffsetDir*parallaxLimit;
915	float numSamples=maxSamples+(dot(vViewDirCoT,vNormalCoT)*(minSamples-maxSamples));
916	float stepSize=1.0/numSamples;
917	
918	float currRayHeight=1.0;
919	vec2 vCurrOffset=vec2(0,0);
920	vec2 vLastOffset=vec2(0,0);
921	float lastSampledHeight=1.0;
922	float currSampledHeight=1.0;
923	for (int i=0; i<iMaxSamples; i++)
924	{
925	currSampledHeight=texture2D(bumpSampler,vBumpUV+vCurrOffset).w;
926	
927	if (currSampledHeight>currRayHeight)
928	{
929	float delta1=currSampledHeight-currRayHeight;
930	float delta2=(currRayHeight+stepSize)-lastSampledHeight;
931	float ratio=delta1/(delta1+delta2);
932	vCurrOffset=(ratio)* vLastOffset+(1.0-ratio)*vCurrOffset;
933	
934	break;
935	}
936	else
937	{
938	currRayHeight-=stepSize;
939	vLastOffset=vCurrOffset;
940	vCurrOffset+=stepSize*vMaxOffset;
941	lastSampledHeight=currSampledHeight;
942	}
943	}
944	return vCurrOffset;
945	}
946	vec2 parallaxOffset(vec3 viewDir,float heightScale)
947	{
948	
949	float height=texture2D(bumpSampler,vBumpUV).w;
950	vec2 texCoordOffset=heightScale*viewDir.xy*height;
951	return -texCoordOffset;
952	}
953	#endif
954	#endif
955	#ifdef CLIPPLANE
956	varying float fClipDistance;
957	#endif
958	#ifdef LOGARITHMICDEPTH
959	uniform float logarithmicDepthConstant;
960	varying float vFragmentDepth;
961	#endif
962	#ifdef FOG
963	#define FOGMODE_NONE 0.
964	#define FOGMODE_EXP 1.
965	#define FOGMODE_EXP2 2.
966	#define FOGMODE_LINEAR 3.
967	#define E 2.71828
968	uniform vec4 vFogInfos;
969	uniform vec3 vFogColor;
970	varying vec3 vFogDistance;
971	float CalcFogFactor()
972	{
973	float fogCoeff=1.0;
974	float fogStart=vFogInfos.y;
975	float fogEnd=vFogInfos.z;
976	float fogDensity=vFogInfos.w;
977	float fogDistance=length(vFogDistance);
978	if (FOGMODE_LINEAR == vFogInfos.x)
979	{
980	fogCoeff=(fogEnd-fogDistance)/(fogEnd-fogStart);
981	}
982	else if (FOGMODE_EXP == vFogInfos.x)
983	{
984	fogCoeff=1.0/pow(E,fogDistance*fogDensity);
985	}
986	else if (FOGMODE_EXP2 == vFogInfos.x)
987	{
988	fogCoeff=1.0/pow(E,fogDistance*fogDistance*fogDensity*fogDensity);
989	}
990	return clamp(fogCoeff,0.0,1.0);
991	}
992	#endif
993	#define CUSTOM_FRAGMENT_DEFINITIONS
994	void main(void) {
995	#define CUSTOM_FRAGMENT_MAIN_BEGIN
996	#ifdef CLIPPLANE
997	if (fClipDistance>0.0)
998	{
999	discard;
1000	}
1001	#endif
1002	vec3 viewDirectionW=normalize(vEyePosition-vPositionW);
1003	
1004	vec4 baseColor=vec4(1.,1.,1.,1.);
1005	vec3 diffuseColor=vDiffuseColor.rgb;
1006	
1007	float alpha=vDiffuseColor.a;
1008	
1009	#ifdef NORMAL
1010	vec3 normalW=normalize(vNormalW);
1011	#else
1012	vec3 normalW=normalize(-cross(dFdx(vPositionW),dFdy(vPositionW)));
1013	#endif
1014	vec2 uvOffset=vec2(0.0,0.0);
1015	#if defined(BUMP) || defined(PARALLAX)
1016	#ifdef NORMALXYSCALE
1017	float normalScale=1.0;
1018	#else 
1019	float normalScale=vBumpInfos.y;
1020	#endif
1021	#if defined(TANGENT) && defined(NORMAL)
1022	mat3 TBN=vTBN;
1023	#else
1024	mat3 TBN=cotangent_frame(normalW*normalScale,vPositionW,vBumpUV);
1025	#endif
1026	#endif
1027	#ifdef PARALLAX
1028	mat3 invTBN=transposeMat3(TBN);
1029	#ifdef PARALLAXOCCLUSION
1030	uvOffset=parallaxOcclusion(invTBN*-viewDirectionW,invTBN*normalW,vBumpUV,vBumpInfos.z);
1031	#else
1032	uvOffset=parallaxOffset(invTBN*viewDirectionW,vBumpInfos.z);
1033	#endif
1034	#endif
1035	#ifdef BUMP
1036	#ifdef OBJECTSPACE_NORMALMAP
1037	normalW=normalize(texture2D(bumpSampler,vBumpUV).xyz*2.0-1.0);
1038	normalW=normalize(mat3(normalMatrix)*normalW); 
1039	#else
1040	normalW=perturbNormal(TBN,vBumpUV+uvOffset);
1041	#endif
1042	#endif
1043	#ifdef TWOSIDEDLIGHTING
1044	normalW=gl_FrontFacing ? normalW : -normalW;
1045	#endif
1046	#ifdef DIFFUSE
1047	baseColor=texture2D(diffuseSampler,vDiffuseUV+uvOffset);
1048	#ifdef ALPHATEST
1049	if (baseColor.a<0.4)
1050	discard;
1051	#endif
1052	#ifdef ALPHAFROMDIFFUSE
1053	alpha*=baseColor.a;
1054	#endif
1055	#define CUSTOM_FRAGMENT_UPDATE_ALPHA
1056	baseColor.rgb*=vDiffuseInfos.y;
1057	#endif
1058	#ifdef DEPTHPREPASS
1059	gl_FragColor=vec4(0.,0.,0.,1.0);
1060	return;
1061	#endif
1062	#ifdef VERTEXCOLOR
1063	baseColor.rgb*=vColor.rgb;
1064	#endif
1065	#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE
1066	
1067	vec3 baseAmbientColor=vec3(1.,1.,1.);
1068	#ifdef AMBIENT
1069	baseAmbientColor=texture2D(ambientSampler,vAmbientUV+uvOffset).rgb*vAmbientInfos.y;
1070	#endif
1071	#define CUSTOM_FRAGMENT_BEFORE_LIGHTS
1072	
1073	#ifdef SPECULARTERM
1074	float glossiness=vSpecularColor.a;
1075	vec3 specularColor=vSpecularColor.rgb;
1076	#ifdef SPECULAR
1077	vec4 specularMapColor=texture2D(specularSampler,vSpecularUV+uvOffset);
1078	specularColor=specularMapColor.rgb;
1079	#ifdef GLOSSINESS
1080	glossiness=glossiness*specularMapColor.a;
1081	#endif
1082	#endif
1083	#else
1084	float glossiness=0.;
1085	#endif
1086	
1087	vec3 diffuseBase=vec3(0.,0.,0.);
1088	lightingInfo info;
1089	#ifdef SPECULARTERM
1090	vec3 specularBase=vec3(0.,0.,0.);
1091	#endif
1092	float shadow=1.;
1093	#ifdef LIGHTMAP
1094	vec3 lightmapColor=texture2D(lightmapSampler,vLightmapUV+uvOffset).rgb*vLightmapInfos.y;
1095	#endif
1096	#ifdef LIGHT0
1097	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED0) && defined(LIGHTMAPNOSPECULAR0))
1098	
1099	#else
1100	#ifdef PBR
1101	#ifdef SPOTLIGHT0
1102	info=computeSpotLighting(viewDirectionW,normalW,vLightData0,vLightDirection0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1103	#endif
1104	#ifdef HEMILIGHT0
1105	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightGround0,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1106	#endif
1107	#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
1108	info=computeLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1109	#endif
1110	#else
1111	#ifdef SPOTLIGHT0
1112	info=computeSpotLighting(viewDirectionW,normalW,vLightData0,vLightDirection0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness);
1113	#endif
1114	#ifdef HEMILIGHT0
1115	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightGround0,glossiness);
1116	#endif
1117	#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
1118	info=computeLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness);
1119	#endif
1120	#endif
1121	#ifdef PROJECTEDLIGHTTEXTURE0
1122	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler0,textureProjectionMatrix0);
1123	#endif
1124	#endif
1125	#ifdef SHADOW0
1126	#ifdef SHADOWCLOSEESM0
1127	#if defined(SHADOWCUBE0)
1128	shadow=computeShadowWithCloseESMCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,depthValues0);
1129	#else
1130	shadow=computeShadowWithCloseESM(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,shadowsInfo0.w);
1131	#endif
1132	#else
1133	#ifdef SHADOWESM0
1134	#if defined(SHADOWCUBE0)
1135	shadow=computeShadowWithESMCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,depthValues0);
1136	#else
1137	shadow=computeShadowWithESM(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z,shadowsInfo0.w);
1138	#endif
1139	#else 
1140	#ifdef SHADOWPCF0
1141	#if defined(SHADOWCUBE0)
1142	shadow=computeShadowWithPCFCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.y,shadowsInfo0.x,depthValues0);
1143	#else
1144	shadow=computeShadowWithPCF(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.y,shadowsInfo0.x,shadowsInfo0.w);
1145	#endif
1146	#else
1147	#if defined(SHADOWCUBE0)
1148	shadow=computeShadowCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,depthValues0);
1149	#else
1150	shadow=computeShadow(vPositionFromLight0,vDepthMetric0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.w);
1151	#endif
1152	#endif
1153	#endif
1154	#endif
1155	#ifdef SHADOWONLY
1156	#ifndef SHADOWINUSE
1157	#define SHADOWINUSE
1158	#endif
1159	globalShadow+=shadow;
1160	shadowLightCount+=1.0;
1161	#endif
1162	#else
1163	shadow=1.;
1164	#endif
1165	#ifndef SHADOWONLY
1166	#ifdef CUSTOMUSERLIGHTING
1167	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1168	#ifdef SPECULARTERM
1169	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1170	#endif
1171	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED0)
1172	diffuseBase+=lightmapColor*shadow;
1173	#ifdef SPECULARTERM
1174	#ifndef LIGHTMAPNOSPECULAR0
1175	specularBase+=info.specular*shadow*lightmapColor;
1176	#endif
1177	#endif
1178	#else
1179	diffuseBase+=info.diffuse*shadow;
1180	#ifdef SPECULARTERM
1181	specularBase+=info.specular*shadow;
1182	#endif
1183	#endif
1184	#endif
1185	#endif
1186	#ifdef LIGHT1
1187	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED1) && defined(LIGHTMAPNOSPECULAR1))
1188	
1189	#else
1190	#ifdef PBR
1191	#ifdef SPOTLIGHT1
1192	info=computeSpotLighting(viewDirectionW,normalW,vLightData1,vLightDirection1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1193	#endif
1194	#ifdef HEMILIGHT1
1195	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightGround1,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1196	#endif
1197	#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
1198	info=computeLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1199	#endif
1200	#else
1201	#ifdef SPOTLIGHT1
1202	info=computeSpotLighting(viewDirectionW,normalW,vLightData1,vLightDirection1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness);
1203	#endif
1204	#ifdef HEMILIGHT1
1205	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightGround1,glossiness);
1206	#endif
1207	#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
1208	info=computeLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness);
1209	#endif
1210	#endif
1211	#ifdef PROJECTEDLIGHTTEXTURE1
1212	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler1,textureProjectionMatrix1);
1213	#endif
1214	#endif
1215	#ifdef SHADOW1
1216	#ifdef SHADOWCLOSEESM1
1217	#if defined(SHADOWCUBE1)
1218	shadow=computeShadowWithCloseESMCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,depthValues1);
1219	#else
1220	shadow=computeShadowWithCloseESM(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,shadowsInfo1.w);
1221	#endif
1222	#else
1223	#ifdef SHADOWESM1
1224	#if defined(SHADOWCUBE1)
1225	shadow=computeShadowWithESMCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,depthValues1);
1226	#else
1227	shadow=computeShadowWithESM(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z,shadowsInfo1.w);
1228	#endif
1229	#else 
1230	#ifdef SHADOWPCF1
1231	#if defined(SHADOWCUBE1)
1232	shadow=computeShadowWithPCFCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.y,shadowsInfo1.x,depthValues1);
1233	#else
1234	shadow=computeShadowWithPCF(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.y,shadowsInfo1.x,shadowsInfo1.w);
1235	#endif
1236	#else
1237	#if defined(SHADOWCUBE1)
1238	shadow=computeShadowCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,depthValues1);
1239	#else
1240	shadow=computeShadow(vPositionFromLight1,vDepthMetric1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.w);
1241	#endif
1242	#endif
1243	#endif
1244	#endif
1245	#ifdef SHADOWONLY
1246	#ifndef SHADOWINUSE
1247	#define SHADOWINUSE
1248	#endif
1249	globalShadow+=shadow;
1250	shadowLightCount+=1.0;
1251	#endif
1252	#else
1253	shadow=1.;
1254	#endif
1255	#ifndef SHADOWONLY
1256	#ifdef CUSTOMUSERLIGHTING
1257	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1258	#ifdef SPECULARTERM
1259	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1260	#endif
1261	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED1)
1262	diffuseBase+=lightmapColor*shadow;
1263	#ifdef SPECULARTERM
1264	#ifndef LIGHTMAPNOSPECULAR1
1265	specularBase+=info.specular*shadow*lightmapColor;
1266	#endif
1267	#endif
1268	#else
1269	diffuseBase+=info.diffuse*shadow;
1270	#ifdef SPECULARTERM
1271	specularBase+=info.specular*shadow;
1272	#endif
1273	#endif
1274	#endif
1275	#endif
1276	#ifdef LIGHT2
1277	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED2) && defined(LIGHTMAPNOSPECULAR2))
1278	
1279	#else
1280	#ifdef PBR
1281	#ifdef SPOTLIGHT2
1282	info=computeSpotLighting(viewDirectionW,normalW,vLightData2,vLightDirection2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1283	#endif
1284	#ifdef HEMILIGHT2
1285	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightGround2,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1286	#endif
1287	#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
1288	info=computeLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1289	#endif
1290	#else
1291	#ifdef SPOTLIGHT2
1292	info=computeSpotLighting(viewDirectionW,normalW,vLightData2,vLightDirection2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness);
1293	#endif
1294	#ifdef HEMILIGHT2
1295	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightGround2,glossiness);
1296	#endif
1297	#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
1298	info=computeLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness);
1299	#endif
1300	#endif
1301	#ifdef PROJECTEDLIGHTTEXTURE2
1302	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler2,textureProjectionMatrix2);
1303	#endif
1304	#endif
1305	#ifdef SHADOW2
1306	#ifdef SHADOWCLOSEESM2
1307	#if defined(SHADOWCUBE2)
1308	shadow=computeShadowWithCloseESMCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,depthValues2);
1309	#else
1310	shadow=computeShadowWithCloseESM(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,shadowsInfo2.w);
1311	#endif
1312	#else
1313	#ifdef SHADOWESM2
1314	#if defined(SHADOWCUBE2)
1315	shadow=computeShadowWithESMCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,depthValues2);
1316	#else
1317	shadow=computeShadowWithESM(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z,shadowsInfo2.w);
1318	#endif
1319	#else 
1320	#ifdef SHADOWPCF2
1321	#if defined(SHADOWCUBE2)
1322	shadow=computeShadowWithPCFCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.y,shadowsInfo2.x,depthValues2);
1323	#else
1324	shadow=computeShadowWithPCF(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.y,shadowsInfo2.x,shadowsInfo2.w);
1325	#endif
1326	#else
1327	#if defined(SHADOWCUBE2)
1328	shadow=computeShadowCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,depthValues2);
1329	#else
1330	shadow=computeShadow(vPositionFromLight2,vDepthMetric2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.w);
1331	#endif
1332	#endif
1333	#endif
1334	#endif
1335	#ifdef SHADOWONLY
1336	#ifndef SHADOWINUSE
1337	#define SHADOWINUSE
1338	#endif
1339	globalShadow+=shadow;
1340	shadowLightCount+=1.0;
1341	#endif
1342	#else
1343	shadow=1.;
1344	#endif
1345	#ifndef SHADOWONLY
1346	#ifdef CUSTOMUSERLIGHTING
1347	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1348	#ifdef SPECULARTERM
1349	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1350	#endif
1351	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED2)
1352	diffuseBase+=lightmapColor*shadow;
1353	#ifdef SPECULARTERM
1354	#ifndef LIGHTMAPNOSPECULAR2
1355	specularBase+=info.specular*shadow*lightmapColor;
1356	#endif
1357	#endif
1358	#else
1359	diffuseBase+=info.diffuse*shadow;
1360	#ifdef SPECULARTERM
1361	specularBase+=info.specular*shadow;
1362	#endif
1363	#endif
1364	#endif
1365	#endif
1366	#ifdef LIGHT3
1367	#if defined(SHADOWONLY) || (defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED3) && defined(LIGHTMAPNOSPECULAR3))
1368	
1369	#else
1370	#ifdef PBR
1371	#ifdef SPOTLIGHT3
1372	info=computeSpotLighting(viewDirectionW,normalW,vLightData3,vLightDirection3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1373	#endif
1374	#ifdef HEMILIGHT3
1375	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightGround3,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1376	#endif
1377	#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
1378	info=computeLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,roughness,NdotV,specularEnvironmentR0,specularEnvironmentR90,NdotL);
1379	#endif
1380	#else
1381	#ifdef SPOTLIGHT3
1382	info=computeSpotLighting(viewDirectionW,normalW,vLightData3,vLightDirection3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness);
1383	#endif
1384	#ifdef HEMILIGHT3
1385	info=computeHemisphericLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightGround3,glossiness);
1386	#endif
1387	#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
1388	info=computeLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness);
1389	#endif
1390	#endif
1391	#ifdef PROJECTEDLIGHTTEXTURE3
1392	info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler3,textureProjectionMatrix3);
1393	#endif
1394	#endif
1395	#ifdef SHADOW3
1396	#ifdef SHADOWCLOSEESM3
1397	#if defined(SHADOWCUBE3)
1398	shadow=computeShadowWithCloseESMCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,depthValues3);
1399	#else
1400	shadow=computeShadowWithCloseESM(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,shadowsInfo3.w);
1401	#endif
1402	#else
1403	#ifdef SHADOWESM3
1404	#if defined(SHADOWCUBE3)
1405	shadow=computeShadowWithESMCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,depthValues3);
1406	#else
1407	shadow=computeShadowWithESM(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z,shadowsInfo3.w);
1408	#endif
1409	#else 
1410	#ifdef SHADOWPCF3
1411	#if defined(SHADOWCUBE3)
1412	shadow=computeShadowWithPCFCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.y,shadowsInfo3.x,depthValues3);
1413	#else
1414	shadow=computeShadowWithPCF(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.y,shadowsInfo3.x,shadowsInfo3.w);
1415	#endif
1416	#else
1417	#if defined(SHADOWCUBE3)
1418	shadow=computeShadowCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,depthValues3);
1419	#else
1420	shadow=computeShadow(vPositionFromLight3,vDepthMetric3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.w);
1421	#endif
1422	#endif
1423	#endif
1424	#endif
1425	#ifdef SHADOWONLY
1426	#ifndef SHADOWINUSE
1427	#define SHADOWINUSE
1428	#endif
1429	globalShadow+=shadow;
1430	shadowLightCount+=1.0;
1431	#endif
1432	#else
1433	shadow=1.;
1434	#endif
1435	#ifndef SHADOWONLY
1436	#ifdef CUSTOMUSERLIGHTING
1437	diffuseBase+=computeCustomDiffuseLighting(info,diffuseBase,shadow);
1438	#ifdef SPECULARTERM
1439	specularBase+=computeCustomSpecularLighting(info,specularBase,shadow);
1440	#endif
1441	#elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED3)
1442	diffuseBase+=lightmapColor*shadow;
1443	#ifdef SPECULARTERM
1444	#ifndef LIGHTMAPNOSPECULAR3
1445	specularBase+=info.specular*shadow*lightmapColor;
1446	#endif
1447	#endif
1448	#else
1449	diffuseBase+=info.diffuse*shadow;
1450	#ifdef SPECULARTERM
1451	specularBase+=info.specular*shadow;
1452	#endif
1453	#endif
1454	#endif
1455	#endif
1456	
1457	
1458	vec3 refractionColor=vec3(0.,0.,0.);
1459	#ifdef REFRACTION
1460	vec3 refractionVector=normalize(refract(-viewDirectionW,normalW,vRefractionInfos.y));
1461	#ifdef REFRACTIONMAP_3D
1462	refractionVector.y=refractionVector.y*vRefractionInfos.w;
1463	if (dot(refractionVector,viewDirectionW)<1.0)
1464	{
1465	refractionColor=textureCube(refractionCubeSampler,refractionVector).rgb*vRefractionInfos.x;
1466	}
1467	#else
1468	vec3 vRefractionUVW=vec3(refractionMatrix*(view*vec4(vPositionW+refractionVector*vRefractionInfos.z,1.0)));
1469	vec2 refractionCoords=vRefractionUVW.xy/vRefractionUVW.z;
1470	refractionCoords.y=1.0-refractionCoords.y;
1471	refractionColor=texture2D(refraction2DSampler,refractionCoords).rgb*vRefractionInfos.x;
1472	#endif
1473	#endif
1474	
1475	vec3 reflectionColor=vec3(0.,0.,0.);
1476	#ifdef REFLECTION
1477	vec3 vReflectionUVW=computeReflectionCoords(vec4(vPositionW,1.0),normalW);
1478	#ifdef REFLECTIONMAP_3D
1479	#ifdef ROUGHNESS
1480	float bias=vReflectionInfos.y;
1481	#ifdef SPECULARTERM
1482	#ifdef SPECULAR
1483	#ifdef GLOSSINESS
1484	bias*=(1.0-specularMapColor.a);
1485	#endif
1486	#endif
1487	#endif
1488	reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW,bias).rgb*vReflectionInfos.x;
1489	#else
1490	reflectionColor=textureCube(reflectionCubeSampler,vReflectionUVW).rgb*vReflectionInfos.x;
1491	#endif
1492	#else
1493	vec2 coords=vReflectionUVW.xy;
1494	#ifdef REFLECTIONMAP_PROJECTION
1495	coords/=vReflectionUVW.z;
1496	#endif
1497	coords.y=1.0-coords.y;
1498	reflectionColor=texture2D(reflection2DSampler,coords).rgb*vReflectionInfos.x;
1499	#endif
1500	#ifdef REFLECTIONFRESNEL
1501	float reflectionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,reflectionRightColor.a,reflectionLeftColor.a);
1502	#ifdef REFLECTIONFRESNELFROMSPECULAR
1503	#ifdef SPECULARTERM
1504	reflectionColor*=specularColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1505	#else
1506	reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1507	#endif
1508	#else
1509	reflectionColor*=reflectionLeftColor.rgb*(1.0-reflectionFresnelTerm)+reflectionFresnelTerm*reflectionRightColor.rgb;
1510	#endif
1511	#endif
1512	#endif
1513	#ifdef REFRACTIONFRESNEL
1514	float refractionFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,refractionRightColor.a,refractionLeftColor.a);
1515	refractionColor*=refractionLeftColor.rgb*(1.0-refractionFresnelTerm)+refractionFresnelTerm*refractionRightColor.rgb;
1516	#endif
1517	#ifdef OPACITY
1518	vec4 opacityMap=texture2D(opacitySampler,vOpacityUV+uvOffset);
1519	#ifdef OPACITYRGB
1520	opacityMap.rgb=opacityMap.rgb*vec3(0.3,0.59,0.11);
1521	alpha*=(opacityMap.x+opacityMap.y+opacityMap.z)* vOpacityInfos.y;
1522	#else
1523	alpha*=opacityMap.a*vOpacityInfos.y;
1524	#endif
1525	#endif
1526	#ifdef VERTEXALPHA
1527	alpha*=vColor.a;
1528	#endif
1529	#ifdef OPACITYFRESNEL
1530	float opacityFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,opacityParts.z,opacityParts.w);
1531	alpha+=opacityParts.x*(1.0-opacityFresnelTerm)+opacityFresnelTerm*opacityParts.y;
1532	#endif
1533	
1534	vec3 emissiveColor=vEmissiveColor;
1535	#ifdef EMISSIVE
1536	emissiveColor+=texture2D(emissiveSampler,vEmissiveUV+uvOffset).rgb*vEmissiveInfos.y;
1537	#endif
1538	#ifdef EMISSIVEFRESNEL
1539	float emissiveFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,emissiveRightColor.a,emissiveLeftColor.a);
1540	emissiveColor*=emissiveLeftColor.rgb*(1.0-emissiveFresnelTerm)+emissiveFresnelTerm*emissiveRightColor.rgb;
1541	#endif
1542	
1543	#ifdef DIFFUSEFRESNEL
1544	float diffuseFresnelTerm=computeFresnelTerm(viewDirectionW,normalW,diffuseRightColor.a,diffuseLeftColor.a);
1545	diffuseBase*=diffuseLeftColor.rgb*(1.0-diffuseFresnelTerm)+diffuseFresnelTerm*diffuseRightColor.rgb;
1546	#endif
1547	
1548	#ifdef EMISSIVEASILLUMINATION
1549	vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1550	#else
1551	#ifdef LINKEMISSIVEWITHDIFFUSE
1552	vec3 finalDiffuse=clamp((diffuseBase+emissiveColor)*diffuseColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1553	#else
1554	vec3 finalDiffuse=clamp(diffuseBase*diffuseColor+emissiveColor+vAmbientColor,0.0,1.0)*baseColor.rgb;
1555	#endif
1556	#endif
1557	#ifdef SPECULARTERM
1558	vec3 finalSpecular=specularBase*specularColor;
1559	#ifdef SPECULAROVERALPHA
1560	alpha=clamp(alpha+dot(finalSpecular,vec3(0.3,0.59,0.11)),0.,1.);
1561	#endif
1562	#else
1563	vec3 finalSpecular=vec3(0.0);
1564	#endif
1565	#ifdef REFLECTIONOVERALPHA
1566	alpha=clamp(alpha+dot(reflectionColor,vec3(0.3,0.59,0.11)),0.,1.);
1567	#endif
1568	
1569	#ifdef EMISSIVEASILLUMINATION
1570	vec4 color=vec4(clamp(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+emissiveColor+refractionColor,0.0,1.0),alpha);
1571	#else
1572	vec4 color=vec4(finalDiffuse*baseAmbientColor+finalSpecular+reflectionColor+refractionColor,alpha);
1573	#endif
1574	
1575	#ifdef LIGHTMAP
1576	#ifndef LIGHTMAPEXCLUDED
1577	#ifdef USELIGHTMAPASSHADOWMAP
1578	color.rgb*=lightmapColor;
1579	#else
1580	color.rgb+=lightmapColor;
1581	#endif
1582	#endif
1583	#endif
1584	#define CUSTOM_FRAGMENT_BEFORE_FOG
1585	color.rgb=max(color.rgb,0.);
1586	#ifdef LOGARITHMICDEPTH
1587	gl_FragDepthEXT=log2(vFragmentDepth)*logarithmicDepthConstant*0.5;
1588	#endif
1589	#ifdef FOG
1590	float fog=CalcFogFactor();
1591	color.rgb=fog*color.rgb+(1.0-fog)*vFogColor;
1592	#endif
1593	
1594	
1595	#ifdef IMAGEPROCESSINGPOSTPROCESS
1596	color.rgb=toLinearSpace(color.rgb);
1597	#else
1598	#ifdef IMAGEPROCESSING
1599	color.rgb=toLinearSpace(color.rgb);
1600	color=applyImageProcessing(color);
1601	#endif
1602	#endif
1603	#ifdef PREMULTIPLYALPHA
1604	
1605	color.rgb*=color.a;
1606	#endif
1607	#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
1608	gl_FragColor=color;
1609	}
1610	
	_ErrorEnabled (babylon.js:4:8244)
	_dumpShadersSource (babylon.js:1:6765)
	_prepareEffect (babylon.js:1:11195)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Error: ERROR: too many uniforms

	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11260)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob
[Error] BJS - [11:39:28]: Trying next fallback.
	_ErrorEnabled (babylon.js:4:8244)
	_prepareEffect (babylon.js:1:11469)
	(anonymous function) (babylon.js:1:4037)
	promiseReactionJob

 

Link to comment
Share on other sites

Ok this is the important part:

#define DIFFUSEDIRECTUV 0 2 #define AMBIENTDIRECTUV 0 3 #define OPACITYDIRECTUV 0 4 #define EMISSIVEDIRECTUV 0 5 #define SPECULARDIRECTUV 0 6 #define BUMPDIRECTUV 0 7 #define SPECULARTERM 8 #define NORMAL 9 #define NUM_BONE_INFLUENCERS 4 10 #define BonesPerMesh 33 11 #define LIGHTMAPDIRECTUV 0 12 #define MORPHTARGETS 13 #define NUM_MORPH_INFLUENCERS 1 14 #define VIGNETTEBLENDMODEMULTIPLY 15 #define SAMPLER3DGREENDEPTH 16 #define SAMPLER3DBGRMAP 17 #define LIGHT0 18 #define POINTLIGHT0

 

So you have 33 bones + morph targets + 1 light

so 33 bones means 33 * 4  = 132. You are already out of budget here

 

Link to comment
Share on other sites

7 hours ago, Deltakosh said:

Ok this is the important part:

#define DIFFUSEDIRECTUV 0 2 #define AMBIENTDIRECTUV 0 3 #define OPACITYDIRECTUV 0 4 #define EMISSIVEDIRECTUV 0 5 #define SPECULARDIRECTUV 0 6 #define BUMPDIRECTUV 0 7 #define SPECULARTERM 8 #define NORMAL 9 #define NUM_BONE_INFLUENCERS 4 10 #define BonesPerMesh 33 11 #define LIGHTMAPDIRECTUV 0 12 #define MORPHTARGETS 13 #define NUM_MORPH_INFLUENCERS 1 14 #define VIGNETTEBLENDMODEMULTIPLY 15 #define SAMPLER3DGREENDEPTH 16 #define SAMPLER3DBGRMAP 17 #define LIGHT0 18 #define POINTLIGHT0

 

So you have 33 bones + morph targets + 1 light

so 33 bones means 33 * 4  = 132. You are already out of budget here

 

Ok cool. Now I gotta figure out how it got to 33.

So let me get this straight.  Morph targets are 1 uniform per morph or just 1 for all morphs? and light is just 1 uniform?

Link to comment
Share on other sites

FIXED IT!

Well, I found a suitable workaround.

I had 26 bones (in the original demo) which was too much.  I normally have 24 but I added 2 more for the eyes.  I noticed that the hands never moved in the animation so I just removed the hand bones and it all works in iOS now.  Shadows, skyboxes, gloss and morph targets all fine.

Link to comment
Share on other sites

  • RaananW changed the title to Too many uniforms for simple mesh

Just out of interest, is this an operating system problem or hardware problem?  Because all the Android devices allow 256 uniforms but all the iPhones (even latest iPhoneX) only allows 128 uniforms.  Wondering why Apple can't just change this in an OS update.

Also, does openGL (or Metal) have a different limitation?  'cos I swear I've seen avatars with finger joints in iPhone apps

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...