Cleanup some shader code
This commit is contained in:
48
src/shaders/include/material.glsl
Normal file
48
src/shaders/include/material.glsl
Normal file
@@ -0,0 +1,48 @@
|
||||
layout (set = 3, binding = 0) uniform Material {
|
||||
uint color_texture_id;
|
||||
vec4 color_factors;
|
||||
|
||||
uint metallic_texture_id;
|
||||
float metallic_factor;
|
||||
float roughness_factor;
|
||||
|
||||
uint normal_texture_id;
|
||||
float normal_scale;
|
||||
|
||||
uint occlusion_texture_id;
|
||||
float occlusion_strength;
|
||||
|
||||
uint emissive_texture_id;
|
||||
vec3 emissive_factors;
|
||||
} materials[];
|
||||
|
||||
|
||||
bool hasColorTexture()
|
||||
{
|
||||
return materials[in_material_id].color_texture_id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
|
||||
bool hasMetallicTexture()
|
||||
{
|
||||
return materials[in_material_id].metallic_texture_id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
|
||||
bool hasNormalTexture()
|
||||
{
|
||||
return materials[in_material_id].normal_texture_id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
|
||||
bool hasOcclusionTexture()
|
||||
{
|
||||
return materials[in_material_id].occlusion_texture_id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
|
||||
bool hasEmissiveTexture()
|
||||
{
|
||||
return materials[in_material_id].emissive_texture_id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
|
||||
bool isValidTex(const uint id)
|
||||
{
|
||||
return id != INVALID_TEXTURE_ID;
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
#version 450
|
||||
|
||||
|
||||
layout (location = 0) in vec3 in_normal;
|
||||
layout (location = 1) in vec2 in_tex_coord;
|
||||
|
||||
layout (location = 2) in flat uint material_id;
|
||||
|
||||
layout(set = 2, binding = 0) uniform sampler2D tex[];
|
||||
|
||||
//TODO: Sort this to be nice and not padded to shit
|
||||
struct Material {
|
||||
uint color_tex_id;
|
||||
vec4 color_factors;
|
||||
|
||||
uint metallic_roughness_tex_id;
|
||||
float metallic_factor;
|
||||
float roughness_factor;
|
||||
|
||||
uint normal_tex_id;
|
||||
float normal_scale;
|
||||
|
||||
uint occlusion_tex_id;
|
||||
float occlusion_strength;
|
||||
|
||||
uint emissive_tex_id;
|
||||
vec3 emissive_factors;
|
||||
};
|
||||
|
||||
layout(set = 3, binding = 0) uniform Material[] materials;
|
||||
|
||||
void main() {
|
||||
out_position = vec4(in_world_pos, 1.0f);
|
||||
|
||||
vec3 N = vec3(0.0f);
|
||||
|
||||
const uint in_normal_idx = materials[material_id].normal_tex_id;
|
||||
const uint in_albedo_idx = materials[material_id].color_tex_id;
|
||||
|
||||
if (in_normal_idx == INVALID_TEXTURE_ID)
|
||||
{
|
||||
N = normalize(in_normal);
|
||||
}
|
||||
else
|
||||
{
|
||||
N = texture(tex[in_normal_idx], in_tex_coord).xyz;
|
||||
}
|
||||
|
||||
out_normal = vec4(N, 1.0);
|
||||
|
||||
vec4 tex_value = texture(tex[in_albedo_idx], in_tex_coord);
|
||||
|
||||
if (tex_value.a < 1.0)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
out_albedo = tex_value;
|
||||
|
||||
out_position.a = linearDepth(out_position.z);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#version 450
|
||||
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec3 color;
|
||||
layout (location = 2) in vec3 normal;
|
||||
layout (location = 3) in vec2 uv;
|
||||
|
||||
layout (location = 4) in mat4 instance_model_matrix;// 4, 5, 6, 7
|
||||
layout (location = 8) in uint material_id;
|
||||
|
||||
|
||||
layout (location = 0) out vec3 out_normal;
|
||||
layout (location = 1) out vec2 out_tex_coord;
|
||||
|
||||
layout (location = 2) out flat uint out_material_id;
|
||||
|
||||
void main() {
|
||||
|
||||
vec4 pos_world = instance_model_matrix * vec4(position, 1.0);
|
||||
|
||||
gl_Position = ubo.projection * ubo.view * pos_world;
|
||||
|
||||
mat3 normal_matrix = transpose(inverse(mat3(instance_model_matrix)));
|
||||
|
||||
out_normal = normalize(normal_matrix * normal);
|
||||
|
||||
out_tex_coord = uv;
|
||||
|
||||
out_material_id = material_id;
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
#extension GL_EXT_debug_printf: enable
|
||||
|
||||
layout (location = 0) in vec3 in_normal;
|
||||
layout (location = 1) in vec2 in_tex_coord;
|
||||
layout (location = 1) in vec2 in_uv;
|
||||
layout (location = 2) in vec3 in_world_pos;
|
||||
layout (location = 3) in flat uint in_material_id;
|
||||
|
||||
@@ -17,23 +17,7 @@ layout (set = 1, binding = 0) uniform CameraInfo {
|
||||
|
||||
layout (set = 2, binding = 0) uniform sampler2D tex[];
|
||||
|
||||
layout (set = 3, binding = 0) uniform Material {
|
||||
uint color_texture_id;
|
||||
vec4 color_factors;
|
||||
|
||||
uint metallic_texture_id;
|
||||
float metallic_factor;
|
||||
float roughness_factor;
|
||||
|
||||
uint normal_texture_id;
|
||||
float normal_scale;
|
||||
|
||||
uint occlusion_texture_id;
|
||||
float occlusion_strength;
|
||||
|
||||
uint emissive_texture_id;
|
||||
vec3 emissive_factors;
|
||||
} materials[];
|
||||
#include "include/material.glsl"
|
||||
|
||||
float linearDepth(float depth)
|
||||
{
|
||||
@@ -51,29 +35,34 @@ void main()
|
||||
return;
|
||||
}
|
||||
|
||||
uint in_normal_idx = materials[in_material_id].normal_texture_id;
|
||||
vec3 N = vec3(0.0f);
|
||||
|
||||
if (in_normal_idx == INVALID_TEXTURE_ID)
|
||||
uint normal_id = materials[in_material_id].normal_texture_id;
|
||||
if (isValidTex(normal_id))
|
||||
{
|
||||
N = normalize(in_normal);
|
||||
vec3 normal_vec = texture(tex[normal_id], in_uv).xyz;
|
||||
out_normal = vec4(normal_vec, 1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
N = texture(tex[in_normal_idx], in_tex_coord).xyz;
|
||||
out_normal = vec4(in_normal, 1.0f);
|
||||
}
|
||||
|
||||
out_normal = vec4(N, 1.0);
|
||||
uint albedo_id = materials[in_material_id].color_texture_id;
|
||||
|
||||
uint in_albedo_idx = materials[in_material_id].color_texture_id;
|
||||
vec4 tex_value = texture(tex[in_albedo_idx], in_tex_coord);
|
||||
|
||||
if (tex_value.a < 1.0)
|
||||
if (albedo_id == INVALID_TEXTURE_ID)
|
||||
{
|
||||
discard;
|
||||
out_albedo = vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
vec4 tex_value = texture(tex[albedo_id], in_uv);
|
||||
|
||||
out_albedo = tex_value;
|
||||
if (tex_value.a < 1.0)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
out_albedo = tex_value;
|
||||
}
|
||||
|
||||
out_position.a = linearDepth(out_position.z);
|
||||
}
|
||||
Reference in New Issue
Block a user