summaryrefslogtreecommitdiffstats
path: root/src/extras/shaders/es2
diff options
context:
space:
mode:
Diffstat (limited to 'src/extras/shaders/es2')
-rw-r--r--src/extras/shaders/es2/distancefieldtext.frag36
-rw-r--r--src/extras/shaders/es2/distancefieldtext.vert17
-rw-r--r--src/extras/shaders/es2/light.inc.frag108
3 files changed, 134 insertions, 27 deletions
diff --git a/src/extras/shaders/es2/distancefieldtext.frag b/src/extras/shaders/es2/distancefieldtext.frag
new file mode 100644
index 000000000..88ead1f68
--- /dev/null
+++ b/src/extras/shaders/es2/distancefieldtext.frag
@@ -0,0 +1,36 @@
+#define FP highp
+
+uniform FP sampler2D distanceFieldTexture;
+uniform FP float minAlpha;
+uniform FP float maxAlpha;
+uniform FP float textureSize;
+uniform FP vec4 color;
+
+varying FP vec3 position;
+varying FP vec2 texCoord;
+
+void main()
+{
+ // determine the scale of the glyph texture within pixel-space coordinates
+ // (that is, how many pixels are drawn for each texel)
+ FP vec2 texelDeltaX = abs(dFdx(texCoord));
+ FP vec2 texelDeltaY = abs(dFdy(texCoord));
+ FP float avgTexelDelta = textureSize * 0.5 * (texelDeltaX.x + texelDeltaX.y + texelDeltaY.x + texelDeltaY.y);
+ FP float texScale = 1.0 / avgTexelDelta;
+
+ // scaled to interval [0.0, 0.15]
+ FP float devScaleMin = 0.00;
+ FP float devScaleMax = 0.15;
+ FP float scaled = (clamp(texScale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin);
+
+ // thickness of glyphs should increase a lot for very small glyphs to make them readable
+ FP float base = 0.5;
+ FP float threshold = base * scaled;
+ FP float range = 0.06 / texScale;
+
+ FP float minAlpha = threshold - range;
+ FP float maxAlpha = threshold + range;
+
+ FP float distVal = texture2D(distanceFieldTexture, texCoord).r;
+ gl_FragColor = color * smoothstep(minAlpha, maxAlpha, distVal);
+}
diff --git a/src/extras/shaders/es2/distancefieldtext.vert b/src/extras/shaders/es2/distancefieldtext.vert
new file mode 100644
index 000000000..f7fc5327b
--- /dev/null
+++ b/src/extras/shaders/es2/distancefieldtext.vert
@@ -0,0 +1,17 @@
+attribute vec3 vertexPosition;
+attribute vec2 vertexTexCoord;
+
+varying vec3 position;
+varying vec2 texCoord;
+
+uniform mat4 modelView;
+uniform mat4 mvp;
+
+void main()
+{
+ texCoord = vertexTexCoord;
+ position = vec3(modelView * vec4(vertexPosition, 1.0));
+
+ gl_Position = mvp * vec4(vertexPosition, 1.0);
+}
+
diff --git a/src/extras/shaders/es2/light.inc.frag b/src/extras/shaders/es2/light.inc.frag
index 02660f008..074af5799 100644
--- a/src/extras/shaders/es2/light.inc.frag
+++ b/src/extras/shaders/es2/light.inc.frag
@@ -26,27 +26,45 @@ void adsModelNormalMapped(const in FP vec3 vpos, const in FP vec3 vnormal, const
FP vec3 n = normalize( vnormal );
FP vec3 s, ts;
+ Light light;
for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
FP float att = 1.0;
- if ( lights[i].type != TYPE_DIRECTIONAL ) {
- s = lights[i].position - vpos;
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
if ( dot(snormal, s) < 0.0 )
att = 0.0;
else {
ts = normalize( tangentMatrix * s );
- if (length( lights[i].attenuation ) != 0.0) {
+ if (length( light.attenuation ) != 0.0) {
FP float dist = length(s);
- att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
}
s = normalize( s );
- if ( lights[i].type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle)
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
att = 0.0;
}
}
} else {
- if ( dot(snormal, -lights[i].direction) > 0.0 )
- s = normalize( tangentMatrix * -lights[i].direction );
+ if ( dot(snormal, -light.direction) > 0.0 )
+ s = normalize( tangentMatrix * -light.direction );
else
att = 0.0;
}
@@ -61,8 +79,8 @@ void adsModelNormalMapped(const in FP vec3 vpos, const in FP vec3 vnormal, const
specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
}
- diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
- specularColor += att * lights[i].intensity * specular * lights[i].color;
+ diffuseColor += att * light.intensity * diffuse * light.color;
+ specularColor += att * light.intensity * specular * light.color;
}
}
@@ -75,21 +93,39 @@ void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3
FP vec3 n = normalize( vnormal );
FP vec3 s;
+ Light light;
for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
FP float att = 1.0;
- if ( lights[i].type != TYPE_DIRECTIONAL ) {
- s = lights[i].position - vpos;
- if (length( lights[i].attenuation ) != 0.0) {
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
+ if (length( light.attenuation ) != 0.0) {
FP float dist = length(s);
- att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
}
s = normalize( s );
- if ( lights[i].type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle)
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
att = 0.0;
}
} else {
- s = normalize( -lights[i].direction );
+ s = normalize( -light.direction );
}
FP float diffuse = max( dot( s, n ), 0.0 );
@@ -102,8 +138,8 @@ void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3
specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
}
- diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
- specularColor += att * lights[i].intensity * specular * lights[i].color;
+ diffuseColor += att * light.intensity * diffuse * light.color;
+ specularColor += att * light.intensity * specular * light.color;
}
}
@@ -114,25 +150,43 @@ void adModel(const in FP vec3 vpos, const in FP vec3 vnormal, out FP vec3 diffus
FP vec3 n = normalize( vnormal );
FP vec3 s;
+ Light light;
for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
FP float att = 1.0;
- if ( lights[i].type != TYPE_DIRECTIONAL ) {
- s = lights[i].position - vpos;
- if (length( lights[i].attenuation ) != 0.0) {
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
+ if (length( light.attenuation ) != 0.0) {
FP float dist = length(s);
- att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
}
s = normalize( s );
- if ( lights[i].type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle)
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
att = 0.0;
}
} else {
- s = normalize( -lights[i].direction );
+ s = normalize( -light.direction );
}
FP float diffuse = max( dot( s, n ), 0.0 );
- diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
+ diffuseColor += att * light.intensity * diffuse * light.color;
}
}