finalize_color.glsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. uniform vec3 light_position;
  2. uniform vec3 camera_position;
  3. uniform vec3 shading;
  4. vec3 float_to_color(float value, float min_val, float max_val, vec3[9] colormap_data){
  5. float alpha = clamp((value - min_val) / (max_val - min_val), 0.0, 1.0);
  6. int disc_alpha = min(int(alpha * 8), 7);
  7. return mix(
  8. colormap_data[disc_alpha],
  9. colormap_data[disc_alpha + 1],
  10. 8.0 * alpha - disc_alpha
  11. );
  12. }
  13. vec4 add_light(vec4 color, vec3 point, vec3 unit_normal){
  14. if(shading == vec3(0.0)) return color;
  15. float reflectiveness = shading.x;
  16. float gloss = shading.y;
  17. float shadow = shading.z;
  18. vec4 result = color;
  19. vec3 to_camera = normalize(camera_position - point);
  20. vec3 to_light = normalize(light_position - point);
  21. float light_to_normal = dot(to_light, unit_normal);
  22. // When unit normal points towards light, brighten
  23. float bright_factor = max(light_to_normal, 0) * reflectiveness;
  24. // For glossy surface, add extra shine if light beam go towards camera
  25. vec3 light_reflection = reflect(-to_light, unit_normal);
  26. float light_to_cam = dot(light_reflection, to_camera);
  27. float shine = gloss * exp(-3 * pow(1 - light_to_cam, 2));
  28. bright_factor += shine;
  29. result.rgb = mix(result.rgb, vec3(1.0), bright_factor);
  30. if (light_to_normal < 0){
  31. // Darken
  32. result.rgb = mix(
  33. result.rgb,
  34. vec3(0.0),
  35. max(-light_to_normal, 0) * shadow
  36. );
  37. }
  38. return result;
  39. }
  40. vec4 finalize_color(vec4 color, vec3 point, vec3 unit_normal){
  41. ///// INSERT COLOR FUNCTION HERE /////
  42. // The line above may be replaced by arbitrary code snippets, as per
  43. // the method Mobject.set_color_by_code
  44. return add_light(color, point, unit_normal);
  45. }