shader.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. return lovr.graphics.newShader([[
  2. // All of these are in view-space.
  3. out vec3 lightDirection; // A vector from the vertex to the light
  4. out vec3 normalDirection;
  5. out vec3 vertexPosition;
  6. const vec3 lightPosition = vec3(0, 10, 3);
  7. vec4 lovrmain() {
  8. vec4 vVertex = ViewFromLocal * VertexPosition;
  9. vec4 vLight = ViewFromWorld * vec4(lightPosition, 1.);
  10. lightDirection = normalize(vec3(vLight - vVertex));
  11. normalDirection = normalize(NormalMatrix * VertexNormal);
  12. vertexPosition = vVertex.xyz;
  13. return DefaultPosition;
  14. }
  15. ]], [[
  16. in vec3 lightDirection;
  17. in vec3 normalDirection;
  18. in vec3 vertexPosition;
  19. const vec3 cAmbient = vec3(.25);
  20. const vec3 cDiffuse = vec3(1);
  21. const vec3 cSpecular = vec3(.35);
  22. vec4 lovrmain() {
  23. vec3 N = normalize(normalDirection);
  24. vec3 L = normalize(lightDirection);
  25. float diffuse = max(dot(N, L), 0.);
  26. float specular = 0.;
  27. if (diffuse > 0.) {
  28. vec3 r = reflect(L, N);
  29. vec3 viewDirection = normalize(-vertexPosition);
  30. float specularAngle = max(dot(r, viewDirection), 0.);
  31. specular = pow(specularAngle, 5.);
  32. }
  33. vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
  34. return vec4(cFinal, 1.) * Color * getPixel(ColorTexture, UV);
  35. }
  36. ]])