Simple World Normal from Depth

Simple shader that reconstructs the world normal from the depth pass. Useful to have access to world normals in Mobile renderer.

Based on this article by János Turánszki (@turanszkij), but without the edge fixes (I’m still figuring out how to implement them).

Shader code
vec3 normal_from_depth(vec2 uv, mat4 inv_proj_mat){
	float depth = texture(depth_texture, uv).r;
	
	//Normal recontruction from Depth:
	float x = uv.x * 2.0 - 1.0;
	float y = (1.0 - uv.y) * 2.0 - 1.0;
	
	vec4 position_s = vec4(x, y, depth, 1.0);
	vec4 position_v = inv_proj_mat * position_s;
	
	vec3 P = position_v.xyz / position_v.w;
	
	//inverting P's y axis and calculating the cross product of dFdy first and dFdx later fixes the y axis
	P.y = -P.y;
	return normalize(cross(dFdy(P), dFdx(P)));
}

void vertex() {
	POSITION = vec4(VERTEX.xy, 1.0, 1.0);
}

void fragment() {
	vec3 normal = normal_from_depth(SCREEN_UV, INV_PROJECTION_MATRIX);
	ALBEDO = vec3(normal);
Tags
approximation, base, buffer, depth, mobile, Normal, pass, reconstruction, world
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

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments