Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uniform sampler2D primaryTexture;
  2. uniform sampler2D secondaryTexture; //normal map
  3. uniform vec3[100] light;
  4. uniform vec4[100] lightColor;
  5. uniform float[100] lightType; // 0=point, 1=spot, 2=directional
  6. uniform vec4[100] lightParams; // x=radius, y=intensity, z=yaw, w=pitch
  7. uniform int lightCount;
  8. uniform vec4 ambiantColor;
  9. uniform vec4 material; // x=ambiant, y=diffuse, z=specular, w=specularMultiplier
  10.  
  11. varying vec4 position;
  12.  
  13. vec3 texToNormal(vec3 vec)
  14. {
  15.     return normalize(vec3(-(vec.x-0.5), vec.y-0.5, -(vec.z-0.5)));
  16. }
  17.  
  18. void point(vec3 normal, int i, out vec4 diffuse, out vec4 specular)
  19. {
  20.     vec3 delta = vec3(position.xyz - light[i]);
  21.  
  22.     float l = length(delta.xy);
  23.     l = (lightParams[i].x - l) / lightParams[i].x;
  24.     if(l < 0.0)
  25.         return;
  26.  
  27.     vec3 R = normalize(reflect(delta, normal));
  28.     vec3 V = vec3(0, 0, 1);
  29.  
  30.     diffuse = max(0.0, dot(normal, normalize(delta))) * material.y * lightColor[i] * l * lightParams[i].y;
  31.     specular = max(0.0, pow(dot(R, V), material.w)) * material.z * lightColor[i] * l * lightParams[i].y;
  32. }
  33.  
  34. void spot(vec3 normal, int i, out vec4 diffuse, out vec4 specular)
  35. {
  36.     if(lightParams[i].w <= 0.0)
  37.         return;
  38.  
  39.     vec3 delta = vec3(position.xyz - light[i]);
  40.     vec2 dir = vec2(cos(lightParams[i].z), sin(lightParams[i].z));
  41.     float angle = acos(dot(normalize(dir), normalize(delta.xy)));
  42.  
  43.     float l = length(delta.xy);
  44.     l = (lightParams[i].x - l) / lightParams[i].x;
  45.     if(l < 0.0)
  46.         return;
  47.     float m = (lightParams[i].w - abs(angle)) / lightParams[i].w;
  48.     if(m < 0.0)
  49.         return;
  50.  
  51.     vec3 R = normalize(reflect(delta, normal));
  52.     vec3 V = vec3(0, 0, 1);
  53.  
  54.     diffuse = max(0.0, dot(normal, normalize(delta))) * material.y * lightColor[i] * l * m * lightParams[i].y;
  55.     specular = max(0.0, pow(dot(R, V), material.w)) * material.z * lightColor[i] * l * m * lightParams[i].y;
  56. }
  57.  
  58. void directional(vec3 normal, int i, out vec4 diffuse, out vec4 specular)
  59. {
  60.     vec3 delta = vec3(cos(lightParams[i].z)*cos(lightParams[i].w), sin(lightParams[i].z)*cos(lightParams[i].w), sin(lightParams[i].w));
  61.  
  62.     vec3 R = normalize(reflect(delta, normal));
  63.     vec3 V = vec3(0, 0, 1);
  64.  
  65.     diffuse = max(0.0, dot(normal, normalize(delta))) * material.y * lightColor[i] * lightParams[i].y;
  66.     if(delta.z < 0)
  67.         specular = max(0.0, pow(dot(R, V), material.w)) * material.z * lightColor[i] * lightParams[i].y;
  68. }
  69.  
  70. void main()
  71. {
  72.     vec4 pixel = texture2D(primaryTexture, gl_TexCoord[0].xy);
  73.     vec3 normal = texToNormal(texture2D(secondaryTexture, gl_TexCoord[0].xy).xyz);
  74.  
  75.     vec4 diffuseColor = vec4(0.0, 0.0, 0.0, 0.0);
  76.     vec4 specularColor = vec4(0.0, 0.0, 0.0, 0.0);
  77.  
  78.     vec4 diffuseTempColor = vec4(0.0, 0.0, 0.0, 0.0);
  79.     vec4 specularTempColor = vec4(0.0, 0.0, 0.0, 0.0);
  80.  
  81.     for(int i = 0 ; i < lightCount ; i++)
  82.     {
  83.         if(lightType[i] <= 0.5)
  84.             point(normal, i, diffuseTempColor, specularTempColor);
  85.         else if(lightType[i] <= 1.5)
  86.             spot(normal, i, diffuseTempColor, specularTempColor);
  87.         else if(lightType[i] <= 2.5)
  88.             directional(normal, i, diffuseTempColor, specularTempColor);
  89.         diffuseColor += diffuseTempColor;
  90.         specularColor += specularTempColor;
  91.  
  92.         diffuseTempColor = vec4(0, 0, 0, 0);
  93.         specularTempColor = vec4(0, 0, 0, 0);
  94.     }
  95.  
  96.     specularColor.w = 0;
  97.  
  98.     gl_FragColor = gl_Color * pixel * (ambiantColor * material.x + diffuseColor) + specularColor;
  99.     gl_FragColor.w = pixel.w;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement