Advertisement
Guest User

Untitled

a guest
Feb 26th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.48 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. #ifdef NORMALMAP
  9.     varying vec4 vTexCoord;
  10.     varying vec4 vTangent;
  11. #else
  12.     varying vec2 vTexCoord;
  13. #endif
  14. varying vec3 vNormal;
  15. varying vec4 vWorldPos;
  16. #ifdef VERTEXCOLOR
  17.     varying vec4 vColor;
  18. #endif
  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.    
  30.    
  31.     varying vec3 vZoneReflectionVec;
  32.    
  33. #else
  34.     varying vec3 vVertexLight;
  35.     varying vec4 vScreenPos;
  36.     #ifdef ENVCUBEMAP
  37.         varying vec3 vReflectionVec;
  38.     #endif
  39.     #if defined(LIGHTMAP) || defined(AO)
  40.         varying vec2 vTexCoord2;
  41.     #endif
  42. #endif
  43.  
  44. void VS()
  45. {
  46.     mat4 modelMatrix = iModelMatrix;
  47.     vec3 worldPos = GetWorldPos(modelMatrix);
  48.     gl_Position = GetClipPos(worldPos);
  49.     vNormal = GetWorldNormal(modelMatrix);
  50.     vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  51.  
  52.     #ifdef VERTEXCOLOR
  53.         vColor = iColor;
  54.     #endif
  55.  
  56.     #ifdef NORMALMAP
  57.         vec3 tangent = GetWorldTangent(modelMatrix);
  58.         vec3 bitangent = cross(tangent, vNormal) * iTangent.w;
  59.         vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  60.         vTangent = vec4(tangent, bitangent.z);
  61.     #else
  62.         vTexCoord = GetTexCoord(iTexCoord);
  63.     #endif
  64.  
  65.     #ifdef PERPIXEL
  66.         // Per-pixel forward lighting
  67.         vec4 projWorldPos = vec4(worldPos, 1.0);
  68.  
  69.         #ifdef SHADOW
  70.             // Shadow projection: transform from world space to shadow space
  71.             for (int i = 0; i < NUMCASCADES; i++)
  72.                 vShadowPos[i] = GetShadowPos(i, projWorldPos);
  73.         #endif
  74.  
  75.         #ifdef SPOTLIGHT
  76.             // Spotlight projection: transform from world space to projector texture coordinates
  77.             vSpotPos = projWorldPos * cLightMatrices[0];
  78.         #endif
  79.    
  80.         #ifdef POINTLIGHT
  81.             vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  82.         #endif
  83.        
  84.  
  85.         vZoneReflectionVec = worldPos - cCameraPos;
  86.     #else
  87.         // Ambient & per-vertex lighting
  88.         #if defined(LIGHTMAP) || defined(AO)
  89.             // If using lightmap, disregard zone ambient light
  90.             // If using AO, calculate ambient in the PS
  91.             vVertexLight = vec3(0.0, 0.0, 0.0);
  92.             vTexCoord2 = iTexCoord2;
  93.         #else
  94.             vVertexLight = GetAmbient(GetZonePos(worldPos));
  95.         #endif
  96.        
  97.         #ifdef NUMVERTEXLIGHTS
  98.             for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  99.                 vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  100.         #endif
  101.        
  102.         vScreenPos = GetScreenPos(gl_Position);
  103.  
  104.         #ifdef ENVCUBEMAP
  105.             vReflectionVec = worldPos - cCameraPos;
  106.         #endif
  107.        
  108.     #endif
  109. }
  110.  
  111. void PS()
  112. {
  113.     // Get material diffuse albedo
  114.     #ifdef DIFFMAP
  115.         vec4 diffInput = texture2D(sDiffMap, vTexCoord.xy);
  116.         #ifdef ALPHAMASK
  117.             if (diffInput.a < 0.5)
  118.                 discard;
  119.         #endif
  120.         vec4 diffColor = cMatDiffColor * diffInput;
  121.     #else
  122.         vec4 diffColor = cMatDiffColor;
  123.     #endif
  124.  
  125.     #ifdef VERTEXCOLOR
  126.         diffColor *= vColor;
  127.     #endif
  128.    
  129.     // Get material specular albedo
  130.     #ifdef SPECMAP
  131.         vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord.xy).rgb;
  132.     #else
  133.         vec3 specColor = cMatSpecColor.rgb;
  134.     #endif
  135.  
  136.     // Get normal
  137.     #ifdef NORMALMAP
  138.         mat3 tbn = mat3(vTangent.xyz, vec3(vTexCoord.zw, vTangent.w), vNormal);
  139.         vec3 normal = normalize(tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord.xy)));
  140.     #else
  141.         vec3 normal = normalize(vNormal);
  142.     #endif
  143.  
  144.     // Get fog factor
  145.     #ifdef HEIGHTFOG
  146.         float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  147.     #else
  148.         float fogFactor = GetFogFactor(vWorldPos.w);
  149.     #endif
  150.  
  151.     #if defined(PERPIXEL)
  152.         // Per-pixel forward lighting
  153.         vec3 lightColor;
  154.         vec3 lightDir;
  155.         vec3 finalColor;
  156.  
  157.         float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  158.  
  159.         #ifdef SHADOW
  160.             diff *= GetShadow(vShadowPos, vWorldPos.w);
  161.         #endif
  162.    
  163.         #if defined(SPOTLIGHT)
  164.             lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  165.         #elif defined(CUBEMASK)
  166.             lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  167.         #else
  168.             lightColor = cLightColor.rgb;
  169.         #endif
  170.    
  171.         #ifdef SPECULAR
  172.             float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  173.             vec3 env = cMatEnvMapColor * textureCube(sZoneCubeMap, reflect(vZoneReflectionVec, normal)).rgb;
  174.             finalColor = diff * lightColor * env.rgb * (diffColor.rgb + spec * specColor * cLightColor.a);
  175.         #else
  176.             vec3 env = cMatEnvMapColor * textureCube(sZoneCubeMap, reflect(vZoneReflectionVec, normal)).rgb;
  177.             finalColor = diff * lightColor * env.rgb * diffColor.rgb;
  178.         #endif
  179.        
  180.         #ifdef ZONEENVCUBEMAP
  181.         #endif
  182.        
  183.         #ifdef AMBIENT
  184.             finalColor += cAmbientColor * diffColor.rgb;
  185.             finalColor += cMatEmissiveColor;
  186.             gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  187.         #else
  188.             gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  189.         #endif
  190.     #elif defined(PREPASS)
  191.         // Fill light pre-pass G-Buffer
  192.         float specPower = cMatSpecColor.a / 255.0;
  193.  
  194.         gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  195.         gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  196.     #elif defined(DEFERRED)
  197.         // Fill deferred G-buffer
  198.         float specIntensity = specColor.g;
  199.         float specPower = cMatSpecColor.a / 255.0;
  200.  
  201.         vec3 finalColor = vVertexLight * diffColor.rgb;
  202.         #ifdef AO
  203.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  204.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  205.         #endif
  206.         #ifdef ENVCUBEMAP
  207.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  208.         #endif
  209.         #ifdef LIGHTMAP
  210.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  211.         #endif
  212.         #ifdef EMISSIVEMAP
  213.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  214.         #else
  215.             finalColor += cMatEmissiveColor;
  216.         #endif
  217.  
  218.         gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  219.         gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  220.         gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  221.         gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  222.     #else
  223.         // Ambient & per-vertex lighting
  224.         vec3 finalColor = vVertexLight * diffColor.rgb;
  225.         #ifdef AO
  226.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  227.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  228.         #endif
  229.        
  230.         #ifdef MATERIAL
  231.             // Add light pre-pass accumulation result
  232.             // Lights are accumulated at half intensity. Bring back to full intensity now
  233.             vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  234.             vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  235.  
  236.             finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  237.         #endif
  238.  
  239.         #ifdef ENVCUBEMAP
  240.             finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  241.         #endif
  242.         #ifdef LIGHTMAP
  243.             finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  244.         #endif
  245.         #ifdef EMISSIVEMAP
  246.             finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  247.         #else
  248.             finalColor += cMatEmissiveColor;
  249.         #endif
  250.  
  251.         gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  252.     #endif
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement