summaryrefslogtreecommitdiff
path: root/drivers/gles2/shaders
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2015-03-02 00:54:10 -0300
committerJuan Linietsky <reduzio@gmail.com>2015-03-02 00:54:43 -0300
commita1f715a4da71fbc2b7d6fad68624bf8b22c6da17 (patch)
tree5cd305c8f8858504f0b5efb804b2c291b1dc5089 /drivers/gles2/shaders
parent0e732637d07d908daf64526f222e71fea402f3db (diff)
support for 2D shadow casters
Added support for 2D shadow casters. *DANGER* Shaders in CanvasItem CHANGED, if you are using shader in a CanvasItem and pull this, you will lose them. Shaders now work through a 2D material system similar to 3D. If you don't want to lose the 2D shader code, save the shader as a .shd, then create a material in CanvasItem and re-assign the shader.
Diffstat (limited to 'drivers/gles2/shaders')
-rw-r--r--drivers/gles2/shaders/SCsub1
-rw-r--r--drivers/gles2/shaders/canvas.glsl123
-rw-r--r--drivers/gles2/shaders/canvas_shadow.glsl62
3 files changed, 176 insertions, 10 deletions
diff --git a/drivers/gles2/shaders/SCsub b/drivers/gles2/shaders/SCsub
index c665cf9036..9679223b16 100644
--- a/drivers/gles2/shaders/SCsub
+++ b/drivers/gles2/shaders/SCsub
@@ -3,6 +3,7 @@ Import('env')
if env['BUILDERS'].has_key('GLSL120GLES'):
env.GLSL120GLES('material.glsl');
env.GLSL120GLES('canvas.glsl');
+ env.GLSL120GLES('canvas_shadow.glsl');
env.GLSL120GLES('blur.glsl');
env.GLSL120GLES('copy.glsl');
diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl
index 227a8b7bdd..9022f30d7f 100644
--- a/drivers/gles2/shaders/canvas.glsl
+++ b/drivers/gles2/shaders/canvas.glsl
@@ -34,6 +34,10 @@ varying vec4 local_rot;
uniform vec2 normal_flip;
#endif
+#ifdef USE_SHADOWS
+highp varying vec2 pos;
+#endif
+
#endif
#if defined(ENABLE_VAR1_INTERP)
@@ -63,6 +67,8 @@ VERTEX_SHADER_CODE
outvec = modelview_matrix * outvec;
#endif
+
+
#ifdef USE_PIXEL_SNAP
outvec.xy=floor(outvec.xy+0.5);
@@ -75,6 +81,9 @@ VERTEX_SHADER_CODE
light_uv_interp.xy = (light_matrix * outvec).xy;
light_uv_interp.zw = outvec.xy-light_pos;
+#ifdef USE_SHADOWS
+ pos=outvec.xy;
+#endif
#if defined(NORMAL_USED)
local_rot.xy=normalize( (modelview_matrix * ( extra_matrix * vec4(1.0,0.0,0.0,0.0) )).xy )*normal_flip.x;
@@ -154,6 +163,15 @@ varying vec4 local_rot;
uniform sampler2D shadow_texture;
uniform float shadow_attenuation;
+uniform highp mat4 shadow_matrix;
+uniform highp mat4 light_local_matrix;
+highp varying vec2 pos;
+uniform float shadowpixel_size;
+
+#ifdef SHADOW_ESM
+uniform float shadow_esm_multiplier;
+#endif
+
#endif
#endif
@@ -173,10 +191,6 @@ void main() {
vec3 normal = vec3(0,0,1);
#endif
-#ifdef USE_MODULATE
-
- color*=modulate;
-#endif
color *= texture2D( texture, uv_interp );
#if defined(ENABLE_SCREEN_UV)
@@ -191,6 +205,12 @@ FRAGMENT_SHADER_CODE
color = vec4(vec3(enc32),1.0);
#endif
+#ifdef USE_MODULATE
+
+ color*=modulate;
+#endif
+
+
#ifdef USE_LIGHTING
#if defined(NORMAL_USED)
@@ -201,13 +221,96 @@ FRAGMENT_SHADER_CODE
vec4 light = texture2D(light_texture,light_uv_interp.xy) * light_color;
#ifdef USE_SHADOWS
- //this might not be that great on mobile?
- float light_dist = length(light_texture.zw);
- float light_angle = atan2(light_texture.x,light_texture.z) + 1.0 * 0.5;
- float shadow_dist = texture2D(shadow_texture,vec2(light_angle,0));
- if (light_dist>shadow_dist) {
- light*=shadow_attenuation;
+
+
+ vec2 lpos = (light_local_matrix * vec4(pos,0.0,1.0)).xy;
+ float angle_to_light = -atan(lpos.x,lpos.y);
+ float PI = 3.14159265358979323846264;
+ /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays
+ float ang*/
+
+ float su,sz;
+
+ float abs_angle = abs(angle_to_light);
+ vec2 point;
+ float sh;
+ if (abs_angle<45.0*PI/180.0) {
+ point = lpos;
+ sh=0+(1.0/8.0);
+ } else if (abs_angle>135.0*PI/180.0) {
+ point = -lpos;
+ sh = 0.5+(1.0/8.0);
+ } else if (angle_to_light>0) {
+
+ point = vec2(lpos.y,-lpos.x);
+ sh = 0.25+(1.0/8.0);
+ } else {
+
+ point = vec2(-lpos.y,lpos.x);
+ sh = 0.75+(1.0/8.0);
+
+ }
+
+
+ vec4 s = shadow_matrix * vec4(point,0.0,1.0);
+ s.xyz/=s.w;
+ su=s.x*0.5+0.5;
+ sz=s.z*0.5+0.5;
+
+ float shadow_attenuation;
+
+#ifdef SHADOW_PCF5
+
+ shadow_attenuation=0.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*2.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*2.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation/=5.0;
+
+#endif
+
+#ifdef SHADOW_PCF13
+
+ shadow_attenuation += texture2D(shadow_texture,vec2(su,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*2.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*3.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*4.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*5.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su+shadowpixel_size*6.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*2.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*3.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*4.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*5.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation += texture2D(shadow_texture,vec2(su-shadowpixel_size*6.0,sh)).z<sz?0.0:1.0;
+ shadow_attenuation/=13.0;
+
+#endif
+
+#ifdef SHADOW_ESM
+
+
+ {
+ float unnormalized = su/shadowpixel_size;
+ float fractional = fract(unnormalized);
+ unnormalized = floor(unnormalized);
+ float zc = texture2D(shadow_texture,vec2((unnormalized-0.5)*shadowpixel_size,sh)).z;
+ float zn = texture2D(shadow_texture,vec2((unnormalized+0.5)*shadowpixel_size,sh)).z;
+ float z = mix(zc,zn,fractional);
+ shadow_attenuation=clamp(exp(shadow_esm_multiplier* ( z - sz )),0.0,1.0);
}
+
+#endif
+
+#if !defined(SHADOW_PCF5) && !defined(SHADOW_PCF13) && !defined(SHADOW_ESM)
+
+ shadow_attenuation = texture2D(shadow_texture,vec2(su+shadowpixel_size,sh)).z<sz?0.0:1.0;
+
+#endif
+
+ light*=shadow_attenuation;
//use shadows
#endif
diff --git a/drivers/gles2/shaders/canvas_shadow.glsl b/drivers/gles2/shaders/canvas_shadow.glsl
new file mode 100644
index 0000000000..40cf321dce
--- /dev/null
+++ b/drivers/gles2/shaders/canvas_shadow.glsl
@@ -0,0 +1,62 @@
+[vertex]
+
+#ifdef USE_GLES_OVER_GL
+#define mediump
+#define highp
+#else
+precision mediump float;
+precision mediump int;
+#endif
+
+uniform highp mat4 projection_matrix;
+uniform highp mat4 light_matrix;
+uniform highp mat4 world_matrix;
+
+attribute highp vec3 vertex; // attrib:0
+
+#ifndef USE_DEPTH_SHADOWS
+
+varying vec4 position_interp;
+
+#endif
+
+
+void main() {
+
+ gl_Position = projection_matrix * (light_matrix * (world_matrix * vec4(vertex,1.0)));
+
+#ifndef USE_DEPTH_SHADOWS
+ position_interp = gl_Position;
+#endif
+
+}
+
+[fragment]
+
+#ifdef USE_GLES_OVER_GL
+#define mediump
+#define highp
+#else
+precision mediump float;
+precision mediump int;
+#endif
+
+#ifndef USE_DEPTH_SHADOWS
+
+varying vec4 position_interp;
+
+#endif
+
+void main() {
+
+#ifdef USE_DEPTH_SHADOWS
+
+#else
+ highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0;//bias;
+ highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0));
+ comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
+ gl_FragColor = comp;
+#endif
+
+}
+