diff options
author | Sebastian Park <SebPark03@gmail.com> | 2024-05-08 01:14:11 -0400 |
---|---|---|
committer | Sebastian Park <SebPark03@gmail.com> | 2024-05-08 01:14:11 -0400 |
commit | 3d9a55316dbcfb2ca1f32f5554d1948fcabb5d74 (patch) | |
tree | f2ca9a05d094ab0a85a42f500fb9ea1cc6630240 /resources | |
parent | dcab788763ff0af5918ca0c50538daa8bbfc84d8 (diff) |
Do some scuffed caustics.
Diffstat (limited to 'resources')
-rw-r--r-- | resources/shaders/color.frag | 42 | ||||
-rw-r--r-- | resources/shaders/color.vert | 92 | ||||
-rwxr-xr-x | resources/shaders/shader.frag | 3 | ||||
-rwxr-xr-x | resources/shaders/shader.vert | 6 |
4 files changed, 130 insertions, 13 deletions
diff --git a/resources/shaders/color.frag b/resources/shaders/color.frag index 4a052a1..fe70983 100644 --- a/resources/shaders/color.frag +++ b/resources/shaders/color.frag @@ -1,8 +1,42 @@ -#version 330 core - -in vec3 col; +#version 410 core out vec4 fragColor; +in vec3 normal_cameraSpace; +in vec3 camera_worldSpace; +in vec3 normal_worldSpace; +in vec3 pos; +in vec3 oldPosFlat; +in float intensity; + +//uniform float multiplier = 1.f; +//uniform float contrast = 10.f; +//uniform float intExp = 0.5f; +//uniform float scale = 1.f; + +uniform float multiplier = .9f; +uniform float contrast = 20.f; +uniform float intExp = 0.f; +uniform float scale = 1.f; + +//uniform float multiplier = .5f; +//uniform float contrast = 1.5f; +//uniform float intExp = 0.f; +//uniform float scale = 1.f; + +//uniform vec4 baseColor =vec4(); + void main() { - fragColor = vec4(col, 1.0); + fragColor = 0.5f * (vec4(normal_worldSpace[0], 0.f, normal_worldSpace[2], 1.f) + 1); + float oldArea = length(dFdx(oldPosFlat)) * length(dFdy(oldPosFlat)); + float newArea = length(dFdx(pos)) * length(dFdy(pos)); + float areaRatio = oldArea / newArea * multiplier; +// if(oldArea / newArea > 3) { +// areaRatio = 0.f; +// } +// fragColor = clamp(vec4(intensity, intensity, (1.f - intensity) * .5f, intensity), 0.f, 1.f); + fragColor = vec4(pow(areaRatio, contrast)) * pow(intensity, intExp); + float finalInt = pow(areaRatio, contrast) * pow(intensity, intExp); + finalInt = clamp(finalInt, 0, 1); + fragColor = vec4(1, 1, 1, finalInt * scale); +// fragColor = vec4(vec3(1), pow(oldArea / newArea * .2f, 1.5f)); } diff --git a/resources/shaders/color.vert b/resources/shaders/color.vert index 6aa941e..677abf6 100644 --- a/resources/shaders/color.vert +++ b/resources/shaders/color.vert @@ -1,10 +1,92 @@ #version 330 core -layout (location = 0) in vec3 position; -layout (location = 1) in vec3 normal; -out vec3 col; +layout(location = 0) in vec3 position; // Position of the vertex +layout(location = 1) in vec3 normal; // Normal of the vertex +layout(location = 2) in vec3 texCoords; // Normal of the vertex + +uniform float depth = -3000.f; +uniform float skyHeight = 500.f; +uniform mat4 proj; +uniform mat4 view; +uniform mat4 model; +uniform mat4 inverseView; +//uniform float width; +//uniform float height; // TODO: Pass in width and height as uniform + +uniform mat3 inverseTransposeModel; + +out vec3 normal_cameraSpace; +out vec3 normal_worldSpace; +out vec3 camera_worldSpace; +out vec3 pos; +out float intensity; +out vec3 oldPosFlat; + +uniform vec2 widthBounds; +uniform vec2 lengthBounds; + +vec3 moveToTopDown(vec3 p) { + return vec3((p[0] - ((widthBounds[0] + widthBounds[1]) / 2)) / (widthBounds[1] - widthBounds[0]) * 2, + -(p[2] - ((lengthBounds[0] + lengthBounds[1]) / 2)) * 2 / (lengthBounds[1] - lengthBounds[0]), 0.f); +} + +vec4 refractToFloor(vec3 l, vec3 p, vec3 n, float d) { + // Refracts incoming light direction l through normal n at point p until hits floor at depth d + vec3 w_o = normalize(l); + float cos_theta_i = dot(-w_o, n); + float n_i = 1; + float n_t = 1.33f; + float determinant = 1.f - (pow((n_i / n_t), 2.f) * (1.f - pow(cos_theta_i, 2.f))); + + float r0 = pow((n_i - n_t) / (n_i + n_t), 2.f); // variable required to calculate probability of reflection + float prob_to_refl = r0 + ((1 - r0) * pow((1 - cos_theta_i), 5.f)); + + if (determinant >= 0) { + float cos_theta_t = sqrt(determinant); + vec3 w_t = (n_i / n_t) * w_o + ((n_i / n_t) * cos_theta_i - cos_theta_t) * n; + float dist = p.y - d; + float depthScale = dist / w_t.y; + vec3 groundContactPoint = -(w_t * depthScale) + p; + return vec4(groundContactPoint, 1.f - prob_to_refl); + } else { + return vec4(0, 0, 0, 0); + } +} + +vec3 wrapGL(vec3 p) { + float newX = p[0]; + float newY = p[1]; + + newX = mod(newX + 1.f, 2.f) - 1.f; + + newY = mod(newY + 1.f, 2.f) - 1.f; + + return vec3(newX, newY, 0.f); +} void main() { - gl_Position = vec4(position, 1.0); - col = normal; + normal_cameraSpace = normalize(inverse(transpose(mat3(view))) * inverseTransposeModel * normal); + camera_worldSpace = vec3(inverseView * vec4(0.f, 0.f, 0.f, 1.f)); + normal_worldSpace = normal; + pos = vec3(model * vec4(position, 1.f)); //vec3(model * vec4(objSpacePos, 1.f)); + vec3 lightDir = -normalize(vec3(1, -1, 1)); + + gl_Position = proj * view * model * vec4(position, 1); + +// gl_Position = vec4((position[0] - ((widthBounds[0] + widthBounds[1]) / 2)) / (widthBounds[1] - widthBounds[0]) * 2, +// (position[2] - ((lengthBounds[0] + lengthBounds[1]) / 2)) / (lengthBounds[1] - lengthBounds[0]) * 2, 0.f, 1.f); + float newX = mod(int(position[0]), 3) - 1; + float newY = mod(int(position[2]), 3) - 1; + + vec4 refractedPositionAndProb = refractToFloor(-lightDir, position, normal_worldSpace, depth); + + float waterMurkiness = .002f; + + gl_Position = vec4(newX, newY, 0.f, 1.f); + + intensity = refractedPositionAndProb[3] * clamp(dot(normal_worldSpace, lightDir), 0.f, 1.f) * exp(-length((position - vec3(refractedPositionAndProb))) * waterMurkiness); + + oldPosFlat = moveToTopDown(position); + pos = moveToTopDown(vec3(refractedPositionAndProb)); + gl_Position = vec4(pos, 1.f); } diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 6d1bc57..88c783d 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -70,7 +70,8 @@ void main() { vec2 refrUV = uvFromWorldPoint(refrPos); vec2 reflUV = uvFromWorldPoint(reflPos); - float waterMurkiness = 0.002f; // TODO: Make uniform +// float waterMurkiness = 0.002f; // TODO: Make uniform + float waterMurkiness = 0.0005f; // TODO: Make uniform vec3 waterVolumeColor = vec3(red * 0.1f, green * 0.2f, blue * 0.2f); float murkDiffuse = 0.3f; float murkAmbient = 0.8f; diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index 1409039..3cd5c17 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -2,9 +2,9 @@ layout(location = 0) in vec3 position; // Position of the vertex layout(location = 1) in vec3 normal; // Normal of the vertex -layout(location = 3) in vec3 texCoords; // Normal of the vertex +layout(location = 2) in vec3 texCoords; // Normal of the vertex -uniform float depth = -1500.f; +uniform float depth = -3000.f; uniform float skyHeight = 500.f; uniform mat4 proj; uniform mat4 view; @@ -26,7 +26,7 @@ out vec2 uv; out float matIor; vec4 getRefrPos() { -// float depth = -1000.f; // TODO: Pass as uniform +// float depth = -1500.f; // TODO: Pass as uniform vec3 w_o = normalize(pos - camera_worldSpace); float cos_theta_i = dot(-w_o, normal_worldSpace); float n_i = 1; |