libGDX着色器坐标和摄像机位置

时间:2015-11-01 08:21:10

标签: java opengl camera libgdx shader

我正在使用libGDX,Tiled Map和Box2D制作平台游戏。我想应用一些着色器。

我想要一个坚持英雄的小插曲效果。对于晕影效果,我使用了this tutorial。 我遇到的问题是:如果相机是固定的,我会得到我想要的。小插图坚持英雄。但是,如果相机移动,这发生在滚动的平台游戏中,那么晕影效果并不会影响英雄。

Here is a video that illustrates my problem

似乎我必须考虑相机位置来计算晕影效果的坐标,但我不知道如何。 这是我用于插图的片段着色器:

varying vec4 v_color;   
varying vec2 v_texCoord0;

uniform vec2 u_resolution;
uniform float u_PosX, u_PosY;
uniform sampler2D u_sampler2D;  

const float outerRadius = 10, innerRadius = 1, intensity = .5;

void main(){
    vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;

    vec2 relativePosition = gl_FragCoord.xy / u_resolution - vec2(u_PosX, u_PosY); //Position of the vignette
    relativePosition.x *= u_resolution.x / u_resolution.y;  //The vignette is a circle
    float len = length(relativePosition);
    float vignette = smoothstep(outerRadius, innerRadius, len);
    color.rgb = mix(color.rgb, color.rgb * vignette, intensity);

    gl_FragColor = color;
}

在游戏屏幕中,相关代码为:

public TestScreen(final MyGdxGame gam){    

    camera = new MyCamera(); 
    camera.setToOrtho(false, width, height);
    camera.update();

    vertexShader = Gdx.files.internal("Shaders/vVignette.glsl").readString();
    fragmentShader = Gdx.files.internal("Shaders/fVignette.glsl").readString();
    shaderProgram = new ShaderProgram(vertexShader,fragmentShader);

    shaderProgram.begin();
    shaderProgram.setUniformf("u_resolution", camera.viewportWidth, camera.viewportHeight);
    shaderProgram.end();            
}


public void render(float delta) {
    Gdx.gl.glClearColor(0.1f,0.1f,0.1f,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();

    //calculation of the position of the effect depending on the position of the hero
    posX = (Gdx.graphics.getWidth()/camera.viewportWidth) * hero.bodyHero.getPosition().x/camera.viewportWidth;
    posY = (Gdx.graphics.getHeight()/camera.viewportHeight) * hero.bodyHero.getPosition().y/camera.viewportHeight;  

    shaderProgram.begin();
    shaderProgram.setUniformf("u_PosX", posX);
    shaderProgram.setUniformf("u_PosY", posY);
    shaderProgram.end();
}

正如我所说,如果相机无法移动,此代码可以正常工作。但是因为它在相机移动时不起作用,我认为这个问题与相机投影有关,我在渲染中尝试了这个代码,没有更好的成功。  :

Vector3 PosHero = new Vector3();
PosHero.set(hero.bodyHero.getPosition().x, hero.bodyHero.getPosition().y, 0);
camera.unproject(PosHero);

posX = (Gdx.graphics.getWidth()/camera.viewportWidth) * PosHero.x/camera.viewportWidth;
posY = (Gdx.graphics.getHeight()/camera.viewportHeight) * PosHero.y/camera.viewportHeight;

shaderProgram.begin();
shaderProgram.setUniformf("u_PosX", posX);
shaderProgram.setUniformf("u_PosY", posY);
shaderProgram.end();

1 个答案:

答案 0 :(得分:6)

OMG ......我在这方面已经苦苦挣扎了近两周,我的问题是一个简单的错误:

正如我想的那样,问题与相机投影有关。在我在邮件末尾提供的代码中,我使用了camera.unproject(PosHero);,但我需要使用camera.project(PosHero);代替!

最后,这里是我用来为片段着色器提供正确坐标的代码:

Vector3 PosHero = new Vector3();
PosHero.set(hero.bodyHero.getPosition().x, hero.bodyHero.getPosition().y, 0);
camera.project(PosHero);

posX = PosHero.x/camera.viewportWidth;
posY = PosHero.y/camera.viewportHeight;

shaderProgram.begin();
shaderProgram.setUniformf("u_PosX", posX);
shaderProgram.setUniformf("u_PosY", posY);
shaderProgram.end();