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.

43 lines
1.3 KiB

4 years ago
  1. uniform vec4 U_AmbientMaterial;
  2. uniform vec4 U_AmbientLight;
  3. uniform vec4 U_DiffuseMaterial;
  4. uniform vec4 U_DiffuseLight;
  5. uniform vec4 U_SpecularMaterial;
  6. uniform vec4 U_SpecularLight;
  7. uniform vec4 U_LightPos;
  8. uniform vec4 U_CameraPos;
  9. varying vec4 V_Normal;
  10. varying vec4 V_WorldPos;
  11. void main(){
  12. //ambient
  13. vec4 ambientColor = U_AmbientMaterial * U_AmbientLight;
  14. //diffuse
  15. vec3 n = normalize(V_Normal.xyz);
  16. vec3 l = normalize(U_LightPos.xyz - V_WorldPos.xyz);
  17. float duffuseIntensity = max(0.0, dot(l,n));
  18. //������˥������
  19. float attenuation = 1.0;
  20. float distance = 0;
  21. float constantFactor = 0.5;
  22. float linearFactor = 0.3;
  23. float expFactor = 0.1;
  24. distance =length(U_LightPos.xyz - V_WorldPos.xyz);
  25. attenuation = 1.0 / (constantFactor + linearFactor*distance + expFactor*distance*distance);
  26. vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity * attenuation;
  27. //specular
  28. vec4 specularColor = vec4(0);
  29. if(duffuseIntensity > 0){
  30. vec3 rd = normalize(reflect(-l,n));
  31. vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz);
  32. float specularIntensity = pow(max(0.0,dot(vd,rd)), 128);
  33. specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity;
  34. }
  35. gl_FragColor = ambientColor + duffuseColor + specularColor;
  36. }