treesummaryrefslogcommitdiff
path: root/shaders
diff options
context:
space:
mode:
authorPatrick2021-03-26 20:12:25 +0100
committerPatrick2021-03-26 20:12:25 +0100
commit0a5e8172a6ee0e79c81eec21a9966bea9385b249 (patch)
tree64b7f94af72ed0d57f3ea769bc14fa01702e20e5 /shaders
parentc99ecda7bed596922125f6b1ef1ef2ae8f27703e (diff)
downloadsubsurface_scattering-0a5e8172a6ee0e79c81eec21a9966bea9385b249.tar.gz
subsurface_scattering-0a5e8172a6ee0e79c81eec21a9966bea9385b249.zip
comments TS SSS
Diffstat (limited to 'shaders')
-rw-r--r--shaders/ts_frag.glsl51
-rw-r--r--shaders/ts_frag_irradiance.glsl47
-rw-r--r--shaders/ts_vert.glsl1
-rw-r--r--shaders/ts_vert_irradiance.glsl4
4 files changed, 18 insertions, 85 deletions
diff --git a/shaders/ts_frag.glsl b/shaders/ts_frag.glsl
index a0a8457..9362e2a 100644
--- a/shaders/ts_frag.glsl
+++ b/shaders/ts_frag.glsl
@@ -20,42 +20,7 @@ uniform float transmittanceScale;
void main()
{
- if (renderState == 0) {
- FragColor = texture(irradianceTexture, UV);
- }
- else if (renderState == 1) {
- vec3 norm = normalize(Normal);
- vec3 lightDir = normalize(lightPos - FragPos);
-
- float diff = max(dot(norm, lightDir), 0.0);
- vec3 diffuse = diff * lightColor;
-
- float ambientStrength = 0.1;
- vec3 ambient = ambientStrength * lightColor;
-
- float specularStrength = 0.5;
- vec3 viewDir = normalize(viewPos - FragPos);
- vec3 reflectDir = reflect(-lightDir, norm);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
- vec3 specular = specularStrength * spec * lightColor;
-
- vec3 result = (ambient + diffuse + specular) * objectColor;
-
- FragColor = vec4(result, 1.0);
- }
- else if (renderState == 2) {
- vec4 result = vec4(0, 0, 0, 1);
- for (int i = 0; i < 13; i++) {
- vec2 sampleCoords = UV + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
- //vec4 sample = texture(irradianceTexture, sampleCoords)
- // * texture(shadowmapTexture, sampleCoords);
- vec4 sample = texture(irradianceTexture, sampleCoords);
- vec4 weight = vec4(sampleWeights[i], 1);
- result += sample * weight;
- }
- FragColor = result;
- }
- else if (renderState == 3) {
+ // light the model
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
@@ -73,28 +38,34 @@ void main()
vec3 result = vec3((ambient + diffuse + specular) * objectColor);
+ // sample irradiance as distance to light combined with angle to the light
+ // with a Gaussian kernel
vec3 result2 = vec3(0, 0, 0);
for (int i = 0; i < 13; i++) {
vec2 sampleCoords = UV + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
//vec4 sample = texture(irradianceTexture, sampleCoords)
// * texture(shadowmapTexture, sampleCoords);
- vec3 sample = vec3(texture(irradianceTexture, sampleCoords));
+ vec3 sample = vec3(texture(irradianceTexture, sampleCoords)) * diff;
vec3 weight = sampleWeights[i];
result2 += sample * weight;
}
+ // multiply to apply irradiance, sqrt to get values between 0 and 1
result = sqrt(result * result2);
+ // sample texture to get distance from light
vec4 t = texture(irradianceTexture, UV);
-
float BacksideIrradiance = t.r; //*100 + t.g + t.b/100;
-
+ // and calculate world pos
vec3 Backside = (lightPos + (normalize(FragPos - lightPos) * BacksideIrradiance));
+ // add translucency by amplifying color inverse to the thickness
+ // (1 - diff) is part of the irradiance term,
+ // if the light hits the object straight at 90°
+ // most light is received
float distanceToBackside = length(FragPos - Backside);
if (distanceToBackside != 0)
result += objectColor * exp(2 / pow(distanceToBackside, 0.6)) * transmittanceScale * (1 - diff);
FragColor = vec4(result, 1);
- }
}
diff --git a/shaders/ts_frag_irradiance.glsl b/shaders/ts_frag_irradiance.glsl
index b9e70ae..37d28f1 100644
--- a/shaders/ts_frag_irradiance.glsl
+++ b/shaders/ts_frag_irradiance.glsl
@@ -10,52 +10,11 @@ uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 viewPos;
-vec4 blur(sampler2D tex, vec2 uv, vec2 res) {
- float Pi = 6.28318530718; // Pi*2
-
- // GAUSSIAN BLUR SETTINGS {{{
- float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)
- float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)
- float Size = 8.0; // BLUR SIZE (Radius)
- // GAUSSIAN BLUR SETTINGS }}}
-
- vec2 Radius = Size/res;
-
- // Pixel colour
- vec4 Color = texture(tex, uv);
-
- // Blur calculations
- for( float d=0.0; d<Pi; d+=Pi/Directions) {
- for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality) {
- Color += texture( tex, uv+vec2(cos(d),sin(d))*Radius*i);
- }
- }
-
- // Output to screen
- Color /= Quality * Directions - 15.0;
- return Color;
-}
-
void main()
{
- vec3 norm = normalize(Normal);
- vec3 lightDir = normalize(lightPos - FragPos);
-
- float diff = max(dot(norm, lightDir), 0.0);
- vec3 diffuse = diff * lightColor;
-
- float ambientStrength = 0.1;
- vec3 ambient = ambientStrength * lightColor;
-
- float specularStrength = 0.5;
- vec3 viewDir = normalize(viewPos - FragPos);
- vec3 reflectDir = reflect(-lightDir, norm);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
- vec3 specular = specularStrength * spec * lightColor;
-
- vec3 result = (ambient + diffuse) * objectColor;
-
- result = vec3(length(FragPos - lightPos));
+ // color the XY-plane according to the distance
+ // of the fragment in world space
+ vec3 result = vec3(length(FragPos - lightPos));
FragColor = vec4(result, 1.0f);
}
diff --git a/shaders/ts_vert.glsl b/shaders/ts_vert.glsl
index f8e790a..3ee202f 100644
--- a/shaders/ts_vert.glsl
+++ b/shaders/ts_vert.glsl
@@ -14,6 +14,7 @@ uniform mat4 projection;
void main()
{
+ // regular mvp
gl_Position = projection * view * model * vec4(pos, 1.0);
FragPos = vec3(model * vec4(pos, 1.0));
Normal = normal;
diff --git a/shaders/ts_vert_irradiance.glsl b/shaders/ts_vert_irradiance.glsl
index afe5645..f0d77b9 100644
--- a/shaders/ts_vert_irradiance.glsl
+++ b/shaders/ts_vert_irradiance.glsl
@@ -13,7 +13,9 @@ uniform mat4 projection;
void main()
{
- FragPos = vec3(model * vec4(pos, 1.0));
+ // lay out the model in the XY-plane according to it's UV coordinates
gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
+ // pass fragment position in world coordinates
+ FragPos = vec3(model * vec4(pos, 1.0));
Normal = normal;
}