Prechádzať zdrojové kódy

More shaders includes

David Catuhe 9 rokov pred
rodič
commit
fbd70c747f
37 zmenil súbory, kde vykonal 1179 pridanie a 2291 odobranie
  1. 8 8
      dist/preview release/babylon.core.js
  2. 493 493
      dist/preview release/babylon.d.ts
  3. 15 16
      dist/preview release/babylon.js
  4. 2 2
      dist/preview release/babylon.max.js
  5. 19 19
      dist/preview release/babylon.noworker.js
  6. 2 2
      materialsLibrary/dist/babylon.pbrMaterial.js
  7. 1 1
      materialsLibrary/dist/babylon.pbrMaterial.min.js
  8. 3 3
      materialsLibrary/dist/babylon.simpleMaterial.js
  9. 1 1
      materialsLibrary/dist/babylon.simpleMaterial.min.js
  10. 4 91
      materialsLibrary/materials/pbr/pbr.fragment.fx
  11. 2 33
      materialsLibrary/materials/pbr/pbr.vertex.fx
  12. 1 1
      materialsLibrary/materials/simple/babylon.simpleMaterial.ts
  13. 12 432
      materialsLibrary/materials/simple/simple.fragment.fx
  14. 3 36
      materialsLibrary/materials/simple/simple.vertex.fx
  15. 2 2
      materialsLibrary/test/refs/babylon.max.js
  16. 4 0
      src/Shaders/ShadersInclude/fogFragment.fx
  17. 35 0
      src/Shaders/ShadersInclude/fogFragmentDeclaration.fx
  18. 3 0
      src/Shaders/ShadersInclude/fogVertex.fx
  19. 3 0
      src/Shaders/ShadersInclude/fogVertexDeclaration.fx
  20. 39 0
      src/Shaders/ShadersInclude/light0Fragment.fx
  21. 22 0
      src/Shaders/ShadersInclude/light0FragmentDeclaration.fx
  22. 39 0
      src/Shaders/ShadersInclude/light1Fragment.fx
  23. 22 0
      src/Shaders/ShadersInclude/light1FragmentDeclaration.fx
  24. 39 0
      src/Shaders/ShadersInclude/light2Fragment.fx
  25. 22 0
      src/Shaders/ShadersInclude/light2FragmentDeclaration.fx
  26. 39 0
      src/Shaders/ShadersInclude/light3Fragment.fx
  27. 22 0
      src/Shaders/ShadersInclude/light3FragmentDeclaration.fx
  28. 97 0
      src/Shaders/ShadersInclude/lightsFragmentFunctions.fx
  29. 149 0
      src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx
  30. 14 0
      src/Shaders/ShadersInclude/shadowsVertex.fx
  31. 18 0
      src/Shaders/ShadersInclude/shadowsVertexDeclaration.fx
  32. 12 541
      src/Shaders/default.fragment.fx
  33. 4 42
      src/Shaders/default.vertex.fx
  34. 14 438
      src/Shaders/legacydefault.fragment.fx
  35. 7 83
      src/Shaders/legacydefault.vertex.fx
  36. 6 44
      src/Shaders/sprites.fragment.fx
  37. 1 3
      src/Shaders/sprites.vertex.fx

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 8 - 8
dist/preview release/babylon.core.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 493 - 493
dist/preview release/babylon.d.ts


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 15 - 16
dist/preview release/babylon.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 2 - 2
dist/preview release/babylon.max.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 19 - 19
dist/preview release/babylon.noworker.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 2 - 2
materialsLibrary/dist/babylon.pbrMaterial.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
materialsLibrary/dist/babylon.pbrMaterial.min.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 3 - 3
materialsLibrary/dist/babylon.simpleMaterial.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
materialsLibrary/dist/babylon.simpleMaterial.min.js


+ 4 - 91
materialsLibrary/materials/pbr/pbr.fragment.fx

@@ -288,97 +288,10 @@ varying vec4 vColor;
 #endif
 
 // Lights
-#ifdef LIGHT0
-uniform vec4 vLightData0;
-uniform vec4 vLightDiffuse0;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular0;
-#endif
-#ifdef SHADOW0
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-varying vec4 vPositionFromLight0;
-uniform sampler2D shadowSampler0;
-#else
-uniform samplerCube shadowSampler0;
-#endif
-uniform vec3 shadowsInfo0;
-#endif
-#ifdef SPOTLIGHT0
-uniform vec4 vLightDirection0;
-#endif
-#ifdef HEMILIGHT0
-uniform vec3 vLightGround0;
-#endif
-#endif
-
-#ifdef LIGHT1
-uniform vec4 vLightData1;
-uniform vec4 vLightDiffuse1;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular1;
-#endif
-#ifdef SHADOW1
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-varying vec4 vPositionFromLight1;
-uniform sampler2D shadowSampler1;
-#else
-uniform samplerCube shadowSampler1;
-#endif
-uniform vec3 shadowsInfo1;
-#endif
-#ifdef SPOTLIGHT1
-uniform vec4 vLightDirection1;
-#endif
-#ifdef HEMILIGHT1
-uniform vec3 vLightGround1;
-#endif
-#endif
-
-#ifdef LIGHT2
-uniform vec4 vLightData2;
-uniform vec4 vLightDiffuse2;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular2;
-#endif
-#ifdef SHADOW2
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-varying vec4 vPositionFromLight2;
-uniform sampler2D shadowSampler2;
-#else
-uniform samplerCube shadowSampler2;
-#endif
-uniform vec3 shadowsInfo2;
-#endif
-#ifdef SPOTLIGHT2
-uniform vec4 vLightDirection2;
-#endif
-#ifdef HEMILIGHT2
-uniform vec3 vLightGround2;
-#endif
-#endif
-
-#ifdef LIGHT3
-uniform vec4 vLightData3;
-uniform vec4 vLightDiffuse3;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular3;
-#endif
-#ifdef SHADOW3
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-varying vec4 vPositionFromLight3;
-uniform sampler2D shadowSampler3;
-#else
-uniform samplerCube shadowSampler3;
-#endif
-uniform vec3 shadowsInfo3;
-#endif
-#ifdef SPOTLIGHT3
-uniform vec4 vLightDirection3;
-#endif
-#ifdef HEMILIGHT3
-uniform vec3 vLightGround3;
-#endif
-#endif
+#include<light0FragmentDeclaration>
+#include<light1FragmentDeclaration>
+#include<light2FragmentDeclaration>
+#include<light3FragmentDeclaration>
 
 // Samplers
 #ifdef ALBEDO

+ 2 - 33
materialsLibrary/materials/pbr/pbr.vertex.fx

@@ -85,25 +85,7 @@ varying float fClipDistance;
 #endif
 
 #include<fogVertexDeclaration>
-
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-uniform mat4 lightMatrix0;
-varying vec4 vPositionFromLight0;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-uniform mat4 lightMatrix1;
-varying vec4 vPositionFromLight1;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-uniform mat4 lightMatrix2;
-varying vec4 vPositionFromLight2;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-uniform mat4 lightMatrix3;
-varying vec4 vPositionFromLight3;
-#endif
-#endif
+#include<shadowsVertexDeclaration>
 
 #ifdef REFLECTIONMAP_SKYBOX
 varying vec3 vPositionUVW;
@@ -233,20 +215,7 @@ void main(void) {
 #include<fogVertex>
 
     // Shadows
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-    vPositionFromLight0 = lightMatrix0 * worldPos;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-    vPositionFromLight1 = lightMatrix1 * worldPos;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-    vPositionFromLight2 = lightMatrix2 * worldPos;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-    vPositionFromLight3 = lightMatrix3 * worldPos;
-#endif
-#endif
+#include<shadowsVertex>
 
     // Vertex color
 #ifdef VERTEXCOLOR

+ 1 - 1
materialsLibrary/materials/simple/babylon.simpleMaterial.ts

@@ -464,7 +464,7 @@ module BABYLON {
                                 }
                             }
                             this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                            this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
+                            this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
                         }
                     }
 

+ 12 - 432
materialsLibrary/materials/simple/simple.fragment.fx

@@ -16,246 +16,23 @@ varying vec4 vColor;
 #endif
 
 // Lights
-#ifdef LIGHT0
-uniform vec4 vLightData0;
-uniform vec4 vLightDiffuse0;
-#ifdef SHADOW0
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-varying vec4 vPositionFromLight0;
-uniform sampler2D shadowSampler0;
-#else
-uniform samplerCube shadowSampler0;
-#endif
-uniform vec3 shadowsInfo0;
-#endif
-#ifdef SPOTLIGHT0
-uniform vec4 vLightDirection0;
-#endif
-#ifdef HEMILIGHT0
-uniform vec3 vLightGround0;
-#endif
-#endif
+#include<light0FragmentDeclaration>
+#include<light1FragmentDeclaration>
+#include<light2FragmentDeclaration>
+#include<light3FragmentDeclaration>
 
-#ifdef LIGHT1
-uniform vec4 vLightData1;
-uniform vec4 vLightDiffuse1;
-#ifdef SHADOW1
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-varying vec4 vPositionFromLight1;
-uniform sampler2D shadowSampler1;
-#else
-uniform samplerCube shadowSampler1;
-#endif
-uniform vec3 shadowsInfo1;
-#endif
-#ifdef SPOTLIGHT1
-uniform vec4 vLightDirection1;
-#endif
-#ifdef HEMILIGHT1
-uniform vec3 vLightGround1;
-#endif
-#endif
 
-#ifdef LIGHT2
-uniform vec4 vLightData2;
-uniform vec4 vLightDiffuse2;
-#ifdef SHADOW2
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-varying vec4 vPositionFromLight2;
-uniform sampler2D shadowSampler2;
-#else
-uniform samplerCube shadowSampler2;
-#endif
-uniform vec3 shadowsInfo2;
-#endif
-#ifdef SPOTLIGHT2
-uniform vec4 vLightDirection2;
-#endif
-#ifdef HEMILIGHT2
-uniform vec3 vLightGround2;
-#endif
-#endif
-
-#ifdef LIGHT3
-uniform vec4 vLightData3;
-uniform vec4 vLightDiffuse3;
-#ifdef SHADOW3
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-varying vec4 vPositionFromLight3;
-uniform sampler2D shadowSampler3;
-#else
-uniform samplerCube shadowSampler3;
-#endif
-uniform vec3 shadowsInfo3;
-#endif
-#ifdef SPOTLIGHT3
-uniform vec4 vLightDirection3;
-#endif
-#ifdef HEMILIGHT3
-uniform vec3 vLightGround3;
-#endif
-#endif
+#include<lightsFragmentFunctions>
+#include<shadowsFragmentFunctions>
 
 // Samplers
 #ifdef DIFFUSE
 varying vec2 vDiffuseUV;
 uniform sampler2D diffuseSampler;
 uniform vec2 vDiffuseInfos;
-#endif
-
-// Shadows
-#ifdef SHADOWS
-
-float unpack(vec4 color)
-{
-	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);
-	return dot(color, bit_shift);
-}
-
-#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
-uniform vec2 depthValues;
-
-float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
-{
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
-	
-	depth = clamp(depth, 0., 1.0);
-
-	directionToLight = normalize(directionToLight);
-	directionToLight.y = - directionToLight.y;
 
-	float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
-
-	if (depth > shadow)
-	{
-		return darkness;
-	}
-	return 1.0;
-}
-
-float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness, float mapSize)
-{
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
-
-	depth = clamp(depth, 0., 1.0);
-	float diskScale = 2.0 / mapSize;
-
-	directionToLight = normalize(directionToLight);
-	directionToLight.y = -directionToLight.y;
-
-	float visibility = 1.;
-
-	vec3 poissonDisk[4];
-	poissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);
-	poissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);
-	poissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);
-	poissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);
-
-	// Poisson Sampling
-	float biasedDepth = depth - bias;
-
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;
-
-	return  min(1.0, visibility + darkness);
-}
 #endif
 
-#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
-float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
-	{
-		return 1.0;
-	}
-
-	float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
-
-	if (depth.z > shadow)
-	{
-		return darkness;
-	}
-	return 1.;
-}
-
-float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
-	{
-		return 1.0;
-	}
-
-	float visibility = 1.;
-
-	vec2 poissonDisk[4];
-	poissonDisk[0] = vec2(-0.94201624, -0.39906216);
-	poissonDisk[1] = vec2(0.94558609, -0.76890725);
-	poissonDisk[2] = vec2(-0.094184101, -0.92938870);
-	poissonDisk[3] = vec2(0.34495938, 0.29387760);
-
-	// Poisson Sampling
-	float biasedDepth = depth.z - bias;
-
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
-
-	return  min(1.0, visibility + darkness);
-}
-
-// Thanks to http://devmaster.net/
-float unpackHalf(vec2 color)
-{
-	return color.x + (color.y / 255.0);
-}
-
-float linstep(float low, float high, float v) {
-	return clamp((v - low) / (high - low), 0.0, 1.0);
-}
-
-float ChebychevInequality(vec2 moments, float compare, float bias)
-{
-	float p = smoothstep(compare - bias, compare, moments.x);
-	float variance = max(moments.y - moments.x * moments.x, 0.02);
-	float d = compare - moments.x;
-	float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
-
-	return clamp(max(p, p_max), 0.0, 1.0);
-}
-
-float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
-	{
-		return 1.0;
-	}
-
-	vec4 texel = texture2D(shadowSampler, uv);
-
-	vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
-	return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
-}
-#endif
-#endif
-
-
 #ifdef CLIPPLANE
 varying float fClipDistance;
 #endif
@@ -263,74 +40,6 @@ varying float fClipDistance;
 // Fog
 #include<fogFragmentDeclaration>
 
-// Light Computing
-struct lightingInfo
-{
-	vec3 diffuse;
-};
-
-lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {
-	lightingInfo result;
-
-	vec3 lightVectorW;
-	float attenuation = 1.0;
-	if (lightData.w == 0.)
-	{
-		vec3 direction = lightData.xyz - vPositionW;
-
-		attenuation = max(0., 1.0 - length(direction) / range);
-		lightVectorW = normalize(direction);
-	}
-	else
-	{
-		lightVectorW = normalize(-lightData.xyz);
-	}
-
-	// diffuse
-	float ndl = max(0., dot(vNormal, lightVectorW));
-	result.diffuse = ndl * diffuseColor * attenuation;
-
-	return result;
-}
-
-lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {
-	lightingInfo result;
-
-	vec3 direction = lightData.xyz - vPositionW;
-	vec3 lightVectorW = normalize(direction);
-	float attenuation = max(0., 1.0 - length(direction) / range);
-
-	// diffuse
-	float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
-	float spotAtten = 0.0;
-
-	if (cosAngle >= lightDirection.w)
-	{
-		cosAngle = max(0., pow(cosAngle, lightData.w));
-		spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
-
-		// Diffuse
-		float ndl = max(0., dot(vNormal, -lightDirection.xyz));
-		result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
-
-		return result;
-	}
-
-	result.diffuse = vec3(0.);
-
-	return result;
-}
-
-lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {
-	lightingInfo result;
-
-	// Diffuse
-	float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
-	result.diffuse = mix(groundColor, diffuseColor, ndl);
-
-	return result;
-}
-
 void main(void) {
 	// Clip plane
 #ifdef CLIPPLANE
@@ -372,142 +81,13 @@ void main(void) {
 	// Lighting
 	vec3 diffuseBase = vec3(0., 0., 0.);
 	float shadow = 1.;
+    float glossiness = 0.;
+    
+#include<light0Fragment>
+#include<light1Fragment>
+#include<light2Fragment>
+#include<light3Fragment>
 
-#ifdef LIGHT0
-#ifdef SPOTLIGHT0
-	lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);
-#endif
-#ifdef HEMILIGHT0
-	lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);
-#endif
-#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
-	lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);
-#endif
-#ifdef SHADOW0
-#ifdef SHADOWVSM0
-	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
-#else
-#ifdef SHADOWPCF0
-	#if defined(POINTLIGHT0)
-	shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x, shadowsInfo0.y);
-	#else
-	shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
-	#endif
-#else
-	#if defined(POINTLIGHT0)
-	shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-	#else
-	shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-	#endif
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#endif
-
-#ifdef LIGHT1
-#ifdef SPOTLIGHT1
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);
-#endif
-#ifdef HEMILIGHT1
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1.a);
-#endif
-#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
-	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);
-#endif
-#ifdef SHADOW1
-#ifdef SHADOWVSM1
-	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
-#else
-#ifdef SHADOWPCF1
-#if defined(POINTLIGHT1)
-	shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x, shadowsInfo1.y);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
-#endif
-#else
-	#if defined(POINTLIGHT1)
-	shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-	#else
-	shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-	#endif
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#endif
-
-#ifdef LIGHT2
-#ifdef SPOTLIGHT2
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);
-#endif
-#ifdef HEMILIGHT2
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);
-#endif
-#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
-	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);
-#endif
-#ifdef SHADOW2
-#ifdef SHADOWVSM2
-	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
-#else
-#ifdef SHADOWPCF2
-#if defined(POINTLIGHT2)
-	shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x, shadowsInfo2.y);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
-#endif
-#else
-	#if defined(POINTLIGHT2)
-	shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-	#else
-	shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-	#endif
-#endif	
-#endif	
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#endif
-
-#ifdef LIGHT3
-#ifdef SPOTLIGHT3
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);
-#endif
-#ifdef HEMILIGHT3
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);
-#endif
-#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
-	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);
-#endif
-#ifdef SHADOW3
-#ifdef SHADOWVSM3
-		shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
-#else
-#ifdef SHADOWPCF3
-#if defined(POINTLIGHT3)
-	shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x, shadowsInfo3.y);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
-#endif
-#else
-	#if defined(POINTLIGHT3)
-	shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-	#else
-	shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-	#endif
-#endif	
-#endif	
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#endif
 
 #ifdef VERTEXALPHA
 	alpha *= vColor.a;

+ 3 - 36
materialsLibrary/materials/simple/simple.vertex.fx

@@ -49,25 +49,7 @@ varying float fClipDistance;
 #endif
 
 #include<fogVertexDeclaration>
-
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-uniform mat4 lightMatrix0;
-varying vec4 vPositionFromLight0;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-uniform mat4 lightMatrix1;
-varying vec4 vPositionFromLight1;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-uniform mat4 lightMatrix2;
-varying vec4 vPositionFromLight2;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-uniform mat4 lightMatrix3;
-varying vec4 vPositionFromLight3;
-#endif
-#endif
+#include<shadowsVertexDeclaration>
 
 void main(void) {
 
@@ -107,24 +89,9 @@ void main(void) {
 	fClipDistance = dot(worldPos, vClipPlane);
 #endif
 
-	// Fog
+    // Fog
 #include<fogVertex>
-
-	// Shadows
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-	vPositionFromLight0 = lightMatrix0 * worldPos;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-	vPositionFromLight1 = lightMatrix1 * worldPos;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-	vPositionFromLight2 = lightMatrix2 * worldPos;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-	vPositionFromLight3 = lightMatrix3 * worldPos;
-#endif
-#endif
+#include<shadowsVertex>
 
 	// Vertex color
 #ifdef VERTEXCOLOR

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 2 - 2
materialsLibrary/test/refs/babylon.max.js


+ 4 - 0
src/Shaders/ShadersInclude/fogFragment.fx

@@ -0,0 +1,4 @@
+#ifdef FOG
+	float fog = CalcFogFactor();
+	color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
+#endif

+ 35 - 0
src/Shaders/ShadersInclude/fogFragmentDeclaration.fx

@@ -0,0 +1,35 @@
+#ifdef FOG
+
+#define FOGMODE_NONE    0.
+#define FOGMODE_EXP     1.
+#define FOGMODE_EXP2    2.
+#define FOGMODE_LINEAR  3.
+#define E 2.71828
+
+uniform vec4 vFogInfos;
+uniform vec3 vFogColor;
+varying float fFogDistance;
+
+float CalcFogFactor()
+{
+	float fogCoeff = 1.0;
+	float fogStart = vFogInfos.y;
+	float fogEnd = vFogInfos.z;
+	float fogDensity = vFogInfos.w;
+
+	if (FOGMODE_LINEAR == vFogInfos.x)
+	{
+		fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
+	}
+	else if (FOGMODE_EXP == vFogInfos.x)
+	{
+		fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
+	}
+	else if (FOGMODE_EXP2 == vFogInfos.x)
+	{
+		fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
+	}
+
+	return clamp(fogCoeff, 0.0, 1.0);
+}
+#endif

+ 3 - 0
src/Shaders/ShadersInclude/fogVertex.fx

@@ -0,0 +1,3 @@
+#ifdef FOG
+fFogDistance = (view * worldPos).z;
+#endif

+ 3 - 0
src/Shaders/ShadersInclude/fogVertexDeclaration.fx

@@ -0,0 +1,3 @@
+#ifdef FOG
+	varying float fFogDistance;
+#endif

+ 39 - 0
src/Shaders/ShadersInclude/light0Fragment.fx

@@ -0,0 +1,39 @@
+#ifdef LIGHT0
+	#ifndef SPECULARTERM
+		vec3 vLightSpecular0 = vec3(0.0);
+	#endif
+	#ifdef SPOTLIGHT0
+		lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
+	#endif
+	#ifdef HEMILIGHT0
+		lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);
+	#endif
+	#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
+		lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
+	#endif
+	#ifdef SHADOW0
+		#ifdef SHADOWVSM0
+			shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
+		#else
+		#ifdef SHADOWPCF0
+			#if defined(POINTLIGHT0)
+				shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
+			#else
+				shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
+			#endif
+		#else
+			#if defined(POINTLIGHT0)
+				shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
+			#else
+				shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
+			#endif
+		#endif
+	#endif
+	#else
+		shadow = 1.;
+	#endif
+		diffuseBase += info.diffuse * shadow;
+	#ifdef SPECULARTERM
+		specularBase += info.specular * shadow;
+	#endif
+#endif

+ 22 - 0
src/Shaders/ShadersInclude/light0FragmentDeclaration.fx

@@ -0,0 +1,22 @@
+#ifdef LIGHT0
+	uniform vec4 vLightData0;
+	uniform vec4 vLightDiffuse0;
+	#ifdef SPECULARTERM
+		uniform vec3 vLightSpecular0;
+	#endif
+	#ifdef SHADOW0
+		#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
+			varying vec4 vPositionFromLight0;
+			uniform sampler2D shadowSampler0;
+		#else
+			uniform samplerCube shadowSampler0;
+		#endif
+		uniform vec3 shadowsInfo0;
+	#endif
+	#ifdef SPOTLIGHT0
+		uniform vec4 vLightDirection0;
+	#endif
+	#ifdef HEMILIGHT0
+		uniform vec3 vLightGround0;
+	#endif
+#endif

+ 39 - 0
src/Shaders/ShadersInclude/light1Fragment.fx

@@ -0,0 +1,39 @@
+#ifdef LIGHT1
+	#ifndef SPECULARTERM
+		vec3 vLightSpecular1 = vec3(0.0);
+	#endif
+	#ifdef SPOTLIGHT1
+		info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
+	#endif
+	#ifdef HEMILIGHT1
+		info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, glossiness);
+	#endif
+	#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
+		info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
+	#endif
+	#ifdef SHADOW1
+		#ifdef SHADOWVSM1
+			shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
+		#else
+			#ifdef SHADOWPCF1
+				#if defined(POINTLIGHT1)
+					shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
+				#else
+					shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
+				#endif
+			#else
+				#if defined(POINTLIGHT1)
+					shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
+				#else
+					shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
+				#endif
+			#endif
+		#endif
+		#else
+			shadow = 1.;
+	#endif
+	diffuseBase += info.diffuse * shadow;
+	#ifdef SPECULARTERM
+		specularBase += info.specular * shadow;
+	#endif
+#endif

+ 22 - 0
src/Shaders/ShadersInclude/light1FragmentDeclaration.fx

@@ -0,0 +1,22 @@
+#ifdef LIGHT1
+	uniform vec4 vLightData1;
+	uniform vec4 vLightDiffuse1;
+	#ifdef SPECULARTERM
+		uniform vec3 vLightSpecular1;
+	#endif
+	#ifdef SHADOW1
+		#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
+			varying vec4 vPositionFromLight1;
+			uniform sampler2D shadowSampler1;
+		#else
+			uniform samplerCube shadowSampler1;
+		#endif
+		uniform vec3 shadowsInfo1;
+	#endif
+	#ifdef SPOTLIGHT1
+		uniform vec4 vLightDirection1;
+	#endif
+	#ifdef HEMILIGHT1
+		uniform vec3 vLightGround1;
+	#endif
+#endif

+ 39 - 0
src/Shaders/ShadersInclude/light2Fragment.fx

@@ -0,0 +1,39 @@
+#ifdef LIGHT2
+	#ifndef SPECULARTERM
+		vec3 vLightSpecular2 = vec3(0.0);
+	#endif
+	#ifdef SPOTLIGHT2
+		info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
+	#endif
+	#ifdef HEMILIGHT2
+		info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);
+	#endif
+	#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
+		info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
+	#endif
+	#ifdef SHADOW2
+		#ifdef SHADOWVSM2
+			shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
+		#else
+			#ifdef SHADOWPCF2
+				#if defined(POINTLIGHT2)
+					shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
+				#else
+					shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
+				#endif
+			#else
+				#if defined(POINTLIGHT2)
+					shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+				#else
+					shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
+				#endif
+			#endif	
+		#endif	
+		#else
+		shadow = 1.;
+	#endif
+	diffuseBase += info.diffuse * shadow;
+	#ifdef SPECULARTERM
+		specularBase += info.specular * shadow;
+	#endif
+#endif

