Screen Space Mesh Projection

This shader projects a 3d mesh into screen space like a UI element.

Shader code
shader_type spatial;
render_mode unshaded, skip_vertex_transform, depth_draw_always, depth_prepass_alpha;
uniform vec2 SCREEN_POSITION = vec2(0.0);
uniform float scale: hint_range(0.0, 1.0, 0.01) = 0.5;

float lerp(float a, float b, float t) {
	return a + (b-a) * t;
}

float inverse_lerp(float a, float b, float t) {
	return (t - a) / (b - a);
}

float remap(float value, float istart, float istop, float ostart, float ostop) {
	return lerp(ostart, ostop, inverse_lerp(istart, istop, value));
}

void vertex() {
	vec4 verts = vec4(VERTEX, 1.0);
	
	verts = PROJECTION_MATRIX * MODEL_MATRIX * verts;
	
	POSITION.xyz = verts.xyz;
	POSITION.xy *= scale;
	POSITION.xy += SCREEN_POSITION;
	
	POSITION.z = remap(POSITION.z, -1.0, 1.0, 0.8, 1.0);
	
	NORMAL = normalize((MODEL_MATRIX * vec4(NORMAL, 0.0)).xyz);
	BINORMAL = normalize((MODEL_MATRIX * vec4(BINORMAL, 0.0)).xyz);
	TANGENT = normalize((MODEL_MATRIX * vec4(TANGENT, 0.0)).xyz);
}
Tags
projection, screen-space, ui, vertex
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

UV and Normals Unwrap (Mesh Projection as UV) World Aligned

Dodecahedral Multi-Planar projection

screen space refraction shader

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments