shader.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. return lovr.graphics.newShader([[
  2. out vec3 lightDirection;
  3. out vec3 normalDirection;
  4. vec3 lightPosition = vec3(0, 10, 3);
  5. vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
  6. vec4 vVertex = transform * vec4(lovrPosition, 1.);
  7. vec4 vLight = lovrView * vec4(lightPosition, 1.);
  8. lightDirection = normalize(vec3(vLight - vVertex));
  9. normalDirection = normalize(lovrNormalMatrix * lovrNormal);
  10. return projection * transform * vertex;
  11. }
  12. ]], [[
  13. in vec3 lightDirection;
  14. in vec3 normalDirection;
  15. vec3 cAmbient = vec3(.25);
  16. vec3 cDiffuse = vec3(.75);
  17. vec3 cSpecular = vec3(.35);
  18. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  19. float diffuse = max(dot(normalDirection, lightDirection), 0.);
  20. float specular = 0.;
  21. if (diffuse > 0.) {
  22. vec3 r = reflect(lightDirection, normalDirection);
  23. vec3 viewDirection = normalize(-vec3(gl_FragCoord));
  24. float specularAngle = max(dot(r, viewDirection), 0.);
  25. specular = pow(specularAngle, 5.);
  26. }
  27. vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
  28. return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
  29. }
  30. ]])