01-08-2016, 10:36 PM
Modification of the emboss / bump map shader. Really much better.
Fragment shader file content
uniform sampler2D sampler0;
uniform vec2 u_texelDelta;
varying vec4 v_texcoord0;
vec3 emboss(vec2 textcoord, float str) {
bool ok = true;
vec3 c = texture2D(sampler0, textcoord.xy).xyz;
vec3 cp = texture2D(sampler0, textcoord.xy - (vec2(1.0, 1.0) * u_texelDelta)).xyz;
vec3 cm = texture2D(sampler0, textcoord.xy + (vec2(1.0, 1.0) * u_texelDelta)).xyz;
float l = max(max(c.r, c.g), c.b);
float nl;
while(ok && str > 0) {
nl = l;
nl += max(max(cp.r, cp.g), cp.b) * str;
nl -= max(max(cm.r, cm.g), cm.b) * str;
if(abs(nl - l) < 0.1) ok = false;
else str-=0.1;
}
if(str > 0) return c * nl / l;
else return vec3(1, 0, 0);
}
void main ()
{
gl_FragColor.rgb = emboss(v_texcoord0.xy, 2); // 2 is the strength of the effect... the higher the more strength
gl_FragColor.a = 1;
}
Fragment shader file content
uniform sampler2D sampler0;
uniform vec2 u_texelDelta;
varying vec4 v_texcoord0;
vec3 emboss(vec2 textcoord, float str) {
bool ok = true;
vec3 c = texture2D(sampler0, textcoord.xy).xyz;
vec3 cp = texture2D(sampler0, textcoord.xy - (vec2(1.0, 1.0) * u_texelDelta)).xyz;
vec3 cm = texture2D(sampler0, textcoord.xy + (vec2(1.0, 1.0) * u_texelDelta)).xyz;
float l = max(max(c.r, c.g), c.b);
float nl;
while(ok && str > 0) {
nl = l;
nl += max(max(cp.r, cp.g), cp.b) * str;
nl -= max(max(cm.r, cm.g), cm.b) * str;
if(abs(nl - l) < 0.1) ok = false;
else str-=0.1;
}
if(str > 0) return c * nl / l;
else return vec3(1, 0, 0);
}
void main ()
{
gl_FragColor.rgb = emboss(v_texcoord0.xy, 2); // 2 is the strength of the effect... the higher the more strength
gl_FragColor.a = 1;
}

