shader.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. return lovr.graphics.newShader([[
  2. // Declare two variables that we are going to calculate in the vertex shader and send to the
  3. // fragment shader.
  4. out vec3 vertexPosition;
  5. out vec3 lightViewPosition;
  6. out vec3 rawNormal;
  7. out vec3 normalDirection;
  8. uniform vec3 lightPosition = vec3(-1000, 3000, -3000);
  9. uniform mat4 viewMat;
  10. vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
  11. vec4 transformedVertex = lovrTransform * vec4(lovrPosition, 1.);
  12. vertexPosition = vec3(transformedVertex) / transformedVertex.w;
  13. lightViewPosition = vec3(viewMat * vec4(lightPosition, 1));
  14. rawNormal = lovrNormal;
  15. normalDirection = normalize(lovrNormalMatrix * lovrNormal);
  16. return projection * transform * vertex;
  17. }
  18. ]], [[
  19. // Declare the two variables that are sent to us by the vertex shader
  20. in vec3 vertexPosition;
  21. in vec3 lightViewPosition;
  22. in vec3 rawNormal;
  23. in vec3 normalDirection;
  24. uniform vec3 ambientColor = vec3(.2, .2, .2);
  25. uniform vec3 diffuseColor = vec3(.7, .7, .7);
  26. uniform vec3 specularColor = vec3(.3, .3, .3);
  27. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  28. vec3 lightDirection = normalize(lightViewPosition - vertexPosition);
  29. float diffuse = max(dot(lightDirection, normalDirection), 0);
  30. float specular = 0.;
  31. if (diffuse > 0.) {
  32. vec3 reflectDir = reflect(-lightDirection, normalDirection);
  33. vec3 viewDirection = normalize(-vertexPosition);
  34. float specularAngle = max(dot(reflectDir, viewDirection), 0.);
  35. specular = pow(specularAngle, 5.);
  36. }
  37. vec3 finalColor = pow(clamp(vec3(diffuse) * diffuseColor + vec3(specular) * specularColor, ambientColor, vec3(1.)), vec3(.4545));
  38. return vec4(finalColor, 1.) * graphicsColor * texture(image, uv);
  39. }
  40. ]])