3D Transformations

Shader code
// CC0, ElSuicio, 2026.
// GODOT v4.6.1.stable.
// x.com/ElSuicio
// github.com/ElSuicio
// Contact email [interdreamsoft@gmail.com]

shader_type spatial;

render_mode unshaded;
render_mode skip_vertex_transform;

group_uniforms _Transform;
group_uniforms _Transform._Position;
uniform float _PositionX = 0.0;
uniform float _PositionY = 0.0;
uniform float _PositionZ = 0.0;

group_uniforms _Transform._Rotation;
uniform float _RotationX : hint_range(-360.0, 360.0, 1e-3) = 0.0;
uniform float _RotationY : hint_range(-360.0, 360.0, 1e-3) = 0.0;
uniform float _RotationZ : hint_range(-360.0, 360.0, 1e-3) = 0.0;

group_uniforms _Transform._Scale;
uniform float _ScaleX = 1.0;
uniform float _ScaleY = 1.0;
uniform float _ScaleZ = 1.0;

varying vec4 _vertex_os;
varying vec4 _normal_os;
varying vec4 _binormal_os;
varying vec4 _tangent_os;

varying vec3 _color;

mat4 translation_matrix(
	in vec3 t
)
{
	return mat4(
	vec4(1, 0, 0, 0),
	vec4(0, 1, 0, 0),
	vec4(0, 0, 1, 0),
	vec4(t.x, t.y, t.z, 1)
	);
}

mat4 rotation_matrix(
	in vec3 angle // In radians.
)
{
	mat4 x = mat4(
		vec4(1, 0, 0, 0),
		vec4(0,  cos(angle.x), sin(angle.x), 0),
		vec4(0, -sin(angle.x), cos(angle.x), 0),
		vec4(0, 0, 0, 1)
	);
	
	mat4 y = mat4(
		vec4(cos(angle.y), 0, -sin(angle.y), 0),
		vec4(0, 1, 0, 0),
		vec4(sin(angle.y), 0,  cos(angle.y), 0),
		vec4(0, 0, 0, 1)
	);
	
	mat4 z = mat4(
		vec4( cos(angle.z), sin(angle.z), 0, 0),
		vec4(-sin(angle.z), cos(angle.z), 0, 0),
		vec4(0, 0, 1, 0),
		vec4(0, 0, 0, 1)
	);
	
	// Godot Default Rotation Order (YXZ).
	return y * x * z;
}

mat4 escalation_matrix(
	in vec3 s
)
{
	return mat4(
	vec4(s.x, 0, 0, 0),
	vec4(0, s.y, 0, 0),
	vec4(0, 0, s.z, 0),
	vec4(0, 0, 0, 1)
	);
}

void vertex()
{
	/* 3D AXIS COLOR */
	_color = normalize(NORMAL * NORMAL); // Axis in Object Space prev to 3D transform.
	
	/* TRANSLATION */
	vec3 position = vec3(_PositionX, _PositionY, _PositionZ);
	mat4 TRANSLATION_MATRIX = translation_matrix(position);
	
	/* ROTATION */
	vec3 angle = radians(vec3(_RotationX, _RotationY, _RotationZ));
	mat4 ROTATION_MATRIX = rotation_matrix(angle);
	
	/* ESCALATION */
	vec3 scale = vec3(_ScaleX, _ScaleY, _ScaleZ);
	mat4 ESCALATION_MATRIX = escalation_matrix(scale);
	
	/* VERTEX */
	_vertex_os = vec4(VERTEX, 1.0);
	
	_vertex_os = ESCALATION_MATRIX * _vertex_os;
	
	_vertex_os = ROTATION_MATRIX * _vertex_os;
	
	vec4 vertex_ws = MODEL_MATRIX * _vertex_os;
	
	vertex_ws = TRANSLATION_MATRIX * vertex_ws;
	
	vec4 vertex_vs = VIEW_MATRIX * vertex_ws;
	
	VERTEX = vertex_vs.xyz;
	
	/* NORMAL */
	_normal_os = vec4(NORMAL, 0.0);
	
	_normal_os = ROTATION_MATRIX * _normal_os;
	
	vec4 normal_ws = MODEL_MATRIX * _normal_os;
	
	vec4 normal_vs = VIEW_MATRIX * normal_ws;
	
	NORMAL = normalize(normal_vs.xyz);
	
	/* Binormal */
	_binormal_os = vec4(BINORMAL, 0.0);
	
	_binormal_os = ROTATION_MATRIX * _binormal_os;
	
	vec4 binormal_ws = MODEL_MATRIX * _binormal_os;
	
	vec4 binormal_vs = VIEW_MATRIX * binormal_ws;
	
	BINORMAL = normalize(binormal_vs.xyz);
	
	/* TANGENT */
	_tangent_os = vec4(TANGENT, 0.0);
	
	_tangent_os = ROTATION_MATRIX * _tangent_os;
	
	vec4 tangent_ws = MODEL_MATRIX * _tangent_os;
	
	vec4 tangent_vs = VIEW_MATRIX * tangent_ws;
	
	TANGENT = -normalize(tangent_vs.xyz);
	
	/* PROJECTION */
	vec4 vertex_proj = PROJECTION_MATRIX * vertex_vs;
	
	POSITION = vertex_proj;
}

void fragment() 
{
	ALBEDO = _color;
}
Live Preview
Tags
3d, rotation, scale, Transform, Translation
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 ElSuicio

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments