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;
}
Thanks! The rotation functions are highly useful for building more complex vertex shaders with proper normals.