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.

63 lines
1.4 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
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_LightDirection;
  5. uniform float U_Cutoff;
  6. uniform vec4 U_DiffuseLightColor;
  7. uniform vec4 U_DiffuseMaterial;
  8. uniform vec3 U_EyePos;
  9. uniform vec4 U_SpecularLightColor;
  10. uniform vec4 U_SpecularMaterial;
  11. varying vec3 V_Normal;
  12. varying vec3 V_WorldPos;
  13. void main()
  14. {
  15. //�Ƕ�ת����
  16. float radianCutoff = U_Cutoff * 3.14 / 180;
  17. float cosThta = cos(radianCutoff);
  18. vec3 spotLightDirection = normalize(U_LightDirection.xyz);
  19. //ambient
  20. vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
  21. //diffuse
  22. vec3 L = vec3(0.0);
  23. float distance = 0.0;
  24. float attenuation = 1.0;
  25. //light attribute
  26. float constantFactor=0.5;
  27. float linearFactor=0.3;
  28. float expFactor=0.1;
  29. if(U_LightPos.w==0.0){
  30. //���Ƿ�����
  31. L = normalize(U_LightPos.xyz);
  32. } else {
  33. //���ǵ���Դ
  34. //model point -> light pos
  35. L = normalize(U_LightPos.xyz - V_WorldPos);
  36. distance = length(U_LightPos.xyz - V_WorldPos);
  37. attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
  38. }
  39. L = normalize(-L);
  40. vec3 N = normalize(V_Normal);
  41. float currentCosThta = max(0.0, dot(L,spotLightDirection));
  42. float diffuseIntensity = 0.0;
  43. if(currentCosThta > cosThta){
  44. diffuseIntensity = 0.8;
  45. }
  46. vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation;
  47. gl_FragColor = ambientColor + diffuseColor;
  48. }