Normal Reconstruct – Z

This is not a shader, it’s simply an algorithm that allows adjusting the Z-coordinate based on XY.

Shader code
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_schlick_ggx;

void fragment() {
    // Задание нормали в XY, Z будет рассчитана
    vec2 normalXY = vec2(0.0, 0.0); // TODO: Подставить входную нормаль

    // Извлечение компонентов
    float normalX = normalXY.x;
    float normalY = normalXY.y;

    // Рассчет квадрата длины XY-вектора (dot product)
    float lengthSquaredXY = dot(vec3(normalX), vec3(normalY));

    // Ограничение значения от 0 до 1
    float clampedLength = clamp(lengthSquaredXY, 0.0, 1.0);

    // Вычисление Z-компоненты нормали (Pythagorean theorem)
    float normalZ = sqrt(1.0 - clampedLength);

    // Сборка 3D-вектора нормали
    vec3 normal = vec3(normalX, normalY, normalZ);

    // Нормализация вектора нормали
    vec3 finalNormal = normalize(normal);

    // Назначение результата в NORMAL_MAP
    NORMAL_MAP = finalNormal;
}
Tags
Derive, DeriveNormalZ, Normal, NormalReconstructZ, Reconstruct, Z
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 Riko

Related shaders

guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Peepeepoopoo
Peepeepoopoo
6 months ago

Translation:
This is not a shader, it’s simply an algorithm that allows adjusting the Z-coordinate based on XY.