Guest User

Untitled

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