+ 22 - 0
src/Shaders/ShadersInclude/light2FragmentDeclaration.fx

@@ -0,0 +1,22 @@
+#ifdef LIGHT2
+	uniform vec4 vLightData2;
+	uniform vec4 vLightDiffuse2;
+	#ifdef SPECULARTERM
+		uniform vec3 vLightSpecular2;
+	#endif
+	#ifdef SHADOW2
+		#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
+			varying vec4 vPositionFromLight2;
+			uniform sampler2D shadowSampler2;
+		#else
+			uniform samplerCube shadowSampler2;
+		#endif
+		uniform vec3 shadowsInfo2;
+	#endif
+	#ifdef SPOTLIGHT2
+		uniform vec4 vLightDirection2;
+	#endif
+	#ifdef HEMILIGHT2
+		uniform vec3 vLightGround2;
+	#endif
+#endif

+ 39 - 0
src/Shaders/ShadersInclude/light3Fragment.fx

@@ -0,0 +1,39 @@
+#ifdef LIGHT3
+	#ifndef SPECULARTERM
+		vec3 vLightSpecular3 = vec3(0.0);
+	#endif
+	#ifdef SPOTLIGHT3
+		info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
+	#endif
+	#ifdef HEMILIGHT3
+		info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);
+	#endif
+	#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
+		info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
+	#endif
+	#ifdef SHADOW3
+		#ifdef SHADOWVSM3
+			shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
+		#else
+			#ifdef SHADOWPCF3
+				#if defined(POINTLIGHT3)
+					shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
+				#else
+					shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
+				#endif
+			#else
+				#if defined(POINTLIGHT3)
+					shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+				#else
+					shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
+				#endif
+			#endif	
+		#endif	
+	#else
+		shadow = 1.;
+	#endif
+	diffuseBase += info.diffuse * shadow;
+	#ifdef SPECULARTERM
+		specularBase += info.specular * shadow;
+	#endif
+#endif

+ 22 - 0
src/Shaders/ShadersInclude/light3FragmentDeclaration.fx

@@ -0,0 +1,22 @@
+#ifdef LIGHT3
+	uniform vec4 vLightData3;
+	uniform vec4 vLightDiffuse3;
+	#ifdef SPECULARTERM
+		uniform vec3 vLightSpecular3;
+	#endif
+	#ifdef SHADOW3
+		#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
+			varying vec4 vPositionFromLight3;
+			uniform sampler2D shadowSampler3;
+		#else
+			uniform samplerCube shadowSampler3;
+		#endif
+		uniform vec3 shadowsInfo3;
+	#endif
+	#ifdef SPOTLIGHT3
+		uniform vec4 vLightDirection3;
+	#endif
+	#ifdef HEMILIGHT3
+		uniform vec3 vLightGround3;
+	#endif
+#endif

+ 97 - 0
src/Shaders/ShadersInclude/lightsFragmentFunctions.fx

@@ -0,0 +1,97 @@
+// Light Computing
+struct lightingInfo
+{
+	vec3 diffuse;
+#ifdef SPECULARTERM
+	vec3 specular;
+#endif
+};
+
+lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
+	lightingInfo result;
+
+	vec3 lightVectorW;
+	float attenuation = 1.0;
+	if (lightData.w == 0.)
+	{
+		vec3 direction = lightData.xyz - vPositionW;
+
+		attenuation = max(0., 1.0 - length(direction) / range);
+		lightVectorW = normalize(direction);
+	}
+	else
+	{
+		lightVectorW = normalize(-lightData.xyz);
+	}
+
+	// diffuse
+	float ndl = max(0., dot(vNormal, lightVectorW));
+	result.diffuse = ndl * diffuseColor * attenuation;
+
+#ifdef SPECULARTERM
+	// Specular
+	vec3 angleW = normalize(viewDirectionW + lightVectorW);
+	float specComp = max(0., dot(vNormal, angleW));
+	specComp = pow(specComp, max(1., glossiness));
+
+	result.specular = specComp * specularColor * attenuation;
+#endif
+	return result;
+}
+
+lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
+	lightingInfo result;
+
+	vec3 direction = lightData.xyz - vPositionW;
+	vec3 lightVectorW = normalize(direction);
+	float attenuation = max(0., 1.0 - length(direction) / range);
+
+	// diffuse
+	float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
+
+	if (cosAngle >= lightDirection.w)
+	{
+		cosAngle = max(0., pow(cosAngle, lightData.w));
+		attenuation *= cosAngle;
+
+		// Diffuse
+		float ndl = max(0., dot(vNormal, -lightDirection.xyz));
+		result.diffuse = ndl * diffuseColor * attenuation;
+
+#ifdef SPECULARTERM
+		// Specular
+		vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
+		float specComp = max(0., dot(vNormal, angleW));
+		specComp = pow(specComp, max(1., glossiness));
+
+		result.specular = specComp * specularColor * attenuation;
+#endif
+		return result;
+	}
+
+	result.diffuse = vec3(0.);
+#ifdef SPECULARTERM
+	result.specular = vec3(0.);
+#endif
+
+	return result;
+}
+
+lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {
+	lightingInfo result;
+
+	// Diffuse
+	float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
+	result.diffuse = mix(groundColor, diffuseColor, ndl);
+
+#ifdef SPECULARTERM
+	// Specular
+	vec3 angleW = normalize(viewDirectionW + lightData.xyz);
+	float specComp = max(0., dot(vNormal, angleW));
+	specComp = pow(specComp, max(1., glossiness));
+
+	result.specular = specComp * specularColor;
+#endif
+
+	return result;
+}

+ 149 - 0
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -0,0 +1,149 @@
+#ifdef SHADOWS
+	float unpack(vec4 color)
+	{
+		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);
+		return dot(color, bit_shift);
+	}
+
+	#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
+		uniform vec2 depthValues;
+
+		float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
+		{
+			vec3 directionToLight = vPositionW - lightPosition;
+			float depth = length(directionToLight);
+			depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
+			depth = clamp(depth, 0., 1.0);
+
+			directionToLight = normalize(directionToLight);
+			directionToLight.y = -directionToLight.y;
+
+			float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
+
+			if (depth > shadow)
+			{
+				return darkness;
+			}
+			return 1.0;
+		}
+
+		float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
+		{
+			vec3 directionToLight = vPositionW - lightPosition;
+			float depth = length(directionToLight);
+
+			depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
+			depth = clamp(depth, 0., 1.0);
+
+			directionToLight = normalize(directionToLight);
+			directionToLight.y = -directionToLight.y;
+
+			float visibility = 1.;
+
+			vec3 poissonDisk[4];
+			poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
+			poissonDisk[1] = vec3(1.0, -1.0, -1.0);
+			poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
+			poissonDisk[3] = vec3(1.0, -1.0, 1.0);
+
+			// Poisson Sampling
+			float biasedDepth = depth - bias;
+
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
+
+			return  min(1.0, visibility + darkness);
+		}
+	#endif
+
+	#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
+		float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
+		{
+			vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
+			depth = 0.5 * depth + vec3(0.5);
+			vec2 uv = depth.xy;
+
+			if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
+			{
+				return 1.0;
+			}
+
+			float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
+
+			if (depth.z > shadow)
+			{
+				return darkness;
+			}
+			return 1.;
+		}
+
+		float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
+		{
+			vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
+			depth = 0.5 * depth + vec3(0.5);
+			vec2 uv = depth.xy;
+
+			if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
+			{
+				return 1.0;
+			}
+
+			float visibility = 1.;
+
+			vec2 poissonDisk[4];
+			poissonDisk[0] = vec2(-0.94201624, -0.39906216);
+			poissonDisk[1] = vec2(0.94558609, -0.76890725);
+			poissonDisk[2] = vec2(-0.094184101, -0.92938870);
+			poissonDisk[3] = vec2(0.34495938, 0.29387760);
+
+			// Poisson Sampling
+			float biasedDepth = depth.z - bias;
+
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
+
+			return  min(1.0, visibility + darkness);
+		}
+
+		// Thanks to http://devmaster.net/
+		float unpackHalf(vec2 color)
+		{
+			return color.x + (color.y / 255.0);
+		}
+
+		float linstep(float low, float high, float v) {
+			return clamp((v - low) / (high - low), 0.0, 1.0);
+		}
+
+		float ChebychevInequality(vec2 moments, float compare, float bias)
+		{
+			float p = smoothstep(compare - bias, compare, moments.x);
+			float variance = max(moments.y - moments.x * moments.x, 0.02);
+			float d = compare - moments.x;
+			float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
+
+			return clamp(max(p, p_max), 0.0, 1.0);
+		}
+
+		float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
+		{
+			vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
+			depth = 0.5 * depth + vec3(0.5);
+			vec2 uv = depth.xy;
+
+			if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
+			{
+				return 1.0;
+			}
+
+			vec4 texel = texture2D(shadowSampler, uv);
+
+			vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
+			return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
+		}
+	#endif
+#endif

+ 14 - 0
src/Shaders/ShadersInclude/shadowsVertex.fx

@@ -0,0 +1,14 @@
+#ifdef SHADOWS
+	#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
+		vPositionFromLight0 = lightMatrix0 * worldPos;
+	#endif
+	#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
+		vPositionFromLight1 = lightMatrix1 * worldPos;
+	#endif
+	#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
+		vPositionFromLight2 = lightMatrix2 * worldPos;
+	#endif
+	#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
+		vPositionFromLight3 = lightMatrix3 * worldPos;
+	#endif
+#endif

+ 18 - 0
src/Shaders/ShadersInclude/shadowsVertexDeclaration.fx

@@ -0,0 +1,18 @@
+#ifdef SHADOWS
+	#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
+		uniform mat4 lightMatrix0;
+		varying vec4 vPositionFromLight0;
+	#endif
+	#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
+		uniform mat4 lightMatrix1;
+		varying vec4 vPositionFromLight1;
+	#endif
+	#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
+		uniform mat4 lightMatrix2;
+		varying vec4 vPositionFromLight2;
+	#endif
+	#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
+		uniform mat4 lightMatrix3;
+		varying vec4 vPositionFromLight3;
+	#endif
+#endif

+ 12 - 541
src/Shaders/default.fragment.fx

@@ -31,97 +31,13 @@ varying vec4 vColor;
 #endif
 
 // Lights
-#ifdef LIGHT0
-uniform vec4 vLightData0;
-uniform vec4 vLightDiffuse0;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular0;
-#endif
-#ifdef SHADOW0
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-varying vec4 vPositionFromLight0;
-uniform sampler2D shadowSampler0;
-#else
-uniform samplerCube shadowSampler0;
-#endif
-uniform vec3 shadowsInfo0;
-#endif
-#ifdef SPOTLIGHT0
-uniform vec4 vLightDirection0;
-#endif
-#ifdef HEMILIGHT0
-uniform vec3 vLightGround0;
-#endif
-#endif
-
-#ifdef LIGHT1
-uniform vec4 vLightData1;
-uniform vec4 vLightDiffuse1;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular1;
-#endif
-#ifdef SHADOW1
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-varying vec4 vPositionFromLight1;
-uniform sampler2D shadowSampler1;
-#else
-uniform samplerCube shadowSampler1;
-#endif
-uniform vec3 shadowsInfo1;
-#endif
-#ifdef SPOTLIGHT1
-uniform vec4 vLightDirection1;
-#endif
-#ifdef HEMILIGHT1
-uniform vec3 vLightGround1;
-#endif
-#endif
-
-#ifdef LIGHT2
-uniform vec4 vLightData2;
-uniform vec4 vLightDiffuse2;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular2;
-#endif
-#ifdef SHADOW2
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-varying vec4 vPositionFromLight2;
-uniform sampler2D shadowSampler2;
-#else
-uniform samplerCube shadowSampler2;
-#endif
-uniform vec3 shadowsInfo2;
-#endif
-#ifdef SPOTLIGHT2
-uniform vec4 vLightDirection2;
-#endif
-#ifdef HEMILIGHT2
-uniform vec3 vLightGround2;
-#endif
-#endif
+#include<light0FragmentDeclaration>
+#include<light1FragmentDeclaration>
+#include<light2FragmentDeclaration>
+#include<light3FragmentDeclaration>
 
-#ifdef LIGHT3
-uniform vec4 vLightData3;
-uniform vec4 vLightDiffuse3;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular3;
-#endif
-#ifdef SHADOW3
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-varying vec4 vPositionFromLight3;
-uniform sampler2D shadowSampler3;
-#else
-uniform samplerCube shadowSampler3;
-#endif
-uniform vec3 shadowsInfo3;
-#endif
-#ifdef SPOTLIGHT3
-uniform vec4 vLightDirection3;
-#endif
-#ifdef HEMILIGHT3
-uniform vec3 vLightGround3;
-#endif
-#endif
+#include<lightsFragmentFunctions>
+#include<shadowsFragmentFunctions>
 
 // Samplers
 #ifdef DIFFUSE
@@ -294,158 +210,6 @@ uniform vec4 reflectionRightColor;
 
 #endif
 
-// Shadows
-#ifdef SHADOWS
-
-float unpack(vec4 color)
-{
-	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);
-	return dot(color, bit_shift);
-}
-
-#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
-uniform vec2 depthValues;
-
-float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
-{
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
-	depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
-	depth = clamp(depth, 0., 1.0);
-
-	directionToLight = normalize(directionToLight);
-	directionToLight.y = - directionToLight.y;
-
-	float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
-
-	if (depth > shadow)
-	{
-		return darkness;
-	}
-	return 1.0;
-}
-
-float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
-{
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
-
-	depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
-	depth = clamp(depth, 0., 1.0);
-
-	directionToLight = normalize(directionToLight);
-	directionToLight.y = -directionToLight.y;
-
-	float visibility = 1.;
-
-	vec3 poissonDisk[4];
-	poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
-	poissonDisk[1] = vec3(1.0, -1.0, -1.0);
-	poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
-	poissonDisk[3] = vec3(1.0, -1.0, 1.0);
-
-	// Poisson Sampling
-	float biasedDepth = depth - bias;
-
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
-
-	return  min(1.0, visibility + darkness);
-}
-#endif
-
-#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
-float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
-	{
-		return 1.0;
-	}
-
-	float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
-
-	if (depth.z > shadow)
-	{
-		return darkness;
-	}
-	return 1.;
-}
-
-float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
-	{
-		return 1.0;
-	}
-
-	float visibility = 1.;
-
-	vec2 poissonDisk[4];
-	poissonDisk[0] = vec2(-0.94201624, -0.39906216);
-	poissonDisk[1] = vec2(0.94558609, -0.76890725);
-	poissonDisk[2] = vec2(-0.094184101, -0.92938870);
-	poissonDisk[3] = vec2(0.34495938, 0.29387760);
-
-	// Poisson Sampling
-	float biasedDepth = depth.z - bias;
-
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
-	if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
-
-	return  min(1.0, visibility + darkness);
-}
-
-// Thanks to http://devmaster.net/
-float unpackHalf(vec2 color)
-{
-	return color.x + (color.y / 255.0);
-}
-
-float linstep(float low, float high, float v) {
-	return clamp((v - low) / (high - low), 0.0, 1.0);
-}
-
-float ChebychevInequality(vec2 moments, float compare, float bias)
-{
-	float p = smoothstep(compare - bias, compare, moments.x);
-	float variance = max(moments.y - moments.x * moments.x, 0.02);
-	float d = compare - moments.x;
-	float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
-
-	return clamp(max(p, p_max), 0.0, 1.0);
-}
-
-float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
-	{
-		return 1.0;
-	}
-
-	vec4 texel = texture2D(shadowSampler, uv);
-
-	vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
-	return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
-}
-#endif
-#endif
-
 // Bump
 #ifdef BUMP
 varying vec2 vBumpUV;
@@ -490,141 +254,7 @@ uniform float logarithmicDepthConstant;
 varying float vFragmentDepth;
 #endif
 
-// Fog
-#ifdef FOG
-
-#define FOGMODE_NONE    0.
-#define FOGMODE_EXP     1.
-#define FOGMODE_EXP2    2.
-#define FOGMODE_LINEAR  3.
-#define E 2.71828
-
-uniform vec4 vFogInfos;
-uniform vec3 vFogColor;
-varying float fFogDistance;
-
-float CalcFogFactor()
-{
-	float fogCoeff = 1.0;
-	float fogStart = vFogInfos.y;
-	float fogEnd = vFogInfos.z;
-	float fogDensity = vFogInfos.w;
-
-	if (FOGMODE_LINEAR == vFogInfos.x)
-	{
-		fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
-	}
-	else if (FOGMODE_EXP == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
-	}
-	else if (FOGMODE_EXP2 == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
-	}
-
-	return clamp(fogCoeff, 0.0, 1.0);
-}
-#endif
-
-// Light Computing
-struct lightingInfo
-{
-	vec3 diffuse;
-#ifdef SPECULARTERM
-	vec3 specular;
-#endif
-};
-
-lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
-	lightingInfo result;
-
-	vec3 lightVectorW;
-	float attenuation = 1.0;
-	if (lightData.w == 0.)
-	{
-		vec3 direction = lightData.xyz - vPositionW;
-
-		attenuation = max(0., 1.0 - length(direction) / range);
-		lightVectorW = normalize(direction);
-	}
-	else
-	{
-		lightVectorW = normalize(-lightData.xyz);
-	}
-
-	// diffuse
-	float ndl = max(0., dot(vNormal, lightVectorW));
-	result.diffuse = ndl * diffuseColor * attenuation;
-
-#ifdef SPECULARTERM
-	// Specular
-	vec3 angleW = normalize(viewDirectionW + lightVectorW);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = pow(specComp, max(1., glossiness));
-
-	result.specular = specComp * specularColor * attenuation;
-#endif
-	return result;
-}
-
-lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
-	lightingInfo result;
-
-	vec3 direction = lightData.xyz - vPositionW;
-	vec3 lightVectorW = normalize(direction);
-	float attenuation = max(0., 1.0 - length(direction) / range);
-
-	// diffuse
-	float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
-
-	if (cosAngle >= lightDirection.w)
-	{
-		cosAngle = max(0., pow(cosAngle, lightData.w));
-		attenuation *= cosAngle;
-
-		// Diffuse
-		float ndl = max(0., dot(vNormal, -lightDirection.xyz));
-		result.diffuse = ndl * diffuseColor * attenuation;
-
-#ifdef SPECULARTERM
-		// Specular
-		vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
-		float specComp = max(0., dot(vNormal, angleW));
-		specComp = pow(specComp, max(1., glossiness));
-
-		result.specular = specComp * specularColor * attenuation;
-#endif
-
-		return result;
-	}
-
-	result.diffuse = vec3(0.);
-#ifdef SPECULARTERM
-	result.specular = vec3(0.);
-#endif
-
-	return result;
-}
-
-lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {
-	lightingInfo result;
-
-	// Diffuse
-	float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
-	result.diffuse = mix(groundColor, diffuseColor, ndl);
-
-#ifdef SPECULARTERM
-	// Specular
-	vec3 angleW = normalize(viewDirectionW + lightData.xyz);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = pow(specComp, max(1., glossiness));
-
-	result.specular = specComp * specularColor;
-#endif
-
-	return result;
-}
+#include<fogFragmentDeclaration>
 
 void main(void) {
 	// Clip plane
@@ -680,7 +310,6 @@ void main(void) {
 	baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
 #endif
 
-
 	// Specular map
 #ifdef SPECULARTERM
 	float glossiness = vSpecularColor.a;
@@ -704,165 +333,10 @@ void main(void) {
 #endif
 	float shadow = 1.;
 
-#ifdef LIGHT0
-#ifndef SPECULARTERM
-	vec3 vLightSpecular0 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT0
-	lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
-#endif
-#ifdef HEMILIGHT0
-	lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);
-#endif
-#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
-	lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
-#endif
-#ifdef SHADOW0
-#ifdef SHADOWVSM0
-	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
-#else
-#ifdef SHADOWPCF0
-#if defined(POINTLIGHT0)
-	shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
-#endif
-#else
-#if defined(POINTLIGHT0)
-	shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-#else
-	shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-#endif
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#ifdef SPECULARTERM
-	specularBase += info.specular * shadow;
-#endif
-#endif
-
-#ifdef LIGHT1
-#ifndef SPECULARTERM
-	vec3 vLightSpecular1 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT1
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
-#endif
-#ifdef HEMILIGHT1
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, glossiness);
-#endif
-#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
-	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
-#endif
-#ifdef SHADOW1
-#ifdef SHADOWVSM1
-	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
-#else
-#ifdef SHADOWPCF1
-#if defined(POINTLIGHT1)
-	shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
-#endif
-#else
-#if defined(POINTLIGHT1)
-	shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-#else
-	shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-#endif
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#ifdef SPECULARTERM
-	specularBase += info.specular * shadow;
-#endif
-#endif
-
-#ifdef LIGHT2
-#ifndef SPECULARTERM
-	vec3 vLightSpecular2 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT2
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
-#endif
-#ifdef HEMILIGHT2
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);
-#endif
-#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
-	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
-#endif
-#ifdef SHADOW2
-#ifdef SHADOWVSM2
-	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
-#else
-#ifdef SHADOWPCF2
-#if defined(POINTLIGHT2)
-	shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
-#endif
-#else
-#if defined(POINTLIGHT2)
-	shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-#else
-	shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-#endif
-#endif	
-#endif	
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#ifdef SPECULARTERM
-	specularBase += info.specular * shadow;
-#endif
-#endif
-
-#ifdef LIGHT3
-#ifndef SPECULARTERM
-	vec3 vLightSpecular3 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT3
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
-#endif
-#ifdef HEMILIGHT3
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);
-#endif
-#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
-	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
-#endif
-#ifdef SHADOW3
-#ifdef SHADOWVSM3
-	shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
-#else
-#ifdef SHADOWPCF3
-#if defined(POINTLIGHT3)
-	shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
-#else
-	shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
-#endif
-#else
-#if defined(POINTLIGHT3)
-	shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-#else
-	shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-#endif
-#endif	
-#endif	
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info.diffuse * shadow;
-#ifdef SPECULARTERM
-	specularBase += info.specular * shadow;
-#endif
-#endif
+#include<light0Fragment>
+#include<light1Fragment>
+#include<light2Fragment>
+#include<light3Fragment>
 
 	// Refraction
 	vec3 refractionColor = vec3(0., 0., 0.);
@@ -1027,10 +501,7 @@ void main(void) {
 	gl_FragDepthEXT = log2(vFragmentDepth) * logarithmicDepthConstant * 0.5;
 #endif
 
-#ifdef FOG
-	float fog = CalcFogFactor();
-	color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
-#endif
+#include<fogFragment>
 
 	gl_FragColor = color;
 }

+ 4 - 42
src/Shaders/default.vertex.fx

@@ -84,28 +84,8 @@ uniform vec4 vClipPlane;
 varying float fClipDistance;
 #endif
 
-#ifdef FOG
-varying float fFogDistance;
-#endif
-
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-uniform mat4 lightMatrix0;
-varying vec4 vPositionFromLight0;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-uniform mat4 lightMatrix1;
-varying vec4 vPositionFromLight1;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-uniform mat4 lightMatrix2;
-varying vec4 vPositionFromLight2;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-uniform mat4 lightMatrix3;
-varying vec4 vPositionFromLight3;
-#endif
-#endif
+#include<fogVertexDeclaration>
+#include<shadowsVertexDeclaration>
 
 #ifdef REFLECTIONMAP_SKYBOX
 varying vec3 vPositionUVW;
