1

我刚刚开始工作(而且看起来工作得很快)Ray Casting 系统来制作基本照明(这里的工作类似于演示:http ://www.redblobgames.com/articles/visibility/ )。它像我想要的那样工作,但是当我为从播放器/光源距离的每个顶点设置点亮区域时,它的插值非常糟糕,例如,如果我想将光/阴影修剪到 X 的距离,我没有得到圆形但看起来介于圆形和多边形/正方形之间的东西。图片应该解释。

另外 - 如果这很重要,我将它用于我的 3D 项目(游戏)和自上而下的相机。(见下图)。算法在不处理着色器本身但在其上绘制纯空间的情况下效果很好。(没有剪辑等)

这是我的顶点着色器:

#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace; //vertex's position
layout(location = 1) in vec3 Normal; //normal, currently not used, but added for future
layout(location = 2) in vec2 vertexUV; //texture coords, used and works ok

out vec2 UV; //here i send tex coords
out float intensity; //and this is distance from light source to current vertex (its meant to be interpolated to FS)

uniform mat4 MVP; //mvp matrix
uniform vec2 light_pos; //light-source's position in 2d

void main(){
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
    UV = vertexUV;
    intensity = distance(light_pos, vertexPosition_modelspace.xz); //distance between two vectors
}

这是我的片段着色器:

#version 330 core

in vec2 UV; //tex coords
in float intensity; //distance from light source
out vec4 color; //output
uniform sampler2D myTextureSampler; //texture
uniform float Opacity; //opacity of layer, used and works as expected (though to be change in near future)

void main() {
    color = texture( myTextureSampler, UV );
    color.rgba = vec4(0.0, 0.0, 0.0, 0.5);
    if(intensity > 5)
        color.a = 0;
    else
        color.a = 0.5;
}

这段代码应该给我很好的 FOV 被圆圈剪裁,但我得到的是这样的: 显然不是圆形

我不知道为什么它可以正常工作...

4

1 回答 1

0

这不是准确性问题,而是算法问题,很容易看出您是否取一个所有边相等的三角形并将对象放在其中一个顶点上。在其他两个顶点中,您将获得相同的距离,并且沿着这两个遥远点之间的边缘,您将获得相同的距离,这是错误的,因为它应该在该边缘的中心更大。

如果您将在光源处使用角度较小的三角形,则可以对此进行平滑处理。

更新:如果您通过每个三角形传递光线位置,您仍然可以获得圆形阴影,并将使用插值坐标计算片段着色器中的实际距离。

于 2013-07-10T09:05:12.820 回答