parallax occlusion

This commit is contained in:
Ковалев Роман Евгеньевич 2023-02-13 10:12:34 +03:00 committed by R.E. Kovalev
parent c877306d5f
commit b373cf8f35

View File

@ -29,7 +29,7 @@ uniform sampler2D tex_normal;
uniform bool normalmapped; uniform bool normalmapped;
uniform bool parallaxmapped; uniform bool parallaxmapped;
uniform float parallax_heightScale = 0.03; uniform float parallax_heightScale = 0.1;
void main() void main()
{ {
@ -45,9 +45,42 @@ void main()
if (parallaxmapped) if (parallaxmapped)
{ {
float height = 1.0 - texture(tex_heights, texCoord).r; // Число слоев
new_texCoord -= viewTBN.xy* (height * parallax_heightScale); float layersCount = 32;
// Вычислим размер каждого слоя
float layerDepth = 1.0 / layersCount;
// Глубина текущего слоя
float currentLayerDepth = 0.0;
// Величина сдвига между слоями
vec2 deltaTexCoords = (parallax_heightScale * viewTBN.xy / viewTBN.z) / layersCount;
// Переменные для вычислений
vec2 currentTexCoords = texCoord;
float currentDepthMapValue = 1.0 - texture(tex_heights, currentTexCoords).r;
// Пока глубина текущего слоя меньше текущего значения глубины из текстуры
while(currentLayerDepth < currentDepthMapValue)
{
// Сдвигаем координаты
currentTexCoords -= deltaTexCoords;
// Обновляем значение глубины из текстуры
currentDepthMapValue = 1.0 - texture(tex_heights, currentTexCoords).r;
// Сдвигаем глубину на следующий слой
currentLayerDepth += layerDepth;
}
// Получим значение текстурных координат с предыдущего шага
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
// Значения глубины до и после пересечения
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = 1.0 - texture(tex_heights, prevTexCoords).r - currentLayerDepth + layerDepth;
// Интерполяция текстурных координат
float weight = afterDepth / (afterDepth - beforeDepth);
new_texCoord = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
// Проверка диапазона [0;1]
if(new_texCoord.x > 1.0 || new_texCoord.y > 1.0 || new_texCoord.x < 0.0 || new_texCoord.y < 0.0) if(new_texCoord.x > 1.0 || new_texCoord.y > 1.0 || new_texCoord.x < 0.0 || new_texCoord.y < 0.0)
discard; discard;
} }