@@ -231,26 +211,8 @@ void main(void) {
 	fClipDistance = dot(worldPos, vClipPlane);
 #endif
 
-	// Fog
-#ifdef FOG
-	fFogDistance = (view * worldPos).z;
-#endif
-
-	// Shadows
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-	vPositionFromLight0 = lightMatrix0 * worldPos;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-	vPositionFromLight1 = lightMatrix1 * worldPos;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-	vPositionFromLight2 = lightMatrix2 * worldPos;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-	vPositionFromLight3 = lightMatrix3 * worldPos;
-#endif
-#endif
+#include<fogVertex>
+#include<shadowsVertex>
 
 	// Vertex color
 #ifdef VERTEXCOLOR

+ 14 - 438
src/Shaders/legacydefault.fragment.fx

@@ -20,97 +20,13 @@ varying vec4 vColor;
 #endif
 
 // Lights
-#ifdef LIGHT0
-uniform vec4 vLightData0;
-uniform vec4 vLightDiffuse0;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular0;
-#endif
-#ifdef SHADOW0
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-varying vec4 vPositionFromLight0;
-uniform sampler2D shadowSampler0;
-#else
-uniform samplerCube shadowSampler0;
-#endif
-uniform vec3 shadowsInfo0;
-#endif
-#ifdef SPOTLIGHT0
-uniform vec4 vLightDirection0;
-#endif
-#ifdef HEMILIGHT0
-uniform vec3 vLightGround0;
-#endif
-#endif
-
-#ifdef LIGHT1
-uniform vec4 vLightData1;
-uniform vec4 vLightDiffuse1;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular1;
-#endif
-#ifdef SHADOW1
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-varying vec4 vPositionFromLight1;
-uniform sampler2D shadowSampler1;
-#else
-uniform samplerCube shadowSampler1;
-#endif
-uniform vec3 shadowsInfo1;
-#endif
-#ifdef SPOTLIGHT1
-uniform vec4 vLightDirection1;
-#endif
-#ifdef HEMILIGHT1
-uniform vec3 vLightGround1;
-#endif
-#endif
-
-#ifdef LIGHT2
-uniform vec4 vLightData2;
-uniform vec4 vLightDiffuse2;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular2;
-#endif
-#ifdef SHADOW2
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-varying vec4 vPositionFromLight2;
-uniform sampler2D shadowSampler2;
-#else
-uniform samplerCube shadowSampler2;
-#endif
-uniform vec3 shadowsInfo2;
-#endif
-#ifdef SPOTLIGHT2
-uniform vec4 vLightDirection2;
-#endif
-#ifdef HEMILIGHT2
-uniform vec3 vLightGround2;
-#endif
-#endif
+#include<light0FragmentDeclaration>
+#include<light1FragmentDeclaration>
+#include<light2FragmentDeclaration>
+#include<light3FragmentDeclaration>
 
-#ifdef LIGHT3
-uniform vec4 vLightData3;
-uniform vec4 vLightDiffuse3;
-#ifdef SPECULARTERM
-uniform vec3 vLightSpecular3;
-#endif
-#ifdef SHADOW3
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-varying vec4 vPositionFromLight3;
-uniform sampler2D shadowSampler3;
-#else
-uniform samplerCube shadowSampler3;
-#endif
-uniform vec3 shadowsInfo3;
-#endif
-#ifdef SPOTLIGHT3
-uniform vec4 vLightDirection3;
-#endif
-#ifdef HEMILIGHT3
-uniform vec3 vLightGround3;
-#endif
-#endif
+#include<lightsFragmentFunctions>
+#include<shadowsFragmentFunctions>
 
 // Samplers
 #ifdef DIFFUSE
@@ -182,229 +98,13 @@ uniform vec4 emissiveRightColor;
 #endif
 
 // Shadows
-#ifdef SHADOWS
-
-float unpack(vec4 color)
-{
-	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);
-	return dot(color, bit_shift);
-}
-
-#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
-float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
-{
-	vec3 directionToLight = vPositionW - lightPosition;
-	float depth = length(directionToLight);
-
-	depth = clamp(depth, 0., 1.);
-
-	directionToLight.y = 1.0 - directionToLight.y;
-
-	float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
-
-	if (depth > shadow)
-	{
-		return darkness;
-	}
-	return 1.0;
-}
-#endif
-
-#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) ||  defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
-float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
-	{
-		return 1.0;
-	}
-
-	float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
-
-	if (depth.z > shadow)
-	{
-		return darkness;
-	}
-	return 1.;
-}
-
-// Thanks to http://devmaster.net/
-float unpackHalf(vec2 color)
-{
-	return color.x + (color.y / 255.0);
-}
-
-float linstep(float low, float high, float v) {
-	return clamp((v - low) / (high - low), 0.0, 1.0);
-}
-
-float ChebychevInequality(vec2 moments, float compare, float bias)
-{
-	float p = smoothstep(compare - bias, compare, moments.x);
-	float variance = max(moments.y - moments.x * moments.x, 0.02);
-	float d = compare - moments.x;
-	float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
-
-	return clamp(max(p, p_max), 0.0, 1.0);
-}
-
-float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
-{
-	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
-	depth = 0.5 * depth + vec3(0.5);
-	vec2 uv = depth.xy;
-
-	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
-	{
-		return 1.0;
-	}
-
-	vec4 texel = texture2D(shadowSampler, uv);
-
-	vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
-	return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
-}
-#endif
-#endif
 
 #ifdef CLIPPLANE
 varying float fClipDistance;
 #endif
 
 // Fog
-#ifdef FOG
-
-#define FOGMODE_NONE    0.
-#define FOGMODE_EXP     1.
-#define FOGMODE_EXP2    2.
-#define FOGMODE_LINEAR  3.
-#define E 2.71828
-
-uniform vec4 vFogInfos;
-uniform vec3 vFogColor;
-varying float fFogDistance;
-
-float CalcFogFactor()
-{
-	float fogCoeff = 1.0;
-	float fogStart = vFogInfos.y;
-	float fogEnd = vFogInfos.z;
-	float fogDensity = vFogInfos.w;
-
-	if (FOGMODE_LINEAR == vFogInfos.x)
-	{
-		fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
-	}
-	else if (FOGMODE_EXP == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
-	}
-	else if (FOGMODE_EXP2 == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
-	}
-
-	return clamp(fogCoeff, 0.0, 1.0);
-}
-#endif
-
-// Light Computing
-mat3 computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 diffuseColor, vec3 specularColor) {
-	mat3 result;
-
-	vec3 lightVectorW;
-	if (lightData.w == 0.)
-	{
-		lightVectorW = normalize(lightData.xyz - vPositionW);
-	}
-	else
-	{
-		lightVectorW = normalize(-lightData.xyz);
-	}
-
-	// diffuse
-	float ndl = max(0., dot(vNormal, lightVectorW));
-
-	result[0] = ndl * diffuseColor.rgb;
-
-#ifdef SPECULARTERM
-	// Specular
-	vec3 angleW = normalize(viewDirectionW + lightVectorW);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = max(0., pow(specComp, max(1.0, vSpecularColor.a)));
-	result[1] = specComp * specularColor;
-#else
-	result[1] = vec3(0.);
-#endif
-
-	result[2] = vec3(0.);
-
-	return result;
-}
-
-mat3 computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec4 diffuseColor, vec3 specularColor) {
-	mat3 result;
-
-	vec3 lightVectorW = normalize(lightData.xyz - vPositionW);
-
-	// diffuse
-	float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
-	float spotAtten = 0.0;
-
-	if (cosAngle >= lightDirection.w)
-	{
-		cosAngle = max(0., pow(cosAngle, lightData.w));
-		spotAtten = max(0., (cosAngle - lightDirection.w) / (1. - cosAngle));
-
-		// Diffuse
-		float ndl = max(0., dot(vNormal, -lightDirection.xyz));
-		result[0] = ndl * spotAtten * diffuseColor.rgb;
-
-#ifdef SPECULARTERM
-		// Specular
-		vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
-		float specComp = max(0., dot(vNormal, angleW));
-		specComp = pow(specComp, vSpecularColor.a);
-		result[1] = specComp * specularColor * spotAtten;
-#else
-		result[1] = vec3(0.);
-#endif
-		result[2] = vec3(0.);
-
-		return result;
-	}
-
-	result[0] = vec3(0.);
-	result[1] = vec3(0.);
-	result[2] = vec3(0.);
-
-	return result;
-}
-
-mat3 computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 diffuseColor, vec3 specularColor, vec3 groundColor) {
-	mat3 result;
-
-	// Diffuse
-	float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
-	result[0] = mix(groundColor, diffuseColor.rgb, ndl);
-
-#ifdef SPECULARTERM
-	// Specular
-	vec3 angleW = normalize(viewDirectionW + lightData.xyz);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = pow(specComp, vSpecularColor.a);
-	result[1] = specComp * specularColor;
-#else
-	result[1] = vec3(0.);
-#endif
-
-	result[2] = vec3(0.);
-
-	return result;
-}
+#include<fogFragmentDeclaration>
 
 void main(void) {
 	// Clip plane
@@ -446,138 +146,17 @@ void main(void) {
 
 	// Lighting
 	vec3 diffuseBase = vec3(0., 0., 0.);
+	float glossiness = 0.;
 #ifdef SPECULARTERM
 	vec3 specularBase = vec3(0., 0., 0.);
+	glossiness = vSpecularColor.a;
 #endif
 	float shadow = 1.;
 
-#ifdef LIGHT0
-#ifndef SPECULARTERM
-	vec3 vLightSpecular0 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT0
-	mat3 info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0, vLightSpecular0);
-#endif
-#ifdef HEMILIGHT0
-	mat3 info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0, vLightGround0);
-#endif
-#if defined(POINTLIGHT0) || defined(DIRLIGHT0)
-	mat3 info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0);
-#endif
-#ifdef SHADOW0
-#ifdef SHADOWVSM0
-	shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
-#else
-#if defined(POINTLIGHT0)
-	shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-#else
-	shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info[0] * shadow;
-#ifdef SPECULARTERM
-	specularBase += info[1] * shadow;
-#endif
-#endif
-
-#ifdef LIGHT1
-#ifndef SPECULARTERM
-	vec3 vLightSpecular1 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT1
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1, vLightSpecular1);
-#endif
-#ifdef HEMILIGHT1
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1, vLightGround1);
-#endif
-#if defined(POINTLIGHT1) || defined(DIRLIGHT1)
-	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1);
-#endif
-#ifdef SHADOW1
-#ifdef SHADOWVSM1
-	shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
-#else
-#if defined(POINTLIGHT1)
-	shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-#else
-	shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info[0] * shadow;
-#ifdef SPECULARTERM
-	specularBase += info[1] * shadow;
-#endif
-#endif
-
-#ifdef LIGHT2
-#ifndef SPECULARTERM
-	vec3 vLightSpecular2 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT2
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2, vLightSpecular2);
-#endif
-#ifdef HEMILIGHT2
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2, vLightGround2);
-#endif
-#if defined(POINTLIGHT2) || defined(DIRLIGHT2)
-	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2);
-#endif
-#ifdef SHADOW2
-#ifdef SHADOWVSM2
-	shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
-#else
-#if defined(POINTLIGHT2)
-	shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-#else
-	shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info[0] * shadow;
-#ifdef SPECULARTERM
-	specularBase += info[1] * shadow;
-#endif
-#endif
-
-#ifdef LIGHT3
-#ifndef SPECULARTERM
-	vec3 vLightSpecular3 = vec3(0.0);
-#endif
-#ifdef SPOTLIGHT3
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3, vLightSpecular3);
-#endif
-#ifdef HEMILIGHT3
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3, vLightGround3);
-#endif
-#if defined(POINTLIGHT3) || defined(DIRLIGHT3)
-	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3);
-#endif
-#ifdef SHADOW3
-#ifdef SHADOWVSM3
-	shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
-#else
-#if defined(POINTLIGHT3)
-	shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-#else
-	shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
-#endif
-#endif
-#else
-	shadow = 1.;
-#endif
-	diffuseBase += info[0] * shadow;
-#ifdef SPECULARTERM
-	specularBase += info[1] * shadow;
-#endif
-#endif
+#include<light0Fragment>
+#include<light1Fragment>
+#include<light2Fragment>
+#include<light3Fragment>
 
 	// Reflection
 	vec3 reflectionColor = vec3(0., 0., 0.);
