Advertisement
PhoenixMee

Untitled

Apr 11th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // FRAGMENT SHADER
  2. #version 330
  3.  
  4. // Matrices
  5. uniform mat4 matrixView;
  6.  
  7. // Materials
  8. uniform vec3 materialAmbient;
  9. uniform vec3 materialDiffuse;
  10. uniform vec3 materialSpecular;
  11. uniform float shininess;
  12. uniform vec3 fogColor;
  13.  
  14. // Textures
  15. uniform sampler2D texture0;
  16. uniform sampler2D textureNormal;
  17. uniform sampler2D textureShore;
  18. uniform sampler2D textureUnderwater;
  19. uniform float NormalMap;
  20.  
  21. // Water
  22. uniform vec3 waterColor;
  23.  
  24. // Local Variables
  25. vec3 finalNormal;
  26.  
  27. in vec4 color;
  28. in vec4 position;
  29. in vec3 normal;
  30. in float fogStrength;
  31. in vec2 texCoord0;
  32. in mat3 matrixTangent;
  33. in float waterDepth;
  34.  
  35. out vec4 outColor;
  36.  
  37.  
  38.  
  39.  
  40. // point light declaration
  41. struct POINT
  42. {
  43. int on;
  44. vec3 position;
  45. vec3 diffuse;
  46. vec3 specular;
  47. };
  48. uniform POINT lightPoint;
  49.  
  50. // spot light declaration
  51. struct SPOT
  52. {
  53. int on;
  54. vec3 position;
  55. vec3 diffuse;
  56. vec3 specular;
  57. vec3 direction;
  58. float cutoff;
  59. float attenuation;
  60. float strength;
  61. mat4 matrix;
  62. };
  63. uniform SPOT lightSpot;
  64.  
  65.  
  66.  
  67.  
  68. // point light function
  69. vec4 PointLight(POINT light)
  70. {
  71.  
  72. // diffuse light
  73. vec4 color = vec4(0, 0, 0, 1);
  74. vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
  75. float NdotL = dot(L, finalNormal);
  76. if (NdotL > 0)
  77. {
  78. color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
  79. }
  80.  
  81. // specular specular
  82. vec3 V = normalize(-position.xyz);
  83. vec3 R = reflect(-L, finalNormal);
  84. float RdotV = dot(R, V);
  85. if (NdotL > 0 && RdotV > 0)
  86. {
  87. color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
  88. }
  89.  
  90. return color;
  91. }
  92.  
  93. // spot light function
  94. vec4 SpotLight(SPOT light)
  95. {
  96. vec4 color = vec4(0, 0, 0, 1);
  97.  
  98. vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
  99.  
  100. // diffuse light
  101. float NdotL = dot(L, finalNormal);
  102. if (NdotL > 0)
  103. {
  104. color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  105. }
  106.  
  107. // specular light
  108. vec3 V = normalize(-position.xyz);
  109. vec3 R = reflect(-L, finalNormal);
  110. float RdotV = dot(R, V);
  111. if (NdotL > 0 && RdotV > 0)
  112. {
  113. color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
  114. }
  115.  
  116.  
  117. // spot light
  118. vec3 D = normalize(mat3(light.matrix) * light.direction);
  119. float S = dot(-L, D);
  120. float A = acos(S);
  121. float cutoff = radians(clamp(light.cutoff, 0, 360));
  122.  
  123. if (A < cutoff) S = pow(S, light.attenuation);
  124. else S = 0;
  125.  
  126. // attenuation
  127. float dist = length(light.matrix * vec4(light.position, 1) - position);
  128. float att = 1 / (dist * dist) / light.strength;
  129.  
  130. return color * S * att;
  131. }
  132.  
  133.  
  134.  
  135.  
  136. void main(void)
  137. {
  138. outColor = color;
  139.  
  140. if (NormalMap == 1)
  141. {
  142. finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
  143. finalNormal = normalize(matrixTangent * finalNormal);
  144. }
  145. else finalNormal = normal;
  146.  
  147. if (lightPoint.on == 1) outColor += PointLight(lightPoint);
  148.  
  149. if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
  150.  
  151. // shoreline multitexturing
  152. float isAboveWater = clamp(-waterDepth, 0, 1);
  153.  
  154. outColor *= texture(texture0, texCoord0);
  155. outColor *= mix(texture(textureUnderwater, texCoord0), texture(textureShore, texCoord0), isAboveWater);
  156. outColor = mix(vec4(fogColor, 1.0), outColor, fogStrength);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement