Falling leaf shader

Very simple 3D falling leaf shader, couldn’t find it on here so I made it myself works by getting pseudo random values and rotating the mesh using some simple matrix maths. There is also a wind element that is dependent on index so leaves move slightly different. If your new to shaders remember to alter the variables in the time dropdown to change the shaders behaviour.

-This is a particle shader so remember to add a material in the particle shader node

-Use local coordinates this doesn’t use global

Shader code
shader_type particles;

//this defines how far out the particles will spawn
uniform float particle_diameter = 5f;


//input a value that changes over time and can also be a coordinate
//use vec(TIME,0) if you got no coordinates
float rand(vec2 co){
    //this will give a pseudorandom value between 0-1
    return fract(sin(dot(co.xy ,vec2(23.21,101.83))) * 34759.214);
}

void vertex(){
	//this sets all the stuff necesary
        //spawn them in a given radius around the emmitor
	if(RESTART)
	{
		
		//set the position to be randomly dispersed in an area
		float x_modifier = (rand(vec2(TIME,0))*particle_diameter);
		float z_modifier = (rand(vec2(TIME+1f,0))*particle_diameter);
		
		//centers effect
		TRANSFORM[3][0] = x_modifier - 0.5f*particle_diameter;
		TRANSFORM[3][2] = z_modifier- 0.5f*particle_diameter;
		
		
		//VELOCITY.y = -rand(vec2(TIME+2f,0));
		VELOCITY.y = -1f;
	}
	
	
	//apply a changing wind that changes with time with a noticable variation
	VELOCITY.x = 3f*sin(TIME/4f)*sin(TIME)*rand(vec2(float(INDEX),0f));
	VELOCITY.z = 3f*cos(TIME/4f)*cos(TIME)*rand(vec2(float(INDEX)+1f,0f));
	
	
	
	//gives a consistent rotation that depends on the index
	float x_rotation = TIME*rand(vec2(float(INDEX),0f));
	float y_rotation = TIME*rand(vec2(float(INDEX)+1f,0f));
	float z_rotation = TIME*rand(vec2(float(INDEX)+2f,0f));
	
	
	
	//these just rotate the leaf using matrix math
	//x rotation
	TRANSFORM[1][1] = cos(x_rotation);
	TRANSFORM[2][1] = -sin(x_rotation);
	TRANSFORM[1][2] = sin(x_rotation);
	TRANSFORM[2][2] = cos(x_rotation);
	//y rotation
	TRANSFORM[0][0] = cos(y_rotation);
	TRANSFORM[2][0] = -sin(y_rotation);
	TRANSFORM[0][2] = sin(y_rotation);
	TRANSFORM[2][2] = cos(y_rotation);
	//z rotation
	TRANSFORM[0][0] = cos(z_rotation);
	TRANSFORM[1][0] = -sin(z_rotation);
	TRANSFORM[0][1] = sin(z_rotation);
	TRANSFORM[1][1] = cos(z_rotation);
	
}
Tags
3d, leaf, Spatial
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.

Related shaders

Team Fortress 2 Shader

Stylized Multimesh Grass Shader

3D Bubble/Spatial shield shader

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments