Draw Laser Distorted
An eye-candy shimmering effect for lasers
Compatibility: GM8(pro)
Tutorial Type: Example
Downloadable Content: GM8(pro)
Skill Level: Advanced
Objective: Learn to use surfaces and textured primitives
Category: Eye-Candy
An eye-candy shimmering effect for lasers
Compatibility: GM8(pro)
Tutorial Type: Example
Downloadable Content: GM8(pro)
Skill Level: Advanced
Objective: Learn to use surfaces and textured primitives
Category: Eye-Candy
THE IDEA
While play-testing a game I´m working on, I found that the lasers lacked something... I like eye-candy and these just didn´t have that "wow!" factor, so I got to thinking about how they could be improved. And that´s when I came up with this!
The idea behind this example is simple. Draw two lines of primitives (one for either side of the laser) and texture them from a surface which has been previously manipulated, then change their vertices in a random manner to create a distortion effect.
HOW IT WORKS
Well, first of all we have a controller object that creates the main surface for using as a texture. We also create a secondary mini-surface onto which we draw the screen reduced to 1/4. This is then re-drawn onto the main surface to create a blurred copy of the screen at full resolution, before deleting that mini surface. Now in any laser object we run the draw_laser_distorted script from the draw event. What this script does is it draws two triangle strip primitives up either side of the line from point A to point B, taking the image from the previously created surface as it´s texture. Now, as the surface is blurred, this gives a nice effect by itself, but we can also specify a range of random movement for the vertices of the primitives which gives rise to a fantastic distortion effect. The script also permits you to define the width, the sprite to draw the laser (or none) and whether to use additive blending or not. The additive blending is nice to get a "bloom" effect from the laser...
THE SCRIPT
(In the examle file this script is FULLY commented)
- Code:
/*
draw_laser_distorted(x, y, xx, yy, sc_w, sc_h, rng, e_wid, tex, l_wid, col, blend, spr)
x=starting x argument0
y=starting y argument1
xx=finishing x argument2
yy=finishing y argument3
sc_w=room/view width argument4
sc_h=room/view screen height argument5
rng=distortion range (between 1 and 10 is best) argument6
e_wid=width of effect argument7
tex=texture to use (usually from a surface) argument8
l_wid=width of laser (only use if no sprite is selected) argument9
col=laser colour argument10
blend=turn on additive blending argument11
spr=sprite for drawing the laser (-1 for no sprite) argument12
*/
var dir,dist,p,r,w,xx,yy;
dir=point_direction(argument0, argument1, argument2, argument3);
dist=max(6, round(point_distance(argument0, argument1, argument2, argument3)/10));
if (dist mod 2)!=0 dist+=1;
p=10;
r=argument6;
w=argument7;
xx=argument0+lengthdir_x(w,dir-90);
yy=argument1+lengthdir_y(w,dir-90);
if argument11=1 draw_set_blend_mode(bm_add);
texture_set_repeat(true);
draw_primitive_begin_texture(pr_trianglestrip, argument8)
draw_vertex_texture_color(argument0, argument1, argument0/argument4, argument1/argument5, c_white, 1);
repeat(dist)
{
var rnd;
rnd=r-random(r*2);
draw_vertex_texture_color(xx + lengthdir_x(p,dir) + r-random(r*2),
yy+lengthdir_y(p,dir)+r-random(r*2),
(xx+lengthdir_x(p,dir))/argument4,
(yy+lengthdir_y(p,dir))/argument5,
c_white,0);
draw_vertex_texture_color(argument0 + lengthdir_x(p + 10-rnd,dir),
argument1+lengthdir_y(p+10-rnd,dir),
(argument0+lengthdir_x(p+10,dir))/argument4,
(argument1+lengthdir_y(p+10,dir))/argument5,
c_white,1);
p+=20;
if p>=dist*10
{
xx=argument0+lengthdir_x(w,dir+90);
yy=argument1+lengthdir_y(w,dir+90);
p=10;
draw_primitive_end();
draw_primitive_begin_texture(pr_trianglestrip, argument8);
draw_vertex_texture_color(argument0, argument1, argument0/argument4, argument1/argument5, c_white, 1);
}
}
draw_primitive_end();
if argument11=false draw_set_blend_mode(bm_add);
if argument12!=-1
{
draw_sprite_ext(argument12, 0, argument0, argument1, point_distance(argument0, argument1, argument2, argument3), 1, dir, argument10, 1);
}
else
{
draw_line_width_color(argument0, argument1, argument2, argument3, argument9, argument10, argument10);
}
draw_set_blend_mode(bm_normal);
THE SCREENSHOT
(Doesn´t do it justice!)
FINAL COMMENTS
Pressing "space" in the example will turn additive blending on or off, and the arrow keys can change the width and random range of the distortion effect.
If you download this please comment (although I know you wont!) and as usual no credit is necessary but appreciated!!!!