Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. #ifdef FLAG_LIGHT
  2. uniform int n_lights;
  3. #endif
  4.  
  5. #ifdef FLAG_DIFFUSE_MAP
  6. uniform sampler2D sBasemap;
  7. #endif
  8.  
  9. #ifdef FLAG_GLOW_MAP
  10. uniform sampler2D sGlowmap;
  11. #endif
  12.  
  13. #ifdef FLAG_SPEC_MAP
  14. uniform sampler2D sSpecmap;
  15. #endif
  16.  
  17. #ifdef FLAG_ENV_MAP
  18. uniform samplerCube sEnvmap;
  19. uniform bool alpha_spec;
  20. varying vec3 envReflect;
  21. #endif
  22.  
  23. #ifdef FLAG_NORMAL_MAP
  24. uniform sampler2D sNormalmap;
  25. varying mat3 tbnMatrix;
  26. #endif
  27.  
  28. #ifdef FLAG_FOG
  29. varying float fogDist;
  30. #endif
  31.  
  32. varying vec4 position;
  33. varying vec3 lNormal;
  34.  
  35. #if SHADER_MODEL == 2
  36. #define MAX_LIGHTS 2
  37. #else
  38. #define MAX_LIGHTS 8
  39. #endif
  40.  
  41. #define SPEC_INTENSITY_POINT 5.3 // Point light
  42. #define SPEC_INTENSITY_DIRECTIONAL 3.0 // Directional light
  43. #define SPECULAR_FACTOR 1.75
  44. #define SPECULAR_ALPHA 0.1
  45. #define SPEC_FACTOR_NO_SPEC_MAP 0.6
  46. #define ENV_ALPHA_FACTOR 0.3
  47. #define GLOW_MAP_INTENSITY 1.5
  48. #define AMBIENT_LIGHT_BOOST 1.0
  49.  
  50. void main()
  51. {
  52. vec3 eyeDir = vec3(normalize(-position).xyz); // Camera is at (0,0,0) in ModelView space
  53. vec4 lightAmbientDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
  54. vec4 lightDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
  55. vec4 lightAmbient = vec4(0.0, 0.0, 0.0, 1.0);
  56. vec4 lightSpecular = vec4(0.0, 0.0, 0.0, 1.0);
  57. vec2 texCoord = gl_TexCoord[0].xy;
  58.  
  59. #ifdef FLAG_LIGHT
  60. #ifdef FLAG_NORMAL_MAP
  61. // Normal map - convert from DXT5nm
  62. vec3 normal;
  63.  
  64. normal.rg = (texture2D(sNormalmap, texCoord).ag * 2.0) - 1.0;
  65. #ifdef FLAG_ENV_MAP
  66. vec3 envOffset = vec3(0.0);
  67. envOffset.xy = normal.xy;
  68. #endif
  69. normal.b = sqrt(1.0 - dot(normal.rg, normal.rg));
  70. normal = normalize(tbnMatrix * normal);
  71. #else
  72. vec3 normal = lNormal;
  73. #endif
  74.  
  75. vec3 lightDir;
  76. lightAmbient = gl_FrontMaterial.emission + (gl_LightModel.ambient * gl_FrontMaterial.ambient);
  77.  
  78. #pragma optionNV unroll all
  79. for (int i = 0; i < MAX_LIGHTS; ++i) {
  80. #if SHADER_MODEL > 2
  81. if (i > n_lights)
  82. break;
  83. #endif
  84. float specularIntensity = 1.0;
  85. float attenuation = 1.0;
  86. float tubeEffect = 0.0;
  87.  
  88. // Attenuation and light direction
  89. #if SHADER_MODEL > 2
  90. if (gl_LightSource[i].position.w == 1.0) {
  91. #else
  92. if (gl_LightSource[i].position.w == 1.0 && i != 0) {
  93. #endif
  94. // Positional light source
  95. float dist = distance(gl_LightSource[i].position.xyz, position.xyz);
  96.  
  97. lightDir = normalize(gl_LightSource[i].position.xyz - position.xyz);
  98.  
  99. #if SHADER_MODEL > 2
  100. if (gl_LightSource[i].spotCutoff < 91.0) { // Tube light
  101. vec3 nearest;
  102. float beamlength = length(gl_LightSource[i].spotDirection);
  103. vec3 beamDir = normalize(gl_LightSource[i].spotDirection);
  104. float neardist = dot(-lightDir, gl_LightSource[i].spotDirection);
  105. if(neardist > 0.0)
  106. nearest = gl_LightSource[i].position.xyz;
  107. else
  108. nearest = gl_LightSource[i].position.xyz + beamDir * neardist;
  109. lightDir = nearest - position.xyz;
  110. dist = length(lightDir);
  111. tubeEffect = pow(500.0/dist,1.2);
  112. lightDir = normalize(lightDir);
  113. }
  114. #endif
  115. attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + (gl_LightSource[i].linearAttenuation * dist) + (gl_LightSource[i].quadraticAttenuation * dist * dist));
  116.  
  117. specularIntensity = SPEC_INTENSITY_POINT; // Point light
  118. } else {
  119. // Directional light source
  120. lightDir = normalize(gl_LightSource[i].position.xyz);
  121. specularIntensity = SPEC_INTENSITY_DIRECTIONAL; // Directional light
  122. }
  123.  
  124. // Ambient and Diffuse
  125. lightAmbient += (gl_FrontLightProduct[i].ambient * attenuation);
  126. lightDiffuse += (gl_FrontLightProduct[i].diffuse * (max(dot(normal, lightDir), 0.0)) * attenuation);
  127. lightSpecular += gl_FrontLightProduct[i].specular * tubeEffect;
  128.  
  129. // Specular
  130. float NdotHV = clamp(dot(normal, normalize(eyeDir + lightDir)), 0.0, 1.0);
  131. lightSpecular += ((gl_FrontLightProduct[i].specular * pow(NdotHV, gl_FrontMaterial.shininess)) * attenuation) * specularIntensity;
  132. }
  133.  
  134. lightAmbientDiffuse = lightAmbient + lightDiffuse;
  135. #else
  136. lightAmbientDiffuse = gl_Color;
  137. lightSpecular = gl_SecondaryColor;
  138. #endif
  139.  
  140. #ifdef FLAG_DIFFUSE_MAP
  141. // Base color
  142. vec4 baseColor = texture2D(sBasemap, texCoord);
  143. #else
  144. vec4 baseColor = gl_Color;
  145. #endif
  146.  
  147. vec4 fragmentColor;
  148. fragmentColor.rgb = baseColor.rgb * max(lightAmbientDiffuse.rgb * AMBIENT_LIGHT_BOOST, gl_LightModel.ambient.rgb - 0.425);
  149. fragmentColor.a = baseColor.a;
  150.  
  151. #ifdef FLAG_SPEC_MAP
  152. // Spec color
  153. fragmentColor.rgb += lightSpecular.rgb * (texture2D(sSpecmap, texCoord).rgb * SPECULAR_FACTOR);
  154. fragmentColor.a += (dot(lightSpecular.a, lightSpecular.a) * SPECULAR_ALPHA);
  155. #else
  156. fragmentColor.rgb += lightSpecular.rgb * (baseColor.rgb * SPEC_FACTOR_NO_SPEC_MAP);
  157. #endif
  158.  
  159. #ifdef FLAG_ENV_MAP
  160. // Env color
  161. #ifdef FLAG_NORMAL_MAP
  162. vec3 envReflectNM = envReflect + envOffset;
  163. vec3 envIntensity = (alpha_spec) ? vec3(texture2D(sSpecmap, texCoord).a) : texture2D(sSpecmap, texCoord).rgb;
  164. fragmentColor.a += (dot(textureCube(sEnvmap, envReflectNM).rgb, textureCube(sEnvmap, envReflectNM).rgb) * ENV_ALPHA_FACTOR);
  165. fragmentColor.rgb += textureCube(sEnvmap, envReflectNM).rgb * envIntensity;
  166. #else
  167. vec3 envIntensity = (alpha_spec) ? vec3(texture2D(sSpecmap, texCoord).a) : texture2D(sSpecmap, texCoord).rgb;
  168. fragmentColor.a += (dot(textureCube(sEnvmap, envReflect).rgb, textureCube(sEnvmap, envReflect).rgb) * ENV_ALPHA_FACTOR);
  169. fragmentColor.rgb += textureCube(sEnvmap, envReflect).rgb * envIntensity;
  170. #endif
  171. #endif
  172.  
  173. #ifdef FLAG_GLOW_MAP
  174. // Glow color
  175. fragmentColor.rgb += texture2D(sGlowmap, texCoord).rgb * GLOW_MAP_INTENSITY;
  176. #endif
  177.  
  178. #ifdef FLAG_FOG
  179. fragmentColor.rgb = mix(fragmentColor.rgb, gl_Fog.color.rgb, fogDist);
  180. #endif
  181.  
  182. gl_FragColor = fragmentColor;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement