You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. uniform vec4 U_AmbientLightColor;
  2. uniform vec4 U_AmbientMaterial;
  3. uniform vec4 U_LightPos;
  4. uniform vec4 U_DiffuseLightColor;
  5. uniform vec4 U_DiffuseMaterial;
  6. uniform vec3 U_EyePos;
  7. uniform vec4 U_SpecularLightColor;
  8. uniform vec4 U_SpecularMaterial;
  9. varying vec3 V_Normal;
  10. varying vec3 V_WorldPos;
  11. void main()
  12. {
  13. //ambient
  14. vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
  15. //diffuse
  16. vec3 L = vec3(0.0);
  17. float distance = 0.0;
  18. float attenuation = 1.0;
  19. //light attribute
  20. float constantFactor=0.5;
  21. float linearFactor=0.3;
  22. float expFactor=0.1;
  23. if(U_LightPos.w==0.0){
  24. //���Ƿ�����
  25. L = normalize(U_LightPos.xyz);
  26. } else {
  27. //���ǵ���Դ
  28. //model point -> light pos
  29. L = normalize(U_LightPos.xyz - V_WorldPos);
  30. distance = length(U_LightPos.xyz - V_WorldPos);
  31. attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
  32. }
  33. vec3 N = normalize(V_Normal);
  34. float diffuseIntensity = max(0.0, dot(L,N));
  35. vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation;
  36. //specular blin
  37. vec3 viewDir = normalize(U_EyePos - V_WorldPos);
  38. vec3 M = normalize(L + viewDir);
  39. vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(M,N)), 64.0) * attenuation;
  40. gl_FragColor = ambientColor + diffuseColor + specularColor;
  41. }