Guest User

material_normal

a guest
Apr 28th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1.  
  2. vec3 lightContribution(int i, vec3 normal)
  3. {
  4. vec4 lightpos = lights[i];
  5. vec4 lightcolor = lights[i+1];
  6. vec4 lightspot1 = lights[i+2];
  7. vec4 lightspot2 = lights[i+3];
  8.  
  9. float lightdistance = distance(lightpos.xyz, pixelpos.xyz);
  10. if (lightpos.w < lightdistance)
  11. return vec3(0.0); // Early out lights touching surface but not this fragment
  12.  
  13. vec3 lightdir = normalize(lightpos.xyz - pixelpos.xyz);
  14. float dotprod = dot(normal, lightdir);
  15. if (dotprod < -0.0001) return vec3(0.0); // light hits from the backside. This can happen with full sector light lists and must be rejected for all cases. Note that this can cause precision issues.
  16.  
  17. float attenuation = clamp((lightpos.w - lightdistance) / lightpos.w, 0.0, 1.0);
  18.  
  19. if (lightspot1.w == 1.0)
  20. attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);
  21.  
  22. if (lightcolor.a < 0.0) // Sign bit is the attenuated light flag
  23. {
  24. attenuation *= clamp(dotprod, 0.0, 1.0);
  25. }
  26.  
  27. if (attenuation > 0.0) // Skip shadow map test if possible
  28. {
  29. attenuation *= shadowAttenuation(lightpos, lightcolor.a);
  30. return lightcolor.rgb * attenuation;
  31. }
  32. else
  33. {
  34. return vec3(0.0);
  35. }
  36. }
  37.  
  38. vec3 ProcessMaterialLight(Material material, vec3 color)
  39. {
  40. vec4 dynlight = uDynLightColor;
  41. vec3 normal = material.Normal;
  42.  
  43. if (uLightIndex >= 0)
  44. {
  45. ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
  46. if (lightRange.z > lightRange.x)
  47. {
  48. // modulated lights
  49. for(int i=lightRange.x; i<lightRange.y; i+=4)
  50. {
  51. dynlight.rgb += lightContribution(i, normal);
  52. }
  53.  
  54. // subtractive lights
  55. for(int i=lightRange.y; i<lightRange.z; i+=4)
  56. {
  57. dynlight.rgb -= lightContribution(i, normal);
  58. }
  59. }
  60. }
  61.  
  62. vec3 frag = material.Base.rgb * clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);
  63.  
  64. if (uLightIndex >= 0)
  65. {
  66. ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
  67. if (lightRange.w > lightRange.z)
  68. {
  69. vec4 addlight = vec4(0.0,0.0,0.0,0.0);
  70.  
  71. // additive lights
  72. for(int i=lightRange.z; i<lightRange.w; i+=4)
  73. {
  74. addlight.rgb += lightContribution(i, normal);
  75. }
  76.  
  77. frag = clamp(frag + desaturate(addlight).rgb, 0.0, 1.0);
  78. }
  79. }
  80.  
  81. return frag;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment