Here's the basic 3d shader code with no lighting:
Code: Select all
DepthShader = love.graphics.newShader[[
uniform mat4 view;
uniform mat4 model_matrix;
#ifdef VERTEX
vec4 position(mat4 transform_projection, vec4 vertex_position) {
return view * model_matrix * vertex_position;
}
#endif
#ifdef PIXEL
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 texturecolor = Texel(texture, texture_coords);
return color*texturecolor;
}
#endif
]]
The light_source vec3 is passed in with the value {0,0,0} in the draw function.
Code: Select all
DepthShader = love.graphics.newShader[[
uniform mat4 view;
uniform mat4 model_matrix;
uniform vec3 light_source;
varying mat4 modelView;
varying mat4 modelViewTrans;
varying vec3 normal;
varying vec3 vposition;
#ifdef VERTEX
vec4 position(mat4 transform_projection, vec4 vertex_position) {
modelView = view * model_matrix;
modelViewTrans = view * model_matrix * transform_projection;
// this normal calculation is wrong and needs to be replaced with the actual normal vector
normal = vec3(normalize(modelView * vertex_position));
vposition = vec3(vertex_position);
return view * model_matrix * vertex_position;
}
#endif
#ifdef PIXEL
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
float distance = length(light_source - vposition);
vec3 lightVector = normalize(light_source - vposition);
float diffuse = max(dot(normal, lightVector), 0);
diffuse = diffuse * (1.0 / (1.0 + (0.001 * distance * distance)));
diffuse = min(diffuse, 1);
vec4 texturecolor = Texel(texture, texture_coords);
texturecolor.x *= diffuse;
texturecolor.y *= diffuse;
texturecolor.z *= diffuse;
return color*texturecolor;
}
#endif
]]
This for loop draws all the models in ModelList. Love draws the entire model in one line with this love.graphics.draw when the shader is applied.
Code: Select all
for i=1, #ModelList do
local model = ModelList[i]
if model.visible then
DepthShader:send("model_matrix", model.transform)
love.graphics.draw(model.mesh, -GraphicsWidth()/2, -GraphicsHeight()/2)
end
end
Because Love draws the entire model at once with the shader, I assume the only way to send vertex normals to the shader is by sending a massive array of all the ones in the model before drawing it. But then how would the vertex shader know which normal in that massive array to use? Or am I just misunderstanding something?
Here's a link download my current engine:
https://drive.google.com/open?id=12oRjn ... cSI2QT-R8n_
To enable my broken lighting shader:
on line 87 change: DepthShader = BasicDepthShader to DepthShader = LightingDepthShader
and uncomment line 198: DepthShader:send("light_source", {0,0,0})
Any help would be appreciated!