如何实现地面雾GLSL着色器 [英] How to implement a ground fog GLSL shader
问题描述
我正在尝试为我的地形渲染引擎实现地面雾着色器. 本文介绍了该技术: http://www.iquilezles.org/www /articles/fog/fog.htm
I'm trying to implement a ground fog shader for my terrain rendering engine. The technique is described in this article: http://www.iquilezles.org/www/articles/fog/fog.htm
这个想法是考虑光线从相机射向碎片,并沿该光线整合雾密度函数.
The idea is to consider the ray going from the camera to the fragment and integrate the fog density function along this ray.
这是我的着色器代码:
#version 330 core
in vec2 UV;
in vec3 posw;
out vec3 color;
uniform sampler2D tex;
uniform vec3 ambientLightColor;
uniform vec3 camPos;
const vec3 FogBaseColor = vec3(1., 1., 1.);
void main()
{
vec3 light = ambientLightColor;
vec TexBaseColor = texture(tex,UV).rgb;
//***************************FOG********************************************
vec3 camFrag = posw - camPos;
float distance = length(camFrag);
float a = 0.02;
float b = 0.01;
float fogAmount = a * exp(-camPos.z*b) * ( 1.0-exp( -distance*camFrag.z*b ) ) / (b*camFrag.z);
color = mix( light*TexBaseColor, light*FogBaseColor, fogAmount );
}
第一件事是我不了解如何选择a和b,以及它们在雾密度函数中的物理作用是什么.
The first thing is that I don't understand how to choose a and b and what are their physical role in the fog density function.
然后,结果不是我所期望的... 我有地面雾,但是fogAmount从0到1的过渡始终以相机高度为中心.我已经尝试了很多不同的a和b,但是当我在相机高度没有过渡时,我要么在所有地形上都充满了雾,要么没有雾.
Then, the result is not what I expect… I have a ground fog but the transition of fogAmount from 0 to 1 is always centered at the camera altitude. I've tried a lot of different a and b but when I don't have a transition at camera altitude, I either have a full fogged or not fogged at all terrain.
我检查了我使用的数据,并且一切正确:
I checked the data I use and everything's correct:
- camPos.z是我的相机的高度
- camFrag.z是从摄像机到片段的向量的垂直分量
我不明白方程式的哪一部分导致了这种情况.
I can't get to understand what part of the equation cause this.
对此有任何想法吗?
EDIT : Here's the effect I'm looking for : image1 image2
推荐答案
我找到了一种给出所需结果的方法.
I found a method that gives the result I was looking for.
该方法在Eric Lengyel的这篇文章中进行了介绍:[link] http://www.terathon.com/lengyel/Lengyel-UnifiedFog.pdf
The method is described in this article of Eric Lengyel : [link]http://www.terathon.com/lengyel/Lengyel-UnifiedFog.pdf
它说明了如何使用密度和高度参数创建雾层.您可以飞过它,它会逐渐融合雾上的所有几何形状.
It explains how to create a fog layer with density and altitude parameters. You can fly through it, it progressively blends all the geometry above the fog.
这篇关于如何实现地面雾GLSL着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!