diff --git a/assets/shaders/deferred.frag b/assets/shaders/deferred.frag index 11ba61e..1f52344 100644 --- a/assets/shaders/deferred.frag +++ b/assets/shaders/deferred.frag @@ -1,24 +1,83 @@ #version 450 +#include "shaders://_builtins/constants.glsl" +#include "shaders://_builtins/PBR.glsl" + +struct Light { + mat4 tranform; + vec4 color; + float intensity; +}; + +const int MAX_LIGHT_COUNT = 128; + +layout( push_constant ) uniform constants +{ + uint lightCount; +} PushConstants; + layout(set = 0, binding = 0) uniform sampler2D positionTexture; - layout(set = 0, binding = 1) uniform sampler2D normalTexture; - layout(set = 0, binding = 2) uniform sampler2D albedoTexture; - layout(set = 0, binding = 3) uniform sampler2D specularTexture; +layout(set = 1, binding = 1) uniform LightBuffer { + layout(align = 16) Light lights[MAX_LIGHT_COUNT]; +} LightsBuffer; + layout (location = 0) in vec2 inUV; +layout(location = 1) in vec3 cameraPos; layout(location = 0) out vec4 outColor; void main() { - vec3 fragPos = texture(positionTexture, inUV).rgb; + vec3 position = texture(positionTexture, inUV).rgb; vec3 normal = texture(normalTexture, inUV).rgb; - vec4 albedo = texture(albedoTexture, inUV); + vec3 albedo = texture(albedoTexture, inUV).rgb; - // outColor = vec4(fragPos, 1.0); - // outColor = vec4(normal, 1.0); - outColor = albedo; + vec3 spec = texture(specularTexture, inUV).rgb; + float metallicFactor = spec.b; + float roughnessFactor = spec.g; + float ao = spec.r; + + vec3 V = normalize(cameraPos - position); + + vec3 Lo = vec3(0); + for(int i = 0; i < PushConstants.lightCount; ++i) + { + Light l = LightsBuffer.lights[i]; + + vec3 L = normalize(l.tranform[3].xyz - position); + vec3 H = normalize(V + L); + float distance = length(l.tranform[3].xyz - position); + float attenuation = 1.0 / (distance * distance); + vec3 radiance = l.color.xyz * attenuation * l.intensity; + + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallicFactor); + + vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); + float NDF = DistributionGGX(normal, H, roughnessFactor); + float G = GeometrySmith(normal, V, L, roughnessFactor); + + vec3 numerator = NDF * G * F; + float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, L), 0.0); + vec3 specular = numerator / max(denominator, 0.001); + + vec3 kS = F; + vec3 kD = vec3(1.0) - kS; + kD *= 1.0 - metallicFactor; + + float NdotL = max(dot(normal, L), 0.0); + Lo += (kD * albedo / PI + specular) * radiance * NdotL; + } + + vec3 ambient = vec3(0.2) * albedo * ao; + + vec3 color = ambient + Lo; + // color += vec3(); + color = color / (color + vec3(1.0)); + + outColor = vec4(color, 1.0); } diff --git a/assets/shaders/deferred.vert b/assets/shaders/deferred.vert index b9a0e0f..206a683 100644 --- a/assets/shaders/deferred.vert +++ b/assets/shaders/deferred.vert @@ -1,9 +1,22 @@ #version 450 +layout( push_constant ) uniform constants +{ + uint lightCount; +} PushConstants; + +layout(set = 1, binding = 0) uniform CameraParam { + mat4 projection; + mat4 view; +} Camera; + layout (location = 0) out vec2 outUV; +layout(location = 1) out vec3 cameraPos; void main() { outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f); + + cameraPos = inverse(Camera.view)[3].xyz; } diff --git a/assets/shaders/mrt.frag b/assets/shaders/mrt.frag index 55caee2..17ba776 100755 --- a/assets/shaders/mrt.frag +++ b/assets/shaders/mrt.frag @@ -1,6 +1,10 @@ #version 450 #extension GL_EXT_nonuniform_qualifier : require +#include "shaders://_builtins/constants.glsl" +#include "shaders://_builtins/srgb_ops.glsl" +#include "shaders://_builtins/PBR.glsl" + struct Material { vec4 baseColor; uint albedoTexture; @@ -52,6 +56,7 @@ layout(location = 4) in vec4 outpos; layout (location = 0) out vec4 gPosition; layout (location = 1) out vec3 gNormal; layout (location = 2) out vec3 gAlbedo; +layout (location = 3) out vec3 gSpecular; float near = 0.001; float far = 50.0; @@ -65,32 +70,37 @@ float LinearizeDepth(float depth) void main() { Material material = MaterialBuffers.materials[ PushConstants.materialBufferIndex ]; - vec3 out_normal; + vec3 outNormal; if(material.normalTexture == 0) { - out_normal = TBN[2].xyz; + outNormal = TBN[2].xyz; } else { - vec3 sampled_normal = texture(texSampler[material.normalTexture], uv).rgb; + vec3 sampled_normal = LinearToSRGB(texture(texSampler[material.normalTexture], uv)).rgb; sampled_normal = 2.0 * sampled_normal - vec3(1.0); - out_normal = normalize(TBN * sampled_normal); + outNormal = normalize(TBN * sampled_normal); } + //float intensity = dot(outNormal, normalize(directional_light)) + 0.2; - float intensity = dot(out_normal, normalize(directional_light)) + 0.2; - - vec3 inColor; + vec3 outColor; if(material.albedoTexture == 0) { - inColor = material.baseColor.xyz; + outColor = material.baseColor.xyz; } else { - inColor = texture(texSampler[material.albedoTexture], uv).xyz; + outColor = texture(texSampler[material.albedoTexture], uv).xyz; + } + + vec3 outSpecular; + if(material.metallicRoughnessTexture == 0) { + outSpecular.b = material.metallicFactor; + outSpecular.g = material.roughnessFactor; + outSpecular.r = 1; + } else { + outSpecular = LinearToSRGB(texture(texSampler[material.metallicRoughnessTexture], uv)).xyz; } float depth = LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration gPosition = outpos; - gNormal = out_normal; - gAlbedo = inColor; - // outColor = vec4(vec3(outpos.z) * 0.5 + 0.5 , 1.0); - // outColor = vec4(inColor, 1.0); - // outColor = vec4(vec3(intensity), 1.0); - // outColor = vec4(out_normal, 1.0); + gNormal = outNormal; + gAlbedo = outColor; + gSpecular = outSpecular; } diff --git a/assets/shaders/skybox.frag b/assets/shaders/skybox.frag new file mode 100644 index 0000000..bf4c796 --- /dev/null +++ b/assets/shaders/skybox.frag @@ -0,0 +1,12 @@ +#version 450 + +layout (set = 0, binding = 0) uniform samplerCube skybox; + +layout(location = 0) in vec3 texCoords; + +layout(location = 0) out vec4 outColor; + +void main() +{ + outColor = texture(skybox, texCoords); +} diff --git a/assets/shaders/skybox.vert b/assets/shaders/skybox.vert new file mode 100644 index 0000000..a716cef --- /dev/null +++ b/assets/shaders/skybox.vert @@ -0,0 +1,61 @@ +#version 450 + +layout(set = 1, binding = 0) uniform CameraParam { + mat4 projection; + mat4 view; +} Camera; + +vec3 positions[36] = vec3[]( + vec3(-1.0f, 1.0f, -1.0f), + vec3(-1.0f, -1.0f, -1.0f), + vec3( 1.0f, -1.0f, -1.0f), + vec3( 1.0f, -1.0f, -1.0f), + vec3( 1.0f, 1.0f, -1.0f), + vec3(-1.0f, 1.0f, -1.0f), + + vec3(-1.0f, -1.0f, 1.0f), + vec3(-1.0f, -1.0f, -1.0f), + vec3(-1.0f, 1.0f, -1.0f), + vec3(-1.0f, 1.0f, -1.0f), + vec3(-1.0f, 1.0f, 1.0f), + vec3(-1.0f, -1.0f, 1.0f), + + vec3( 1.0f, -1.0f, -1.0f), + vec3( 1.0f, -1.0f, 1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3( 1.0f, 1.0f, -1.0f), + vec3( 1.0f, -1.0f, -1.0f), + + vec3(-1.0f, -1.0f, 1.0f), + vec3(-1.0f, 1.0f, 1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3( 1.0f, -1.0f, 1.0f), + vec3(-1.0f, -1.0f, 1.0f), + + vec3(-1.0f, 1.0f, -1.0f), + vec3( 1.0f, 1.0f, -1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3( 1.0f, 1.0f, 1.0f), + vec3(-1.0f, 1.0f, 1.0f), + vec3(-1.0f, 1.0f, -1.0f), + + vec3(-1.0f, -1.0f, -1.0f), + vec3(-1.0f, -1.0f, 1.0f), + vec3( 1.0f, -1.0f, -1.0f), + vec3( 1.0f, -1.0f, -1.0f), + vec3(-1.0f, -1.0f, 1.0f), + vec3( 1.0f, -1.0f, 1.0f) +); + +layout(location = 0) out vec3 texCoords; + +void main() +{ + mat4 normalView = mat4(mat3(Camera.view)); + + texCoords = positions[gl_VertexIndex]; + vec4 pos = Camera.projection * normalView * vec4(positions[gl_VertexIndex], 1.0); + gl_Position = pos.xyww; +} diff --git a/assets/textures/skybox/back.tx b/assets/textures/skybox/back.tx new file mode 100644 index 0000000..f35261b Binary files /dev/null and b/assets/textures/skybox/back.tx differ diff --git a/assets/textures/skybox/bottom.tx b/assets/textures/skybox/bottom.tx new file mode 100644 index 0000000..c99ee82 Binary files /dev/null and b/assets/textures/skybox/bottom.tx differ diff --git a/assets/textures/skybox/front.tx b/assets/textures/skybox/front.tx new file mode 100644 index 0000000..321cc04 Binary files /dev/null and b/assets/textures/skybox/front.tx differ diff --git a/assets/textures/skybox/left.tx b/assets/textures/skybox/left.tx new file mode 100644 index 0000000..b2b61d9 Binary files /dev/null and b/assets/textures/skybox/left.tx differ diff --git a/assets/textures/skybox/right.tx b/assets/textures/skybox/right.tx new file mode 100644 index 0000000..0f5344d Binary files /dev/null and b/assets/textures/skybox/right.tx differ diff --git a/assets/textures/skybox/top.tx b/assets/textures/skybox/top.tx new file mode 100644 index 0000000..ab911d9 Binary files /dev/null and b/assets/textures/skybox/top.tx differ