shader.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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. vec3 lightPosition = vec3(0, 10, 5);
  7. vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
  8. vec4 vVertex = transform * vec4(lovrPosition, 1.);
  9. vec4 vLight = lovrView * vec4(lightPosition, 1.);
  10. lightDirection = normalize(vec3(vLight - vVertex));
  11. normalDirection = normalize(lovrNormalMatrix * lovrNormal);
  12. vertexPosition = vVertex.xyz;
  13. return projection * transform * vertex;
  14. }
  15. ]], [[
  16. in vec3 lightDirection;
  17. in vec3 normalDirection;
  18. in vec3 vertexPosition;
  19. vec3 cAmbient = vec3(.02);
  20. vec3 cDiffuse = vec3(1);
  21. vec3 cSpecular = vec3(.85);
  22. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  23. float diffuse = max(dot(normalDirection, lightDirection), 0.);
  24. float specular = 0.;
  25. if (diffuse > 0.) {
  26. vec3 r = reflect(lightDirection, normalDirection);
  27. vec3 viewDirection = normalize(-vertexPosition);
  28. float specularAngle = max(dot(r, viewDirection), 0.);
  29. specular = pow(specularAngle, 8.);
  30. }
  31. vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
  32. return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
  33. }
  34. ]])