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.

59 lines
1.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
  4. uniform sampler2D U_Texture;
  5. uniform vec4 U_LightPos;
  6. uniform vec4 U_LightAmbient;
  7. uniform vec4 U_LightDiffuse;
  8. uniform vec4 U_LightSpecular;
  9. uniform vec4 U_AmbientMaterial;
  10. uniform vec4 U_DiffuseMaterial;
  11. uniform vec4 U_SpecularMaterial;
  12. uniform vec4 U_CameraPos;
  13. uniform vec4 U_LightOpt;
  14. varying vec4 V_Color;
  15. varying vec4 V_Normal;
  16. varying vec4 V_WorldPos;
  17. varying vec4 V_Texcoord;
  18. vec4 GetPointLight(){
  19. float distance=0.0;
  20. float constantFactor=1.0;
  21. float linearFactor=0.0;
  22. float quadricFactor=0.0;
  23. vec4 ambientColor=U_LightAmbient*U_AmbientMaterial;
  24. vec3 L=vec3(0.0,1.0,0.0)-V_WorldPos.xyz;
  25. distance=length(L);
  26. float attenuation=1.0/(constantFactor+linearFactor*distance+quadricFactor*quadricFactor*distance);
  27. L=normalize(L);
  28. vec3 n=normalize(V_Normal.xyz);
  29. float diffuseIntensity=max(0.0,dot(L,n));
  30. vec4 diffuseColor=vec4(1.0,1.0,1.0,1.0)*vec4(0.1,0.4,0.6,1.0)*diffuseIntensity*attenuation*2.0;
  31. return ambientColor+diffuseColor;
  32. }
  33. void main()
  34. {
  35. vec4 color=vec4(0.0,0.0,0.0,0.0);
  36. vec4 ambientColor=U_LightAmbient*U_AmbientMaterial;
  37. vec3 lightPos=U_LightPos.xyz;
  38. vec3 L=lightPos;
  39. L=normalize(L);
  40. vec3 n=normalize(V_Normal.xyz);
  41. float diffuseIntensity=max(0.0,dot(L,n));
  42. vec4 diffuseColor=U_LightDiffuse*U_DiffuseMaterial*diffuseIntensity;
  43. vec4 specularColor=vec4(0.0,0.0,0.0,0.0);
  44. if(diffuseIntensity!=0.0){
  45. vec3 reflectDir=normalize(reflect(-L,n));
  46. vec3 viewDir=normalize(U_CameraPos.xyz-V_WorldPos.xyz);
  47. specularColor=U_LightSpecular*U_SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),U_LightOpt.x);
  48. }
  49. if(U_LightOpt.w==1.0){
  50. color=ambientColor+diffuseColor*texture2D(U_Texture,V_Texcoord.xy)+specularColor;
  51. }else{
  52. color=ambientColor+diffuseColor+GetPointLight();
  53. color=color*texture2D(U_Texture,V_Texcoord.xy);
  54. }
  55. gl_FragData[0]=color;
  56. gl_FragData[1]=color;
  57. }