rj-action-library/External/Imposter/materials/depth_baker.gdshader

33 lines
822 B
Plaintext
Raw Normal View History

2024-12-01 17:07:41 +00:00
shader_type spatial;
render_mode unshaded, depth_test_disabled;
uniform float depth_scaler = 1.0f;
uniform sampler2D DEPTH_TEXTURE:hint_depth_texture,filter_linear_mipmap;
varying mat4 CAMERA;
2025-01-03 12:09:23 +00:00
void vertex()
{
2024-12-01 17:07:41 +00:00
POSITION = vec4(VERTEX, 1.0);
CAMERA = INV_VIEW_MATRIX;
}
2025-01-03 12:09:23 +00:00
void fragment()
{
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
float depth = texture( DEPTH_TEXTURE, SCREEN_UV ).x;
2024-12-01 17:07:41 +00:00
//Vulkan.z size range 0-1 penGL.z size range -11
vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
// vec3 ndc = vec3(SCREEN_UV, depth) * 2.0 - 1.0;
vec4 world = CAMERA * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
vec3 world_position = world.xyz / world.w;
2025-01-03 12:09:23 +00:00
float aabb_full = depth_scaler;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
world_position *= 2.0; // going back to aabb but around 0
world_position.z = aabb_full/2.0 - world_position.z;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
ALBEDO = vec3(clamp(world_position.z/aabb_full,0,1));
2024-12-01 17:07:41 +00:00
}