Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 KB | None | 0 0
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7.  
  8. #define PI 3.1415926535
  9. #define e 2.7182818284
  10.  
  11. #ifdef NORMALMAP
  12.     varying vec4 vTexCoord;
  13.     varying vec4 vTangent;
  14. #else
  15.     varying vec2 vTexCoord;
  16. #endif
  17. varying vec3 vNormal;
  18. varying vec4 vWorldPos;
  19. #ifdef PERPIXEL
  20.     #ifdef SHADOW
  21.         varying vec4 vShadowPos[NUMCASCADES];
  22.     #endif
  23.     #ifdef SPOTLIGHT
  24.         varying vec4 vSpotPos;
  25.     #endif
  26.     #ifdef POINTLIGHT
  27.         varying vec3 vCubeMaskVec;
  28.     #endif
  29. #else
  30.     varying vec3 vVertexLight;
  31.     varying vec4 vScreenPos;
  32.     #ifdef ENVCUBEMAP
  33.         varying vec3 vReflectionVec;
  34.     #endif
  35.     #if defined(LIGHTMAP) || defined(AO)
  36.         varying vec2 vTexCoord2;
  37.     #endif
  38. #endif
  39.  
  40. void VS()
  41. {
  42.     mat4 modelMatrix = iModelMatrix;
  43.     vec3 worldPos = GetWorldPos(modelMatrix);
  44.     gl_Position = GetClipPos(worldPos);
  45.     vNormal = GetWorldNormal(modelMatrix);
  46.     vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  47.  
  48.     #ifdef NORMALMAP
  49.         vec3 tangent = GetWorldTangent(modelMatrix);
  50.         vec3 bitangent = cross(tangent, vNormal) * iTangent.w;
  51.         vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  52.         vTangent = vec4(tangent, bitangent.z);
  53.     #else
  54.         vTexCoord = GetTexCoord(iTexCoord);
  55.     #endif
  56.  
  57.     #ifdef PERPIXEL
  58.         // Per-pixel forward lighting
  59.         vec4 projWorldPos = vec4(worldPos, 1.0);
  60.  
  61.         #ifdef SHADOW
  62.             // Shadow projection: transform from world space to shadow space
  63.             for (int i = 0; i < NUMCASCADES; i++)
  64.                 vShadowPos[i] = GetShadowPos(i, projWorldPos);
  65.         #endif
  66.  
  67.         #ifdef SPOTLIGHT
  68.             // Spotlight projection: transform from world space to projector texture coordinates
  69.             vSpotPos =  projWorldPos * cLightMatrices[0];
  70.         #endif
  71.    
  72.         #ifdef POINTLIGHT
  73.             vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  74.         #endif
  75.     #else
  76.         // Ambient & per-vertex lighting
  77.         #if defined(LIGHTMAP) || defined(AO)
  78.             // If using lightmap, disregard zone ambient light
  79.             // If using AO, calculate ambient in the PS
  80.             vVertexLight = vec3(0.0, 0.0, 0.0);
  81.             vTexCoord2 = iTexCoord2;
  82.         #else
  83.             vVertexLight = GetAmbient(GetZonePos(worldPos));
  84.         #endif
  85.        
  86.         #ifdef NUMVERTEXLIGHTS
  87.             for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  88.                 vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  89.         #endif
  90.        
  91.         vScreenPos = GetScreenPos(gl_Position);
  92.  
  93.         #ifdef ENVCUBEMAP
  94.             vReflectionVec = worldPos - cCameraPos;
  95.         #endif
  96.     #endif
  97. }
  98.  
  99.  
  100. float G_Term( float NoH, float NoL, float NoV, float VoH)
  101. {
  102.     float a = min( (2 * (NoH*NoV) / VoH),( 2 * (NoH*NoL) / VoH ) );
  103.     return min(1, a);
  104. }
  105.  
  106. float D_Beckmann( float NoH, float roughness)
  107. {
  108.    
  109.     float R_sqr = roughness * roughness;
  110.     float NoH_sqr = NoH * NoH;
  111.     float d_a = pow ( e, (NoH_sqr - 1) / ( R_sqr * NoH_sqr));
  112.     float d_b = 4 * (R_sqr * R_sqr);
  113.     return ( d_a / d_b );
  114. };
  115.  
  116. float CookTorrance ( vec3 H, float NoV, float NoL, float NoH, float VoH, float rougness, float fresnel)
  117. {
  118.     float D = D_Beckmann( NoH, rougness );
  119.     float G = G_Term (NoH, NoL, NoV, VoH);
  120.     return (D * fresnel * G) / ( NoV*NoL );
  121. };
  122.  
  123. vec4 LambertDiffuse( vec4 diffuse )
  124. {
  125.   return diffuse * (1 / PI);
  126. };
  127.  
  128. void PS()
  129. {
  130.     //const float e = 2.7182818284;
  131.     //const float pi = 3.1415926;
  132.    
  133.     // Get material diffuse albedo
  134.     #ifdef DIFFMAP
  135.         vec4 diffInput = texture2D(sDiffMap, vTexCoord.xy);
  136.         #ifdef ALPHAMASK
  137.             if (diffInput.a < 0.5)
  138.                 discard;
  139.         #endif
  140.         vec4 diffColor = cMatDiffColor * diffInput;
  141.     #else
  142.         vec4 diffColor = cMatDiffColor;
  143.     #endif
  144.    
  145.     // Get material specular albedo
  146.     #ifdef SPECMAP
  147.         vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord.xy).rgb;
  148.     #else
  149.         vec3 specColor = cMatSpecColor.rgb;
  150.     #endif
  151.  
  152.     // Get normal
  153.     #ifdef NORMALMAP
  154.         mat3 tbn = mat3(vTangent.xyz, vec3(vTexCoord.zw, vTangent.w), vNormal);
  155.         vec3 normal = normalize(tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord.xy)));
  156.     #else
  157.         vec3 normal = normalize(vNormal);
  158.     #endif
  159.  
  160.     // Get fog factor
  161.     #ifdef HEIGHTFOG
  162.         float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  163.     #else
  164.         float fogFactor = GetFogFactor(vWorldPos.w);
  165.     #endif
  166.  
  167.     #if defined(PERPIXEL)
  168.         // Per-pixel forward lighting
  169.         vec3 lightColor;
  170.         vec3 lightDir;
  171.         vec3 finalColor;
  172.  
  173.         float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  174.  
  175.         #ifdef SHADOW
  176.             diff *= GetShadow(vShadowPos, vWorldPos.w);
  177.         #endif
  178.    
  179.         #if defined(SPOTLIGHT)
  180.             lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  181.         #elif defined(CUBEMASK)
  182.             lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  183.         #else
  184.             lightColor = cLightColor.rgb;
  185.         #endif
  186.    
  187.         #ifdef SPECULAR
  188.             float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  189.             //finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  190.         #else
  191.             float spec = 1;
  192.             //finalColor = diff * lightColor * diffColor.rgb;
  193.         #endif
  194.        
  195.        
  196.         #ifdef COOKTORRANCE
  197.            
  198.             vec3 l = normalize ( cLightPosPS.xyz - vWorldPos.xyz );
  199.             vec3 v = normalize ( cCameraPosPS.xyz - vWorldPos.xyz );
  200.             vec3 n = normal;
  201.             vec3 h = normalize ( l + v);  
  202.                        
  203.             float VoH = dot(v, h);
  204.             float NoH = dot(n, h);
  205.             float NoL = dot(n, l);
  206.             float NoV = dot(n, v);
  207.            
  208.             //float rougness = 0.75;
  209.             //float fresnel = 0.7;
  210.            
  211.             vec4 diffCt = LambertDiffuse( diffColor );
  212.                  
  213.             float ct = CookTorrance(h, NoV, NoL, NoH, VoH, cRougness, cFresnel);
  214.             vec3 specCt = (lightColor.xyz + ( specColor )) * max(0.0, ct);
  215.             //vec3 specCt = (diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a)) * max(0.0, ct);
  216.            
  217.             finalColor = diff * (diffCt.xyz + (spec * specCt.xyz));
  218.            
  219.         #endif
  220.  
  221.         #ifdef AMBIENT
  222.             finalColor += cAmbientColor * diffColor.rgb;
  223.             finalColor += cMatEmissiveColor;
  224.             gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  225.         #else
  226.             gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  227.         #endif
  228.     #elif defined(PREPASS)
  229.         // Fill light pre-pass G-Buffer
  230.         float specPower = cMatSpecColor.a / 255.0;
  231.  
  232.         gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  233.         gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  234.     #elif defined(DEFERRED)
  235.         // Fill deferred G-buffer
  236.         float specIntensity = specColor.g;
  237.         float specPower = cMatSpecColor.a / 255.0;
  238.  
  239.         vec3 finalColor = vVertexLight * diffColor.rgb;
  240.         #ifdef AO
  241.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  242.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  243.         #endif
  244.  
  245.         #ifdef ENVCUBEMAP
  246.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  247.         #endif
  248.         #ifdef LIGHTMAP
  249.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  250.         #endif
  251.         #ifdef EMISSIVEMAP
  252.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  253.         #else
  254.             finalColor += cMatEmissiveColor;
  255.         #endif
  256.  
  257.         gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  258.         gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  259.         gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  260.         gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  261.     #else
  262.         // Ambient & per-vertex lighting
  263.         vec3 finalColor = vVertexLight * diffColor.rgb;
  264.         #ifdef AO
  265.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  266.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  267.         #endif
  268.        
  269.         #ifdef MATERIAL
  270.             // Add light pre-pass accumulation result
  271.             // Lights are accumulated at half intensity. Bring back to full intensity now
  272.             vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  273.             vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  274.  
  275.             finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  276.         #endif
  277.  
  278.         #ifdef ENVCUBEMAP
  279.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  280.         #endif
  281.         #ifdef LIGHTMAP
  282.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  283.         #endif
  284.         #ifdef EMISSIVEMAP
  285.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  286.         #else
  287.             finalColor += cMatEmissiveColor;
  288.         #endif
  289.  
  290.         gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  291.     #endif
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement