Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #version 330
  2.  
  3. // Matrices
  4. uniform mat4 matrixView;
  5.  
  6. // Materials
  7. uniform vec3 materialAmbient;
  8. uniform vec3 materialDiffuse;
  9. uniform vec3 materialSpecular;
  10. uniform float shininess;
  11. uniform vec3 fogColor;
  12. uniform bool brightnessTest;
  13.  
  14. // Textures
  15. uniform sampler2D texture0;
  16. uniform sampler2D textureNormal;
  17. uniform sampler2DShadow shadowMap;
  18. uniform float NormalMap;
  19. vec3 finalNormal;
  20.  
  21. uniform float shadowStrength;
  22.  
  23. in vec4 color;
  24. in vec4 position;
  25. in vec3 normal;
  26. in float fogStrength;
  27. in vec2 texCoord0;
  28. in vec4 shadowCoord;
  29. in mat3 matrixTangent;
  30.  
  31. out vec4 outColor;
  32.  
  33.  
  34. // directional light declaration
  35. struct DIRECTIONAL
  36. {
  37. int on;
  38. vec3 direction;
  39. vec3 diffuse;
  40. };
  41. uniform DIRECTIONAL lightDir;
  42.  
  43. // point light declaration
  44. struct POINT
  45. {
  46. int on;
  47. vec3 position;
  48. vec3 diffuse;
  49. vec3 specular;
  50. };
  51. uniform POINT lightPoint;
  52.  
  53. // spot light declaration
  54. struct SPOT
  55. {
  56. int on;
  57. vec3 position;
  58. vec3 diffuse;
  59. vec3 specular;
  60. vec3 direction;
  61. float cutoff;
  62. float attenuation;
  63. float strength;
  64. mat4 matrix;
  65. };
  66. uniform SPOT lightSpot;
  67.  
  68.  
  69.  
  70.  
  71. // directional light function
  72. vec4 DirectionalLight(DIRECTIONAL light)
  73. {
  74. vec4 color = vec4(0, 0, 0, 0);
  75. vec3 L = normalize(mat3(matrixView) * light.direction);
  76. float NdotL = dot(normal, L);
  77. if (NdotL > 0)
  78. {
  79. color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  80. }
  81. return color;
  82. }
  83.  
  84. // point light function
  85. vec4 PointLight(POINT light)
  86. {
  87.  
  88. // diffuse light
  89. vec4 color = vec4(0, 0, 0, 1);
  90. vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
  91. float NdotL = dot(L, finalNormal);
  92. if (NdotL > 0)
  93. {
  94. color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
  95. }
  96.  
  97. // specular specular
  98. vec3 V = normalize(-position.xyz);
  99. vec3 R = reflect(-L, finalNormal);
  100. float RdotV = dot(R, V);
  101. if (NdotL > 0 && RdotV > 0)
  102. {
  103. color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
  104. }
  105.  
  106. return color;
  107. }
  108.  
  109. // spot light function
  110. vec4 SpotLight(SPOT light)
  111. {
  112. vec4 color = vec4(0, 0, 0, 1);
  113.  
  114. vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
  115.  
  116. // diffuse light
  117. float NdotL = dot(L, finalNormal);
  118. if (NdotL > 0)
  119. {
  120. color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  121. }
  122.  
  123. // specular light
  124. vec3 V = normalize(-position.xyz);
  125. vec3 R = reflect(-L, finalNormal);
  126. float RdotV = dot(R, V);
  127. if (NdotL > 0 && RdotV > 0)
  128. {
  129. color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
  130. }
  131.  
  132.  
  133. // spot light
  134. vec3 D = normalize(mat3(light.matrix) * light.direction);
  135. float S = dot(-L, D);
  136. float A = acos(S);
  137. float cutoff = radians(clamp(light.cutoff, 0, 360));
  138.  
  139. if (A < cutoff) S = pow(S, light.attenuation);
  140. else S = 0;
  141.  
  142. // attenuation
  143. float dist = length(light.matrix * vec4(light.position, 1) - position);
  144. float att = 1 / (dist * dist) / light.strength;
  145.  
  146. return color * S * att;
  147. }
  148.  
  149.  
  150.  
  151. //--
  152. // shadows (PCF sampling)
  153. float shadow;
  154.  
  155. // number of samples
  156. const int samplingPositions = 8;
  157.  
  158. // offsets for rectangular PCF sampling
  159. vec2 samplePositions[8] = vec2[]
  160. (
  161. //vec2(2.0, 2.0), vec2(-2.0, 2.0), vec2(-2.0, -2.0), vec2(2.0, -2.0),
  162. //vec2(1.5, 1.5), vec2(-1.5, 1.5), vec2(-1.5, -1.5), vec2(1.5, -1.5),
  163. vec2(1.0, 1.0), vec2(-1.0, 1.0), vec2(-1.0, -1.0), vec2(1.0, -1.0),
  164. vec2(0.5, 0.5), vec2(-0.5, 0.5), vec2(-0.5, -0.5), vec2(0.5, -0.5)
  165. );
  166. //--
  167.  
  168.  
  169. void main(void)
  170. {
  171. outColor = color;
  172.  
  173. if (NormalMap == 1)
  174. {
  175. finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
  176. finalNormal = normalize(matrixTangent * finalNormal);
  177. }
  178. else finalNormal = normal;
  179.  
  180. if (lightPoint.on == 1) outColor += PointLight(lightPoint);
  181. if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
  182. if (lightDir.on == 1) outColor += DirectionalLight(lightDir);
  183.  
  184. if (shadowCoord.w > 0)
  185. {
  186. // shadows (PDF sampling)
  187. vec2 shadowMapSize = textureSize(shadowMap, 0);
  188. vec2 shadowMapScale = vec2(1.0 / shadowMapSize.x, 1.0 / shadowMapSize.y);
  189.  
  190. // sample each PCF texel around current fragment
  191. float shadowTotal = 0.0;
  192. float samplesUsed = 0.0;
  193. for (int i = 0; i < samplingPositions; i++)
  194. {
  195. // performs sampling and accumulates shadowing factor
  196. shadowTotal += (0.5 + 0.5 * textureProj(shadowMap, vec4(shadowCoord.xy + samplePositions[i] * shadowMapScale * shadowCoord.w, shadowCoord.z, shadowCoord.w)));
  197. samplesUsed += 1;
  198. }
  199.  
  200. // normalize shadows
  201. shadow += shadowTotal / samplesUsed;
  202. }
  203.  
  204. outColor *= shadow;
  205. outColor *= texture(texture0, texCoord0);
  206. //outColor = mix(vec4(fogColor, 1), outColor, fogStrength);
  207.  
  208. // brightness test for bloom PPE
  209. if (brightnessTest)
  210. {
  211. float brightness = dot(outColor.rgb, vec3(0.2126, 0.7152, 0.0722));
  212. if (brightness > 0.7) outColor = outColor;
  213. else outColor = vec4(0.0, 0.0, 0.0, 1.0);
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement