Vertex Mesh Rotator

Shader that rotates the vertex and normals of a mesh using the matrices rotations functions to move in XYZ, also works with a custom pivot

Shader code
shader_type spatial;
render_mode cull_disabled;



uniform float x;
uniform float y;
uniform float z;



uniform sampler2D albedo: hint_albedo;



mat3 rotateX(float theta)
{
	
	float cosa = cos(theta);
	float sina = sin(theta);

 
    //Y rotation	
	mat3 rotate_x  = mat3(
	   vec3(1.0, 0.0, 0.0),
	   vec3(0.0, cosa, -sina),
	   vec3(0.0, sina, cosa)
	);
	
	return rotate_x;
}





mat3 rotateY(float theta)
{
	
	float cosa = cos(theta);
	float sina = sin(theta);

 
    //Y rotation	
	mat3 rotate_y  = mat3(
	   vec3(cosa, 0.0, sina),
	   vec3(0.0, 1.0, 0.0),
	   vec3(-sina, 0.0, cosa)
	);
	
	return rotate_y;
}



mat3 rotateZ(float theta)
{
	
	float cosa = cos(theta);
	float sina = sin(theta);

 
    //Y rotation	
	mat3 rotate_z  = mat3(
	   vec3(cosa,-sina, 0.0),
	   vec3(sina, cosa, 0.0),
	   vec3(0.0, 0.0, 1.0)
	);
	
	return rotate_z;
}


void vertex()
{


	mat3 rotation_matrix =  rotateX(x) * rotateY(y) * rotateZ(z);
	vec3 rotated_vertex =  rotation_matrix * VERTEX;
	vec3 rotated_normals =  rotation_matrix * NORMAL;

	VERTEX =  rotated_vertex;
	NORMAL =  rotated_normals;
	

}

void fragment()
{
	ALBEDO = texture(albedo, UV).rgb;	
	
}
Tags
vertex, vertex animation
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

Godot 4.x Color Swap for 3D Mesh Models

Mesh smearing

Liquid In Mesh

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Huraqan
1 year ago

Thanks! The rotation functions are highly useful for building more complex vertex shaders with proper normals.