Ultimate Lenseflares

From GameStudio Wiki

Jump to: navigation, search

Lenseflares are rendered over level geometry and don't use view entities etc. It is pretty fast (it can still be improved of course). Supports: -omni light flares -spot light flares -flare spinning depending on distance -fading instead of just disappearing

MATERIAL* mat_flare = {
  effect="
	technique test {
		pass p0 {
			ZWRITEENABLE =TRUE;
 			ZFUNC=ALWAYS;
    		}
	}
  ";
}

// title: lenseflare
// Desc: Sprite action to simulate lenseflares
//
// section: settings
// uses: style rollspin spotlight fl_scale alpha_fac spot_pan spot_tilt
action FX_lenseflare() {
	my.material = mat_flare;
	wait(1);
	var myvec[3]; 
	set(my, BRIGHT|PASSABLE|TRANSLUCENT); 
	my.ambient = 100;
	my.alpha = 100; 

	/********************************/
	/* Add your sprites here!!!	*/
	/********************************/
	if(my.skill1 == 1 || my.skill1 == 0) { ent_morph(my,"flare.tga"); }
	if(my.skill1 == 2) { ent_morph(my,"flare2.tga"); }
	if(my.skill1 == 3) { ent_morph(my,"flare3.tga"); }
	if(my.skill1 == 4) { ent_morph(my,"flare4.tga"); }
	if(my.skill1 == 5) { ent_morph(my,"flare5.tga"); }
	if(my.skill1 == 6) { ent_morph(my,"flare6.tga"); }

	// defaults
	if(my.skill2 == 0) { my.skill2 = 1; }
	if(my.skill3 == 0) { my.skill3 = 90; }
	if(my.skill4 == 0) { my.skill4 = 90; }
	if(my.skill5 == 0) { my.skill5 = 1; } else { my.skill5 = 1 / my.skill5; }

	// scale & alpha factor
	my.scale_x = my.skill2;
	my.scale_y = my.scale_x;
	my.alpha /= my.skill5;

	proc_mode = PROC_LATE;
	while(1) {
		vec_set(myvec,my.x);

		trace_mode = IGNORE_ME + IGNORE_PASSABLE;
		result = c_trace(camera.x, my.x, trace_mode);
		if(trace_hit != 0) 					// not on screen or covered by level geometry
		{ 
			my.alpha = max(0,my.alpha - 8 * time);  	// fades out instead of just making it invisible
			if(my.alpha <= 0) { set(my,INVISIBLE); }
		} 	
		else
		{							// on screen and visible
			reset(my,INVISIBLE);
			if is(my,FLAG1) { my.roll = vec_dist(camera.x,my.x)/30; }
			if is(my,FLAG2) { 
					var temp[3];
					vec_set(temp,my.x);	
					vec_sub(temp,camera.x);
					vec_to_angle(temp,temp); 
					temp[0] = 100-(sin(temp[0]-my.pan+90)+1)*(9000/my.skill3); 
				       	temp[1] = 100-(sin(temp[1]-my.tilt-90)+1)*(9000/my.skill4); 
				       	my.alpha = min(temp[0],temp[1])/ my.skill5;
			}
			else { my.alpha = 100/my.skill5; }
		}	
		wait(1);
	}
}

How to use it:
Choose any entity and assign it the "FX_lenseflare" action. Now choose rightclick -> behavior. Settings are:

  • Style - The flare you want to use (see code)
  • Fl_scale - size of the flare (i.e. just scale factor)


Furthermore alpha_fac offers you the ability to make your lenseflare transparent. The default value is 1 (100%), 0.5 makes the lenseflare more transparent.

The "rollspin" flag turns on spinning of lenseflares if you come closer.


The "spotlight" flag makes the omni lenseflare a spotlight lenseflare (which is mostly used for small light). The spotlight's direction is the one your entity has which you added in WED. Depending on the angle to the spotlight the lenseflare is more or less transparent. You can adjust this with spot_pan and spot_tilt that have a default of 90 (degrees). This means if you look from the side you can't see any lenseflare anymore.

Personal tools