-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Festini
committed
Jun 16, 2024
1 parent
140dde9
commit b56842e
Showing
23 changed files
with
379 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#version 420 | ||
|
||
in vec4 color; | ||
in vec2 texcoords; | ||
|
||
out vec4 out_color; | ||
|
||
uniform sampler2D tex_sampler; | ||
|
||
|
||
void main() { | ||
out_color = color * texture(tex_sampler, texcoords.xy).rgba; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#version 430 | ||
|
||
layout(location = 0) in vec4 in_vertex; | ||
layout(location = 1) in vec4 in_color; | ||
layout(location = 2) in vec2 in_texcoord; | ||
|
||
layout(location = 0) uniform mat4 mvp_mat; | ||
|
||
out vec4 position; | ||
out vec4 color; | ||
out vec2 texcoords; | ||
|
||
|
||
void main() { | ||
gl_Position = mvp_mat * in_vertex; | ||
color = in_color; | ||
texcoords = in_texcoord; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#version 430 | ||
|
||
in vec4 position; | ||
in vec4 color; | ||
in vec3 light_dir; | ||
in vec3 normal; | ||
in vec2 texcoord; | ||
in mat3 tbn; | ||
|
||
out vec4 out_color; | ||
|
||
layout(location = 0) uniform uint config; | ||
layout(location = 3) uniform vec4 base_color_factors; | ||
layout(location = 4) uniform vec3 emissive_factors; | ||
layout(location = 5) uniform vec2 metallic_factors; | ||
|
||
layout (binding = 0) uniform sampler2D base_color_sampler; | ||
layout (binding = 1) uniform sampler2D metallic_roughness_sampler; | ||
layout (binding = 2) uniform sampler2D normal_sampler; | ||
layout (binding = 3) uniform sampler2D occlusion_sampler; | ||
layout (binding = 4) uniform sampler2D emissive_sampler; | ||
|
||
|
||
const float PI = 3.14159265359; | ||
|
||
|
||
float DistributionGGX(float NdotH, float roughness) { | ||
float a = roughness * roughness; | ||
float a_sq = a * a; | ||
|
||
float d = (NdotH * NdotH) * (a_sq - 1) + 1; | ||
return a_sq / max(PI * d * d, 0.00001); | ||
} | ||
|
||
float GeometrySchlickGGX(vec3 N, vec3 X, float k) { | ||
float NdotX = max(dot(N, X), 0); | ||
float denominator = NdotX * (1 - k) + k; | ||
return NdotX / denominator; | ||
} | ||
|
||
float GeometrySmith(vec3 N, vec3 L, vec3 V, float roughness) { | ||
float k = ((roughness + 1) * (roughness + 1)) / 8.0; | ||
return GeometrySchlickGGX(N, V, k) * GeometrySchlickGGX(N, L, k); | ||
} | ||
|
||
vec3 Fresnel(vec3 F0, float HdotV) { | ||
return F0 + (1 - F0) * pow(1 - HdotV, 5); | ||
} | ||
|
||
|
||
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv) { | ||
// get edge vectors of the pixel triangle | ||
vec3 dp1 = dFdx(p); | ||
vec3 dp2 = dFdy(p); | ||
vec2 duv1 = dFdx(uv); | ||
vec2 duv2 = dFdy(uv); | ||
// solve the linear system | ||
vec3 dp2perp = cross(dp2, N); | ||
vec3 dp1perp = cross(N, dp1); | ||
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; | ||
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; | ||
// construct a scale-invariant frame | ||
float invmax = inversesqrt(max(dot(T,T), dot(B,B))); | ||
return mat3(T * invmax, B * invmax, N); | ||
} | ||
|
||
|
||
void main() { | ||
float metalness = metallic_factors.x; | ||
float roughness = metallic_factors.y; | ||
|
||
vec3 metallic_roughness = texture(metallic_roughness_sampler, texcoord).rgb; | ||
roughness *= metallic_roughness.g; | ||
metalness *= metallic_roughness.b; | ||
|
||
vec3 N = normal.xyz; | ||
if ((config & (1<<18)) != 0) { | ||
mat3 tbn_mat = tbn; | ||
if ((config & 16) == 0) { | ||
tbn_mat = cotangent_frame(normal.xyz, position.xyz, texcoord); | ||
} | ||
N = normalize(tbn_mat * (texture(normal_sampler, texcoord).rgb * 2.0 - 1.0)); | ||
} | ||
|
||
vec4 albedo = base_color_factors * color; | ||
albedo *= texture(base_color_sampler, texcoord); | ||
|
||
vec3 ambient = vec3(.03) * albedo.rgb; | ||
|
||
float occlusion = texture(occlusion_sampler, texcoord).r; | ||
ambient *= vec3(occlusion); | ||
|
||
vec3 emission = emissive_factors; | ||
emission *= texture(emissive_sampler, texcoord).rgb; | ||
|
||
// PBR | ||
vec3 V = normalize(-position.xyz); | ||
vec3 L = light_dir; | ||
vec3 H = normalize(light_dir + V); | ||
|
||
float NdotV = max(dot(N, V), 0); | ||
float NdotL = max(dot(N, L), 0); | ||
float NdotH = max(dot(N, H), 0.00001); | ||
float HdotV = max(dot(H, V), 0.00001); | ||
|
||
const float reflectance = .5; | ||
vec3 f0 = vec3(0.16 * reflectance * reflectance); | ||
vec3 base_reflect = mix(f0, albedo.rgb, metalness); | ||
|
||
float D = DistributionGGX(NdotH, roughness); | ||
float G = GeometrySmith(N, L, V, roughness); | ||
vec3 F = Fresnel(base_reflect, HdotV); | ||
|
||
vec3 specular = (D * G * F) / max(4 * NdotL * NdotV, 0.00001); | ||
vec3 diffuse = (vec3(1.0) - F) * (1.0 - metalness); | ||
|
||
vec3 Lo = (diffuse * albedo.rgb / PI + specular) * NdotL; | ||
Lo += emission; | ||
|
||
|
||
vec3 col_out = ambient + Lo + emission; | ||
col_out *= PI; | ||
//col_out /= col_out + vec3(1.0); | ||
//col_out = pow(col_out, vec3(1.0 / 2.2)); | ||
|
||
out_color = vec4(col_out, albedo.a); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#version 440 | ||
|
||
layout(location = 0) in vec3 in_vertex; | ||
layout(location = 1) in vec3 in_normal; | ||
layout(location = 2) in vec2 in_texcoord; | ||
layout(location = 3) in vec4 in_color; | ||
layout(location = 4) in vec4 in_tangent; | ||
|
||
layout(location = 0) uniform uint config; | ||
layout(location = 1) uniform mat4 viewproj_mat; | ||
layout(location = 2) uniform mat4 model_mat; | ||
|
||
layout(std140, binding = 1) uniform InstanceData { | ||
vec4 inst_positions[512]; | ||
vec4 inst_colors[512]; | ||
}; | ||
|
||
out vec4 position; | ||
out vec4 color; | ||
out vec3 light_dir; | ||
out vec3 normal; | ||
out vec2 texcoord; | ||
out mat3 tbn; | ||
|
||
|
||
void main() { | ||
if ((config & 1) != 0) { | ||
position = model_mat * vec4(in_vertex + inst_positions[gl_InstanceID].xyz, 1.0); | ||
gl_Position = viewproj_mat * position; | ||
} | ||
if ((config & 2) != 0) { | ||
normal = normalize(model_mat * vec4(in_normal, 0)).xyz; | ||
} | ||
if ((config & 4) != 0) { | ||
texcoord = in_texcoord; | ||
} | ||
color = inst_colors[gl_InstanceID]; | ||
if ((config & 8) != 0) { | ||
color *= in_color; | ||
} | ||
|
||
light_dir = normalize(vec3(1, .35, 1)); | ||
|
||
if ((config & 16) != 0) { | ||
vec3 tangent = normalize((model_mat * vec4(in_tangent.xyz, 0)).xyz); | ||
tangent = normalize(tangent - dot(tangent, normal) * normal); | ||
vec3 bitangent = cross(normal, tangent.xyz) * in_tangent.w; | ||
tbn = mat3(tangent, bitangent, normal); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#version 430 | ||
|
||
in vec4 position; | ||
in vec4 color; | ||
in vec3 light_dir; | ||
in vec3 normal; | ||
in vec2 texcoord; | ||
in mat3 tbn; | ||
|
||
out vec4 out_color; | ||
|
||
layout(location = 0) uniform uint config; | ||
layout(location = 3) uniform vec4 base_color_factors; | ||
layout(location = 4) uniform vec3 emissive_factors; | ||
layout(location = 5) uniform vec2 metallic_factors; | ||
|
||
layout (binding = 0) uniform sampler2D base_color_sampler; | ||
layout (binding = 1) uniform sampler2D metallic_roughness_sampler; | ||
layout (binding = 2) uniform sampler2D normal_sampler; | ||
layout (binding = 3) uniform sampler2D occlusion_sampler; | ||
layout (binding = 4) uniform sampler2D emissive_sampler; | ||
|
||
|
||
const float PI = 3.14159265359; | ||
|
||
|
||
float DistributionGGX(vec3 N, vec3 H, float roughness) { | ||
float a = roughness * roughness; | ||
float a_sq = a * a; | ||
float NH = max(dot(N, H), 0); | ||
|
||
float d = (NH * NH) * (a_sq - 1) + 1; | ||
return a_sq / max(PI * d * d, 0.00001); | ||
} | ||
|
||
float GeometrySchlickGGX(vec3 N, vec3 d, float k) { | ||
float Nd = max(dot(N, d), 0); | ||
float denominator = Nd * (1 - k) + k; | ||
return Nd / denominator; | ||
} | ||
|
||
float GeometrySmith(vec3 N, vec3 H, vec3 L, vec3 V, float roughness) { | ||
float k = ((roughness + 1) * (roughness + 1)) / 8.0; | ||
return GeometrySchlickGGX(N, V, k) * GeometrySchlickGGX(N, L, k); | ||
} | ||
|
||
vec3 Fresnel(vec3 F0, float HdotV) { | ||
return F0 + (1 - F0) * pow(1 - HdotV, 5); | ||
} | ||
|
||
|
||
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv) { | ||
// get edge vectors of the pixel triangle | ||
vec3 dp1 = dFdx(p); | ||
vec3 dp2 = dFdy(p); | ||
vec2 duv1 = dFdx(uv); | ||
vec2 duv2 = dFdy(uv); | ||
// solve the linear system | ||
vec3 dp2perp = cross(dp2, N); | ||
vec3 dp1perp = cross(N, dp1); | ||
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; | ||
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; | ||
// construct a scale-invariant frame | ||
float invmax = inversesqrt(max(dot(T,T), dot(B,B))); | ||
return mat3(T * invmax, B * invmax, N); | ||
} | ||
|
||
|
||
void main() { | ||
float metalness = metallic_factors.x; | ||
float roughness = metallic_factors.y; | ||
|
||
vec3 metallic_roughness = texture(metallic_roughness_sampler, texcoord).rgb; | ||
roughness *= metallic_roughness.g; | ||
metalness *= metallic_roughness.b; | ||
|
||
vec3 N = normal.xyz; | ||
if ((config & (1<<18)) != 0) { | ||
mat3 tbn_mat = tbn; | ||
if ((config & 16) == 0) { | ||
tbn_mat = cotangent_frame(normal.xyz, position.xyz, texcoord); | ||
} | ||
N = normalize(tbn_mat * (texture(normal_sampler, texcoord).rgb * 2.0 - 1.0)); | ||
} | ||
|
||
vec4 albedo = base_color_factors * color; | ||
albedo *= texture(base_color_sampler, texcoord); | ||
|
||
vec3 ambient = vec3(.03) * albedo.rgb; | ||
|
||
float occlusion = texture(occlusion_sampler, texcoord).r; | ||
ambient *= vec3(occlusion); | ||
|
||
vec3 emission = emissive_factors; | ||
emission *= texture(emissive_sampler, texcoord).rgb; | ||
|
||
// PBR | ||
vec3 V = normalize(-position.xyz); | ||
vec3 L = light_dir; | ||
vec3 H = normalize(light_dir + V); | ||
|
||
float NdotV = max(dot(N, V), 0); | ||
float NdotL = max(dot(N, L), 0); | ||
float NdotH = max(dot(N, H), 0.00001); | ||
float HdotV = max(dot(H, V), 0.00001); | ||
|
||
vec3 base_reflect = mix(vec3(0.04), albedo.rgb, metalness); | ||
|
||
float D = DistributionGGX(N, H, roughness); | ||
float G = GeometrySmith(N, H, L, V, roughness); | ||
vec3 F = Fresnel(base_reflect, HdotV); | ||
|
||
vec3 specular = (D * G * F) / max(4 * NdotL * NdotV, 0.00001); | ||
vec3 diffuse = (vec3(1.0) - F) * (1.0 - metalness); | ||
|
||
vec3 Lo = (diffuse * albedo.rgb / PI + specular) * NdotL; | ||
Lo += emission; | ||
|
||
|
||
vec3 col_out = ambient + Lo + emission; | ||
col_out *= 2; | ||
//col_out /= col_out + vec3(1.0); | ||
col_out = pow(col_out, vec3(1.0 / 2.2)); | ||
|
||
out_color = vec4(col_out, albedo.a); | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.