SHARE
TWEET

Untitled

a guest Oct 17th, 2014 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. precision highp float;
  2.  
  3. // Compiler should remove unneeded stuff
  4. uniform vec3 view_position;
  5.  
  6. uniform vec3 light_globalAmbient;
  7.  
  8. varying vec3 vPositionW;
  9. varying vec3 vNormalW;
  10. varying vec3 vTangentW;
  11. varying vec3 vBinormalW;
  12. varying vec2 vUv0;
  13. varying vec2 vUv1;
  14. varying vec4 vVertexColor;
  15. varying vec3 vNormalV;
  16.  
  17. struct psInternalData {
  18.     vec3 albedo;
  19.     vec3 specularity;
  20.     float glossiness;
  21.     vec3 emission;
  22.     vec3 normalW;
  23.     mat3 TBN;
  24.     vec3 viewDirW;
  25.     vec3 reflDirW;
  26.     vec3 diffuseLight;
  27.     vec3 specularLight;
  28.     float alpha;
  29.     vec3 lightDirNormW;
  30.     vec3 lightDirW;
  31.     float atten;
  32.     vec3 shadowCoord;
  33.     vec2 uvOffset;
  34. };
  35.  
  36. void getViewDir(inout psInternalData data) {
  37.     data.viewDirW = normalize(view_position - vPositionW);
  38. }
  39.  
  40. void getReflDir(inout psInternalData data) {
  41.     data.reflDirW = normalize(-reflect(data.viewDirW, data.normalW));
  42. }
  43.  
  44. void addAmbientConstant(inout psInternalData data) {
  45.     data.diffuseLight += light_globalAmbient;
  46. }
  47.  
  48. void getLightDirPoint(inout psInternalData data, vec3 lightPosW) {
  49.     data.lightDirW = vPositionW - lightPosW;
  50.     data.lightDirNormW = normalize(data.lightDirW);
  51. }
  52.  
  53. float getFalloffLinear(inout psInternalData data, float lightRadius) {
  54.     float d = length(data.lightDirW);
  55.     return max(((lightRadius - d) / lightRadius), 0.0);
  56. }
  57.  
  58. float getFalloffInvSquared(inout psInternalData data) {
  59.     float sqrDist = dot(data.lightDirW, data.lightDirW);
  60.     return 1.0 / sqrDist;
  61. }
  62.  
  63. float getSpotEffect(inout psInternalData data, vec3 lightSpotDirW, float lightInnerConeAngle, float lightOuterConeAngle) {
  64.     float cosAngle = dot(data.lightDirNormW, lightSpotDirW);
  65.     return smoothstep(lightOuterConeAngle, lightInnerConeAngle, cosAngle);
  66. }
  67.  
  68. varying vec2 vUvDiffuse;
  69. varying vec2 vUvNormal;
  70. varying vec2 vUvHeight;
  71. uniform vec3 light0_color;
  72. uniform vec3 light0_direction;
  73. uniform mat4 light0_shadowMatrix;
  74. uniform vec3 light0_shadowParams;
  75. uniform sampler2D light0_shadowMap;
  76. uniform vec3 light1_color;
  77. uniform vec3 light1_position;
  78. uniform float light1_radius;
  79. uniform vec3 light2_color;
  80. uniform vec3 light2_position;
  81. uniform float light2_radius;
  82. uniform vec3 light3_color;
  83. uniform vec3 light3_position;
  84. uniform float light3_radius;
  85. uniform vec3 light4_color;
  86. uniform vec3 light4_position;
  87. uniform float light4_radius;
  88. uniform vec3 light4_spotDirection;
  89. uniform float light4_innerConeAngle;
  90. uniform float light4_outerConeAngle;
  91. uniform mat4 light4_shadowMatrix;
  92. uniform vec3 light4_shadowParams;
  93. uniform sampler2D light4_shadowMap;
  94.  
  95. uniform sampler2D texture_normalMap;
  96. uniform float material_bumpMapFactor;
  97. void getNormal(inout psInternalData data) {
  98.     vec3 normalMap = texture2D(texture_normalMap, vUvNormal + data.uvOffset).xyz * 2.0 - 1.0;
  99.     normalMap = normalize(mix(vec3(0.0, 0.0, 1.0), normalMap, material_bumpMapFactor));
  100.  
  101.     data.normalW = data.TBN * normalMap;
  102. }
  103.  
  104. void getTBN(inout psInternalData data) {
  105.     data.TBN = mat3(normalize(vTangentW), normalize(vBinormalW), normalize(vNormalW));
  106. }
  107.  
  108. vec4 texture2DAlbedo(sampler2D tex, vec2 uv) {
  109.     vec4 rgba = texture2D(tex, uv);
  110.     rgba.rgb = pow(rgba.rgb, vec3(2.2)); // when no sRGB extension available
  111.     return rgba;
  112. }
  113.  
  114. vec3 gammaCorrectOutput(vec3 color) {
  115.     return pow(color, vec3(0.45));
  116. }
  117.  
  118. uniform sampler2D texture_diffuseMap;
  119. void getAlbedo(inout psInternalData data) {
  120.     vec4 tex = texture2DAlbedo(texture_diffuseMap, vUvDiffuse + data.uvOffset);
  121.     data.albedo = tex.rgb;
  122. }
  123.  
  124. uniform float material_shininess;
  125. void getGlossiness(inout psInternalData data) {
  126.     // Hack: On Mac OS X, calling pow with zero for the exponent generates hideous artifacts so bias up a little
  127.     data.glossiness = material_shininess + 0.0001;
  128. }
  129.  
  130. uniform float material_opacity;
  131. void getOpacity(inout psInternalData data) {
  132.     data.alpha = material_opacity;
  133. }
  134.  
  135. uniform vec3 material_emissive;
  136. vec3 getEmission(inout psInternalData data) {
  137.     return material_emissive;
  138. }
  139.  
  140. uniform sampler2D texture_heightMap;
  141. void getParallax(inout psInternalData data) {
  142.     const float parallaxScale = 0.025;
  143.     const float parallaxBias = 0.01;
  144.  
  145.     float height = texture2D(texture_heightMap, vUvHeight).r * parallaxScale - parallaxBias;
  146.     vec3 viewDirT = data.viewDirW * data.TBN;
  147.  
  148.     data.uvOffset = min(height * viewDirT.xy, vec2(parallaxBias));
  149. }
  150.  
  151. float unpackFloat(vec4 rgbaDepth) {
  152.     const vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  153.     return dot(rgbaDepth, bitShift);
  154. }
  155.  
  156. void getShadowCoord(inout psInternalData data, mat4 shadowMatrix, vec3 shadowParams) {
  157.     vec4 projPos = shadowMatrix * vec4(vPositionW, 1.0);
  158.     projPos.xyz /= projPos.w;
  159.     projPos.z += shadowParams.z;
  160.     data.shadowCoord = projPos.xyz;
  161. }
  162.  
  163. bool shadowContained(psInternalData data) {
  164.     bvec4 containedVec = bvec4(data.shadowCoord.x >= 0.0, data.shadowCoord.x <= 1.0, data.shadowCoord.y >= 0.0, data.shadowCoord.y <= 1.0);
  165.     return all(bvec2(all(containedVec), data.shadowCoord.z <= 1.0));
  166. }
  167.  
  168. float getShadow(inout psInternalData data, sampler2D shadowMap, vec3 shadowParams) {
  169.     if (shadowContained(data)) {
  170.         float depth = unpackFloat(texture2D(shadowMap, data.shadowCoord.xy));
  171.         return (depth < data.shadowCoord.z) ? 0.0 : 1.0;
  172.     }
  173.     return 1.0;
  174. }
  175.  
  176. float getShadowPCF3x3(inout psInternalData data, sampler2D shadowMap, vec3 shadowParams) {
  177.     if (shadowContained(data)) {
  178.         float shadowAccum = 0.0;
  179.         vec3 shadowCoord = data.shadowCoord;
  180.  
  181.         float xoffset = 1.0 / shadowParams.x; // 1/shadow map width
  182.         float yoffset = 1.0 / shadowParams.y; // 1/shadow map height
  183.         float dx0 = -xoffset;
  184.         float dy0 = -yoffset;
  185.         float dx1 = xoffset;
  186.         float dy1 = yoffset;
  187.  
  188.         mat3 shadowKernel;
  189.         mat3 depthKernel;
  190.  
  191.         depthKernel[0][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, dy0)));
  192.         depthKernel[0][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, 0.0)));
  193.         depthKernel[0][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, dy1)));
  194.         depthKernel[1][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(0.0, dy0)));
  195.         depthKernel[1][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy));
  196.         depthKernel[1][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(0.0, dy1)));
  197.         depthKernel[2][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, dy0)));
  198.         depthKernel[2][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, 0.0)));
  199.         depthKernel[2][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, dy1)));
  200.  
  201.         vec3 shadowZ = vec3(shadowCoord.z);
  202.         shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ));
  203.         shadowKernel[0] *= vec3(0.25);
  204.         shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ));
  205.         shadowKernel[1] *= vec3(0.25);
  206.         shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ));
  207.         shadowKernel[2] *= vec3(0.25);
  208.  
  209.         vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowParams.xy );
  210.  
  211.         shadowKernel[0] = mix(shadowKernel[1], shadowKernel[0], fractionalCoord.x);
  212.         shadowKernel[1] = mix(shadowKernel[2], shadowKernel[1], fractionalCoord.x);
  213.  
  214.         vec4 shadowValues;
  215.         shadowValues.x = mix(shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y);
  216.         shadowValues.y = mix(shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y);
  217.         shadowValues.z = mix(shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y);
  218.         shadowValues.w = mix(shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y);
  219.  
  220.         shadowAccum = 1.0 - dot( shadowValues, vec4( 1.0 ) );
  221.  
  222.         return shadowAccum;
  223.     }
  224.     return 1.0;
  225. }
  226.  
  227. float getLightDiffuse(inout psInternalData data) {
  228.     return max(dot(data.normalW, -data.lightDirNormW), 0.0);
  229. }
  230.  
  231. vec3 combineColor(inout psInternalData data) {
  232.     return data.albedo * data.diffuseLight;
  233. }
  234.  
  235.  
  236. void main(void) {
  237.    psInternalData data;
  238.    data.diffuseLight = vec3(0);
  239.    data.specularLight = vec3(0);
  240.        
  241.        
  242.    getViewDir(data);
  243.    getTBN(data);
  244.    getParallax(data);
  245.    getNormal(data);
  246.    getAlbedo(data);
  247.    getOpacity(data);
  248.    addAmbientConstant(data);
  249.    data.lightDirNormW = light0_direction;
  250.    data.atten = 1.0;
  251.    data.atten *= getLightDiffuse(data);
  252.    getShadowCoord(data, light0_shadowMatrix, light0_shadowParams);
  253.    data.atten *= getShadowPCF3x3(data, light0_shadowMap, light0_shadowParams);
  254.    data.diffuseLight += data.atten * light0_color;
  255.  
  256.    getLightDirPoint(data, light1_position);
  257.    data.atten = getFalloffLinear(data, light1_radius);
  258.    data.atten *= getLightDiffuse(data);
  259.    data.diffuseLight += data.atten * light1_color;
  260.  
  261.    getLightDirPoint(data, light2_position);
  262.    data.atten = getFalloffLinear(data, light2_radius);
  263.    data.atten *= getLightDiffuse(data);
  264.    data.diffuseLight += data.atten * light2_color;
  265.  
  266.    getLightDirPoint(data, light3_position);
  267.    data.atten = getFalloffLinear(data, light3_radius);
  268.    data.atten *= getLightDiffuse(data);
  269.    data.diffuseLight += data.atten * light3_color;
  270.  
  271.    getLightDirPoint(data, light4_position);
  272.    data.atten = getFalloffLinear(data, light4_radius);
  273.    data.atten *= getSpotEffect(data, light4_spotDirection, light4_innerConeAngle, light4_outerConeAngle);
  274.    data.atten *= getLightDiffuse(data);
  275.    getShadowCoord(data, light4_shadowMatrix, light4_shadowParams);
  276.    data.atten *= getShadowPCF3x3(data, light4_shadowMap, light4_shadowParams);
  277.    data.diffuseLight += data.atten * light4_color;
  278.  
  279.  
  280.    gl_FragColor.rgb = combineColor(data);
  281.    gl_FragColor.rgb += getEmission(data);
  282.    gl_FragColor.rgb = gammaCorrectOutput(gl_FragColor.rgb);
  283.    gl_FragColor.a = data.alpha;
  284. }
RAW Paste Data
Top