Guest User

Untitled

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