我刚刚开始工作(而且看起来工作得很快)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 被圆圈剪裁,但我得到的是这样的:
我不知道为什么它可以正常工作...