mesh.frag 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // #define ATTRIBUTE_COLOR
  7. #define INSTANCE_COLOR
  8. precision highp float;
  9. struct Light {
  10. vec3 position;
  11. vec3 color;
  12. vec3 ambient;
  13. float falloff;
  14. float radius;
  15. };
  16. uniform Light light;
  17. uniform mat4 view;
  18. varying vec3 vNormal, vViewPosition;
  19. #if defined( UNIFORM_COLOR )
  20. uniform vec3 color;
  21. #elif defined( ATTRIBUTE_COLOR ) || defined( INSTANCE_COLOR ) || defined( ELEMENT_COLOR )
  22. varying vec3 vColor;
  23. #endif
  24. float phongSpecular(vec3 lightDirection, vec3 viewDirection, vec3 surfaceNormal, float shininess) {
  25. //Calculate Phong power
  26. vec3 R = -reflect(lightDirection, surfaceNormal);
  27. return pow(max(0.0, dot(viewDirection, R)), shininess);
  28. }
  29. #define PI 3.14159265
  30. float orenNayarDiffuse(vec3 lightDirection, vec3 viewDirection, vec3 surfaceNormal, float roughness, float albedo) {
  31. float LdotV = dot(lightDirection, viewDirection);
  32. float NdotL = dot(lightDirection, surfaceNormal);
  33. float NdotV = dot(surfaceNormal, viewDirection);
  34. float s = LdotV - NdotL * NdotV;
  35. float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
  36. float sigma2 = roughness * roughness;
  37. float A = 1.0 + sigma2 * (albedo / (sigma2 + 0.13) + 0.5 / (sigma2 + 0.33));
  38. float B = 0.45 * sigma2 / (sigma2 + 0.09);
  39. return albedo * max(0.0, NdotL) * (A + B * s / t) / PI;
  40. }
  41. #pragma glslify: attenuation = require(./attenuation.glsl)
  42. const float specularScale = 0.65;
  43. const float shininess = 30.0;
  44. const float roughness = 5.0;
  45. const float albedo = 0.95;
  46. void main() {
  47. // material color
  48. #if defined( UNIFORM_COLOR )
  49. vec3 material = color;
  50. #elif defined( ATTRIBUTE_COLOR ) || defined( INSTANCE_COLOR ) || defined( ELEMENT_COLOR )
  51. vec3 material = vColor;
  52. #endif
  53. // determine surface to light direction
  54. // vec4 lightPosition = view * vec4(light.position, 1.0);
  55. vec4 lightPosition = vec4(vec3(0.0, 0.0, -10000.0), 1.0);
  56. vec3 lightVector = lightPosition.xyz - vViewPosition;
  57. // calculate attenuation
  58. // float lightDistance = length(lightVector);
  59. float falloff = 1.0; // attenuation(light.radius, light.falloff, lightDistance);
  60. vec3 L = normalize(lightVector); // light direction
  61. vec3 V = normalize(vViewPosition); // eye direction
  62. vec3 N = normalize(-vNormal); // surface normal
  63. // compute our diffuse & specular terms
  64. float specular = phongSpecular(L, V, N, shininess) * specularScale * falloff;
  65. vec3 diffuse = light.color * orenNayarDiffuse(L, V, N, roughness, albedo) * falloff;
  66. vec3 ambient = light.ambient;
  67. // add the lighting
  68. vec3 finalColor = material * (diffuse + ambient) + specular;
  69. // gl_FragColor.rgb = N;
  70. // gl_FragColor.rgb = vec3(1.0, 0.0, 0.0);
  71. gl_FragColor.rgb = finalColor;
  72. gl_FragColor.a = 1.0;
  73. }