ashton 0.0.1alpha → 0.0.2alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/LICENSE +21 -21
  2. data/README.md +95 -68
  3. data/Rakefile +41 -23
  4. data/examples/bloom_example.rb +59 -0
  5. data/examples/lighting_example.rb +127 -0
  6. data/examples/media/SmallStar.png +0 -0
  7. data/examples/media/Starfighter.png +0 -0
  8. data/examples/media/simple.png +0 -0
  9. data/examples/noise_example.rb +94 -0
  10. data/examples/outline_example.rb +86 -0
  11. data/examples/particle_emitter_example.rb +114 -0
  12. data/examples/pixelate_example.rb +51 -49
  13. data/examples/pixelated_texture_example.rb +69 -0
  14. data/examples/radial_blur_example.rb +60 -62
  15. data/examples/shader_image_example.rb +74 -41
  16. data/examples/{shockwave2_example.rb → shockwave_example.rb} +74 -75
  17. data/examples/stencil_shader_example.rb +104 -0
  18. data/examples/{framebuffer_example.rb → texture_render_example.rb} +53 -49
  19. data/examples/{tv_screen_and_noise_example.rb → tv_screen_and_static_example.rb} +59 -59
  20. data/ext/ashton/GLee.c +18170 -0
  21. data/ext/ashton/GLee.h +17647 -0
  22. data/ext/ashton/ashton.c +42 -0
  23. data/ext/ashton/ashton.h +31 -0
  24. data/ext/ashton/color.c +45 -0
  25. data/ext/ashton/color.h +25 -0
  26. data/ext/ashton/common.h +41 -0
  27. data/ext/ashton/extconf.rb +42 -0
  28. data/ext/ashton/fast_math.c +30 -0
  29. data/ext/ashton/fast_math.h +30 -0
  30. data/ext/ashton/font.c +8 -0
  31. data/ext/ashton/font.h +16 -0
  32. data/ext/ashton/gosu.c +18 -0
  33. data/ext/ashton/gosu.h +19 -0
  34. data/ext/ashton/image.c +8 -0
  35. data/ext/ashton/image.h +16 -0
  36. data/ext/ashton/particle_emitter.c +788 -0
  37. data/ext/ashton/particle_emitter.h +171 -0
  38. data/ext/ashton/pixel_cache.c +237 -0
  39. data/ext/ashton/pixel_cache.h +58 -0
  40. data/ext/ashton/shader.c +9 -0
  41. data/ext/ashton/shader.h +16 -0
  42. data/ext/ashton/texture.c +442 -0
  43. data/ext/ashton/texture.h +63 -0
  44. data/ext/ashton/window.c +8 -0
  45. data/ext/ashton/window.h +16 -0
  46. data/lib/ashton.rb +38 -26
  47. data/lib/ashton/1.9/ashton.so +0 -0
  48. data/lib/ashton/gosu_ext/color.rb +24 -11
  49. data/lib/ashton/gosu_ext/font.rb +58 -0
  50. data/lib/ashton/gosu_ext/gosu_module.rb +16 -0
  51. data/lib/ashton/gosu_ext/image.rb +95 -31
  52. data/lib/ashton/gosu_ext/window.rb +78 -35
  53. data/lib/ashton/image_stub.rb +32 -36
  54. data/lib/ashton/lighting/light_source.rb +146 -0
  55. data/lib/ashton/lighting/manager.rb +98 -0
  56. data/lib/ashton/mixins/version_checking.rb +23 -0
  57. data/lib/ashton/particle_emitter.rb +87 -0
  58. data/lib/ashton/pixel_cache.rb +24 -0
  59. data/lib/ashton/shader.rb +353 -35
  60. data/lib/ashton/shaders/bloom.frag +41 -0
  61. data/lib/ashton/shaders/color_inversion.frag +11 -0
  62. data/lib/ashton/{post_process → shaders}/contrast.frag +16 -16
  63. data/lib/ashton/{shader → shaders}/default.frag +22 -19
  64. data/lib/ashton/{shader → shaders}/default.vert +13 -13
  65. data/lib/ashton/shaders/fade.frag +14 -0
  66. data/lib/ashton/shaders/grayscale.frag +15 -0
  67. data/lib/ashton/shaders/include/classicnoise2d.glsl +113 -0
  68. data/lib/ashton/shaders/include/classicnoise3d.glsl +177 -0
  69. data/lib/ashton/shaders/include/classicnoise4d.glsl +302 -0
  70. data/lib/ashton/{include/simplex.glsl → shaders/include/noise2d.glsl} +70 -63
  71. data/lib/ashton/shaders/include/noise3d.glsl +102 -0
  72. data/lib/ashton/shaders/include/noise4d.glsl +128 -0
  73. data/lib/ashton/shaders/include/rand.glsl +5 -0
  74. data/lib/ashton/shaders/lighting/distort.frag +57 -0
  75. data/lib/ashton/shaders/lighting/draw_shadows.frag +60 -0
  76. data/lib/ashton/shaders/lighting/shadow_blur.frag +60 -0
  77. data/lib/ashton/shaders/mezzotint.frag +22 -0
  78. data/lib/ashton/shaders/multitexture2.vert +19 -0
  79. data/lib/ashton/shaders/outline.frag +45 -0
  80. data/lib/ashton/{post_process → shaders}/pixelate.frag +48 -48
  81. data/lib/ashton/shaders/radial_blur.frag +63 -0
  82. data/lib/ashton/shaders/sepia.frag +26 -0
  83. data/lib/ashton/{post_process/shockwave2.frag → shaders/shockwave.frag} +38 -35
  84. data/lib/ashton/shaders/signed_distance_field.frag +80 -0
  85. data/lib/ashton/{post_process/noise.frag → shaders/static.frag} +25 -27
  86. data/lib/ashton/shaders/stencil.frag +27 -0
  87. data/lib/ashton/shaders/tv_screen.frag +23 -0
  88. data/lib/ashton/signed_distance_field.rb +151 -0
  89. data/lib/ashton/texture.rb +186 -0
  90. data/lib/ashton/version.rb +2 -2
  91. data/lib/ashton/window_buffer.rb +16 -0
  92. data/spec/ashton/ashton_spec.rb +22 -0
  93. data/spec/ashton/gosu_ext/color_spec.rb +34 -0
  94. data/spec/ashton/gosu_ext/font_spec.rb +57 -0
  95. data/spec/ashton/gosu_ext/gosu_spec.rb +11 -0
  96. data/spec/ashton/gosu_ext/image_spec.rb +66 -0
  97. data/spec/ashton/gosu_ext/window_spec.rb +71 -0
  98. data/spec/ashton/image_stub_spec.rb +46 -0
  99. data/spec/ashton/particle_emitter_spec.rb +123 -0
  100. data/spec/ashton/pixel_cache_spec.rb +153 -0
  101. data/spec/ashton/shader_spec.rb +152 -0
  102. data/spec/ashton/signed_distance_field_spec.rb +163 -0
  103. data/spec/ashton/texture_spec.rb +347 -0
  104. data/spec/helper.rb +12 -0
  105. metadata +159 -28
  106. data/examples/output/README.txt +0 -1
  107. data/lib/ashton/base_shader.rb +0 -172
  108. data/lib/ashton/framebuffer.rb +0 -183
  109. data/lib/ashton/post_process.rb +0 -83
  110. data/lib/ashton/post_process/default.vert +0 -9
  111. data/lib/ashton/post_process/fade.frag +0 -11
  112. data/lib/ashton/post_process/mezzotint.frag +0 -24
  113. data/lib/ashton/post_process/radial_blur.frag +0 -31
  114. data/lib/ashton/post_process/sepia.frag +0 -19
  115. data/lib/ashton/post_process/shockwave.frag +0 -40
  116. data/lib/ashton/post_process/tv_screen.frag +0 -32
