mesh.frag 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. #ifdef FLAT_SHADED
  7. #extension GL_OES_standard_derivatives : enable
  8. #endif
  9. precision highp float;
  10. // uniform vec3 lightPosition;
  11. uniform vec3 lightColor;
  12. uniform vec3 lightAmbient;
  13. uniform mat4 view;
  14. uniform float alpha;
  15. #ifndef FLAT_SHADED
  16. varying vec3 vNormal;
  17. #endif
  18. varying vec3 vViewPosition;
  19. #pragma glslify: import('./chunks/color-frag-params.glsl')
  20. #pragma glslify: attenuation = require(./utils/attenuation.glsl)
  21. #pragma glslify: calculateSpecular = require(./utils/phong-specular.glsl)
  22. #pragma glslify: calculateDiffuse = require(./utils/oren-nayar-diffuse.glsl)
  23. const float specularScale = 0.65;
  24. const float shininess = 100.0;
  25. const float roughness = 5.0;
  26. const float albedo = 0.95;
  27. void main() {
  28. // material color
  29. #pragma glslify: import('./chunks/color-assign-material.glsl')
  30. // determine surface to light direction
  31. // vec4 viewLightPosition = view * vec4(lightPosition, 1.0);
  32. // vec3 lightVector = viewLightPosition.xyz - vViewPosition;
  33. vec3 lightVector = vViewPosition;
  34. vec3 L = normalize(lightVector); // light direction
  35. vec3 V = normalize(vViewPosition); // eye direction
  36. // surface normal
  37. #ifdef FLAT_SHADED
  38. vec3 fdx = dFdx(vViewPosition);
  39. vec3 fdy = dFdy(vViewPosition);
  40. vec3 N = -normalize(cross(fdx, fdy));
  41. #else
  42. vec3 N = -normalize(vNormal);
  43. #ifdef DOUBLE_SIDED
  44. N = N * (float(gl_FrontFacing) * 2.0 - 1.0);
  45. #endif
  46. #endif
  47. // compute our diffuse & specular terms
  48. float specular = calculateSpecular(L, V, N, shininess) * specularScale;
  49. vec3 diffuse = lightColor * calculateDiffuse(L, V, N, roughness, albedo);
  50. vec3 ambient = lightAmbient;
  51. // add the lighting
  52. vec3 finalColor = material * (diffuse + ambient) + specular;
  53. // gl_FragColor.rgb = N;
  54. // gl_FragColor.a = 1.0;
  55. // gl_FragColor.rgb = vec3(1.0, 0.0, 0.0);
  56. gl_FragColor.rgb = finalColor;
  57. gl_FragColor.a = alpha;
  58. }