Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 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. varying vec2 vTexCoord;
  9.  
  10. #ifndef GL_ES
  11. varying vec2 vDetailTexCoord;
  12. #else
  13. varying mediump vec2 vDetailTexCoord;
  14. #endif
  15.  
  16. varying vec3 vNormal;
  17. varying vec4 vWorldPos;
  18. #ifdef PERPIXEL
  19. #ifdef SHADOW
  20. #ifndef GL_ES
  21. varying vec4 vShadowPos[NUMCASCADES];
  22. #else
  23. varying highp vec4 vShadowPos[NUMCASCADES];
  24. #endif
  25. #endif
  26. #ifdef SPOTLIGHT
  27. varying vec4 vSpotPos;
  28. #endif
  29. #ifdef POINTLIGHT
  30. varying vec3 vCubeMaskVec;
  31. #endif
  32. #else
  33. varying vec3 vVertexLight;
  34. varying vec4 vScreenPos;
  35. #ifdef ENVCUBEMAP
  36. varying vec3 vReflectionVec;
  37. #endif
  38. #if defined(LIGHTMAP) || defined(AO)
  39. varying vec2 vTexCoord2;
  40. #endif
  41. #endif
  42.  
  43. uniform sampler2D sWeightMap0;
  44. uniform sampler2D sDetailMap1;
  45. uniform sampler2D sDetailMap2;
  46. uniform sampler2D sDetailMap3;
  47. uniform sampler2D sNormalMap4;
  48.  
  49. #ifndef GL_ES
  50. uniform vec2 cDetailTiling;
  51. #else
  52. uniform mediump vec2 cDetailTiling;
  53. #endif
  54.  
  55. void VS()
  56. {
  57. mat4 modelMatrix = iModelMatrix;
  58. vec3 worldPos = GetWorldPos(modelMatrix);
  59. gl_Position = GetClipPos(worldPos);
  60. vNormal = GetWorldNormal(modelMatrix);
  61. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  62. vTexCoord = GetTexCoord(iTexCoord);
  63. vDetailTexCoord = cDetailTiling * vTexCoord;
  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, vNormal, 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. #else
  84. // Ambient & per-vertex lighting
  85. #if defined(LIGHTMAP) || defined(AO)
  86. // If using lightmap, disregard zone ambient light
  87. // If using AO, calculate ambient in the PS
  88. vVertexLight = vec3(0.0, 0.0, 0.0);
  89. vTexCoord2 = iTexCoord1;
  90. #else
  91. vVertexLight = GetAmbient(GetZonePos(worldPos));
  92. #endif
  93.  
  94. #ifdef NUMVERTEXLIGHTS
  95. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  96. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  97. #endif
  98.  
  99. vScreenPos = GetScreenPos(gl_Position);
  100.  
  101. #ifdef ENVCUBEMAP
  102. vReflectionVec = worldPos - cCameraPos;
  103. #endif
  104. #endif
  105. }
  106.  
  107. void PS()
  108. {
  109. // Get material diffuse albedo
  110. vec3 weights = texture2D(sWeightMap0, vTexCoord).rgb;
  111. float sumWeights = weights.r + weights.g + weights.b;
  112. weights /= sumWeights;
  113. vec4 diffColor = cMatDiffColor * (
  114. weights.r * texture2D(sDetailMap1, vDetailTexCoord) +
  115. weights.g * texture2D(sDetailMap2, vDetailTexCoord) +
  116. weights.b * texture2D(sDetailMap3, vDetailTexCoord)
  117. );
  118.  
  119. vec3 norm = texture2D(sNormalMap4, vTexCoord).rgb;
  120.  
  121. // Get material specular albedo
  122. vec3 specColor = cMatSpecColor.rgb;
  123.  
  124. // Get normal
  125. //vec3 normal = normalize(vNormal);
  126. vec3 normal = normalize(norm.rbg*2.0-1.0);
  127.  
  128. // Get fog factor
  129. #ifdef HEIGHTFOG
  130. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  131. #else
  132. float fogFactor = GetFogFactor(vWorldPos.w);
  133. #endif
  134.  
  135. #if defined(PERPIXEL)
  136. // Per-pixel forward lighting
  137. vec3 lightColor;
  138. vec3 lightDir;
  139. vec3 finalColor;
  140.  
  141. float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  142.  
  143. #ifdef SHADOW
  144. diff *= GetShadow(vShadowPos, vWorldPos.w);
  145. #endif
  146.  
  147. #if defined(SPOTLIGHT)
  148. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  149. #elif defined(CUBEMASK)
  150. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  151. #else
  152. lightColor = cLightColor.rgb;
  153. #endif
  154.  
  155. #ifdef SPECULAR
  156. float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  157. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  158. #else
  159. finalColor = diff * lightColor * diffColor.rgb;
  160. #endif
  161.  
  162. #ifdef AMBIENT
  163. finalColor += cAmbientColor.rgb * diffColor.rgb;
  164. finalColor += cMatEmissiveColor;
  165. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  166. #else
  167. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  168. #endif
  169. #elif defined(PREPASS)
  170. // Fill light pre-pass G-Buffer
  171. float specPower = cMatSpecColor.a / 255.0;
  172.  
  173. gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  174. gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  175. #elif defined(DEFERRED)
  176. // Fill deferred G-buffer
  177. float specIntensity = specColor.g;
  178. float specPower = cMatSpecColor.a / 255.0;
  179.  
  180. gl_FragData[0] = vec4(GetFog(vVertexLight * diffColor.rgb, fogFactor), 1.0);
  181. gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  182. gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  183. gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  184. #else
  185. // Ambient & per-vertex lighting
  186. vec3 finalColor = vVertexLight * diffColor.rgb;
  187.  
  188. #ifdef MATERIAL
  189. // Add light pre-pass accumulation result
  190. // Lights are accumulated at half intensity. Bring back to full intensity now
  191. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  192. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  193.  
  194. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  195. #endif
  196.  
  197. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  198. #endif
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement