Mech Gunfire Effect – Muzzleflash Shader

Gunfire muzzleflash effect for your projects, this one is about the gunfire effect, updated with inverse fresnel to fade away if viewed from the front. The gunfire smoke effect: https://godotshaders.com/shader/mech-gunfire-effect-smoke-shader/

Shader code
shader_type spatial;
render_mode blend_add,depth_draw_opaque,cull_disabled,
diffuse_toon,specular_disabled,unshaded,shadows_disabled,
ambient_light_disabled,fog_disabled;
uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;

uniform int particles_anim_h_frames;
uniform int particles_anim_v_frames;
//uniform bool particles_anim_loop;

void vertex() {
	float h_frames = float(particles_anim_h_frames);
	float v_frames = float(particles_anim_v_frames);
	float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);
	float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));
	
	// We don't want to cycle so  the if is removed.
	particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);
	//if (!particles_anim_loop) {
		//particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);
	//} else {
		//particle_frame = mod(particle_frame, particle_total_frames);
	//}
	
	UV /= vec2(h_frames, v_frames);
	UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);
}


float fresnel(float amount, vec3 normal, vec3 view){
	return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0 )), amount);
}
// The inverse fresnel is used to fade away the sprite-like geometry when viewed from the front,
// this improves the look of it and whenever is looked from the sides it get's displayed at full
// opacity, control the amount for a stronger or weaker effect.
float inverse_fresnel(float amount, vec3 normal, vec3 view){
	return pow(clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0), amount);
}

void fragment() {
	vec4 albedo_tex = texture(texture_albedo,UV);
	ALBEDO = albedo_tex.rgb * COLOR.rgb * albedo_tex.a * 4.0;
	ALPHA = (albedo_tex.a * inverse_fresnel(1.5, NORMAL, VIEW) * COLOR.a); // Fade away when looking at front view the muzzleflare using a fresnel.
	ALPHA_SCISSOR_THRESHOLD = 0.001;
}
Tags
gun fx, gunfire, gunfire effect, muzzlefire, muzzleflash
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Nanotech Gamedev

3D HP Bar of 2 Blended Colors

Mech Gunfire Effect – Smoke Shader

Albedo Terrain Mix Shader

Related shaders

Mech Gunfire Effect – Smoke Shader

Post Effect Outline Shader for GLES2

Radar Scanning Effect Shader

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
doradoro
25 days ago

The video is private 🙁

cyzaine
cyzaine
15 days ago

Love your videos! Thanks for sharing this one.