@@ -664,10 +243,7 @@ void main(void) {
 
 	vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
 
-#ifdef FOG
-	float fog = CalcFogFactor();
-	color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
-#endif
+#include<fogFragment>
 
 	gl_FragColor = color;
 }

+ 7 - 83
src/Shaders/legacydefault.vertex.fx

@@ -12,18 +12,7 @@ attribute vec2 uv2;
 #ifdef VERTEXCOLOR
 attribute vec4 color;
 #endif
-#if NUM_BONE_INFLUENCERS > 0
-
-	// having bone influencers implies you have bones
-	uniform mat4 mBones[BonesPerMesh];
-
-	attribute vec4 matricesIndices;
-	attribute vec4 matricesWeights;
-	#if NUM_BONE_INFLUENCERS > 4
-		attribute vec4 matricesIndicesExtra;
-		attribute vec4 matricesWeightsExtra;
-	#endif
-#endif
+#include<bonesDeclaration>
 
 // Uniforms
 uniform mat4 world;
@@ -79,28 +68,8 @@ uniform vec4 vClipPlane;
 varying float fClipDistance;
 #endif
 
-#ifdef FOG
-varying float fFogDistance;
-#endif
-
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-uniform mat4 lightMatrix0;
-varying vec4 vPositionFromLight0;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-uniform mat4 lightMatrix1;
-varying vec4 vPositionFromLight1;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-uniform mat4 lightMatrix2;
-varying vec4 vPositionFromLight2;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-uniform mat4 lightMatrix3;
-varying vec4 vPositionFromLight3;
-#endif
-#endif
+#include<fogVertexDeclaration>
+#include<shadowsVertexDeclaration>
 
 #ifdef REFLECTION
 uniform vec3 vEyePosition;
@@ -148,35 +117,8 @@ vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
 void main(void) {
 	mat4 finalWorld = world;
 
-#if NUM_BONE_INFLUENCERS > 0
-	mat4 influence;
-	influence = mBones[int(matricesIndices[0])] * matricesWeights[0];
-
-	#if NUM_BONE_INFLUENCERS > 1
-		influence += mBones[int(matricesIndices[1])] * matricesWeights[1];
-	#endif 
-	#if NUM_BONE_INFLUENCERS > 2
-		influence += mBones[int(matricesIndices[2])] * matricesWeights[2];
-	#endif	
-	#if NUM_BONE_INFLUENCERS > 3
-		influence += mBones[int(matricesIndices[3])] * matricesWeights[3];
-	#endif	
-
-	#if NUM_BONE_INFLUENCERS > 4
-		influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];
-	#endif
-	#if NUM_BONE_INFLUENCERS > 5
-		influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];
-	#endif	
-	#if NUM_BONE_INFLUENCERS > 6
-		influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];
-	#endif	
-	#if NUM_BONE_INFLUENCERS > 7
-		influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];
-	#endif	
-
-	finalWorld = finalWorld * influence;
-#endif
+#include<bonesVertex>
+
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 
 	vec4 worldPos = finalWorld * vec4(position, 1.0);
@@ -266,26 +208,8 @@ void main(void) {
 	fClipDistance = dot(worldPos, vClipPlane);
 #endif
 
-	// Fog
-#ifdef FOG
-	fFogDistance = (view * worldPos).z;
-#endif
-
-	// Shadows
-#ifdef SHADOWS
-#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
-	vPositionFromLight0 = lightMatrix0 * worldPos;
-#endif
-#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
-	vPositionFromLight1 = lightMatrix1 * worldPos;
-#endif
-#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
-	vPositionFromLight2 = lightMatrix2 * worldPos;
-#endif
-#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
-	vPositionFromLight3 = lightMatrix3 * worldPos;
-#endif
-#endif
+#include<fogVertex>
+#include<shadowsVertex>
 
 	// Vertex color
 #ifdef VERTEXCOLOR

+ 6 - 44
src/Shaders/sprites.fragment.fx

@@ -9,58 +9,20 @@ varying vec2 vUV;
 uniform sampler2D diffuseSampler;
 
 // Fog
-#ifdef FOG
-
-#define FOGMODE_NONE    0.
-#define FOGMODE_EXP     1.
-#define FOGMODE_EXP2    2.
-#define FOGMODE_LINEAR  3.
-#define E 2.71828
-
-uniform vec4 vFogInfos;
-uniform vec3 vFogColor;
-varying float fFogDistance;
-
-float CalcFogFactor()
-{
-	float fogCoeff = 1.0;
-	float fogStart = vFogInfos.y;
-	float fogEnd = vFogInfos.z;
-	float fogDensity = vFogInfos.w;
-
-	if (FOGMODE_LINEAR == vFogInfos.x)
-	{
-		fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
-	}
-	else if (FOGMODE_EXP == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
-	}
-	else if (FOGMODE_EXP2 == vFogInfos.x)
-	{
-		fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
-	}
-
-	return min(1., max(0., fogCoeff));
-}
-#endif
-
+#include<fogFragmentDeclaration>
 
 void main(void) {
-	vec4 baseColor = texture2D(diffuseSampler, vUV);
+	vec4 color = texture2D(diffuseSampler, vUV);
 
 	if (alphaTest) 
 	{
-		if (baseColor.a < 0.95)
+		if (color.a < 0.95)
 			discard;
 	}
 
-	baseColor *= vColor;
+	color *= vColor;
 
-#ifdef FOG
-	float fog = CalcFogFactor();
-	baseColor.rgb = fog * baseColor.rgb + (1.0 - fog) * vFogColor;
-#endif
+#include<fogFragment>
 
-	gl_FragColor = baseColor;
+	gl_FragColor = color;
 }

+ 1 - 3
src/Shaders/sprites.vertex.fx

@@ -15,9 +15,7 @@ uniform mat4 projection;
 varying vec2 vUV;
 varying vec4 vColor;
 
-#ifdef FOG
-varying float fFogDistance;
-#endif
+#include<fogVertexDeclaration>
 
 void main(void) {	
 	vec3 viewPos = (view * vec4(position.xyz, 1.0)).xyz;