Skip to content

Commit

Permalink
Mess around with lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
eira-fransham committed Jun 11, 2020
1 parent 1adfddf commit 71178c9
Show file tree
Hide file tree
Showing 14 changed files with 801 additions and 324 deletions.
8 changes: 6 additions & 2 deletions shaders/common.vert.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ layout(binding = 0) uniform Locals {
mat4 u_Proj;
};

void transformTexturedVertex(mat4 view, mat4 proj) {
void transformTexturedVertex(mat4 view, mat4 proj, vec4 pos) {
v_TexCoord = a_TexCoord;
v_Tex = a_Tex;
gl_Position = proj * view * a_Pos;
gl_Position = proj * view * pos;
}

void transformTexturedVertex(mat4 view, mat4 proj) {
transformTexturedVertex(view, proj, a_Pos);
}
42 changes: 25 additions & 17 deletions shaders/models.frag
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
#version 450
#pragma shader_stage(fragment)

#define MAX_LIGHTS 4
#define MAX_LIGHTS 16
#define LIGHT_MULTIPLIER 1
#define LIGHT_FALLOFF 1
#define LIGHT_FALLOFF_SCALE 10

layout(location = 0) in vec2 v_TexCoord;
layout(location = 1) in flat uvec4 v_Tex;
layout(location = 2) in vec3 v_Normal;
layout(location = 3) in vec4 v_Position;
layout(location = 2) in vec3 v_Pos;
layout(location = 3) in vec3 v_Normal;

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 1) uniform texture2D t_Diffuse;
layout(set = 0, binding = 2) uniform sampler s_Color;

struct Light {
vec4 pos;
vec4 color;
};

layout(set = 0, binding = 1) uniform texture2D t_Diffuse;
layout(set = 0, binding = 2) uniform sampler s_Color;
layout(set = 0, binding = 3) uniform Locals {
vec4 _unused;
uint numLights;
float ambientLight;
};
layout(set = 0, binding = 4) uniform Lights {

layout(set = 0, binding = 5) uniform Lights {
Light lights[MAX_LIGHTS];
};

layout(set = 0, binding = 6) uniform NumLights {
uint numLights;
};

float ifLt(float a, float b, float ifTrue, float ifFalse) {
float lt = step(b, a);
Expand All @@ -48,22 +55,23 @@ void main() {
)
);

vec3 shadedAmount = vec3(0.001);
vec3 shadedAmount = vec3(ambientLight);

for (uint i = 0; i < MAX_LIGHTS; i++) {
float dist = length(vec3(lights[i].pos - v_Position));
float amt = 1.0 / (1.0 + (0.01 * dist * dist));
vec3 lightVec = lights[i].pos.xyz - v_Pos;

float dist = length(lightVec) * LIGHT_FALLOFF_SCALE;
float amt = LIGHT_MULTIPLIER * lights[i].color.a / (1.0 + pow(dist, LIGHT_FALLOFF));
float normAmt = max(dot(v_Normal, normalize(lightVec)), 0);

amt = ifLt(amt, 0.001, 0, amt);

shadedAmount += step(i + 1, numLights) *
amt *
max(dot(v_Normal, vec3(lights[i].pos - v_Position)), 0) *
vec3(lights[i].color);
pow(normAmt, 2) *
lights[i].color.rgb;
}

shadedAmount.x = min(shadedAmount.x, 1);
shadedAmount.y = min(shadedAmount.y, 1);
shadedAmount.z = min(shadedAmount.z, 1);

outColor = vec4(shadedAmount, 1) * texture(
sampler2D(t_Diffuse, s_Color), (offset + v_Tex.xy) /
textureSize(sampler2D(t_Diffuse, s_Color), 0)
Expand Down
14 changes: 10 additions & 4 deletions shaders/models.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

layout(location = 3) in vec3 a_Normal;

layout(location = 2) out vec3 v_Normal;
layout(location = 3) out vec4 v_Position;
layout(location = 2) out vec3 v_Pos;
layout(location = 3) out vec3 v_Normal;

layout(set = 0, binding = 4) uniform ModelData {
mat4 translation;
};

void main() {
transformTexturedVertex(u_View, u_Proj);
vec4 pos = translation * a_Pos;

v_Pos = pos.xyz;
v_Normal = a_Normal;
v_Position = a_Pos;

transformTexturedVertex(u_View, u_Proj, pos);
}
2 changes: 1 addition & 1 deletion shaders/post.frag
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ vec3 aces(vec3 color) {
// color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), 0.0, 1.0);
// return pow(color, vec3(invGamma));

lum = clamp((lum * (a * lum + b)) / (lum * (c * lum + d) + e), 0.0, 1.0);
lum = (lum * (a * lum + b)) / (lum * (c * lum + d) + e);
lum = pow(lum, invGamma);

return applyLuminance(color, lum);
Expand Down
3 changes: 1 addition & 2 deletions shaders/skybox.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ layout(set = 0, binding = 1) uniform texture2D t_Diffuse;
layout(set = 0, binding = 2) uniform sampler s_Color;

void main() {
// For some reason the skyboxes seem to be REALLY bright, so we dim them down here
outColor = texture(
sampler2D(t_Diffuse, s_Color), v_TexCoord /
textureSize(sampler2D(t_Diffuse, s_Color), 0)
) / 4;
);
}
2 changes: 1 addition & 1 deletion shaders/world.frag
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ void main() {
sampler2D(t_Diffuse, s_Color),
(offset + vec2(v_Tex.xy) + vec2((v_Tex.z + atlasPadding * 2) * (animationFrame % v_TexCount), 0)) /
textureSize(sampler2D(t_Diffuse, s_Color), 0)
) * light;
) * light * 4;
}
Loading

0 comments on commit 71178c9

Please sign in to comment.