mesh.frag 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 dFlatShaded
  7. #extension GL_OES_standard_derivatives : enable
  8. #endif
  9. precision highp float;
  10. precision highp int;
  11. #pragma glslify: import('./chunks/common-frag-params.glsl')
  12. #pragma glslify: import('./chunks/color-frag-params.glsl')
  13. // uniform vec3 uLightPosition;
  14. uniform vec3 uLightColor;
  15. uniform vec3 uLightAmbient;
  16. uniform mat4 uView;
  17. #ifndef dFlatShaded
  18. varying vec3 vNormal;
  19. #endif
  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/assign-material-color.glsl')
  30. #if defined(dColorType_objectPicking) || defined(dColorType_instancePicking) || defined(dColorType_groupPicking)
  31. // gl_FragColor = vec4(material.r, material.g, material.a, 1.0);
  32. gl_FragColor = material;
  33. #else
  34. // determine surface to light direction
  35. // vec4 viewLightPosition = view * vec4(lightPosition, 1.0);
  36. // vec3 lightVector = viewLightPosition.xyz - vViewPosition;
  37. vec3 lightVector = vViewPosition;
  38. vec3 L = normalize(lightVector); // light direction
  39. vec3 V = normalize(vViewPosition); // eye direction
  40. // surface normal
  41. #ifdef dFlatShaded
  42. vec3 fdx = dFdx(vViewPosition);
  43. vec3 fdy = dFdy(vViewPosition);
  44. vec3 N = -normalize(cross(fdx, fdy));
  45. #else
  46. vec3 N = -normalize(vNormal);
  47. #ifdef dDoubleSided
  48. N = N * (float(gl_FrontFacing) * 2.0 - 1.0);
  49. #endif
  50. #endif
  51. // compute our diffuse & specular terms
  52. float specular = calculateSpecular(L, V, N, shininess) * specularScale;
  53. vec3 diffuse = uLightColor * calculateDiffuse(L, V, N, roughness, albedo);
  54. vec3 ambient = uLightAmbient;
  55. // add the lighting
  56. vec3 finalColor = material.rgb * (diffuse + ambient) + specular;
  57. // gl_FragColor.rgb = N;
  58. // gl_FragColor.a = 1.0;
  59. // gl_FragColor.rgb = vec3(1.0, 0.0, 0.0);
  60. gl_FragColor.rgb = finalColor;
  61. gl_FragColor.a = material.a;
  62. #pragma glslify: import('./chunks/apply-marker-color.glsl')
  63. #pragma glslify: import('./chunks/apply-fog.glsl')
  64. #endif
  65. }