Guest User

material_specular

a guest
Apr 28th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1.  
  2. vec2 lightAttenuation(int i, vec3 normal, vec3 viewdir, float lightcolorA)
  3. {
  4. vec4 lightpos = lights[i];
  5. vec4 lightspot1 = lights[i+2];
  6. vec4 lightspot2 = lights[i+3];
  7.  
  8. float lightdistance = distance(lightpos.xyz, pixelpos.xyz);
  9. if (lightpos.w < lightdistance)
  10. return vec2(0.0); // Early out lights touching surface but not this fragment
  11.  
  12. float attenuation = clamp((lightpos.w - lightdistance) / lightpos.w, 0.0, 1.0);
  13.  
  14. if (lightspot1.w == 1.0)
  15. attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);
  16.  
  17. vec3 lightdir = normalize(lightpos.xyz - pixelpos.xyz);
  18.  
  19. if (lightcolorA < 0.0) // Sign bit is the attenuated light flag
  20. attenuation *= clamp(dot(normal, lightdir), 0.0, 1.0);
  21.  
  22. if (attenuation > 0.0) // Skip shadow map test if possible
  23. attenuation *= shadowAttenuation(lightpos, lightcolorA);
  24.  
  25. if (attenuation <= 0.0)
  26. return vec2(0.0);
  27.  
  28. float glossiness = uSpecularMaterial.x;
  29. float specularLevel = uSpecularMaterial.y;
  30.  
  31. vec3 halfdir = normalize(viewdir + lightdir);
  32. float specAngle = clamp(dot(halfdir, normal), 0.0f, 1.0f);
  33. float phExp = glossiness * 4.0f;
  34. return vec2(attenuation, attenuation * specularLevel * pow(specAngle, phExp));
  35. }
  36.  
  37. vec3 ProcessMaterialLight(Material material, vec3 color)
  38. {
  39. vec4 dynlight = uDynLightColor;
  40. vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
  41.  
  42. vec3 normal = material.Normal;
  43. vec3 viewdir = normalize(uCameraPos.xyz - pixelpos.xyz);
  44.  
  45. if (uLightIndex >= 0)
  46. {
  47. ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
  48. if (lightRange.z > lightRange.x)
  49. {
  50. // modulated lights
  51. for(int i=lightRange.x; i<lightRange.y; i+=4)
  52. {
  53. vec4 lightcolor = lights[i+1];
  54. vec2 attenuation = lightAttenuation(i, normal, viewdir, lightcolor.a);
  55. dynlight.rgb += lightcolor.rgb * attenuation.x;
  56. specular.rgb += lightcolor.rgb * attenuation.y;
  57. }
  58.  
  59. // subtractive lights
  60. for(int i=lightRange.y; i<lightRange.z; i+=4)
  61. {
  62. vec4 lightcolor = lights[i+1];
  63. vec2 attenuation = lightAttenuation(i, normal, viewdir, lightcolor.a);
  64. dynlight.rgb -= lightcolor.rgb * attenuation.x;
  65. specular.rgb -= lightcolor.rgb * attenuation.y;
  66. }
  67. }
  68. }
  69.  
  70. dynlight.rgb = clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);
  71. specular.rgb = clamp(desaturate(specular).rgb, 0.0, 1.4);
  72.  
  73. vec3 frag = material.Base.rgb * dynlight.rgb + material.Specular * specular.rgb;
  74.  
  75. if (uLightIndex >= 0)
  76. {
  77. ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
  78. if (lightRange.w > lightRange.z)
  79. {
  80. vec4 addlight = vec4(0.0,0.0,0.0,0.0);
  81.  
  82. // additive lights
  83. for(int i=lightRange.z; i<lightRange.w; i+=4)
  84. {
  85. vec4 lightcolor = lights[i+1];
  86. vec2 attenuation = lightAttenuation(i, normal, viewdir, lightcolor.a);
  87. addlight.rgb += lightcolor.rgb * attenuation.x;
  88. }
  89.  
  90. frag = clamp(frag + desaturate(addlight).rgb, 0.0, 1.0);
  91. }
  92. }
  93.  
  94. return frag;
  95. }
Add Comment
Please, Sign In to add comment