Tunnel Speed Lines

This is a 3D Particles Shader done in Godot 3.4.stable, to draw Tunnel Speed Lines.

Tunnel dimensions are set by Tunnel Radius and Tunnel Length.

Velocity is set by Lifetime ( = Preprocess ).

Draw Pass 1 draws Quad-stripes with editable size x=3.0, y=0.4.

Draw Pass 1 Material has a Shader Param Texture Albedo, to use your own texture with alpha channel.

Shader code
shader_type particles;

uniform float tunnel_radius : hint_range( 0.0, 10.0 ) = 2.0;
uniform float tunnel_length : hint_range( 0.0, 100.0 ) = 50.0;

float rand_from_seed(in uint seed) {
	int k;
	int s = int(seed);
	if (s == 0)
	s = 305420679;
	k = s / 127773;
	s = 16807 * (s - k * 127773) - 2836 * k;
	if (s < 0)
	s += 2147483647;
	seed = uint(s);
	return float(seed % uint(65536)) / 65535.0;
}

uint hash(uint x) {
	x = ((x >> uint(16)) ^ x) * uint(73244475);
	x = ((x >> uint(16)) ^ x) * uint(73244475);
	x = (x >> uint(16)) ^ x;
	return x;
}

void vertex() {
	if (RESTART) {
		uint alt_seed1 = hash(NUMBER + uint(1) + RANDOM_SEED);
		// set particle position in model space with tunnel_radius and tunnel_length
		float angle_rand = rand_from_seed( alt_seed1 ) * 6.283185;
		TRANSFORM[3].x = tunnel_length;
		TRANSFORM[3].y = sin( angle_rand ) * tunnel_radius;
		TRANSFORM[3].z = cos( angle_rand ) * tunnel_radius;
		// calculate VELOCITY in negative x direction
		VELOCITY.x = -tunnel_length / LIFETIME;
		// calculate 2D normal and send it to \"Pass 1\" shader with CUSTOM variable
		CUSTOM.yz = vec2( sin( angle_rand - 1.570796 ), cos( angle_rand - 1.570796 ) );
	}
}




shader_type spatial;

render_mode unshaded;
uniform sampler2D texture_albedo : hint_albedo;

void vertex() {
	// caculate world_y: get local 2D normal from particle shader variable CUSTOM.yz and transform it from model space to world space
	vec4 world_y = WORLD_MATRIX * vec4( 0.0, INSTANCE_CUSTOM.y, INSTANCE_CUSTOM.z, 0.0);
	// calculate world_z by the cross product of WORLD_MATRIX[0] ( = world_x) and world_y
	vec4 world_z = vec4( cross( WORLD_MATRIX[0].xyz, world_y.xyz ), 0.0 );
	// calculate model space to view space
	MODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat4( WORLD_MATRIX[0], world_y, world_z, WORLD_MATRIX[3] );
}

void fragment() {
	ALBEDO = texture( texture_albedo, UV ).rgb;
	ALPHA = texture( texture_albedo, UV ).a;
}
Tags
Tunnel Speed Lines
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.
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ninjakrom
Ninjakrom
5 months ago

This coulde be used in a sprite node or how?