diff options
author | Sebastian Park <SebPark03@gmail.com> | 2024-05-09 23:18:46 -0400 |
---|---|---|
committer | Sebastian Park <SebPark03@gmail.com> | 2024-05-09 23:18:46 -0400 |
commit | f9819ae7f8b11b298c16eb14d929a9c09038a877 (patch) | |
tree | cef7f0c104d49d41a1e2c76bec2343f875ca8dff /resources | |
parent | ac3f1e423e696392ea0a50c2652b829e5c5734b6 (diff) | |
parent | 99c588d4da2ca044df8276b7efbd9d27c19c8352 (diff) |
Merge branch 'shaders'
Diffstat (limited to 'resources')
-rw-r--r-- | resources/shaders/caustics.frag | 20 | ||||
-rw-r--r-- | resources/shaders/caustics.vert | 53 | ||||
-rw-r--r-- | resources/shaders/color.frag | 15 | ||||
-rw-r--r-- | resources/shaders/color.vert | 3 | ||||
-rwxr-xr-x | resources/shaders/shader.frag | 1 |
5 files changed, 84 insertions, 8 deletions
diff --git a/resources/shaders/caustics.frag b/resources/shaders/caustics.frag new file mode 100644 index 0000000..f746896 --- /dev/null +++ b/resources/shaders/caustics.frag @@ -0,0 +1,20 @@ +#version 410 core +out vec4 fragColor; + +in vec3 normal_worldSpace; +in vec3 pos; +in vec3 newPos; +in vec4 col; +in float refractProb; + +uniform sampler2D normSamp; + +void main() { +// fragColor = vec4(vec3((pos[0] + 1) / 2, (pos[1] + 1) / 2, 0.f), 1.f); + float oldArea = length(dFdx(vec3(pos[0], pos[2], pos[1]))) * length(dFdy(vec3(pos[0], pos[2], pos[1]))); + float newArea = length(dFdx(vec3(newPos[0], newPos[2], newPos[1]))) * length(dFdy(vec3(newPos[0], newPos[2], newPos[1]))); + float areaRatio = oldArea / newArea; + float intensity = pow(areaRatio * .3f, 1.5f); + fragColor = vec4(0.98, 1, .78, intensity * refractProb); +// fragColor = col; +} diff --git a/resources/shaders/caustics.vert b/resources/shaders/caustics.vert new file mode 100644 index 0000000..298f5e1 --- /dev/null +++ b/resources/shaders/caustics.vert @@ -0,0 +1,53 @@ +#version 330 core + +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 = -1000.f; +uniform float skyHeight = 500.f; + +uniform sampler2D normSamp; + +out vec3 normal_worldSpace; +out vec3 pos; +out vec3 newPos; +out vec4 col; +out float refractProb; + +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.z - d; + float depthScale = dist / w_t.z; +// vec3 groundContactPoint = -(w_t * depthScale) + p; + vec3 groundContactPoint = (w_t * depthScale) + p; + return vec4(groundContactPoint, 1.f - prob_to_refl); + } else { + return vec4(0, 0, 0, 0); + } +} + +void main() { + normal_worldSpace = normal; + pos = position; + vec4 sampledNormal = texture(normSamp, vec2((pos + 1) / 2)); + sampledNormal = (sampledNormal * 2.f) - 1.f; + col = sampledNormal; + vec4 newPosAndProb = refractToFloor(vec3(0, 0, 1), pos, normalize(vec3(sampledNormal)), 0.01f); + newPos = vec3(newPosAndProb[0], newPosAndProb[1], 0.f); + refractProb = newPosAndProb[3]; +// newPos = pos; + gl_Position = vec4(newPos, 1.f); +} diff --git a/resources/shaders/color.frag b/resources/shaders/color.frag index fe70983..7c41c89 100644 --- a/resources/shaders/color.frag +++ b/resources/shaders/color.frag @@ -13,16 +13,16 @@ in float intensity; //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 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() { @@ -39,4 +39,5 @@ void main() { finalInt = clamp(finalInt, 0, 1); fragColor = vec4(1, 1, 1, finalInt * scale); // fragColor = vec4(vec3(1), pow(oldArea / newArea * .2f, 1.5f)); + fragColor = vec4((normal_worldSpace + 1) / 2, 1.f); } diff --git a/resources/shaders/color.vert b/resources/shaders/color.vert index 677abf6..d607ff9 100644 --- a/resources/shaders/color.vert +++ b/resources/shaders/color.vert @@ -4,7 +4,7 @@ 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 depth = -1000.f; uniform float skyHeight = 500.f; uniform mat4 proj; uniform mat4 view; @@ -89,4 +89,5 @@ void main() { oldPosFlat = moveToTopDown(position); pos = moveToTopDown(vec3(refractedPositionAndProb)); gl_Position = vec4(pos, 1.f); + gl_Position = vec4(oldPosFlat, 1.f); } diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 88c783d..1b73b96 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -71,6 +71,7 @@ void main() { vec2 reflUV = uvFromWorldPoint(reflPos); // float waterMurkiness = 0.002f; // TODO: Make uniform +// float waterMurkiness = 0.0005f; // 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; |