@@ -0,0 +1,60 @@
1
+ #version 110
2
+
3
+ // Based on Catalin Zima's shader based dynamic shadows system.
4
+ // http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/
5
+
6
+
7
+ const float minBlur = 0.0;
8
+ const float maxBlur = 5.0; // TODO: Make this a uniform?
9
+ const int blurSamples = 13;
10
+
11
+ uniform sampler2D in_Texture; // Original texture.
12
+ uniform int in_TextureWidth;
13
+
14
+ varying vec2 var_TexCoord; // Pixel to process on this pass.
15
+
16
+ void main()
17
+ {
18
+ vec2 offsetAndWeight[blurSamples];
19
+
20
+ offsetAndWeight[0] = vec2( 0.0, 0.199471);
21
+ offsetAndWeight[1] = vec2( 1.0, 0.176033);
22
+ offsetAndWeight[2] = vec2( 2.0, 0.120985);
23
+ offsetAndWeight[3] = vec2( 3.0, 0.064759);
24
+ offsetAndWeight[4] = vec2( 4.0, 0.026995);
25
+ offsetAndWeight[5] = vec2( 5.0, 0.008764);
26
+ offsetAndWeight[6] = vec2( 6.0, 0.002216);
27
+
28
+ vec4 color = texture2D(in_Texture, var_TexCoord);
29
+ float distance = color.b;
30
+ float pixel_width = 1.0 / float(in_TextureWidth);
31
+
32
+ // Take a reading from the center pixel once,
33
+ // but multiply since it would be added twice in the loop.
34
+ float sum = color.r * offsetAndWeight[0].y * 2.0;
35
+
36
+ for (int i = 1; i < blurSamples; i++)
37
+ {
38
+ float offset = offsetAndWeight[i].x;
39
+ float weight = offsetAndWeight[i].y;
40
+
41
+ offset *= pixel_width * mix(minBlur, maxBlur, distance);
42
+
43
+ // Take colour from above, below, left and right at once.
44
+ float effect = texture2D(in_Texture, var_TexCoord + vec2( offset, 0.0)).r +
45
+ texture2D(in_Texture, var_TexCoord + vec2(-offset, 0.0)).r +
46
+ texture2D(in_Texture, var_TexCoord + vec2( 0.0, offset)).r +
47
+ texture2D(in_Texture, var_TexCoord + vec2( 0.0, -offset)).r;
48
+
49
+ sum += effect * weight;
50
+ }
51
+
52
+ // Halve the sum, since we have applied horizontal and vertical at once.
53
+ sum *= 0.5;
54
+
55
+ // Light is brighter at the center.
56
+ float d = 2.0 * length(var_TexCoord - 0.5);
57
+ float attenuation = clamp(1.0 - d, 0.0, 1.0);
58
+
59
+ gl_FragColor = vec4(vec3(sum * attenuation), 1.0);
60
+ }
@@ -0,0 +1,22 @@
1
+ #version 110
2
+
3
+ #include <rand>
4
+
5
+ uniform sampler2D in_Texture;
6
+ uniform int in_WindowWidth;
7
+
8
+ uniform int in_T;
9
+
10
+ varying vec2 var_TexCoord;
11
+
12
+ void main() {
13
+ vec4 color = texture2D(in_Texture, var_TexCoord);
14
+ vec3 mezzo = vec3(0.0);
15
+ float width = float(in_WindowWidth);
16
+
17
+ if(rand(var_TexCoord + float(in_T) / width) <= color.r) { mezzo.r = 1.0; }
18
+ if(rand(var_TexCoord + float(in_T) / width) <= color.g) { mezzo.g = 1.0; }
19
+ if(rand(var_TexCoord + float(in_T) / width) <= color.b) { mezzo.b = 1.0; }
20
+
21
+ gl_FragColor.rgb = mezzo;
22
+ }
@@ -0,0 +1,19 @@
1
+ #version 110
2
+
3
+ attribute vec4 in_Color;
4
+
5
+ varying vec4 var_Color;
6
+ varying vec2 var_TexCoord0;
7
+ varying vec2 var_TexCoord1;
8
+
9
+ void main()
10
+ {
11
+ gl_Position = ftransform();
12
+ var_Color = in_Color;
13
+
14
+ // Set the coordinates for TEXTURE0
15
+ var_TexCoord0 = gl_MultiTexCoord0.xy;
16
+
17
+ // Set the coordinates for TEXTURE1
18
+ var_TexCoord1 = gl_MultiTexCoord1.xy;
19
+ }
@@ -0,0 +1,45 @@
1
+ #version 110
2
+
3
+ const float ALPHA_THRESHOLD = 0.5;
4
+
5
+ const vec2 TextureSize = vec2(1024.0, 1024.0); // Gosu-specific!
6
+ const vec2 PixelSize = 1.0 / TextureSize;
7
+
8
+ // Automatically set by Ray (actually passed from the vertex shader).
9
+ uniform sampler2D in_Texture; // Original texture.
10
+ //uniform int in_WindowWidth;
11
+ //uniform int in_WindowHeight;
12
+
13
+ uniform vec4 in_OutlineColor;
14
+ uniform float in_OutlineWidth; // In pixels.
15
+
16
+ varying vec2 var_TexCoord; // Pixel to process on this pass
17
+ varying vec4 var_Color;
18
+
19
+ void main()
20
+ {
21
+ gl_FragColor = texture2D(in_Texture, var_TexCoord); // * var_Color;
22
+
23
+ if(gl_FragColor.a < ALPHA_THRESHOLD)
24
+ {
25
+ for(int i = -1; i < 2; i++)
26
+ {
27
+ for(int j = -1; j < 2; j++)
28
+ {
29
+ if(i != 0 && j != 0)
30
+ {
31
+ // Get the color of the adjacent pixel.
32
+ vec2 pos = var_TexCoord + vec2(i, j) * PixelSize * in_OutlineWidth;
33
+ if(texture2D(in_Texture, pos).a > ALPHA_THRESHOLD)
34
+ {
35
+ gl_FragColor = in_OutlineColor;
36
+
37
+ // Simplified return, since GLSL 1.10 hates `return`.
38
+ i = 2;
39
+ j = 2;
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
@@ -1,48 +1,48 @@
1
- #version 110
2
-
3
- #define KERNEL_SIZE 9
4
-
5
- uniform int in_WindowWidth;
6
- uniform int in_WindowHeight;
7
-
8
- uniform sampler2D in_Texture;
9
- uniform int in_PixelSize;
10
-
11
- varying vec2 var_TexCoord;
12
-
13
- vec2 texCoords[KERNEL_SIZE];
14
-
15
- void main(void)
16
- {
17
- vec4 avgColor;
18
- vec2 texCoordsStep = 1.0 /
19
- (vec2(float(in_WindowWidth), float(in_WindowHeight))/float(in_PixelSize));
20
- vec2 pixelBin = floor(var_TexCoord / texCoordsStep);
21
- vec2 inPixelStep = texCoordsStep / 3.0;
22
- vec2 inPixelHalfStep = inPixelStep / 2.0;
23
-
24
-
25
- texCoords[0] = vec2(inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
26
- texCoords[1] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
27
- texCoords[2] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
28
- texCoords[3] = vec2(inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
29
- texCoords[4] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
30
- texCoords[5] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
31
- texCoords[6] = vec2(inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
32
- texCoords[7] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
33
- texCoords[8] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
34
-
35
- avgColor = texture2D(in_Texture, texCoords[0]) +
36
- texture2D(in_Texture, texCoords[1]) +
37
- texture2D(in_Texture, texCoords[2]) +
38
- texture2D(in_Texture, texCoords[3]) +
39
- texture2D(in_Texture, texCoords[4]) +
40
- texture2D(in_Texture, texCoords[5]) +
41
- texture2D(in_Texture, texCoords[6]) +
42
- texture2D(in_Texture, texCoords[7]) +
43
- texture2D(in_Texture, texCoords[8]);
44
-
45
- avgColor /= float(KERNEL_SIZE);
46
-
47
- gl_FragColor = avgColor;
48
- }
1
+ #version 110
2
+
3
+ #define KERNEL_SIZE 9
4
+
5
+ uniform int in_WindowWidth;
6
+ uniform int in_WindowHeight;
7
+
8
+ uniform sampler2D in_Texture;
9
+ uniform int in_PixelSize;
10
+
11
+ varying vec2 var_TexCoord;
12
+
13
+ vec2 texCoords[KERNEL_SIZE];
14
+
15
+ void main(void)
16
+ {
17
+ vec4 avgColor;
18
+ vec2 texCoordsStep = 1.0 /
19
+ (vec2(float(in_WindowWidth), float(in_WindowHeight))/float(in_PixelSize));
20
+ vec2 pixelBin = floor(var_TexCoord / texCoordsStep);
21
+ vec2 inPixelStep = texCoordsStep / 3.0;
22
+ vec2 inPixelHalfStep = inPixelStep / 2.0;
23
+
24
+
25
+ texCoords[0] = vec2(inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
26
+ texCoords[1] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
27
+ texCoords[2] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
28
+ texCoords[3] = vec2(inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
29
+ texCoords[4] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
30
+ texCoords[5] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
31
+ texCoords[6] = vec2(inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
32
+ texCoords[7] = vec2(inPixelStep.x + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
33
+ texCoords[8] = vec2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
34
+
35
+ avgColor = texture2D(in_Texture, texCoords[0]) +
36
+ texture2D(in_Texture, texCoords[1]) +
37
+ texture2D(in_Texture, texCoords[2]) +
38
+ texture2D(in_Texture, texCoords[3]) +
39
+ texture2D(in_Texture, texCoords[4]) +
40
+ texture2D(in_Texture, texCoords[5]) +
41
+ texture2D(in_Texture, texCoords[6]) +
42
+ texture2D(in_Texture, texCoords[7]) +
43
+ texture2D(in_Texture, texCoords[8]);
44
+
45
+ avgColor /= float(KERNEL_SIZE);
46
+
47
+ gl_FragColor = avgColor;
48
+ }
@@ -0,0 +1,63 @@
1
+ #version 110
2
+
3
+ // http://www.gamerendering.com/2008/12/20/radial-blur-filter/
4
+
5
+ uniform sampler2D in_Texture; // Texture to manipulate.
6
+ uniform int in_WindowWidth;
7
+ uniform int in_WindowHeight;
8
+
9
+ uniform float in_Spacing; // good value is 1.0;
10
+ uniform float in_Strength; // good value is 2.2;
11
+
12
+ varying vec2 var_TexCoord;
13
+
14
+ void main(void)
15
+ {
16
+ // some sample positions. GLSL 1.10 is painfully dumb with arrays :)
17
+ float samples[10];
18
+ samples[0] = -0.08;
19
+ samples[1] = -0.05;
20
+ samples[2] = -0.03;
21
+ samples[3] = -0.02;
22
+ samples[4] = -0.01;
23
+ samples[5] = 0.01;
24
+ samples[6] = 0.02;
25
+ samples[7] = 0.03;
26
+ samples[8] = 0.05;
27
+ samples[9] = 0.08;
28
+
29
+ // 0.5,0.5 is the center of the screen
30
+ // so substracting uv from it will result in
31
+ // a vector pointing to the middle of the screen
32
+ vec2 dir = 0.5 - var_TexCoord;
33
+
34
+ // calculate the distance to the center of the screen
35
+ float dist = sqrt(dir.x * dir.x + dir.y * dir.y);
36
+
37
+ // normalize the direction (reuse the distance)
38
+ dir /= dist;
39
+
40
+ // this is the original colour of this fragment
41
+ // using only this would result in a non-blurred version
42
+ vec4 color = texture2D(in_Texture, var_TexCoord);
43
+
44
+ vec4 sum = color;
45
+
46
+ // take 10 additional blur samples in the direction towards
47
+ // the center of the screen
48
+ for (int i = 0; i < 10; i++)
49
+ {
50
+ sum += texture2D(in_Texture, var_TexCoord + dir * samples[i] * in_Spacing);
51
+ }
52
+
53
+ // we have taken eleven samples
54
+ sum *= 1.0/11.0;
55
+
56
+ // weighten the blur effect with the distance to the
57
+ // center of the screen ( further out is blurred more)
58
+ float t = dist * in_Strength;
59
+ t = clamp(t, 0.0, 1.0); //0 &lt;= t &lt;= 1
60
+
61
+ // Blend the original color with the averaged pixels
62
+ gl_FragColor = mix(color, sum, t);
63
+ }
@@ -0,0 +1,26 @@
1
+ #version 110
2
+
3
+ uniform sampler2D in_Texture;
4
+
5
+ varying vec2 var_TexCoord;
6
+
7
+ void main()
8
+ {
9
+ vec4 Sepia1 = vec4( 0.2, 0.05, 0.0, 1.0 );
10
+ vec4 Sepia2 = vec4( 1.0, 0.9, 0.5, 1.0 );
11
+
12
+ vec4 Color = texture2D(in_Texture, vec2(var_TexCoord));
13
+
14
+ if(Color.a == 0.0)
15
+ {
16
+ gl_FragColor = Color;
17
+ }
18
+ else
19
+ {
20
+ float SepiaMix = dot(vec3(0.3, 0.59, 0.11), vec3(Color));
21
+ Color = mix(Color, vec4(SepiaMix), vec4(0.5));
22
+ vec4 Sepia = mix(Sepia1, Sepia2, SepiaMix);
23
+
24
+ gl_FragColor = mix(Color, Sepia, 1.0);
25
+ }
26
+ }
@@ -1,35 +1,38 @@
1
- #version 110
2
-
3
- // http://www.geeks3d.com/20091116/shader-library-2d-shockwave-post-processing-filter-glsl/
4
-
5
- uniform sampler2D in_Texture; // 0
6
- uniform vec2 in_Center; // Mouse position
7
- uniform float in_Time; // effect elapsed time. Multiply this to affect speed.
8
-
9
- // Amplitude?, Refraction?, Width? e.g. 10.0, 0.8, 0.1
10
- uniform vec3 in_ShockParams;
11
-
12
- uniform int in_WindowWidth;
13
- uniform int in_WindowHeight;
14
-
15
- varying vec2 var_TexCoord;
16
-
17
- void main()
18
- {
19
- vec2 uv = var_TexCoord;
20
- vec2 texCoord = uv;
21
- float distance = distance(uv, in_Center / vec2(float(in_WindowWidth), float(in_WindowHeight)));
22
-
23
- if ( (distance <= (in_Time + in_ShockParams.z)) &&
24
- (distance >= (in_Time - in_ShockParams.z)) )
25
- {
26
- float diff = (distance - in_Time);
27
- float powDiff = 1.0 - pow(abs(diff * in_ShockParams.x),
28
- in_ShockParams.y);
29
- float diffTime = diff * powDiff;
30
- vec2 diffUV = normalize(uv - in_Center);
31
- texCoord = uv + (diffUV * diffTime);
32
- }
33
-
34
- gl_FragColor = texture2D(in_Texture, texCoord);
35
- }
1
+ #version 110
2
+
3
+ // http://empire-defense.crystalin.fr/blog/2d_shock_wave_texture_with_shader
4
+ // http://www.geeks3d.com/20091116/shader-library-2d-shockwave-post-processing-filter-glsl/
5
+
6
+ uniform sampler2D in_Texture; // 0
7
+ uniform vec2 in_Center; // Mouse position
8
+ uniform float in_Time; // effect elapsed time. Multiply this to affect speed.
9
+
10
+ // Amplitude?, Refraction?, Width? e.g. 10.0, 0.8, 0.1
11
+ uniform vec3 in_ShockParams;
12
+
13
+ uniform int in_WindowWidth;
14
+ uniform int in_WindowHeight;
15
+
16
+ varying vec2 var_TexCoord;
17
+
18
+ void main()
19
+ {
20
+ vec2 uv = var_TexCoord;
21
+ vec2 texCoord = uv;
22
+ float x = in_Center.x / float(in_WindowWidth);
23
+ float y = (float(in_WindowHeight) - in_Center.y) / float(in_WindowHeight);
24
+ float distance = distance(uv, vec2(x, y));
25
+
26
+ if ( (distance <= (in_Time + in_ShockParams.z)) &&
27
+ (distance >= (in_Time - in_ShockParams.z)) )
28
+ {
29
+ float diff = (distance - in_Time);
30
+ float powDiff = 1.0 - pow(abs(diff * in_ShockParams.x),
31
+ in_ShockParams.y);
32
+ float diffTime = diff * powDiff;
33
+ vec2 diffUV = normalize(uv - in_Center);
34
+ texCoord = uv + (diffUV * diffTime);
35
+ }
36
+
37
+ gl_FragColor = texture2D(in_Texture, texCoord);
38
+ }
@@ -0,0 +1,80 @@
1
+ #version 110
2
+
3
+ // Calculates a signed distance field, based on how far any pixel is from an opaque pixel.
4
+ // In each pixel, the signed value is actually r/g/b - 128
5
+ // if r/g/b > 0 => distance from an opaque pixel.
6
+ // if r/g/b <= 0 => distance from an opaque pixel that is adjacent to a transparent pixel.
7
+
8
+ uniform sampler2D in_Texture; // Original texture.
9
+
10
+ uniform vec2 in_TextureSize; // Width and height of the texture, so we know how big pixels are.
11
+ uniform int in_StepSize; // Distance to check each time (larger steps will be faster, but less accurate).
12
+ uniform int in_MaxDistance; // Maximum distance to search out to. Cannot be more than 127!
13
+
14
+ varying vec2 var_TexCoord; // Pixel to process on this pass.
15
+
16
+ const float NUM_SPOKES = 36.0; // Number of radiating lines to check in.
17
+ const float ANGULAR_STEP = 360.0 / NUM_SPOKES;
18
+
19
+ const int ZERO_VALUE = 128; // Color channel containing 0 => -128, 128 => 0, 255 => +127
20
+
21
+ // Returns true an alpha value is found at this distance (any direction).
22
+ bool find_alpha_at_distance(in vec2 center, in vec2 distance, in float alpha)
23
+ {
24
+ bool found = false;
25
+
26
+ for(float angle = 0.0; angle < 360.0; angle += ANGULAR_STEP)
27
+ {
28
+ vec2 position = center + distance * vec2(cos(angle), sin(angle));
29
+
30
+ if(texture2D(in_Texture, position).a == alpha)
31
+ {
32
+ found = true;
33
+ angle = 361.0;
34
+ }
35
+ }
36
+
37
+ return found;
38
+ }
39
+
40
+ void main()
41
+ {
42
+ vec2 pixel_size = 1.0 / in_TextureSize;
43
+
44
+ int distance;
45
+
46
+ if(texture2D(in_Texture, var_TexCoord).a == 0.0)
47
+ {
48
+ // Texel is transparent, search for nearest opaque.
49
+ distance = ZERO_VALUE + 1;
50
+ for(int i = in_StepSize; i < in_MaxDistance; i += in_StepSize)
51
+ {
52
+ if(find_alpha_at_distance(var_TexCoord, float(i) * pixel_size, 1.0))
53
+ {
54
+ i = in_MaxDistance + 1; // BREAK!
55
+ }
56
+ else
57
+ {
58
+ distance = ZERO_VALUE + 1 + i;
59
+ }
60
+ }
61
+ }
62
+ else
63
+ {
64
+ // Texel is opaque, search for nearest transparent.
65
+ distance = ZERO_VALUE;
66
+ for(int i = in_StepSize; i <= in_MaxDistance; i += in_StepSize)
67
+ {
68
+ if(find_alpha_at_distance(var_TexCoord, float(i) * pixel_size, 0.0))
69
+ {
70
+ i = in_MaxDistance + 1; // BREAK!
71
+ }
72
+ else
73
+ {
74
+ distance = ZERO_VALUE - i;
75
+ }
76
+ }
77
+ }
78
+
79
+ gl_FragColor = vec4(vec3(float(distance) / 255.0), 1.0);
80
+ }