如何实现地面雾 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屋!