Skip to content

Commit ceb4a69

Browse files
committed
reduced SSR noise
1 parent dfb0262 commit ceb4a69

File tree

12 files changed

+300
-85
lines changed

12 files changed

+300
-85
lines changed

Assets/Shaders/SSR/SSR.frag

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const float step = 0.1;
1818
const float minRayStep = 0.1;
1919
const float maxSteps = 30;
2020
const int numBinarySearchSteps = 10;
21-
const float reflectionSpecularFalloffExponent = 0.0;
21+
const float reflectionSpecularFalloffExponent = 3.0;
2222

2323
//Credits to imanolfotia for the code, you can find there video and the code at https://imanolfotia.com/blog/1
2424

@@ -153,8 +153,8 @@ void main()
153153
vec3 SSR1 = texture(gFinal, texcoord).xyz * clamp(ReflectionMultiplier, 0.0, 0.9) * Fresnel;
154154
//vec3 SSR = textureLod(gFinal, coords.xy, 0).rgb * clamp(ReflectionMultiplier, 0.0, 0.9) * Fresnel;
155155

156-
if(texture(gRMA,texcoord).z == 1 && Metallic > 0.01)
157-
gSSR = vec4(texture(gFinal, UV).xyz, Metallic);
156+
if(texture(gRMA,texcoord).z > 0.99)
157+
gSSR = vec4(texture(gFinal, UV).xyz, 1);
158158
else
159-
gSSR = vec4(SSR1, Metallic);
159+
gSSR = vec4(SSR1, Metallic);
160160
}

Assets/Shaders/Water/water.frag

+119-23
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,140 @@ layout (binding = 4) uniform sampler2D DefaultNormal;
2020
layout (binding = 2) uniform sampler2D RoughnessTextureSampler;
2121
layout (binding = 3) uniform sampler2D MetalicTextureSampler;
2222

23+
#define MAXLIGHTS 26
24+
25+
uniform vec3 LightColors[MAXLIGHTS];
26+
uniform vec3 LightPositions_worldspace[MAXLIGHTS];
27+
uniform vec3 Lightdirection[MAXLIGHTS];
28+
uniform float LightLinears[MAXLIGHTS];
29+
uniform float LightQuadratics[MAXLIGHTS];
30+
uniform float LightRadius[MAXLIGHTS];
31+
uniform float LightCutOff[MAXLIGHTS];
32+
uniform float LightOuterCutOff[MAXLIGHTS];
33+
uniform samplerCube depthMap[MAXLIGHTS];
34+
2335
uniform float Roughness;
2436
uniform float Metalic;
25-
2637
uniform mat4 V;
27-
2838
uniform bool IsEmissive;
39+
uniform vec3 viewPos;
40+
41+
42+
const float PI = 3.14159265359;
43+
const float far_plane = 25.0; // Constant, moved outside main
44+
vec3 gridSamplingDisk[20] = vec3[]
45+
(
46+
vec3(1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
47+
vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
48+
vec3(1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
49+
vec3(1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
50+
vec3(0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1)
51+
);
52+
53+
float ShadowCalculation(vec3 fragPos, int index)
54+
{
55+
vec3 fragToLight = fragPos - LightPositions_worldspace[index];
56+
float currentDepth = length(fragToLight);
57+
float shadow = 0.0;
58+
float bias = 0.15;
59+
int samples = 20;
60+
float viewDistance = length(viewPos - fragPos);
61+
float diskRadius = (1.0 + (viewDistance / far_plane)) / 25.0;
62+
for(int i = 0; i < samples; ++i)
63+
{
64+
float closestDepth = texture(depthMap[index], fragToLight + gridSamplingDisk[i] * diskRadius).r;
65+
closestDepth *= far_plane; // undo mapping [0;1]
66+
if(currentDepth - bias > closestDepth)
67+
shadow += 1.0;
68+
}
69+
shadow /= float(samples);
70+
71+
return shadow;
72+
}
73+
74+
// ----------------------------------------------------------------------------
75+
float DistributionGGX(vec3 N, vec3 H, float roughness) {
76+
float a = roughness * roughness;
77+
float a2 = a * a;
78+
float NdotH = max(dot(N, H), 0.0);
79+
float NdotH2 = NdotH * NdotH;
80+
81+
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
82+
denom = PI * denom * denom;
83+
84+
return a2 / denom;
85+
}
86+
// ----------------------------------------------------------------------------
87+
float GeometrySchlickGGX(float NdotV, float roughness) {
88+
float k = (roughness + 1.0);
89+
k = (k * k) / 8.0;
90+
91+
return NdotV / (NdotV * (1.0 - k) + k);
92+
}
93+
// ----------------------------------------------------------------------------
94+
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
95+
float NdotV = max(dot(N, V), 0.0);
96+
float NdotL = max(dot(N, L), 0.0);
97+
return GeometrySchlickGGX(NdotV, roughness) * GeometrySchlickGGX(NdotL, roughness);
98+
}
99+
// ----------------------------------------------------------------------------
100+
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
101+
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
102+
}
103+
// ----------------------------------------------------------------------------
29104

30105

31106

32107
void main()
33108
{
34-
vec3 MaterialDiffuseColor = texture(DiffuseTextureSampler, UV).rgb;
109+
vec3 albedo = vec3(0, 0.851, 0.651);
110+
float roughness = 0.5;
111+
float metallic = 1;
112+
vec3 N = TrueNormal;
113+
vec3 V = normalize(viewPos - FragPos.xyz);
114+
// Reflectance at normal incidence
115+
vec3 F0 = mix(vec3(0.04), albedo, metallic);
35116

36-
float MaterialRoughness = Roughness;
37-
if(MaterialRoughness == -1)
38-
MaterialRoughness = texture(RoughnessTextureSampler, UV).r;
117+
vec3 Lo = vec3(0.0);
118+
for (int i = 0; i < MAXLIGHTS; ++i) {
119+
if (LightRadius[i] == 0) continue;
120+
121+
// Calculate distance between light and fragment
122+
vec3 L = normalize(LightPositions_worldspace[i] - FragPos.xyz);
123+
float distance = length(LightPositions_worldspace[i] - FragPos.xyz);
124+
float attenuation = 1.0 / (1.0 + LightLinears[i] * distance + LightQuadratics[i] * (distance * distance));
125+
vec3 radiance = LightColors[i] * attenuation;
126+
127+
// Cook-Torrance BRDF
128+
vec3 H = normalize(V + L);
129+
float NDF = DistributionGGX(N, H, roughness);
130+
float G = GeometrySmith(N, V, L, roughness);
131+
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
132+
133+
vec3 numerator = NDF * G * F;
134+
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
135+
vec3 specular = numerator / denominator;
136+
137+
vec3 kS = F;
138+
vec3 kD = (1.0 - kS) * (1.0 - metallic);
139+
float NdotL = max(dot(N, L), 0.0);
140+
//the 1.3 makes it a little brighter
141+
Lo += ((kD * albedo) / PI + specular) * radiance * NdotL;
142+
143+
}
144+
145+
vec3 color = Lo * 2 + albedo * vec3(0.2);
39146

40-
float MaterialMetalic = Metalic;
41-
if(MaterialRoughness == -1)
42-
MaterialMetalic = texture(MetalicTextureSampler, UV).r;
43-
44-
// Sample the normal map and transform it to world space using the TBN matrix
45-
vec3 normalMap = texture(NormalTextureSampler, UV).rgb;
46-
normalMap = normalMap * 2.0 - 1.0; // Convert from [0,1] range to [-1,1]
47-
vec3 transformedNormal = normalize(TBN * normalMap);
48-
49147

148+
// HDR to ldexp
149+
color = color / (color + vec3(1.0));
150+
vec4 waterColor = vec4(color ,0.7);
151+
//waterColor = vec4(N,1);
50152
// store the fragment position vector in the first gbuffer texture
51153
gPosition = FragPos;
52154
// also store the per-fragment normals into the gbuffer
53155
gRMA = vec4(0.1,1,0,0);
54-
gNormal = vec4(transformedNormal, 0);
55-
gAlbedo = vec4(0,0,1, 0.2); // RGB for Albedo, R for Specular Intensity
156+
gNormal = vec4(N, 0);
157+
gAlbedo = waterColor; // RGB for Albedo, R for Specular Intensity
56158
gTrueNormal = vec4(TrueNormal,0); // Use transpose of TBN to inverse the transformation
57-
if(IsEmissive){
58-
gEmission = vec4(MaterialDiffuseColor, 1);
59-
}
60-
61-
62-
63159
}

Assets/Shaders/Water/water.vert

+1-17
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,17 @@ void main()
5353
BoneTransform += finalBonesMatrices[boneIds[2]] * weights[2];
5454
BoneTransform += finalBonesMatrices[boneIds[3]] * weights[3];
5555
animationTrnasformed = BoneTransform * vec4(vertexPosition_modelspace,1);
56-
57-
mat3 normalMatrix = transpose(inverse(mat3(V * M)));
58-
vec3 normal = normalize(normalMatrix * mat3(BoneTransform) * vertexNormal_modelspace);
59-
vec3 tangent = normalize(normalMatrix * mat3(BoneTransform) * vertexTangent_modelspace);
60-
vec3 bitangent = normalize(normalMatrix * mat3(BoneTransform) * vertexBitangent_modelspace);
61-
62-
TBN = mat3(tangent, bitangent, normal); // Construct TBN matrix for transforming the normal map
6356
}
6457
else{
6558
animationTrnasformed = vec4(vertexPosition_modelspace,1.0f);
66-
mat3 normalMatrix = transpose(inverse(mat3(V * M)));
67-
vec3 normal = normalize(normalMatrix * vertexNormal_modelspace);
68-
vec3 tangent = normalize(normalMatrix * vertexTangent_modelspace);
69-
vec3 bitangent = normalize(normalMatrix * vertexBitangent_modelspace);
70-
71-
TBN = mat3(tangent, bitangent, normal); // Construct TBN matrix for transforming the normal map
7259
}
7360

