SHARE
TWEET

Untitled

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