片段着色器多个光点

时间:2018-11-26 15:21:04

标签: c++ sfml fragment-shader

我得到了这个着色器,基本上可以让玩家周围有少量的光,现在我想为游戏增加1分以上。例如发射子弹时,它也会在其周围创建这个“区域”……仅使用我当前拥有的东西,它只会画出子弹,而不是玩家。.

如何向其中添加多个点,例如它是否能够编辑着色器以处理数组?

参与渲染功能

    sf::Shader shader;
shader.setParameter("texture", sf::Shader::CurrentTexture);
shader.setParameter("lcolor", sf::Color(255, 255, 255, 100));
std::vector<sf::Vector3f> unarray;


if (shader.loadFromFile("lights.frag", sf::Shader::Fragment))
{
    size_t i = 0;
    for (auto e : m_entityManager.getEntities())
    {
        if (e->hasComponent<CLight>())
        {
            Vec2 pos = e->getComponent<CTransform>()->pos;
            //unarray.push_back();
            int offSet = windowCenterX==pPos.x ? pPos.x -m_game.window().getSize().x / 2.0f :0 ;
            shader.setParameter("light", sf::Vector3f(pos.x- offSet, pos.y, e->getComponent<CLight>()->size));


            i++;
        }
    }
    //converting to the correct coords
    sf::Vector2f coord_pos = m_game.window().mapPixelToCoords(sf::Vector2i(0, 0));
    sf::RectangleShape rect;
    rect.setPosition(coord_pos);
    rect.setSize(sf::Vector2f(m_game.window().getSize().x, m_game.window().getSize().y));
    m_game.window().draw(rect, &shader);
}

着色器:

 uniform sampler2D texture ;
uniform vec3 light;
uniform vec4 lcolor;

void main() {
    float distance = sqrt(pow(gl_FragCoord.x - light.x, 2) + pow(gl_FragCoord.y - light.y, 2));

    if (floor(light.x) == floor(gl_FragCoord.x) && floor(light.y) == floor (gl_FragCoord.y)) 
        distance = 0.1;

    if (distance > light.z) 
        distance = light.z;

    vec2 pos = gl_TexCoord[0].xy ; 
    gl_FragColor = mix(texture2D(texture,pos), lcolor, 1.0-(distance/light.z)); 

}

outlook of shader

0 个答案:

没有答案