因此,我进行了流体模拟,并且尝试使用双边过滤器以在保留边缘的同时模糊表面。我的方法基于此演示文稿(尤其是幻灯片26)http://developer.download.nvidia.com/presentations/2010/gdc/Direct3D_Effects.pdf
我遇到的问题是根本没有保留我的边缘。即使我渲染了单个粒子,它也被模糊了,正如您在下面的图像中看到的那样,模糊是不正确的。
同样,如pdf中所述,分离一个双边过滤器通常是不可接受的,但在流体情况下,这样做是合理的,因为只有很小的伪影。这就是下面两次通过的原因,但这不应导致边缘模糊。
模糊明暗器:
#version 400
in vec2 coord;
uniform sampler2D depthMap;
uniform vec2 screenSize;
uniform mat4 projection;
uniform vec2 blurDir;
const float filterRadius = 10;
const float blurScale = .1;
const float blurDepthFalloff = 1000;
void main() {
float depth = texture(depthMap, coord).x;
float sum = 0.0f;
float wsum = 0.0f;
for (float x = -filterRadius; x <= filterRadius; x += 1.0f) {
float s = texture(depthMap, coord + x*blurDir).x;
float r = x * blurScale;
float w = exp(-r*r);
float r2 = (s - depth) * blurDepthFalloff;
float g = exp(-r2*r2);
sum += s * w * g;
wsum += w * g;
}
if (wsum > 0.0f) {
sum /= wsum;
}
gl_FragDepth = sum;
}
绘图代码:
//--------------------Particle Blur-------------------------
{
glUseProgram(blurShader.program);
//Vertical blur
glBindFramebuffer(GL_FRAMEBUFFER, blurShader.fboV);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
blurShader.blurDepthVAO();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthShader.tex);
glUniform1i(blurShader.depthMap, 0);
RenderUtility.setVector2(blurShader, screenSize, "screenSize");
RenderUtility.setMatrix(blurShader, projection, "projection");
RenderUtility.setVector2(blurShader, new Vector2(0.0f, 1.0f / screenSize.y), "blurDir");
glEnable(GL_DEPTH_TEST);
glBindVertexArray(blurShader.vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//Horizontal Blur
glBindFramebuffer(GL_FRAMEBUFFER, blurShader.fboH);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, blurShader.texV);
glUniform1i(blurShader.depthMap, 0);
RenderUtility.setVector2(blurShader, screenSize, "screenSize");
RenderUtility.setMatrix(blurShader, projection, "projection");
RenderUtility.setVector2(blurShader, new Vector2(1.0f / screenSize.x, 0.0f), "blurDir");
glEnable(GL_DEPTH_TEST);
glBindVertexArray(blurShader.vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
当前状态图片:
最佳答案
终于明白了。缺少的部分是需要丢弃深度大于.9999(或其他最大深度数)的像素,以便您不会试图模糊不属于模拟范围的像素。回想起来很明显,我花了很长时间才意识到。
因此正确的着色器如下所示:
uniform sampler2D depthMap;
uniform vec2 screenSize;
uniform mat4 projection;
uniform vec2 blurDir;
const float filterRadius = 10;
const float blurScale = .1;
const float blurDepthFalloff = 1000;
void main() {
float depth = texture(depthMap, coord).x;
if (depth == 1.0f) {
discard;
}
float sum = 0.0f;
float wsum = 0.0f;
for (float x = -filterRadius; x <= filterRadius; x += 1.0f) {
float s = texture(depthMap, coord + x*blurDir).x;
float r = x * blurScale;
float w = exp(-r*r);
float r2 = (s - depth) * blurDepthFalloff;
float g = exp(-r2*r2);
sum += s * w * g;
wsum += w * g;
}
if (wsum > 0.0f) {
sum /= wsum;
}
gl_FragDepth = sum;
}
关于java - 用于流体模拟的双边过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27662477/