7461
// Calculate wave 1 displacement
7562
float wave1 = amplitude1 * sin(dot(animationTrnasformed.xz, direction1) * frequency1 + time * speed1);
76-
7763
// Calculate wave 2 displacement
7864
float wave2 = amplitude2 * sin(dot(animationTrnasformed.xz, direction2) * frequency2 + time * speed2);
79-
8065
// Calculate wave 3 displacement
8166
float wave3 = amplitude3 * sin(dot(animationTrnasformed.xz, direction3) * frequency3 + time * speed3) - 1;
82-
8367
// Sum of the waves
8468
float totalWave = wave1 + wave2 + pow(2.718281828459045,wave3);
8569

@@ -115,7 +99,7 @@ void main()
11599
TrueNormal = vec3(M * vec4(normal,0));
116100
worldPos = M * vec4(displacedPosition,1);
117101
viewPos = V * worldPos;
118-
FragPos = viewPos.xyz;
102+
FragPos = worldPos.xyz;
119103
UV = vertexUV;
120104
gl_Position = P * viewPos;
121105

Assets/UI/left_arrow.png

441 Bytes
Loading

Assets/UI/rectangle_1.png

231 Bytes
Loading

Assets/UI/right_arrow.png

412 Bytes
Loading

Engine/Backend.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ namespace Backend
2323
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
2424
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 3.3
2525
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
26-
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
2726
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
2827
#ifdef __APPLE__
2928
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
@@ -46,15 +45,7 @@ namespace Backend
4645
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
4746
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);
4847
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
49-
50-
/*
51-
IMGUI_CHECKVERSION();
52-
ImGui::CreateContext();
53-
ImGuiIO& io = ImGui::GetIO(); (void)io;
54-
ImGui::StyleColorsDark();
55-
ImGui_ImplGlfw_InitForOpenGL(window, true);
56-
ImGui_ImplOpenGL3_Init("#version 430");
57-
*/
48+
5849
Renderer::init();
5950
return 0;
6051
}

Engine/Core/Scene/Scene.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void Scene::Load() {
180180

181181

182182
AssetManager::AddGameObject("map_indirectLight", AssetManager::GetModel("map_indirectLight"), glm::vec3(0, 6, 0), true, 0, Concave);
183-
AssetManager::AddGameObject("water_test", AssetManager::GetModel("water"), glm::vec3(3, 10, -3), true, 0, Concave);
183+
AssetManager::AddGameObject("water_test", AssetManager::GetModel("water"), glm::vec3(-1, 10, -6), true, 0, Concave);
184184
AssetManager::GetGameObject("water_test")->SetShaderType("water");
185185

186186
// Sets renderer

Engine/Core/UI/UI.cpp

-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
11
#include "UI.h"
2-
3-
#define NK_INCLUDE_FIXED_TYPES
4-
#define NK_INCLUDE_STANDARD_IO
5-
#define NK_INCLUDE_STANDARD_VARARGS
6-
#define NK_INCLUDE_DEFAULT_ALLOCATOR
7-
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
8-
#define NK_INCLUDE_FONT_BAKING
9-
#define NK_INCLUDE_DEFAULT_FONT
10-
#define NK_IMPLEMENTATION
11-
#define NK_GLFW_GL4_IMPLEMENTATION
12-
#define NK_KEYSTATE_BASED_INPUT
13-
14-
#include "Engine/nuklear/nuklear.h"
15-
#include "Engine/nuklear/nuklear_glfw_gl4.h"
16-
17-
#define MAX_VERTEX_BUFFER 512 * 1024
18-
#define MAX_ELEMENT_BUFFER 128 * 1024

0 commit comments

Comments
 (0)